I tweaked the formula in this knowledge base How generate backtest statistics from a list of historical trades stored in a file
I made it work to most of my Trades however i realized that it is only generating the first entry(Buy/Short) and first exit (Sell/Cover).
Is it possible to generate the series of entries and series of exits? If so hope you can guide me achieve that.
Here is the CSV where the data is being taken from:
Symbol,Trade,Datetime,Price,Shares,Time
AMD,Buy,2023-12-12,133.15,30,9:35:10
AMD,Buy,2023-12-12,133.5,40,9:36:57
AMD,Sell,2023-12-12,133.9,35,9:37:58
AMD,Sell,2023-12-12,134.13,28,9:40:11
AMD,Sell,2023-12-12,133.51,7,9:41:47
AMD,Buy,2023-12-12,133.35,30,9:44:39
AMD,Sell,2023-12-12,133.73,24,9:50:26
AMD,Sell,2023-12-12,134.09,6,9:51:34
AMD,Buy,2023-12-14,139.73,30,9:33:03
AMD,Sell,2023-12-14,138.68,30,9:35:03
Only the trades (in Bold) above were generated and did not include the other lines. Here's what the backtesting report looked like
Here's the my formula (I have included time and changed the Datetime):
file = "G:\\My Drive\\AmiBroker\\AFL\\Amibroker-Journal-trades.csv"; // change this to real location of your data file
//DATE
yr = Year();
mo = Month();
dy = day();
//TIME
hh = Hour();
mm = Minute();
// Initialize variables
Buy = Sell = Short = Cover = 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 );
mYear = StrToNum( StrExtract(StrExtract(line, 2),0,seperator='-'));
mMonth = StrToNum( StrExtract(StrExtract(line, 2),1,seperator='-'));
mDay = StrToNum( StrExtract(StrExtract(line, 2),2,seperator='-'));
trade_datetime = NumToStr(mYear)+"-"+NumToStr(mMonth)+"-"+NumToStr(mDay);
price = StrToNum( StrExtract( line, 3 ) );
shares = StrToNum( StrExtract( line, 4 ) );
mHour = StrToNum( StrExtract(StrExtract(line, 5),0,seperator=':'));
mMin = StrToNum( StrExtract(StrExtract(line, 5),1,seperator=':'));
//BUY
if ( trade == "Buy" )
{
newbuy = yr==mYear AND mo==mMonth AND dy == mDay AND hh==mHour AND mm==mMin;
Buy = Buy OR newbuy; // combine previous buy signals with new
BuyPrice = IIf( newbuy, price, BuyPrice );
possize = IIf( newbuy, shares, possize );
}
//SELL
if ( trade == "Sell" )
{
newsell = yr==mYear AND mo==mMonth AND dy == mDay AND hh==mHour AND mm==mMin;
Sell = Sell OR newsell; // combine previous sell signals with new
SellPrice = IIf( newsell, price, SellPrice );
possize = IIf( newsell, shares, possize );
}
//SHORT
if ( trade == "Short" )
{
newshort = yr==mYear AND mo==mMonth AND dy == mDay AND hh==mHour AND mm==mMin;
Short = Short OR newshort; // combine previous short signals with new
ShortPrice = IIf( newshort, price, ShortPrice );
possize = IIf( newshort, shares, possize );
}
//COVER
if ( trade == "Cover" )
{
newcover = yr==mYear AND mo==mMonth AND dy == mDay AND hh==mHour AND mm==mMin;
Cover = Cover OR newcover; // combine previous cover signals with new
CoverPrice = IIf( newcover, price, CoverPrice );
possize = IIf( newcover, shares, possize );
}
}
}
//
fclose( fh );
}
else
{
Error( "ERROR: file can not be open" );
}
//
SetPositionSize( possize, spsShares );
Hope you can help out. Thanks in Advance