MACD query - is the calculation correct in cTrader?

Created at 02 Dec 2024, 12:19
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MA

markmath01

Joined 07.11.2024

MACD query - is the calculation correct in cTrader?
02 Dec 2024, 12:19


Hi,

I have checked the values that cTrader is calculating for the MACD and I was hoping someone could confirm if the values in cTrader are accurate as they are at odds with each other (visual charts versus underlying MACD time-series data); or if it's actually in my manual cross-checks where the calculation is incorrect.

This has arisen because I have seen a discrepancy between the MACD values being shown in a chart and the corresponding value printed out for the same bar in an algo using the Print statement.  The discrepancy occurs on ALL price bars / MACD values.

I am using 3-bar and 10-bar periods for the averages and these are 100-tick bars in EUR-USD.

Here is the screenshot of the chart with an example MACD value highlighted :

image.png

I have highlighted the value that the code outputs for this price bar.  As you can see, they are very different.

And here is the code snippet that picks up the MACD value directly from the cTrader time-series data and outputs it:

Now here are my manual checks in Excel:

 

As you can see, they are very different from the cTrader MACD values.  

(Note: I calculated an EMA and SMA version of the MACD for comparison purposes.  |Although the latter is never used to my knowledge, it produced a value closer to the one cTrader shows on the chart above, not printed out from the time-series data.)

Many thanks

Mark


@markmath01
Replies

markmath01
02 Dec 2024, 18:38

Hi All,

I just wanted to add that there is definitely something strange / errorneous happening - and it doesn't appear to be with just the MACD.

I have just decided to output the previous and current MACD values, alongside the Bars.ClosePrices.Last(0) and Bars.ClosePrices.LastValue properties and they are definitely NOT Closing Prices.

They are OpenPrices.

See highlight below

Can someone shed some light as to what could be happening here as I can't see why the MACD is incorrect or why this is returning Open Prices and not Closing Prices?

Thanks

Mark


@markmath01

PanagiotisCharalampous
03 Dec 2024, 07:13

Hi there,

Please share the code you are using to print these values.

Best regards,

Panagiotis


@PanagiotisCharalampous

markmath01
03 Dec 2024, 09:09

RE: MACD query - is the calculation correct in cTrader?

PanagiotisCharalampous said: 

Hi there,

Please share the code you are using to print these values.

Best regards,

Panagiotis

Hi - see code below.  Thanks.

 

        protected override void OnStart()
       {
           _macd = Indicators.MacdCrossOver(SlowMAPeriod, FastMAPeriod, SignalLinePeriod);
           Print("MACD-Analysis started with MACD parameters: Fast MA = {0}, Slow MA = {1}, Signal Line = {2}",
                 FastMAPeriod, SlowMAPeriod, SignalLinePeriod);
       }

       protected override void OnBar()
       {
           double macdValue = _macd.MACD.LastValue;
           //Print("MACD = {0:F7}, Time = {1}, {2}", macdValue, Bars.OpenTimes.LastValue, Bars.OpenTimes.Last(1));
           //Print("MACD = {0:F7}", macdValue);
  Print("Last MACD = {0:F7}, MACD = {1}, Last(0) = {2}, LastVal = {3}", lastMACD, macdValue, Bars.ClosePrices.Last(0), Bars.ClosePrices.LastValue);
  
           // Check for zero-line crossing
           if (lastMACD.HasValue)
           {
               if ((lastMACD < 0 && macdValue > 0) || (lastMACD > 0 && macdValue < 0))
               {
                   // A zero-line crossing occurred
                   ProcessWave(macdValue < 0);
               }
           }

           lastMACD = macdValue; // Update the last MACD value
           //Print("Last MACD = " + lastMACD) ;
           
       }
 


@markmath01

PanagiotisCharalampous
03 Dec 2024, 10:03

RE: RE: MACD query - is the calculation correct in cTrader?

markmath01 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the code you are using to print these values.

Best regards,

Panagiotis

Hi - see code below.  Thanks.

 

        protected override void OnStart()
       {
           _macd = Indicators.MacdCrossOver(SlowMAPeriod, FastMAPeriod, SignalLinePeriod);
           Print("MACD-Analysis started with MACD parameters: Fast MA = {0}, Slow MA = {1}, Signal Line = {2}",
                 FastMAPeriod, SlowMAPeriod, SignalLinePeriod);
       }

       protected override void OnBar()
       {
           double macdValue = _macd.MACD.LastValue;
           //Print("MACD = {0:F7}, Time = {1}, {2}", macdValue, Bars.OpenTimes.LastValue, Bars.OpenTimes.Last(1));
           //Print("MACD = {0:F7}", macdValue);
  Print("Last MACD = {0:F7}, MACD = {1}, Last(0) = {2}, LastVal = {3}", lastMACD, macdValue, Bars.ClosePrices.Last(0), Bars.ClosePrices.LastValue);
  
           // Check for zero-line crossing
           if (lastMACD.HasValue)
           {
               if ((lastMACD < 0 && macdValue > 0) || (lastMACD > 0 && macdValue < 0))
               {
                   // A zero-line crossing occurred
                   ProcessWave(macdValue < 0);
               }
           }

           lastMACD = macdValue; // Update the last MACD value
           //Print("Last MACD = " + lastMACD) ;
           
       }
 

Hi there,

You are printing the _macd.MACD.LastValue which prints the value of the current at the opening of the bar and then you compare it with the MA values of closed bars. The LastValue  can change by the time the bar is closed. You should print  _macd.MACD.Last(1) instead.

Best regards,

Panagiotis


@PanagiotisCharalampous