How to make it work in AmiBroker 6?

Hello,

Can someone point out how to make this afl work again in amibroker 6? When I run it in my 5.5 it still working...

thanks

http://www.amibroker.com/kb/2014/09/30/gen-backtest-from-a-file/

file = "C:\\TEMP\\trades.csv"; // change this to real location of your data file
dt = DateTime();
//
// 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 );
             }
         }
     }
     //
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }
//
SetPositionSize( possize, spsShares ); 

@sikatgigi the code works in AmiBroker 6 too.

I simply added the following code line to override any non-default settings that I may have in my general configuration:

SetTradeDelays(0, 0, 0, 0);

I tested it with the following sample trades.csv file

Symbol,Trade,Date,Price,Shares
AAPL,Buy,2018-05-01,164.41,100
AAPL,Sell,2018-07-02,187.18,100

immagine

If it still does not work, try to use the "Detailed Log" of the backtester settings dialog to see why some trades are not entered.

immagine

and add some _TRACE instructions to the formula to see what values are read from the supplied file (check for valid dates, correct share sizes, etc.)

2 Likes