Switching sell rules with if statements - slow backtest & runs out of RAM

Hi there,

I'm looking for some help with a problem in my code. I'm writing a system that switches sell instructions based on whether a ticker's index is falling above or below it's moving average.

I believe I'm coding it correctly as you would with if statements, but I'm finding that the backtest is very slow to run and Amibroker also runs out of memory when I backtest it on an entire stock market. (I'm using the 32 bit version).

Am I coding it correctly? If so, is there another way to code it so that it can run more efficiently and won't run out of memory?

Thanks in advance.

TwoFiftyHigh = Ref( HHV( C, 250), -1 );
TwoFiftyLow = Ref( LLV( C, 250), -1 );

IndexChart = "SPX";
ic = Foreign( IndexChart, "C" );
MAperiod = 200;

IndexMA = MA(ic, MAperiod);
IndexMAtrend = IIf(ic > IndexMA, True, False);
IndexMAdowntrend = IIf(ic < IndexMA, True, False);

signalHighestSince = 0.9*HighestSince(IndexMAdowntrend,H,1);

tenpcStopLoss = IIf(C<=signalHighestSince,True, False);

Buy = Close > TwoFiftyHigh & IndexMAtrend ; 

Sell = 0;


for( i = 1; i < BarCount; i++ )
{
	if ( IndexMAdowntrend[ i ] == True )
	{
		_TRACE ("downTrend");
		Sell[ i ] = tenpcStopLoss[i];
		SellPrice[ i ] = C[ i ];
	}

	if ( IndexMAdowntrend[ i ] == False ){
		_TRACE ("upTrend");
		Sell[ i ] = Close[i] < TwoFiftyLow[i];
		SellPrice[ i ] = C[ i ];
	}

}


SetOption("MaxOpenPositions", 20);

SetPositionSize(5, spsPercentOfEquity);

A necessary read for you:

https://www.amibroker.com/guide/x_performance.html

You are aware that you don't need this loop, aren't you?

You could remove entire for loop and just write:

Sell = IIF( IndexMAdowntrend, tenpcStopLoss, Close < TwoFiftyLow );
SellPrice = Close;

Having said that, the loop has nothing to do with running out of memory. You just need to read this:
https://www.amibroker.com/guide/x_performance.html

1 Like

Hi Tomasz,

Thanks for your reply and the info. I was able to resolve the issue.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.