Simple MACD crossover strategy

Hi,
I am using a simple MACD crossover strategy, but getting a wrong signal in back test.
Please find the code below:
Buy = Cross( MACD(), SIGNAL() );
Sell = Cross( Signal(), MACD() );
AlertIF(Buy,"Test01","Simple text alert",4);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Filter = Cross( MACD(), SIGNAL() );
AddColumn (MACD(), "MACD");
AddColumn (Signal(), "Signal");

I have back tested for HDFC stock and it shows Buy date as 20.05.2019 whereas the actual crossover happened on 17.05.2019. Please refer the screen shot below and correct me if I miss anything.
P1

P2

Regards,
Vijayaragavan.A

I would code MACD() like this: MACD(12, 26) - the reason why is that what is showing on your chart has different settings to your code.

Hi,
I have changed the coding as below:
Buy = Cross( MACD(), SIGNAL() );
Sell = Cross( Signal(), MACD() );
AlertIF(Buy,"Test01","Simple text alert",4);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Filter = Cross( MACD(12,26), SIGNAL(12,26,9) );
AddColumn (MACD(), "MACD");
AddColumn (Signal(), "Signal");

But still getting the same results.

Regards,
Vijayaragavan.A

What you are saying is incorrect.
E.g. MACD() is equal to MACD(12, 26) because default values of MACD function are 12 and 26. So both arguments of that function are untouched in upper code and that one tells AmiBroker engine to use those default values. It won't use random values in such case but always the same ones.

Hover with your mouse over a function and you will see whether it has arguments and what's their default parameters.

5

But also the manual tells you whether a function has any arguments and what are default values of a function. E.g. look up MACD().


Apparently you have set trade delay setting of Trades tab of analysis setting to larger than zero and entry/exit prices being Open.

Either change trade tab setting to close and zero delay

5

or add this one to your code (as it will override analysis settings).

SetTradeDelays(0,0,0,0);

BuyPrice = SellPrice = Close;

Buy = Cross( MACD(), SIGNAL() );
Sell = Cross( Signal(), MACD() );
AlertIF(Buy,"Test01","Simple text alert",4);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Filter = Cross( MACD(12,26), SIGNAL(12,26,9) );
AddColumn (MACD(), "MACD");
AddColumn (Signal(), "Signal");

And last but not least... @av.ragavan, it is mandatory rule here to use code tags if adding code to a post.
Please read here.

(below animation copied from that thread)
pastecode3

Hi,
I have changed the coding as below:

SetTradeDelays(0,0,0,0);
BuyPrice = SellPrice = Close;

Buy = Cross( MACD(), SIGNAL() );
Sell = Cross( Signal(), MACD() );
AlertIF(Buy,"Test01","Simple text alert",4);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Filter = Cross( MACD(12,26), SIGNAL(12,26,9) );
AddColumn (MACD(), "MACD");
AddColumn (Signal(), "Signal");

Please refer the screen shots below:

P11

P12

P13

But still getting the incorrect signal.

Regards,
Vijayaragavan.A

You are misinterpreting. In your picture of 1st post MACD seems to be lower than Signal line on May 17th. You haven't even clicked on that bar because title of 1st chart pane is showing June 20th. Click (select) bar of May 17th and compare MACD to Signal value.

For a cross to be true MACD has to be larger than Signal line. That is true on May 20th in your case.

Please check the screen shot of crossover of MACD(Red colour) and signal line (Blue colour) from my initial post which is on 17th May 2019. But back test results show the buy date as 20-05-2019.

Are you listening what I said in previous post??

Since you don't I have imported HDFC using these data
https://in.finance.yahoo.com/quote/HDFC.NS/history?p=HDFC.NS&.tsrc=fin-srch
Note: June 20th is showing same OHLC values as in first pane of your initial picture. So that one seems to be the same symbol plus data.

Now if selecting May 17th of that symbol you can clearly see that MACD value is lower than the one Signal line. -6.0 is lower than -5.3! Is this noted?
Select bar of May 17th and you check values. If MACD is lower than Signal then Cross function will return FALSE and as such no entry signal on that bar but one bar later on May 20th.
5

3 Likes