Custom backtest - buy based on previous trade outcomes


I am using a portfolio custom backtest which calculates the performance per stock (from AB KB). This part is working fine. I'm wondering is there a way to use the report list in a second layer of backtesting? ie. buy if the previous n trades (of the stock in question) are profitable. Or am I doing it the wrong way?


Should have read this first. Looping of custom backest Looks like I'm doing it the wrong way.

Still, I don't know the general process for looking at the previous n trades, then entering (or not) based upon their profitability, and doing that for each stock. Is it possible? thanks/


one possible approach can be to loop through all closed trades at each entry signal and then decide to skip it or not based on last trade result.

Maybe there are more efficient ways to do this but I'm not sure if we can access last trade result directly instead of having to loop through all closed trades each time we have an entry signal, but this is one way to do it.

// sample buy, sell rules

if (Status("action") == actionPortfolio) 
    _date=DateTime() ;
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)
    for (i = 0; i < BarCount; i++)	//  Loop through all bars
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) ) // custom signal processing
			if ( sig.IsEntry() )	 
				skip=Nz(StaticVarGet("skip"+sig.Symbol)); //this variable indicates to skip signal or not
                _TRACE("#AB_1 "+"date: "+DateTimeToStr(_date[i]) +" "+sig.symbol+" skip: "+skip);
				if( skip==1  ) 	// skipped last trade so do not modify signal and pass to next signal
				 skip=0; //skip variable back to zero
				 _TRACE("#AB_2 "+"date: "+DateTimeToStr(_date[i])+sig.symbol+" skip variable back to zero");  
				 continue; // pass to next signal
				if( skip==0 ) //entry signal and not skipped trade, evaluate previous trade result
					for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
								if (trade.getprofit()<0) 
									skip=1; // skip signal if last trade was a loss
					_TRACE("#AB_3 "+"date: "+DateTimeToStr(_date[i])+sig.symbol+" "+skip);	
				if(skip==1  ) 
				  sig.Price=-1; //skip this signal
				  _TRACE("#AB_4 "+"date: "+DateTimeToStr(_date[i])+sig.symbol+" skip trade");  
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
    bo.PostProcess(); //  Do post-processing (always required)

Thanks very much Aron.

When I alter this line:

if (trade.getprofit()<0)


if (trade.getprofit()< -5)

I get the same equity curve. Since I don't follow the logic I can't tell what's happening, but your efforts are apreciated.

In the Amibroker guide, you can see

GetProfit () // Method from the trade object
retrieves current dollar (point) profit of the trade

so you are just checking and the difference is 5 points.

1 Like

Thanks mate. Have changed it to:

if (trade.getpercentprofit()< -5) 

It still takes every trade, even when the previous one lost more than 5%.

The logic behind the CBT part is to skip an entry signal if the last trade result was a loss.

You can add more trace statements to follow the logic if it's not clear.

I tested the code before posting and it's working.

For example without the CBT part, we get 6 trades:

With CBT we get 4 trades. Both Trades after loss are excluded (11/05/2012 and 24/06/2016) :