Need programing afl to backtest historical trade list

Hello,

I tried afl from Amibroker Knowledge base about backtest our historical trade list, http://www.amibroker.com/kb/2014/09/30/gen-backtest-from-a-file/

but strangely, sometimes it works but mostly it did not. I think its related with the CSV file. Did anybody experience the same problem? Some of my CSV files is working, but some not, and some of the transaction is missing. I tried paste my transaction in the working CSV files, but then it also stop working when i saved it. I write the transaction in excel and save it in csv. Can someone point out how to get a steady working result?

and can someone help me to add backtest optimisation for taking profit and cut losses?

the idea is simple, to backtest the historical trade and to find the optimum money management, taking profit and cut losses via our beloved amibroker :slight_smile:

I still using amibroker 5.5 by the way…

thanks in advance

As user (sikatgigi) reported there seems to be some challenges with getting the sample code here listed below to work correctly
http://www.amibroker.com/kb/2014/09/30/gen-backtest-from-a-file/

It seems to partially work for me, but for some reason it is skipping most of the trades in my .CSV. I've included the AFL and a list of trades I'm referencing (shorted list because of post character restriction).

Maybe the code no longer works with later versions of Amibroker? Really appreciate any help / clues. Thanks for your consideration!

file = "C:\\temp\\amiTrades_RangeBound_2.csv"; // change this to real location of your data file
dt = DateTime();


Buy = Sell = Short = Cover = 0;


SetOption("MaxOpenPositions", 300);
SetOption("InitialEquity", 1000000);
// ApplyStop(stopTypeNBar, stopModeBars, 1);



//
// Initialize variables
Buy = Sell = possize = 0; 
//
fh = fopen( file, "r" );
//
if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         // get the ticker symbol from the file
         sym = StrExtract( line, 0 );
         // if ticker matches current symbol
         if ( Name() == sym )
         {
             // extract data from line of text
             trade = StrExtract( line, 1 );
             trade_datetime = StrToDateTime( StrExtract( line, 2 ) );
             price = StrToNum( StrExtract( line, 3 ) );
             shares = StrToNum( StrExtract( line, 4 ) );
             //
             if ( trade == "Buy" )
             {
                 newbuy = dt == trade_datetime;
                 Buy = Buy OR newbuy; // combine previous buy signals with new
                 BuyPrice = IIf( newbuy, price, BuyPrice );
                 possize = IIf( newbuy, shares, possize );
             }
             //
             if ( trade == "Sell" )
             {
                 newsell = dt == trade_datetime;
                 Sell = Sell OR newsell; // combine previous sell signals with new
                 SellPrice = IIf( newsell, price, SellPrice );
             }
             
             
             if ( trade == "Short" )
             {
				newshort = dt == trade_datetime;
				Short = Short OR newshort;
				ShortPrice = IIf( newshort, price, ShortPrice );
				possize = IIf ( newshort, shares, possize );
			}
			
			if ( trade == "Cover" )
			{
				newcover = dt == trade_datetime;
				Cover = Cover OR newcover;
				CoverPrice = IIf( newcover, price, CoverPrice );			
			}
			
		}
     }
     //
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }
//
SetPositionSize( possize, spsShares ); 


Symbol,Trade,Date,Price,Shares
AAPL,Buy,2015-02-06,120.02,35.45508372718022
AGN,Buy,2015-02-06,274.18,15.52016612785823
AMZN,Buy,2015-02-06,374.87,68.10871740501244
BA,Buy,2015-02-06,148.62,14.31610533217659
BIIB,Buy,2015-02-06,368.72,46.163149804037424
CAT,Buy,2015-02-06,83.6,25.450473378804844
CELG,Buy,2015-02-06,120.04,124.07211780470338
COF,Buy,2015-02-06,76.32,55.756278156920466
CVX,Buy,2015-02-06,109.66,19.402330607952624
DIS,Buy,2015-02-06,102.67,41.44656812054319
F,Buy,2015-02-06,15.46,137.62351710660315
HAL,Buy,2015-02-06,44.1,48.24624885415158
PCLN,Buy,2015-02-06,1027.56,4.141188007450825
SPG,Buy,2015-02-06,201.75,31.63806058688602
USB,Buy,2015-02-06,44.31,96.035187292624
AMZN,Sell,2015-02-09,371.0,17.02717935125311
BIIB,Sell,2015-02-09,366.64,28.85196862752339
CELG,Sell,2015-02-09,118.46,70.89835303125908
PCLN,Sell,2015-02-09,1020.65,4.141188007450825
ABBV,Buy,2015-02-09,55.67,49.89721174380776
AGN,Buy,2015-02-09,275.45,20.169016357072266
AIG,Buy,2015-02-09,51.67,53.75997247489409
ALL,Buy,2015-02-09,70.32,39.501959297181145
AMZN,Buy,2015-02-09,371.0,59.898173105720275
BIIB,Buy,2015-02-09,366.64,53.034160060125586
BLK,Buy,2015-02-09,360.03,7.7154064321800355
CELG,Buy,2015-02-09,118.46,23.44907798225374
COF,Buy,2015-02-09,75.69,73.39880506745351
F,Buy,2015-02-09,15.38,180.6097384770987
HAL,Buy,2015-02-09,43.67,63.60837595094522
JNJ,Buy,2015-02-09,101.06,27.486421707676406
LOW,Buy,2015-02-09,71.45,38.877225721172536
MS,Buy,2015-02-09,35.5,78.24726134585289
RTN,Buy,2015-02-09,106.74,26.02377532113339
TWX,Buy,2015-02-09,80.05,104.10160316468873
USB,Buy,2015-02-09,44.09,126.00488898969279
WBA,Buy,2015-02-09,75.64,36.72366178976438
AAPL,Sell,2015-02-10,120.17,35.45508372718022
AGN,Sell,2015-02-10,274.79,15.52016612785823
AMZN,Sell,2015-02-10,371.19,51.08153805375934
BA,Sell,2015-02-10,148.2,14.31610533217659
BIIB,Sell,2015-02-10,366.64,17.311181176514033
CAT,Sell,2015-02-10,85.0,25.450473378804844
CELG,Sell,2015-02-10,119.03,53.173764773444304
COF,Sell,2015-02-10,77.42,55.756278156920466
CVX,Sell,2015-02-10,110.52,19.402330607952624
DIS,Sell,2015-02-10,101.96,41.44656812054319
F,Sell,2015-02-10,15.66,137.62351710660315
HAL,Sell,2015-02-10,43.44,48.24624885415158
SPG,Sell,2015-02-10,195.0,31.63806058688602
USB,Sell,2015-02-10,44.57,96.035187292624
AMZN,Sell,2015-02-10,371.19,22.461814914645103
BIIB,Sell,2015-02-10,366.64,37.881542900089705
AAPL,Buy,2015-02-10,120.17,43.7976025192381
ABBV,Buy,2015-02-10,55.93,94.10259064432044
AIG,Buy,2015-02-10,52.53,100.19337321029587
AMZN,Buy,2015-02-10,371.19,42.53744358471545
CELG,Buy,2015-02-10,119.03,44.217070442214926
COF,Buy,2015-02-10,77.42,135.96377924920802
F,Buy,2015-02-10,15.66,336.08926530886606
MS,Buy,2015-02-10,35.95,146.40216675206793
PCLN,Buy,2015-02-10,1040.8,10.113677737772566
TWX,Buy,2015-02-10,80.76,260.6814212351086
WBA,Buy,2015-02-10,76.31,68.97074950513488
XOM,Buy,2015-02-10,91.74,57.370371645267525
ABBV,Sell,2015-02-11,57.36,49.89721174380776
AGN,Sell,2015-02-11,279.15,20.169016357072266
AIG,Sell,2015-02-11,52.32,53.75997247489409
ALL,Sell,2015-02-11,70.14,39.501959297181145
AMZN,Sell,2015-02-11,371.23,37.43635819107517
BIIB,Sell,2015-02-11,363.71,15.152617160035883
BLK,Sell,2015-02-11,369.55,7.7154064321800355
CELG,Sell,2015-02-11,119.83,23.44907798225374
COF,Sell,2015-02-11,77.1,73.39880506745351
F,Sell,2015-02-11,15.69,180.6097384770987
HAL,Sell,2015-02-11,42.02,63.60837595094522
JNJ,Sell,2015-02-11,100.46,27.486421707676406
LOW,Sell,2015-02-11,71.59,38.877225721172536
MS,Sell,2015-02-11,36.09,78.24726134585289
RTN,Sell,2015-02-11,107.47,26.02377532113339
TWX,Sell,2015-02-11,81.26,104.10160316468873
USB,Sell,2015-02-11,44.33,126.00488898969279
WBA,Sell,2015-02-11,76.8,36.72366178976438
AAPL,Buy,2015-02-11,122.77,29.090401331176768
AGN,Buy,2015-02-11,279.15,25.587881579284055
AMZN,Buy,2015-02-11,371.23,19.241055795213594
CELG,Buy,2015-02-11,119.83,238.43301820436096
COF,Buy,2015-02-11,77.1,92.64406151565686
DIS,Buy,2015-02-11,102.01,35.010573193104314
F,Buy,2015-02-11,15.69,227.6245106073022
MS,Buy,2015-02-11,36.09,98.95895182678224
PCLN,Buy,2015-02-11,1064.24,13.423395367317791
TWX,Buy,2015-02-11,81.26,175.80253858865723
USB,Buy,2015-02-11,44.33,80.56459669362896
WBA,Buy,2015-02-11,76.8,46.5029761904762
AAPL,Sell,2015-02-12,126.06,43.7976025192381
ABBV,Sell,2015-02-12,57.4,94.10259064432044
AIG,Sell,2015-02-12,52.31,100.19337321029587
AMZN,Sell,2015-02-12,375.1,42.53744358471545
CELG,Sell,2015-02-12,119.2,44.217070442214926
COF,Sell,2015-02-12,78.03,135.96377924920802
F,Sell,2015-02-12,15.94,336.08926530886606
MS,Sell,2015-02-12,36.51,146.40216675206793
PCLN,Sell,2015-02-12,1077.21,10.113677737772566
TWX,Sell,2015-02-12,81.91,260.6814212351086
WBA,Sell,2015-02-12,79.17,68.97074950513488
XOM,Sell,2015-02-12,91.46,57.370371645267525
AMZN,Sell,2015-02-12,375.1,9.620527897606797
CELG,Sell,2015-02-12,119.2,149.0206363777256
PCLN,Sell,2015-02-12,1077.21,13.423395367317791
TWX,Sell,2015-02-12,81.91,43.95063464716431
AAPL,Buy,2015-02-12,126.06,54.708485833237596
AMZN,Buy,2015-02-12,375.1,73.54360676233463
COF,Buy,2015-02-12,78.03,88.38333620579176
CSCO,Buy,2015-02-12,28.99,118.94708044391051
EXC,Buy,2015-02-12,34.32,100.47423840527289
F,Buy,2015-02-12,15.94,216.32847315363648
FOXA,Short,2015-02-12,34.94,197.38270532735925
MS,Buy,2015-02-12,36.51,94.44743527989498
PCLN,Buy,2015-02-12,1077.21,16.005587870837466
TWX,Buy,2015-02-12,81.91,210.49175082828503
WBA,Buy,2015-02-12,79.17,43.55533487519219
AAPL,Sell,2015-02-13,127.28,29.090401331176768
AGN,Sell,2015-02-13,280.84,25.587881579284055
AMZN,Sell,2015-02-13,378.41,9.620527897606797
CELG,Sell,2015-02-13,116.32,89.41238182663537
COF,Sell,2015-02-13,79.39,92.64406151565686
DIS,Sell,2015-02-13,103.73,35.010573193104314
F,Sell,2015-02-13,15.93,227.6245106073022
MS,Sell,2015-02-13,36.93,98.95895182678224
TWX,Sell,2015-02-13,83.0,131.85190394149294
USB,Sell,2015-02-13,45.12,80.56459669362896
WBA,Sell,2015-02-13,79.65,46.5029761904762
AMZN,Sell,2015-02-13,378.41,9.19295084529183
PCLN,Sell,2015-02-13,1100.15,16.005587870837466
TWX,Sell,2015-02-13,83.0,42.09835016565701
AAPL,Buy,2015-02-13,127.28,54.184095884176074
AGN,Buy,2015-02-13,280.84,24.556871258146746
AIG,Buy,2015-02-13,51.16,67.40179558383436
AMZN,Buy,2015-02-13,378.41,63.7877726129932
COF,Buy,2015-02-13,79.39,86.86927477185957
DIS,Buy,2015-02-13,103.73,33.2428021022748
F,Buy,2015-02-13,15.93,216.46427257181202
MS,Buy,2015-02-13,36.93,93.37329710449406
PCLN,Buy,2015-02-13,1100.15,12.537475297255703
TWX,Buy,2015-02-13,83.0,249.27295388450352
USB,Buy,2015-02-13,45.12,76.42455368060651
WBA,Buy,2015-02-13,79.65,43.2928545143624
AAPL,Sell,2015-02-17,127.49,54.708485833237596
AMZN,Sell,2015-02-17,377.72,64.3506559170428
COF,Sell,2015-02-17,78.79,88.38333620579176
CSCO,Sell,2015-02-17,29.26,118.94708044391051
EXC,Sell,2015-02-17,33.43,100.47423840527289
F,Sell,2015-02-17,15.82,216.32847315363648
FOXA,Cover,2015-02-17,34.68,197.38270532735925
MS,Sell,2015-02-17,36.63,94.44743527989498
TWX,Sell,2015-02-17,83.27,168.39340066262804
WBA,Sell,2015-02-17,78.38,43.55533487519219
AMZN,Sell,2015-02-17,377.72,9.112538944713314
PCLN,Sell,2015-02-17,1115.0,12.537475297255703
TWX,Sell,2015-02-17,83.27,83.09098462816785
AAPL,Buy,2015-02-17,127.49,47.537893643470554
ACN,Short,2015-02-17,89.63,33.80902633385062
AGN,Buy,2015-02-17,287.53,21.078169445296357
AIG,Buy,2015-02-17,54.09,56.023350532501944
AMZN,Buy,2015-02-17,377.72,72.2035562658246
C,Buy,2015-02-17,50.95,118.95203259285694
COF,Buy,2015-02-17,78.79,76.9210059729161
DIS,Buy,2015-02-17,104.23,29.07323256550926
MS,Buy,2015-02-17,36.63,82.72735545462818
PCLN,Buy,2015-02-17,1115.0,10.871042261176791
TWX,Buy,2015-02-17,83.27,254.73905622818802
UTX,Buy,2015-02-17,121.0,25.04382669671926
AAPL,Sell,2015-02-18,127.63,54.184095884176074
AGN,Sell,2015-02-18,289.13,24.556871258146746
AIG,Sell,2015-02-18,54.72,67.40179558383436
AMZN,Sell,2015-02-18,373.78,54.67523366827989
COF,Sell,2015-02-18,79.73,86.86927477185957
DIS,Sell,2015-02-18,103.4,33.2428021022748
F,Sell,2015-02-18,15.77,216.46427257181202
MS,Sell,2015-02-18,36.87,93.37329710449406
TWX,Sell,2015-02-18,83.65,166.1819692563357
USB,Sell,2015-02-18,44.93,76.42455368060651
WBA,Sell,2015-02-18,77.59,43.2928545143624
AMZN,Sell,2015-02-18,373.78,16.0452347257388
PCLN,Sell,2015-02-18,1116.44,10.871042261176791
TWX,Sell,2015-02-18,83.65,109.17388124065201
AAPL,Buy,2015-02-18,127.63,52.23432317375748
ABBV,Buy,2015-02-18,59.15,112.70780501549733
AGN,Buy,2015-02-18,289.13,34.5865181752153
AMZN,Buy,2015-02-18,373.78,8.917901796065422
BIIB,Buy,2015-02-18,365.52,9.119428029473992
C,Buy,2015-02-18,51.3,129.95451591942822
COF,Buy,2015-02-18,79.73,83.61553576654542
HAL,Buy,2015-02-18,44.35,75.15971439308531
LOW,Buy,2015-02-18,72.23,46.14887627486271
MRK,Buy,2015-02-18,58.67,56.81495369581274
MS,Buy,2015-02-18,36.87,90.40773890245006
PCLN,Buy,2015-02-18,1116.44,11.942722701921584
TWX,Buy,2015-02-18,83.65,318.78860330743174
UTX,Buy,2015-02-18,121.32,27.475546763380596
AAPL,Sell,2015-02-19,128.48,47.537893643470554
ACN,Cover,2015-02-19,89.53,33.80902633385062
AGN,Sell,2015-02-19,281.67,21.078169445296357
AIG,Sell,2015-02-19,54.66,56.023350532501944
AMZN,Sell,2015-02-19,373.49,56.15832154008581
C,Sell,2015-02-19,51.28,118.95203259285694
COF,Sell,2015-02-19,79.39,76.9210059729161
DIS,Sell,2015-02-19,104.0,29.07323256550926
MS,Sell,2015-02-19,36.3,82.72735545462818
TWX,Sell,2015-02-19,83.51,145.565174987536
UTX,Sell,2015-02-19,121.58,25.04382669671926
PCLN,Sell,2015-02-19,1219.0,11.942722701921584
TWX,Sell,2015-02-19,83.51,119.54572624028691
AAPL,Buy,2015-02-19,128.48,64.86093814860939
ABBV,Buy,2015-02-19,58.92,141.4347137361394
BA,Buy,2015-02-19,151.05,55.16936996579499
BIIB,Buy,2015-02-19,369.4,11.279552427359684
CELG,Buy,2015-02-19,118.33,246.4858165018733
COF,Buy,2015-02-19,79.39,52.48352017466516
CSCO,Short,2015-02-19,29.31,142.15853519845334
PCLN,Buy,2015-02-19,1219.0,13.67240907847963
TWX,Buy,2015-02-19,83.51,149.68267273380434
UTX,Buy,2015-02-19,121.58,34.27098755277732
AAPL,Sell,2015-02-20,128.62,52.23432317375748
ABBV,Sell,2015-02-20,59.61,112.70780501549733
AGN,Sell,2015-02-20,290.0,34.5865181752153
AMZN,Sell,2015-02-20,378.62,8.917901796065422
BIIB,Sell,2015-02-20,374.34,9.119428029473992
C,Sell,2015-02-20,51.05,129.95451591942822
COF,Sell,2015-02-20,79.2,83.61553576654542
HAL,Sell,2015-02-20,44.1,75.15971439308531
LOW,Sell,2015-02-20,72.13,46.14887627486271
MRK,Sell,2015-02-20,58.15,56.81495369581274
MS,Sell,2015-02-20,36.24,90.40773890245006
TWX,Sell,2015-02-20,83.84,199.24287706714483
UTX,Sell,2015-02-20,122.99,27.475546763380596
CELG,Sell,2015-02-20,121.31,140.84903800107045
PCLN,Sell,2015-02-20,1217.0,13.67240907847963
AAPL,Buy,2015-02-20,128.62,18.511525275636608
AGN,Buy,2015-02-20,290.0,24.63054187192118
AMZN,Buy,2015-02-20,378.62,25.154005398049556
AXP,Buy,2015-02-20,78.22,30.4391764376423
BA,Buy,2015-02-20,153.8,30.961669453216913
BIIB,Buy,2015-02-20,374.34,57.243605889222174
CAT,Short,2015-02-20,83.14,28.6378684261773
CELG,Buy,2015-02-20,121.31,176.64307500264962
COF,Buy,2015-02-20,79.2,30.062530062530058
F,Buy,2015-02-20,15.99,148.90258792697816
PCLN,Buy,2015-02-20,1217.0,7.825644637477011
QCOM,Buy,2015-02-20,70.83,33.615027261787105
TWX,Buy,2015-02-20,83.84,113.59505634314793
UTX,Buy,2015-02-20,122.99,19.35891032565559
AAPL,Sell,2015-02-23,130.02,64.86093814860939
ABBV,Sell,2015-02-23,61.54,141.4347137361394
BA,Sell,2015-02-23,155.0,55.16936996579499
BIIB,Sell,2015-02-23,376.04,11.279552427359684
CELG,Sell,2015-02-23,124.0,105.63677850080285
COF,Sell,2015-02-23,80.39,52.48352017466516
CSCO,Cover,2015-02-23,29.63,142.15853519845334
TWX,Sell,2015-02-23,84.47,149.68267273380434
UTX,Sell,2015-02-23,123.63,34.27098755277732
AMZN,Sell,2015-02-23,383.44,6.288501349512389
BIIB,Sell,2015-02-23,376.04,38.162403926148116
CELG,Sell,2015-02-23,124.0,117.76205000176641
PCLN,Sell,2015-02-23,1210.47,7.825644637477011
QCOM,Sell,2015-02-23,71.26,33.615027261787105
AAPL,Buy,2015-02-23,130.02,14.511555551685806
AGN,Buy,2015-02-23,296.43,12.73010459690442
AMZN,Buy,2015-02-23,383.44,49.206980305398204
BA,Buy,2015-02-23,155.0,24.345709068776628
BIIB,Buy,2015-02-23,376.04,45.15778128781964
CELG,Buy,2015-02-23,124.0,167.3767498478393
COF,Buy,2015-02-23,80.39,46.94097407215297
KMI,Buy,2015-02-23,41.72,45.22513070062773
MS,Buy,2015-02-23,36.56,51.60810866603361
QCOM,Buy,2015-02-23,71.26,26.477581431801692
RTN,Buy,2015-02-23,108.88,17.329100411739425
TWX,Buy,2015-02-23,84.47,156.35784503150612
UNH,Buy,2015-02-23,112.99,66.79502443862957
UTX,Buy,2015-02-23,123.63,15.261606833537076
AAPL,Sell,2015-02-24,132.94,18.511525275636608
AGN,Sell,2015-02-24,292.21,24.63054187192118
AMZN,Sell,2015-02-24,378.65,18.86550404853717
AXP,Sell,2015-02-24,80.3,30.4391764376423
BA,Sell,2015-02-24,153.5,30.961669453216913
BIIB,Sell,2015-02-24,377.88,19.081201963074058
CAT,Cover,2015-02-24,83.77,28.6378684261773
CELG,Sell,2015-02-24,124.17,58.881025000883206
COF,Sell,2015-02-24,79.84,30.062530062530058
F,Sell,2015-02-24,15.96,148.90258792697816
TWX,Sell,2015-02-24,83.6,113.59505634314793
UTX,Sell,2015-02-24,123.42,19.35891032565559
AMZN,Sell,2015-02-24,378.65,9.84139606107964
BIIB,Sell,2015-02-24,377.88,30.10518752521309
CELG,Sell,2015-02-24,124.17,121.72854534388314
QCOM,Sell,2015-02-24,70.74,26.477581431801692
TWX,Sell,2015-02-24,83.6,44.67367000900175
UNH,Sell,2015-02-24,116.26,66.79502443862957
AAPL,Buy,2015-02-24,132.94,20.330244491520254
ABBV,Buy,2015-02-24,60.85,88.83164183082013
AGN,Buy,2015-02-24,292.21,18.498358733121403
AMZN,Buy,2015-02-24,378.65,21.413199810136295
BIIB,Buy,2015-02-24,377.88,57.21822171488732
CELG,Buy,2015-02-24,124.17,174.12919079988419
COF,Buy,2015-02-24,79.84,33.851486757298375
DIS,Buy,2015-02-24,104.54,25.853287762604765
FOXA,Buy,2015-02-24,35.38,76.39069255801871
GILD,Buy,2015-02-24,104.59,25.840928412876014
MA,Short,2015-02-24,90.55,29.847627859775844
RTN,Buy,2015-02-24,109.01,24.793163037360813
SLB,Buy,2015-02-24,86.59,31.212642368665
SPG,Buy,2015-02-24,192.85,14.014533070794412
UNH,Buy,2015-02-24,116.26,92.98822304155178
WBA,Buy,2015-02-24,78.33,34.50405595177713
AAPL,Sell,2015-02-25,131.56,14.511555551685806
AGN,Sell,2015-02-25,291.0,12.73010459690442
AMZN,Sell,2015-02-25,377.27,39.36558424431856
BA,Sell,2015-02-25,154.3,24.345709068776628
BIIB,Sell,2015-02-25,373.34,15.052593762606545
CELG,Sell,2015-02-25,123.46,45.64820450395618
COF,Sell,2015-02-25,80.18,46.94097407215297
KMI,Sell,2015-02-25,42.17,45.22513070062773
MS,Sell,2015-02-25,36.69,51.60810866603361
RTN,Sell,2015-02-25,110.18,17.329100411739425
TWX,Sell,2015-02-25,83.43,111.68417502250436
UTX,Sell,2015-02-25,123.85,15.261606833537076
BIIB,Sell,2015-02-25,373.34,42.913666286165494
CELG,Sell,2015-02-25,123.46,152.36304194989867
UNH,Sell,2015-02-25,115.82,92.98822304155178
AAPL,Buy,2015-02-25,131.56,47.50684098510185
AMZN,Buy,2015-02-25,377.27,16.566384817239644
BIIB,Buy,2015-02-25,373.34,16.740772486205604
CELG,Buy,2015-02-25,123.46,101.24736756844322
COF,Buy,2015-02-25,80.18,233.84884010975304
GOOG,Buy,2015-02-25,534.43,11.694702767434464
GOOGL,Buy,2015-02-25,538.44,11.607607161429312
GS,Buy,2015-02-25,191.55,32.62855651265988
LOW,Buy,2015-02-25,75.9,82.3451910408432
MA,Short,2015-02-25,92.05,67.89788158609451
RTN,Buy,2015-02-25,110.18,56.72535850426574
SLB,Buy,2015-02-25,85.39,73.19358238669633
WBA,Buy,2015-02-25,80.25,77.88161993769471
AAPL,Sell,2015-02-26,128.79,20.330244491520254
ABBV,Sell,2015-02-26,60.35,88.83164183082013
AGN,Sell,2015-02-26,290.25,18.498358733121403
AMZN,Sell,2015-02-26,384.07,21.413199810136295
BIIB,Sell,2015-02-26,376.56,14.30455542872183
CELG,Sell,2015-02-26,123.28,21.766148849985523
COF,Sell,2015-02-26,79.48,33.851486757298375
DIS,Sell,2015-02-26,105.5,25.853287762604765
FOXA,Sell,2015-02-26,34.78,76.39069255801871
GILD,Sell,2015-02-26,104.3,25.840928412876014
MA,Cover,2015-02-26,92.89,29.847627859775844
RTN,Sell,2015-02-26,109.16,24.793163037360813
SLB,Sell,2015-02-26,84.48,31.212642368665
SPG,Sell,2015-02-26,190.03,14.014533070794412
WBA,Sell,2015-02-26,79.9,34.50405595177713
ABBV,Buy,2015-02-26,60.35,57.13795960346256
AMZN,Buy,2015-02-26,384.07,80.80423557846406
BIIB,Buy,2015-02-26,376.56,45.78653948997457
CELG,Buy,2015-02-26,123.28,55.94217816464902
DIS,Buy,2015-02-26,105.5,32.685079261317206
F,Buy,2015-02-26,16.0,215.51724137931035
GM,Short,2015-02-26,37.81,91.2001021441144
MA,Short,2015-02-26,92.89,37.122142987070355
NKE,Short,2015-02-26,48.33,71.34855911584866
PCLN,Buy,2015-02-26,1248.98,11.043494249928631
QCOM,Buy,2015-02-26,71.87,47.97934968789433
SLB,Buy,2015-02-26,84.48,81.63531870428422
AAPL,Sell,2015-02-27,130.0,47.50684098510185
AMZN,Sell,2015-02-27,384.0,16.566384817239644
BIIB,Sell,2015-02-27,377.86,16.740772486205604
CELG,Sell,2015-02-27,122.81,101.24736756844322
COF,Sell,2015-02-27,78.86,233.84884010975304
GOOG,Sell,2015-02-27,552.72,11.694702767434464
GOOGL,Sell,2015-02-27,558.15,11.607607161429312
GS,Sell,2015-02-27,191.19,32.62855651265988
LOW,Sell,2015-02-27,73.86,82.3451910408432
MA,Cover,2015-02-27,91.01,67.89788158609451
RTN,Sell,2015-02-27,108.8,56.72535850426574
SLB,Sell,2015-02-27,83.97,73.19358238669633
WBA,Sell,2015-02-27,81.28,77.88161993769471
AMZN,Sell,2015-02-27,384.0,17.956496795214235
BIIB,Sell,2015-02-27,377.86,27.471923693984746
PCLN,Sell,2015-02-27,1240.75,11.043494249928631
QCOM,Sell,2015-02-27,71.81,47.97934968789433
ABBV,Buy,2015-02-27,60.53,78.67015962175387
AMZN,Buy,2015-02-27,384.0,49.6031746031746
BAC,Buy,2015-02-27,15.78,301.7683626048645
BIIB,Buy,2015-02-27,377.86,50.40919665383752
CELG,Buy,2015-02-27,122.81,193.87284267994306
KMI,Short,2015-02-27,41.23,115.49611355577885
MET,Buy,2015-02-27,45.32,105.07292060690118
NKE,Short,2015-02-27,48.47,98.24437305353335
QCOM,Buy,2015-02-27,71.81,66.31255760903441
TWX,Buy,2015-02-27,82.85,57.47621921430008
WBA,Buy,2015-02-27,81.28,58.58642669666291
ABBV,Sell,2015-03-02,60.3,57.13795960346256
AMZN,Sell,2015-03-02,380.85,62.847738783249824
BIIB,Sell,2015-03-02,378.27,18.31461579598983
CELG,Sell,2015-03-02,122.59,55.94217816464902
DIS,Sell,2015-03-02,104.35,32.685079261317206
F,Sell,2015-03-02,16.01,215.51724137931035
GM,Cover,2015-03-02,37.41,91.2001021441144
MA,Cover,2015-03-02,89.56,37.122142987070355
NKE,Cover,2015-03-02,48.44,71.34855911584866
SLB,Sell,2015-03-02,83.69,81.63531870428422
AMZN,Sell,2015-03-02,380.85,12.40079365079365
BIIB,Sell,2015-03-02,378.27,25.20459832691876
CELG,Sell,2015-03-02,122.59,38.774568535988614
QCOM,Sell,2015-03-02,71.8,66.31255760903441
AXP,Buy,2015-03-02,81.73,203.92348790733718
BAC,Buy,2015-03-02,15.79,1055.520371543171
CSCO,Buy,2015-03-02,29.36,567.6657584014533
NKE,Short,2015-03-02,48.44,344.0682631434077
QCOM,Buy,2015-03-02,71.8,232.12627669452183
WBA,Buy,2015-03-02,83.24,200.22425116130069
ABBV,Sell,2015-03-03,60.35,78.67015962175387
AMZN,Sell,2015-03-03,383.95,37.20238095238095
BAC,Sell,2015-03-03,16.03,301.7683626048645
BIIB,Sell,2015-03-03,382.26,25.20459832691876
CELG,Sell,2015-03-03,120.0,155.09827414395446
KMI,Cover,2015-03-03,40.95,115.49611355577885
MET,Sell,2015-03-03,45.83,105.07292060690118
NKE,Cover,2015-03-03,49.22,98.24437305353335
TWX,Sell,2015-03-03,82.31,57.47621921430008
WBA,Sell,2015-03-03,83.43,58.58642669666291
QCOM,Sell,2015-03-03,72.01,232.12627669452183
AGN,Buy,2015-03-03,296.8,21.057951482479783
AMZN,Buy,2015-03-03,383.95,73.25172548508921
BIIB,Buy,2015-03-03,382.26,65.40051274001988
COF,Buy,2015-03-03,79.15,78.96399241945672
DIS,Buy,2015-03-03,105.78,29.54244658725657
F,Buy,2015-03-03,15.68,199.29846938775512
LOW,Buy,2015-03-03,74.9,41.72229639519359
MON,Short,2015-03-03,121.09,25.807250805186225
MS,Buy,2015-03-03,36.0,86.80555555555556
NKE,Short,2015-03-03,49.22,63.49045103616416
ORCL,Buy,2015-03-03,43.83,71.29819758156513
PCLN,Buy,2015-03-03,1245.0,5.020080321285141
QCOM,Buy,2015-03-03,72.01,43.3967504513262
WBA,Buy,2015-03-03,83.43,37.456550401534216
AXP,Sell,2015-03-04,81.55,203.92348790733718
BAC,Sell,2015-03-04,15.96,1055.520371543171
CSCO,Sell,2015-03-04,29.35,567.6657584014533
NKE,Cover,2015-03-04,48.83,344.0682631434077
WBA,Sell,2015-03-04,82.79,200.22425116130069
AMZN,Sell,2015-03-04,385.71,16.278161218908714
BIIB,Sell,2015-03-04,378.53,40.87532046251242
PCLN,Sell,2015-03-04,1233.5,5.020080321285141
QCOM,Sell,2015-03-04,71.1,43.3967504513262
AAPL,Buy,2015-03-04,129.1,55.32809560694921
AGN,Buy,2015-03-04,295.93,48.27396440277865
BIIB,Buy,2015-03-04,378.53,18.869989546025792
CELG,Buy,2015-03-04,117.41,60.836872011388664
DIS,Buy,2015-03-04,105.86,67.47456209009204
FOX,Buy,2015-03-04,34.7,205.8460271716756
FOXA,Buy,2015-03-04,35.58,401.5096763832009
PM,Buy,2015-03-04,82.69,86.3811481782216
TWX,Buy,2015-03-04,82.86,344.8156960104824
AGN,Sell,2015-03-05,297.1,21.057951482479783
AMZN,Sell,2015-03-05,385.61,56.9735642661805
BIIB,Sell,2015-03-05,384.49,24.525192277507454
COF,Sell,2015-03-05,78.65,78.96399241945672
DIS,Sell,2015-03-05,105.54,29.54244658725657
F,Sell,2015-03-05,15.7,199.29846938775512
LOW,Sell,2015-03-05,74.38,41.72229639519359
MON,Cover,2015-03-05,120.0,25.807250805186225
MS,Sell,2015-03-05,35.24,86.80555555555556
NKE,Cover,2015-03-05,48.98,63.49045103616416
ORCL,Sell,2015-03-05,43.66,71.29819758156513
WBA,Sell,2015-03-05,83.45,37.456550401534216
BAC,Buy,2015-03-05,15.92,1046.9011725293133
BIIB,Buy,2015-03-05,384.49,43.34746460679515
GM,Short,2015-03-05,37.64,885.582713425434
GM,Buy,2015-03-05,37.64,442.791356712717
PM,Short,2015-03-05,82.2,202.75750202757501
AAPL,Sell,2015-03-06,128.4,55.32809560694921
AGN,Sell,2015-03-06,295.29,48.27396440277865
BIIB,Sell,2015-03-06,390.76,18.869989546025792
CELG,Sell,2015-03-06,120.03,60.836872011388664
DIS,Sell,2015-03-06,104.67,67.47456209009204
FOX,Sell,2015-03-06,34.02,205.8460271716756
FOXA,Sell,2015-03-06,34.73,401.5096763832009
PM,Sell,2015-03-06,81.28,86.3811481782216
TWX,Sell,2015-03-06,83.21,344.8156960104824
AMZN,Buy,2015-03-06,385.52,107.3337578979757
AXP,Buy,2015-03-06,80.63,42.766660821889694
BIIB,Buy,2015-03-06,390.76,88.24536447100435
BMY,Short,2015-03-06,66.21,52.080892041518894
CELG,Buy,2015-03-06,120.03,28.7284500713902
LLY,Buy,2015-03-06,70.26,49.07879109121784
TWX,Buy,2015-03-06,83.21,124.32192748716378
BAC,Sell,2015-03-09,16.31,1046.9011725293133
BIIB,Sell,2015-03-09,384.31,43.34746460679515
GM,Cover,2015-03-09,37.55,885.582713425434
GM,Sell,2015-03-09,37.55,442.791356712717
PM,Cover,2015-03-09,80.18,202.75750202757501
AMZN,Sell,2015-03-09,378.4,17.888959649662617
BIIB,Sell,2015-03-09,384.31,61.771755129703045
AIG,Buy,2015-03-09,55.86,149.1824800095477
BIIB,Buy,2015-03-09,384.31,21.683883670300887
C,Buy,2015-03-09,52.88,157.58951084215835
JNJ,Buy,2015-03-09,100.2,83.16699933466401
MSFT,Buy,2015-03-09,42.19,197.51915935845778
ORCL,Buy,2015-03-09,42.25,197.23865877712032
SBUX,Buy,2015-03-09,46.19,180.4142310745472
SLB,Buy,2015-03-09,82.87,100.55910864406098
TWX,Buy,2015-03-09,84.0,396.82539682539687
AMZN,Sell,2015-03-10,377.45,89.44479824831308
AXP,Sell,2015-03-10,79.73,42.766660821889694
BIIB,Sell,2015-03-10,377.35,26.473609341301305
BMY,Cover,2015-03-10,65.78,52.080892041518894
CELG,Sell,2015-03-10,117.5,28.7284500713902
LLY,Sell,2015-03-10,69.12,49.07879109121784
TWX,Sell,2015-03-10,84.53,124.32192748716378
AXP,Buy,2015-03-10,79.73,78.38956478113633
COF,Buy,2015-03-10,79.02,237.2817008352316
GM,Short,2015-03-10,37.43,166.97835960459525
PM,Buy,2015-03-10,80.0,78.125
QCOM,Buy,2015-03-10,73.87,84.60809530255854
TWX,Buy,2015-03-10,84.53,443.6294806577547
USB,Buy,2015-03-10,44.48,281.02517985611513
WBA,Buy,2015-03-10,82.7,75.57436517533253
AIG,Sell,2015-03-11,54.4,149.1824800095477
BIIB,Sell,2015-03-11,382.54,21.683883670300887
C,Sell,2015-03-11,51.62,157.58951084215835
JNJ,Sell,2015-03-11,99.83,83.16699933466401
MSFT,Sell,2015-03-11,42.31,197.51915935845778
ORCL,Sell,2015-03-11,41.93,197.23865877712032
SBUX,Sell,2015-03-11,46.06,180.4142310745472
SLB,Sell,2015-03-11,81.44,100.55910864406098
TWX,Sell,2015-03-11,83.24,396.82539682539687
QCOM,Sell,2015-03-11,71.52,84.60809530255854
TWX,Sell,2015-03-11,83.24,147.8764935525849
ABBV,Buy,2015-03-11,56.19,222.4595123687489
AIG,Buy,2015-03-11,54.4,229.77941176470588
AMZN,Buy,2015-03-11,370.61,33.728177868918806
BAC,Buy,2015-03-11,15.92,785.1758793969849
FDX,Buy,2015-03-11,170.0,73.52941176470588
GM,Short,2015-03-11,37.6,332.4468085106383
MSFT,Buy,2015-03-11,42.31,295.43843063105646
SBUX,Buy,2015-03-11,46.06,271.3851498046027
AXP,Sell,2015-03-12,80.1,78.38956478113633
COF,Sell,2015-03-12,79.04,237.2817008352316
GM,Cover,2015-03-12,37.86,166.97835960459525
PM,Sell,2015-03-12,78.26,78.125
TWX,Sell,2015-03-12,83.47,295.7529871051698
USB,Sell,2015-03-12,44.13,281.02517985611513
WBA,Sell,2015-03-12,83.13,75.57436517533253
AIG,Buy,2015-03-12,54.98,202.0936906349784
AMZN,Buy,2015-03-12,368.82,30.126107887617568
AXP,Buy,2015-03-12,80.1,138.715494520738
BAC,Buy,2015-03-12,16.05,692.2810661128418
C,Short,2015-03-12,54.0,205.76131687242798
GOOG,Buy,2015-03-12,551.99,20.12918913587404
KO,Buy,2015-03-12,40.1,277.0850651149903
MSFT,Buy,2015-03-12,41.33,268.83888485630564
SBUX,Buy,2015-03-12,45.99,241.59841511439686
ABBV,Sell,2015-03-13,58.12,222.4595123687489
AIG,Sell,2015-03-13,55.52,229.77941176470588
AMZN,Sell,2015-03-13,371.52,33.728177868918806
BAC,Sell,2015-03-13,16.08,785.1758793969849
FDX,Sell,2015-03-13,173.59,73.52941176470588
GM,Cover,2015-03-13,38.39,332.4468085106383
MSFT,Sell,2015-03-13,40.7,295.43843063105646
SBUX,Sell,2015-03-13,46.34,271.3851498046027
ABBV,Buy,2015-03-13,58.12,191.17534602737632
AGN,Buy,2015-03-13,298.26,37.25310504630561
AIG,Buy,2015-03-13,55.52,100.06404098623118
C,Short,2015-03-13,53.9,103.07153164296022
CELG,Buy,2015-03-13,118.29,46.96555546162444
COF,Buy,2015-03-13,81.4,273.000273000273
DIS,Buy,2015-03-13,107.28,51.78556632695335
F,Buy,2015-03-13,15.77,352.28633833579937
FOXA,Short,2015-03-13,34.5,161.0305958132045
MS,Buy,2015-03-13,36.94,150.39403236479578
TWX,Buy,2015-03-13,84.43,131.60145814415623
WBA,Buy,2015-03-13,84.25,65.94131223211342
AIG,Sell,2015-03-16,55.63,202.0936906349784
AMZN,Sell,2015-03-16,370.38,30.126107887617568
AXP,Sell,2015-03-16,80.88,138.715494520738
BAC,Sell,2015-03-16,16.11,692.2810661128418
C,Cover,2015-03-16,53.74,205.76131687242798
GOOG,Sell,2015-03-16,549.44,20.12918913587404
KO,Sell,2015-03-16,40.0,277.0850651149903
MSFT,Sell,2015-03-16,41.47,268.83888485630564
SBUX,Sell,2015-03-16,47.0,241.59841511439686
TWX,Sell,2015-03-16,84.55,65.80072907207811
AGN,Buy,2015-03-16,300.58,166.34506620533637
F,Buy,2015-03-16,15.87,1575.2993068683052
SLB,Buy,2015-03-16,79.91,312.85195845325995
ABBV,Sell,2015-03-17,58.72,191.17534602737632
AGN,Sell,2015-03-17,308.34,37.25310504630561
AIG,Sell,2015-03-17,55.47,100.06404098623118
C,Cover,2015-03-17,53.21,103.07153164296022
CELG,Sell,2015-03-17,120.05,46.96555546162444
COF,Sell,2015-03-17,81.0,273.000273000273
DIS,Sell,2015-03-17,107.0,51.78556632695335
F,Sell,2015-03-17,15.99,352.28633833579937
FOXA,Cover,2015-03-17,34.14,161.0305958132045
MS,Sell,2015-03-17,36.37,150.39403236479578
TWX,Sell,2015-03-17,84.73,65.80072907207811
WBA,Sell,2015-03-17,86.51,65.94131223211342
AAPL,Buy,2015-03-17,125.9,18.471655245026508
AGN,Buy,2015-03-17,308.34,22.62678921335705
AMZN,Buy,2015-03-17,371.11,18.79966636858751
BAC,Buy,2015-03-17,16.07,144.7157059955717
BIIB,Buy,2015-03-17,386.95,36.060184447843454
BMY,Short,2015-03-17,67.61,34.39700333306963
CELG,Buy,2015-03-17,120.05,174.34596050095405
COF,Buy,2015-03-17,81.0,57.42176284811944
DIS,Buy,2015-03-17,107.0,21.734405564007826
F,Buy,2015-03-17,15.99,145.43973704495542
GILD,Buy,2015-03-17,100.8,23.07124400147656
KMI,Buy,2015-03-17,40.61,57.26622495318487
LOW,Buy,2015-03-17,74.98,31.016022877418475
MO,Short,2015-03-17,51.82,44.87806629387953
RTN,Buy,2015-03-17,108.85,21.365010522267685
SBUX,Buy,2015-03-17,46.88,49.6071116755298
TWX,Buy,2015-03-17,84.73,137.23482800359005
USB,Buy,2015-03-17,44.69,104.07614210556444
WBA,Buy,2015-03-17,86.51,26.882226278451476
WMT,Short,2015-03-17,82.98,28.025806162314257
AGN,Sell,2015-03-18,308.5,166.34506620533637
F,Sell,2015-03-18,15.91,1575.2993068683052
SLB,Sell,2015-03-18,80.06,312.85195845325995
BIIB,Sell,2015-03-18,391.47,24.040122965228967
CELG,Sell,2015-03-18,121.17,116.23064033396938
AGN,Buy,2015-03-18,308.5,30.388978930307943
BIIB,Buy,2015-03-18,391.47,87.81004930135131
BMY,Short,2015-03-18,67.27,46.4545859967296
CELG,Buy,2015-03-18,121.17,103.1608483948172
COF,Buy,2015-03-18,80.97,77.18908237618871
GM,Short,2015-03-18,38.17,81.87057898873461
GOOG,Buy,2015-03-18,550.99,5.671609285104993
GOOGL,Buy,2015-03-18,557.73,5.603069585641798
MO,Short,2015-03-18,51.36,60.84501557632399
RTN,Buy,2015-03-18,108.74,28.738274783888176
TWX,Buy,2015-03-18,85.86,218.37875611460515
AAPL,Sell,2015-03-19,128.75,18.471655245026508
AGN,Sell,2015-03-19,313.0,22.62678921335705
AMZN,Sell,2015-03-19,373.87,18.79966636858751
BAC,Sell,2015-03-19,15.96,144.7157059955717
BIIB,Sell,2015-03-19,400.49,12.020061482614484
BMY,Cover,2015-03-19,68.37,34.39700333306963
CELG,Sell,2015-03-19,123.8,58.11532016698469
COF,Sell,2015-03-19,81.48,57.42176284811944
DIS,Sell,2015-03-19,107.84,21.734405564007826
F,Sell,2015-03-19,16.02,145.43973704495542
GILD,Sell,2015-03-19,100.29,23.07124400147656
KMI,Sell,2015-03-19,41.48,57.26622495318487
LOW,Sell,2015-03-19,74.69,31.016022877418475






Are you sure you have sufficient capital to enter all the trades? You might want to turn on Detailed Log in the Analysis Settings to see why some trades are not being taken.

1 Like

Have you looked for missing dates in the date array? If the date of the CSV file does not exist in the symbol's data of your data base then no trade is entered.

For instance this line looks for equality

newbuy = dt == trade_datetime;

You may also have to check number of max. open positions as well as whether a symbol is allowed to have more positions at the same time.

1 Like

Thanks for your reply, mradtke. While it's not in the code I posted here, I have explored the capital and number of position issues with SetOption codes so there's ample capital and position overhead:

SetOption("InitialEquity", 100000000);
SetOption("MaxOpenPositions", 300);

However, in the Detailed Log, I do get the notification such as:

"AAPL not entered because of insufficient funds or wrong position size/value (reqEntryPrice: 118.55, reqEntryPosSize: 0 shares (value = 0), reqLotSize: 1).

Thanks for your reply, codejunkie. I don't know where you can check whether a symbol is allowed multiple simultaneous positions... is that pyramiding?

When looking at the date array in the debugger it was hard to read actual dates. It was an array of large floating point numbers. Can anyone tell what that means...or what we are looking for in the date array?

Also it takes some trades on a date but skips other trades on that same date.

I’m not really clear on what the Name() function ( if ( Name() == sym ) does and if that's part of the problem?

It sounds like your Position Size of 0 is your primary problem. I don't see a price of 118.55 for AAPL in your sample file, so it's hard to troubleshoot where the invalid position size is coming from. The two most obvious answers would be that you're reading a bad value from the file, or you're overwriting the position value after reading it. If you add some simple Exploration code, this should be easy to track down.

The Name() function just returns the current symbol. Your phase 1 AFL gets executed once for each symbol in your watchlist. So for example, if you have 500 symbols in your watch list then you are executing that code (and thus reading your entire text file) 500 times.

1 Like

@codejunkie ... I think you're right...it's the allowing more than one position in the same symbol.

The trades its dropping in my csv are the ones where it has a current position already in that symbol.

What's the best way to deal with this? Is it sigScaleIn...not sure how to use it?

Thanks, again, @mradtke. Appreciate your comments and consideration.

Bumping this old post. I've been unable to get this trade file backtest to run properly but not sure what I'm doing wrong. I have:

  1. Copied the AFL code from Tomasz's KB post into a new analysis.
  2. Created a trade file (in the example format) with just one Buy and one Sell for AAPL.
  3. Set the backtester to Current security (AAPL) with a date range covering the two trades.
  4. Verified the trade prices in the data file match the date/price in the data file (Norgate).
  5. Set the Report options to Trade List

When I run the backtest, I'm not getting any read errors but the trades are not generated.

Before starting the troubleshooting process recommended by Tomasz, and since more than one user mentioned problems getting this to work, I thought I might ask if others have had recent success with the code as posted in the KB article. I'm running AB 6.39.

TIA for any responses,
Jim

Okay, found the culprit testing with PrintF function. For some reason, the text file contained quotes so wasn't being read properly. Edited out the quotes and all is working well.

Spoke too soon. Not all trades getting executed, even with huge starting value and limiting position size to 50. While trying to troubleshoot this, I learned that the posted code (Tomasz's) appears to be ignoring the price listed in the file and instead executing the Buys at the High for stocks, and at the lows for indices ($SPX). Still digging to figure out why...

Hi @japple55 have you figured this out? Im havin the same problem.
Is this supposed to be generated automatically or should we use the backtest window. I used both but to no avail.

I have used the prinf function to check the values:

//(existing code)
 if ( trade == "Buy" )
             {
                 newbuy = dt == trade_datetime;
                 Buy = Buy OR newbuy; // combine previous buy signals with new
                 BuyPrice = IIf( newbuy, price, BuyPrice );
                 possize = IIf( newbuy, shares, possize );
                 //check
                 printf("Share Size = %s\n",NumToStr(possize));
                 printf("isBuy = %s\n",NumToStr(Buy)); 
                 printf("isnewbuy = %s\n",NumToStr(newbuy));
             }

and it printed these results
image

While my CSV only contains this values:
image

Your printf() calls are wrong. NumToStr would give you value at the END of selected date range.
If you want to print anything you have to use either scalar values representing current state, or iterate through bars and select appropriate bar or Lookup function to lookup the value at given bar.

The code given in the Knowledge Base JUST WORKS, with ANY version of AmiBroker. Just follow closely the article and make sure your INPUT FILE is correctly formatted and does not have unexpected characters