Backtest From Buy and Sell Signals in csv.file - Position Size Issue

I have several strategies where the Buy and Sell signals and Dates are in a csv file. The afl below successfully reads the signals and backtest it. I have read this post that talks about this

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

(however in this example, he is also bringing in the SHARES... I am not).. I am trying to us Set Position Size as percent of equity..

My Issue, I am having an issue with the Position Size.... This strategy contains 5 positions and I have the Positionsize set to 20% of equity.... the problem is as you can see in the image... if my portfolio equity is 10,000 .. and sell 2 positions that generate proceeds of say $1,000... when I goto to buy 2 new positions @ 20% of equity... I don't have enough money so I only partially buy 1 ... What I need to figure out... is there a way to base the position size on the Cash Available + Proceeds from Sells..?

I want each BUY to equally buy based on the Cash +Closed Trade Proceeds...... (if I have 2 buys, then its 50% for each... 4 buys its 25% for each, etc)

Any Ideas?

amipic

Code below... and below the code is the csv file

Wlist = CategoryFind("csvSmartMoney5",categoryWatchlist);
file = "C:\\Program Files\\AmiBroker\\Formulas\\Custom\\Strategies\\EQ Smart Money Five\\aflSmartMoney5.csv"; // change this to real location of your data file
TotalPositions =5;
PosSize = 20;

dt = DateTime();
Buy = Sell = possize = 0; 
SetOption("AccountMargin",100);

fh = fopen( file, "r" );
if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         
         sym = StrExtract( line, 0 );
         CategoryAddSymbol( sym, categoryWatchlist, Wlist ); 
     }
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }

// Run and Process Trades
fh = fopen( file, "r" );
if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         sym = StrExtract( line, 0 );
         if ( Name() == sym )
         {
             trade = StrExtract( line, 1 );
             trade_datetime = StrToDateTime( StrExtract( line, 2 ) );
             if ( trade == "Buy" )
             {
                 newbuy = dt == trade_datetime;
                 Buy = Buy OR newbuy; 
             }
             if ( trade == "Sell" )
             {
                 newsell = dt == trade_datetime;
                 Sell = Sell OR newsell; 
             }
         }
     }
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }
SetPositionSize( 20, spsPercentOfEquity ); 

CSV File
Symbol Trade Date
VTR Buy 5/16/2001
CBS Buy 5/16/2001
MO Buy 5/16/2001
HCA-200611 Buy 5/16/2001
L-200605 Buy 5/16/2001
VTR Sell 8/15/2001
CBS Sell 8/15/2001
MO Sell 8/15/2001
SCZ.B-200205 Buy 8/15/2001
NHI Buy 8/15/2001
SPY Buy 8/15/2001
L-200605 Sell 11/15/2001
SPY Sell 11/15/2001
CBS Buy 11/15/2001
GDT-200604 Buy 11/15/2001
SCZ.B-200205 Sell 2/15/2002
CBS Sell 2/15/2002
GDT-200604 Sell 2/15/2002
ACN Buy 2/15/2002
NKE Buy 2/15/2002
WLP-200411 Buy 2/15/2002
ACN Sell 5/16/2002
NKE Sell 5/16/2002
WLP-200411 Sell 5/16/2002
JCI Buy 5/16/2002
TSM Buy 5/16/2002
CBS Buy 5/16/2002
NHI Sell 8/15/2002
JCI Sell 8/15/2002
CBS Sell 8/15/2002
MSFT Buy 8/15/2002
CDWC-200710 Buy 8/15/2002
IGT-201504 Buy 8/15/2002
TSM Sell 11/15/2002
L-200605 Buy 11/15/2002
CDWC-200710 Sell 2/14/2003
SPY Buy 2/14/2003
SPY Sell 5/16/2003
IGT-201504 Sell 5/16/2003
CBS Buy 5/16/2003
YUM Buy 5/16/2003
CBS Sell 8/15/2003
L-200605 Sell 8/15/2003
HCA-200611 Sell 8/15/2003
C Buy 8/15/2003
TWX-201806 Buy 8/15/2003
KMX Buy 8/15/2003
C Sell 11/14/2003
KMX Sell 11/14/2003
NTES Buy 11/14/2003
LXK-201611 Buy 11/14/2003
NTES Sell 2/13/2004
MSFT Sell 2/13/2004
TWX-201806 Sell 2/13/2004
CFC-200806 Buy 2/13/2004
SHY Buy 2/13/2004
WLP-200411 Buy 2/13/2004
YUM Sell 5/14/2004
WLP-200411 Sell 5/14/2004
AMX Buy 5/14/2004
MSFT Buy 5/14/2004
SHY Sell 11/15/2004
CFC-200806 Sell 11/15/2004
LXK-201611 Sell 11/15/2004
STRA Buy 11/15/2004
MCIP-200601 Buy 11/15/2004
LEN Buy 11/15/2004
MSFT Sell 2/15/2005
MCIP-200601 Sell 2/15/2005
APOL-201702 Buy 2/15/2005
FOX Buy 2/15/2005
APOL-201702 Sell 5/16/2005
LEN Sell 5/16/2005
FOX Sell 5/16/2005
BBY Buy 5/16/2005
FSL-200612 Buy 5/16/2005
GOOGL Buy 5/16/2005
STRA Sell 8/15/2005
BBY Sell 8/15/2005
FOX Buy 8/15/2005
APOL-201702 Buy 8/15/2005
FOX Sell 11/15/2005
APOL-201702 Sell 11/15/2005
WEN Buy 11/15/2005
BB Buy 11/15/2005
FSL-200612 Sell 2/15/2006
BB Sell 2/15/2006
MON-201806 Buy 2/15/2006
S Buy 2/15/2006
GOOGL Sell 5/16/2006
WEN Sell 5/16/2006
ORCL Buy 5/16/2006
CSCO Buy 5/16/2006
ORCL Sell 8/15/2006
S Sell 8/15/2006
FDC-200709 Buy 8/15/2006
MSFT Buy 8/15/2006
MON-201806 Sell 11/15/2006
FDC-200709 Sell 11/15/2006
QCOM Buy 11/15/2006
TPR Buy 11/15/2006
TPR Sell 2/15/2007
WU Buy 2/15/2007
WU Sell 5/16/2007
GOOGL Buy 5/16/2007
QCOM Sell 8/15/2007
EMC-201609 Buy 8/15/2007
EMC-201609 Sell 11/15/2007
QCOM Buy 11/15/2007
GOOGL Sell 2/15/2008
MA Buy 2/15/2008
MA Sell 5/16/2008
CSCO Sell 5/16/2008
GOOGL Buy 5/16/2008
AAPL Buy 5/16/2008
QCOM Sell 8/15/2008
AAPL Sell 8/15/2008
LO-201506 Buy 8/15/2008
TGT Buy 8/15/2008
GOOGL Sell 11/14/2008
MSFT Sell 11/14/2008
QCOM Buy 11/14/2008
V Buy 11/14/2008
AMX Sell 2/13/2009
LO-201506 Sell 2/13/2009
V Sell 2/13/2009
TGT Sell 2/13/2009
MSFT Buy 2/13/2009
MA Buy 2/13/2009
GOOGL Buy 2/13/2009
XTO-201006 Buy 2/13/2009
MSFT Sell 5/15/2009
XTO-201006 Sell 5/15/2009
V Buy 5/15/2009
AAPL Buy 5/15/2009
QCOM Sell 11/13/2009
GOOGL Sell 11/13/2009
JPM Buy 11/13/2009
MON-201806 Buy 11/13/2009
MON-201806 Sell 2/12/2010
MA Sell 2/12/2010
PFE Buy 2/12/2010
WFC Buy 2/12/2010
WFC Sell 5/14/2010
GOOGL Buy 5/14/2010
GOOGL Sell 8/13/2010
V Sell 8/13/2010
MSFT Buy 8/13/2010
CIT Buy 8/13/2010
CIT Sell 2/15/2011
C Buy 2/15/2011
C Sell 5/16/2011
AMZN Buy 5/16/2011
MSFT Sell 8/15/2011
AMZN Sell 8/15/2011
GOOGL Buy 8/15/2011
C Buy 8/15/2011
PFE Sell 11/15/2011
C Sell 11/15/2011
BKNG Buy 11/15/2011
AMZN Buy 11/15/2011
AMZN Sell 2/15/2012
NTAP Buy 2/15/2012
NTAP Sell 5/16/2012
QCOM Buy 5/16/2012
JPM Sell 8/15/2012
CTSH Buy 8/15/2012
CTSH Sell 11/15/2012
AIG Buy 11/15/2012
QCOM Sell 2/15/2013
RL Buy 2/15/2013
AIG Sell 5/16/2013
RL Sell 5/16/2013
QCOM Buy 5/16/2013
DG Buy 5/16/2013
GOOGL Sell 8/15/2013
CTSH Buy 8/15/2013
CTSH Sell 11/15/2013
LBTYA Buy 11/15/2013
LBTYA Sell 2/14/2014
AIG Buy 2/14/2014
DG Sell 5/16/2014
QCOM Sell 5/16/2014
AIG Sell 5/16/2014
LBTYK Buy 5/16/2014
MON-201806 Buy 5/16/2014
FOXA Buy 5/16/2014
MON-201806 Sell 8/15/2014
FOXA Sell 8/15/2014
DG Buy 8/15/2014
CHTR Buy 8/15/2014
DG Sell 11/14/2014
CHTR Sell 11/14/2014
MA Buy 11/14/2014
VRX-201009 Buy 11/14/2014
AAPL Sell 2/13/2015
VRX-201009 Sell 2/13/2015
CHTR Buy 2/13/2015
FLT Buy 2/13/2015
FLT Sell 5/15/2015
MA Sell 5/15/2015
MU Buy 5/15/2015
GOOG Buy 5/15/2015
MU Sell 8/14/2015
GOOG Sell 8/14/2015
CHTR Sell 8/14/2015
AGN Buy 8/14/2015
FLT Buy 8/14/2015
JD Buy 8/14/2015
BKNG Sell 11/16/2015
AGN Sell 11/16/2015
JD Sell 11/16/2015
FLT Sell 11/16/2015
LBTYK Sell 11/16/2015
GM Buy 11/16/2015
HCA Buy 11/16/2015
DAL Buy 11/16/2015
NXPI Buy 11/16/2015
GT Buy 11/16/2015
GM Sell 2/16/2016
HCA Sell 2/16/2016
DAL Sell 2/16/2016
NXPI Sell 2/16/2016
GT Sell 2/16/2016
AAPL Buy 2/16/2016
BKNG Buy 2/16/2016
FLT Buy 2/16/2016
CHTR Buy 2/16/2016
TDG Buy 2/16/2016
TDG Sell 5/17/2016
FLT Sell 5/17/2016
FB Buy 5/17/2016
AGN Buy 5/17/2016
FB Sell 8/16/2016
AAPL Sell 8/16/2016
AMZN Buy 8/16/2016
GOOG Buy 8/16/2016
AGN Sell 11/17/2016
AMZN Sell 11/17/2016
FB Buy 11/17/2016
AAPL Buy 11/17/2016
AAPL Sell 2/15/2017
GOOG Sell 2/15/2017
BKNG Sell 2/15/2017
CHTR Sell 2/15/2017
FLT Buy 2/15/2017
TDG Buy 2/15/2017
AGN Buy 2/15/2017
AMZN Buy 2/15/2017
AMZN Sell 5/16/2017
TDG Sell 5/16/2017
CHTR Buy 5/16/2017
GOOG Buy 5/16/2017
GOOG Sell 8/15/2017
FLT Sell 8/15/2017
MSFT Buy 8/15/2017
BABA Buy 8/15/2017
CHTR Sell 11/15/2017
MSFT Sell 11/15/2017
GOOG Buy 11/15/2017
BKNG Buy 11/15/2017
BKNG Sell 2/15/2018
AGN Sell 2/15/2018
MSFT Buy 2/15/2018
AMZN Buy 2/15/2018
AMZN Sell 5/15/2018
MU Buy 5/15/2018
MU Sell 8/15/2018
AABA Buy 8/15/2018

Well... I got closer (I think).... my only problem is now.. I cant get my first signals (5 buys) to buy 5 positions at 20% each... its buying 3 postions at 33% each? Any ideas?

// Start Date 05-16-2001
Strategy = "~SmartFive(0%)";
Wlist = CategoryFind("csvSmartMoney5",categoryWatchlist);
file = "C:\\Program Files\\AmiBroker\\Formulas\\Custom\\Strategies\\EQ Smart Money Five\\aflSmartMoney5.csv"; // change this to real location of your data file

TotalPositions =5;
PosSize = 20;

dt = DateTime();
Buy = Sell = possize = 0; 
SetOption("AccountMargin",100);

fh = fopen( file, "r" );
if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         
         sym = StrExtract( line, 0 );
         CategoryAddSymbol( sym, categoryWatchlist, Wlist ); 
     }
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }

// Run and Process Trades
fh = fopen( file, "r" );
if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         sym = StrExtract( line, 0 );
         if ( Name() == sym )
         {
             trade = StrExtract( line, 1 );
             trade_datetime = StrToDateTime( StrExtract( line, 2 ) );
             if ( trade == "Buy" )
             {
                 newbuy = dt == trade_datetime;
                 Buy = Buy OR newbuy; 
             }
             if ( trade == "Sell" )
             {
                 newsell = dt == trade_datetime;
                 Sell = Sell OR newsell; 
             }
         }
     }
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be open" );
 }



SetPositionSize( 20, spsPercentOfEquity ); 

//<<<<<<<<<<<<<<<<<<<<<< NEW CODE ADDED TO MANUALLY ADJUST Position Size based on Cash Available AFTER SELLS>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

SetCustomBacktestProc( "" );

if( Status( "action" ) == actionPortfolio ) 
{
	
    bo = GetBacktesterObject();			
    bo.PreProcess();			
    PosQty = 5; 
	for( i = 0; i < BarCount; i++ )	
	{	
			
		Cnt=0;
		cnt = bo.GetSignalQty(i,1);
		
		for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) 
		{ 
			if (sig.isexit() && sig.IsLong())
			bo.ExitTrade(i, sig.Symbol, sig.Price); 
		}

		CurrentPositions=0;
		for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() ) 
		{
			CurrentPositions += pos.GetPositionValue;
		}
		
		NetInvest = bo.Equity - CurrentPositions;
		Adjust1 = 100*((NetInvest / bo.Equity) / Cnt);
		
		// *** process entries
		for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) 
		{ 
			if (sig.IsEntry())
			{
				sig.PosSize = -Adjust1;
				bo.EnterTrade(i, sig.Symbol, sig.IsLong(), sig.Price, sig.PosSize); 
			}
		}
	
		bo.UpdateStats(i,1);
		bo.UpdateStats(i,2);
	
	}
		
	
    bo.PostProcess();
}


1 Like

Here is my first bar...its buying 3 positions at 33% each... should be 5 positions at 20% each

ami5

Ooops... I was a bit off on what I thought the problem was.... the 08/15/2001 trades above are just fine. I just realized the start date is 05/16/2001..... so what is happening with my above code is, I am not getting the 05/16/01 trades.... my guess is due to the fact I am processing exits prior to entryes in CBT?... can someone please help... how can I get the 05/16/01 (StartDate) to appear as the first trades???

ami6

I AM AN IDIOT!!!!!!
I feel stupid... all I had to do was change my from range to 1 day before the actual start date...hopefully, this code will help someone on here... SOLVED

ami7