Different values in Analysis tab VS. Chart

Hi, I’m comparing my stop length values between the Analysis tab and chart and I’m getting different values. That means Amibroker is exiting position at different instances.

I’ve added the stop length on the Analysis tab via AddCustomMetric. On the Chart I just chose to plot the variable value.

My stop length is calculated in a “for” loop because I need to plot it on the chart. Here is the code:

trailARRAY = Null;
trailstop = 0;

for( i = 0; i < BarCount - 1 ; i++)
{
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[i] - StopLength [i];
}
else
{
Buy[ i ] = 0; // remove excess buy signals
}

if( trailstop > 0 AND Low[ i ] < trailstop )
{
	Sell[ i ] = 1;
	SellPrice[ i ] = trailstop;
	trailstop = 0;
}

if(trailstop > 0)
{
	trailstop = Max( High[i] - StopLength [i], trailstop );
	trailARRAY[ i ] = trailstop;
}

}

for( i = 0; i < BarCount - 1 ; i++)
{

if( trailstop == 0 AND Short[ i ] )
{
	trailstop = Low[i] + StopLength [i];
}
else
{
	Short[ i ] = 0; // remove excess short signals
}
	
if( trailstop > 0 AND High[ i ] > trailstop )
{
	Cover[ i ] = 1;
	CoverPrice[ i ] = trailstop;
	trailstop = 0;
}

if(trailstop > 0)
{
	trailstop = Min( Low[i] + StopLength [i], trailstop );
	trailARRAY[ i ] = trailstop;
}

}

Any clues?

Thank you all very much.

Best
Claudio

First your ‘for’ loops are wrong - one bar too short.

Yes Tomasz, I guess I took < for <=… Sorry for the lack of attention. I fixed this error and executed the code again. It still doesn’t work.

To be more specific, the code works in most cases. However, there is a very specific situation where it doesn’t… It’s when the stop happens to be very close to High or Low Prices, depending whether it’s a Buy or a Sell. In the Chart, the candles do not touch the stop and so the trade goes on. However, on the Analysis, the trade stops just in the candle where the High or Low almost touches the stop in the Chart.

The problem seems to be that something which is taken into account on the Analysis isn’t considered in the Chart. This difference suffices to stop the trade in the Analysis but not in the Chart. The problem seems to be minor but might hide a bigger one. Could you please tell me what do you think is happening?

Thank you!
Claudio

Recommended reading
http://www.amibroker.com/kb/2008/07/03/quickafl/

Your chart may be using less bars than Analysis and it may affect recursive calculations (if you do any in other parts of your formula that you simply did NOT attach, so no one without magic wand can figure out what they are)

Sorry for bothering you again with a false problem… This problem was caused by the fact that my configuration for the stop ATR was DIFFERENT in the Chart and in the Analysis… So, it is natural that the results were different. I don’t know why but the default values didn’t work very well and I got different values in each case. The difference was small, so I only took notice of them in very specific situations…

However, I took the time to read about QuickAFL and I find it might be useful in the future.

Thanks again for the help,
Claudio