Getting different signals for apply option in formula editor and back testing( show arrows for actual trade)

Buy=Cross(MACD(), Signal())
     ;
Sell=Cross(Signal(), MACD());
    
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorBlue, colorYellow ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;

amibroker%20signal%20prob

in the image , above one is giving signal form backtesting and below one is giving signal form apply option. what can be done to backtest for trades generated in apply one.

One reason that Analysis results can differ from Chart results is when you use Pad & Align, which only applies to the Analysis. If you're trying to get the signals to match up, turn Pad & Align off for now.

To help find your problem, I would suggest a couple of things:

  1. Add a MACD pane to your chart, so you can see where the crossovers are occurring in the Chart
  2. Add some Exploration code so that you can see where MACD crossovers, Buy Signals, and Sell Signals are occurring in the analysis (back test).

Also, since your code comes directly from one of the examples for the PlotShapes documentation, it would be appropriate for you to note that in your post.

hi , thankyou , but Pad & Align is off .
Capture

Backtester takes into account your initial equity when taking trades.

Your lower chart window is just applying code you have written and not backtesting.

but the bactesting is showing unnesessary buy sell signal what should i do generate signals like lower chart window

@mayank you need to be able to debug your code. I gave you a couple of suggestions already, which you appear not to have tried. There are additional pointers here: How do I debug my formula?

axis

explore

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

Buy=Cross(MACD(), Signal())
     ;
Sell=Cross(Signal(), MACD());
    
 Filter = 1; // show all bars

cond1=Cross(MACD(), Signal())
     ;
cond2= Cross(Signal(), MACD());
bi = BarIndex();
//
AddColumn( C, "Close" );
AddColumn( bi, "BarIndex" );
AddColumn( cond1, "MACDup",1,colorDefault,IIf(cond1,colorGreen,colorDefault));

AddColumn( ValueWhen( cond1, bi ), "ValueWhen( cond1, BarIndex() )" );
AddColumn( ValueWhen( cond1, Close), "ValueWhen( cond1, Close )" );

AddColumn( ValueWhen( cond2, bi ), "ValueWhen( cond2, BarIndex() )" );
AddColumn( cond2, "SIGNALup",1,colorDefault,IIf(cond2,colorRed,colorDefault));
AddColumn( ValueWhen( cond2, Close), "ValueWhen( cond2, Close )" );   
    
    
    
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

AddColumn( MACD(), "MACD", 1.4 , IIf( MACD() > Signal(), colorGreen, colorRed ) );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorBlue, colorYellow ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;

backtesting is showing unnecessary buy sell signal what should I do generate signals more like lower chart window

explore .....http://www.amibroker.com/kb/2014/09/29/debugging-techniques-part-1-exploration/

You're making progress. Add a Signal() column to your Exploration, and then compare both MACD and Signal in the Exploration to the values in the chart. If you look at a specific date and time, do the values match? If you look at a date and time where the chart says there's a cross, do you see that cross in the exploration results?

1 Like

mradtke thank you for your reply and all the advices you have given me till now i was able to do see things in AB, i will add a Signal() column to my exploration. and now when i look to compare the crossover of MACD with exploration it shows signals when crossover happens. and when i compare same with backtesting results that is showing the same signals too. it now just that when i right click on backtest result and click on show arrows for trades its showing wrong signals on chart .

So have you checked your backtesting rules against your"'Applying" code.Backtesting will give you the true results of your current settings against the "Applying only " code. Of course there will be differences if they are not exactly the same.

ok, but I was saying that now the backtesting and its signal arrows on charts are not same
ax%202

bt%202

Would you mind opening the analysis window where you backtested and click on the EDITt button at the far right and post the code that opens.

I ran a backtest with the code previously posted and signals match the MACD.

1 Like

hi, yes sure

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

Buy=Cross(MACD(), Signal())
     ;
Sell=Cross(Signal(), MACD());
    
 Filter = 1; // show all bars

cond1=Cross(MACD(), Signal())
     ;
cond2= Cross(Signal(), MACD());
bi = BarIndex();
//
AddColumn( C, "Close" );
AddColumn( bi, "BarIndex" );
AddColumn( cond1, "MACDup",1,colorDefault,IIf(cond1,colorGreen,colorDefault));

AddColumn( ValueWhen( cond1, bi ), "ValueWhen( cond1, BarIndex() )" );
AddColumn( ValueWhen( cond1, Close), "ValueWhen( cond1, Close )" );

AddColumn( ValueWhen( cond2, bi ), "ValueWhen( cond2, BarIndex() )" );
AddColumn( cond2, "SIGNALup",1,colorDefault,IIf(cond2,colorRed,colorDefault));
AddColumn( ValueWhen( cond2, Close), "ValueWhen( cond2, Close )" );   
    
    
    
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

AddColumn( MACD(), "MACD", 1.4 , IIf( MACD() > Signal(), colorGreen, colorRed ) );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorBlue, colorYellow ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;


I just copied the posted code and backtested, all signals match.

Would you mind setting focus on the price graph and right clicking on the price graph and select EDIT and post the formula that opens up.

yeah sure but its not opening the MACD formula rather opening price chart formula because i m just applying arrows by right-clicking show arrows for actual trades on backtesting window to price chart.
when i do the same on macd chart this happens
aaa

blue and yellow are signals which are generated in backtesting and explore and green red are unwanted one.
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

I notice from your screen shot that you have 2 tabs open with AXISBANK (5-minute ), would you mind closing 1 tab and run backtest again. It would seem to me that something is conflicting.

i closed all tabs form chart window and formula editor and then reopened AB and this time there was just one chat and that was axisbank but when i applied arrows to the chart it's showing the same result

When I open a new chart window and "APPLY " the following code:

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("trade rules");
Buy=Cross(MACD(), Signal());
     
Sell=Cross(Signal(), MACD());


Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);  
 _SECTION_END();
 
 
 Filter = 1; // show all bars

cond1=Cross(MACD(), Signal());
     
cond2= Cross(Signal(), MACD());
bi = BarIndex();
//
AddColumn( C, "Close" );
AddColumn( bi, "BarIndex" );
AddColumn( cond1, "MACDup",1,colorDefault,IIf(cond1,colorGreen,colorDefault));

AddColumn( ValueWhen( cond1, bi ), "ValueWhen( cond1, BarIndex() )" );
AddColumn( ValueWhen( cond1, Close), "ValueWhen( cond1, Close )" );

AddColumn( ValueWhen( cond2, bi ), "ValueWhen( cond2, BarIndex() )" );
AddColumn( cond2, "SIGNALup",1,colorDefault,IIf(cond2,colorRed,colorDefault));
AddColumn( ValueWhen( cond2, Close), "ValueWhen( cond2, Close )" );   
addColumn( MACD(), "MACD", 1.4 , IIf( MACD() > Signal(), colorGreen, colorRed ) );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

Plot( Close, "Price", colorBlack, styleCandle );

PlotShapes( shape, IIf( Buy, colorBlue, colorYellow ), 0, IIf( Buy, Low, High ) );

GraphXSpace = 5;

I get the Blue and yellow arrows correctly matched to the MACD. And then when I right click in the chart pane select EDIT then select analysis > backtest > right click and show arrows for actual trades I get Green ( buy) and red (sell ) arrows correctly matched to the MACD and the blue and yellow arrows are not visible.

Copy the above formula into a clean editor and apply, then from that chart window do as above and run backtest etc.

i did same as you asked but i don't know why mine is showing wrong signals
sameerror

Could you go to View and select Refresh all and show the screen shot after that.