Different arrows from backtest and from plotshapes

I am trying to backtest simple test idea, but getting different results when ploting trades using backtest (there are "wrong entries") and using plotshapes - I get correct entries there.

Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
pr = Param( "ZigZag change amount", 2, 0.05, 20, 0.05 );

pk = PeakBars( H, pr ) == 0;

lastSwingH=ValueWhen(pk>0,H,1);

Plot( lastSwingH, "", colorBlue , styleNoLabel );

Buy = C>lastSwingH;
Sell = True;
Sell = ExRem( Sell, Buy );
Buy = ExRem( Buy, Sell );

shape = Buy * shapeUpArrow;

PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

Screenshot:
buyarrow

Any tip why I get the buying arrows on different positions in backtest and in plot?

Thank you.

when using

Sell = True; that means every element in the Sell array is 1. Where do you want to sell?

here some code to get rid of the multiple buy signals:

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
pr = Param( "ZigZag change amount", 2, 0.05, 20, 0.05 );

pk = PeakBars( H, pr ) == 0;
plotShapes( shapeSmallCircle * pk, ColorRGB( 0, 250, 0 ), 0, H, 10 );

lastSwingH = ValueWhen( pk, H, 1 );

Plot( lastSwingH, "", colorBlue , styleNoLabel );

Buy = C > lastSwingH;
Buy = ExRem( Buy, pk );
//Sell = True;
//Sell = ExRem( Sell, Buy );
//Buy = ExRem( Buy, Sell );

shape = Buy * shapeUpArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
1 Like

I am using Sell = True; just to exit the open position somehow. I understand the system will exit the position on the exact moment after entry.

So the code does not have exit logic, it just closes open positions asap.

What I do not understand why there are sometimes Arrows (for opening of trades) using PlotShapes and not arrows from backtest (when I use Show arrows from actual traders after backtest).

Using your modification, the situation is the same:

arrows

I just get trades from backtest sometimes on different places than I from Plotting Buy shape.

I tried to debug the code and found out that the value pk = PeakBars( H, pr ) == 0; is different in plot and in exploration (thus also in backtest).

I use following code:

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
pr = Param( "ZigZag change amount", 2, 0.05, 20, 0.05 );

pk = PeakBars( H, pr ) == 0;
plotShapes( shapeSmallCircle * pk, ColorRGB( 0, 250, 0 ), 0, H, 10 );

lastSwingH = ValueWhen( pk, H, 1 );

Plot( lastSwingH, "", colorBlue , styleNoLabel );

cond1 = C > lastSwingH;

Buy = cond1;
Buy = ExRem( Buy, pk );
Sell = True;

Filter = 1; // show all bars

bi = BarIndex();
//
AddColumn( C, "Close" );
AddColumn( bi, "BarIndex" );
AddColumn( lastSwingH, "lastSwingH" );
AddColumn( pk, "PeakBar",1,colorDefault,IIf(pk,colorRed,colorDefault));
AddColumn( cond1, "C>lastSwingH",1,colorDefault,IIf(cond1,colorGreen,colorDefault));

shape = Buy * shapeUpArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

In plot I can see PeakBar for example on 18.Dec 2017:

AAPL_pk

But in exploration the pk has 0 value:

AAPL_pk2

Do you have any idea what am I doing wrong?

Thank you.

for the exploration and backtest you have to set the parameter not in the chart but in the parameter window of the Analysis window. It has to be the same value as you use in the chart (if you want to see the same result). Did you check this?

http://www.amibroker.com/kb/2015/01/28/why-analysis-results-and-chart-output-may-differ/

1 Like

Thank you for the reminder. What a stupid mistake - I did have different parametr settings in Backtest windonw.