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 );
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.
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.
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
or add this one to your code (as it will override analysis settings).
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.
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.