MACD showing incorrect information

Created at 27 May 2024, 17:13
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!
EL

eliezer_barros

Joined 21.12.2018

MACD showing incorrect information
27 May 2024, 17:13


Hi,

I am using UTC time. Check 14:30 H (USNDA100 - 15 minutes)

Look for this print screen showing negative macd result, but it is not correct. How can I fix this problem?

 

The graphic is correct but the calculation is not, but the funciont is defautl.

Please, can you identify the problem?

Tks


@eliezer_barros
Replies

PanagiotisCharalampous
28 May 2024, 05:55

Hi there,

Please share your cBot code and cBot parameters to reproduce this behavior.

Best regards,

Panagiotis


@PanagiotisCharalampous

eliezer_barros
29 May 2024, 13:39

RE: MACD showing incorrect information

PanagiotisCharalampous said: 

Hi there,

Please share your cBot code and cBot parameters to reproduce this behavior.

Best regards,

Panagiotis

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
   // [Robot(AccessRights = AccessRights.FullAccess)]
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class Macdnasdaq : Robot
    {
           
        [Parameter("Volume", DefaultValue = 1.00)]
        public double VolumeToOpenOrders { get; set; }

        [Parameter("Profit", DefaultValue = 30)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Stop Loss", DefaultValue = 15)]
        public int StopLoss { get; set; }

        [Parameter("Data Source")]
        public DataSeries Price { get; set; }

        [Parameter("MACD Period", DefaultValue = 9)]
        public int MACDPeriod { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }
        
        private MacdCrossOver _MACD;
        public DirectionalMovementSystem _DMS;


        [Parameter("DMS Period", DefaultValue = 9)]
        public int DMSPeriod { get; set; }

        private const string label = "MACD";

        protected override void OnStart()
        {
           _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
           _DMS = Indicators.DirectionalMovementSystem(DMSPeriod);
        }

        protected override void OnBar()
        {
            var upposition = Positions.Find(label, SymbolName, TradeType.Buy);
            var downposition = Positions.Find(label, SymbolName, TradeType.Sell);
            
                       
        
          Print (" Mac last value " , _MACD.MACD.LastValue, " MAC sinal   " , _MACD.Signal.LastValue, "  Histogram  " , _MACD.Histogram.LastValue);  
      
 
            if (_MACD.MACD.HasCrossedAbove(_MACD.Signal.LastValue,1))
    
            {
                if (downposition != null)
                {
                    ClosePosition(downposition);
                }

                if (upposition == null)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, VolumeToOpenOrders, label, StopLoss, TakeProfitInPips);

                }
            }
            
            if (_MACD.MACD.HasCrossedBelow(_MACD.Signal.LastValue,1))
            {
                if (upposition != null)
                {
                    ClosePosition(upposition);
                }

                if (downposition == null)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, VolumeToOpenOrders, label, StopLoss, TakeProfitInPips);
                   }
            }
           }
        }
    }

 


@eliezer_barros

PanagiotisCharalampous
30 May 2024, 06:31

RE: RE: MACD showing incorrect information

eliezer_barros said: 

PanagiotisCharalampous said: 

Hi there,

Please share your cBot code and cBot parameters to reproduce this behavior.

Best regards,

Panagiotis

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
   // [Robot(AccessRights = AccessRights.FullAccess)]
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class Macdnasdaq : Robot
    {
           
        [Parameter("Volume", DefaultValue = 1.00)]
        public double VolumeToOpenOrders { get; set; }

        [Parameter("Profit", DefaultValue = 30)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Stop Loss", DefaultValue = 15)]
        public int StopLoss { get; set; }

        [Parameter("Data Source")]
        public DataSeries Price { get; set; }

        [Parameter("MACD Period", DefaultValue = 9)]
        public int MACDPeriod { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }
        
        private MacdCrossOver _MACD;
        public DirectionalMovementSystem _DMS;


        [Parameter("DMS Period", DefaultValue = 9)]
        public int DMSPeriod { get; set; }

        private const string label = "MACD";

        protected override void OnStart()
        {
           _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
           _DMS = Indicators.DirectionalMovementSystem(DMSPeriod);
        }

        protected override void OnBar()
        {
            var upposition = Positions.Find(label, SymbolName, TradeType.Buy);
            var downposition = Positions.Find(label, SymbolName, TradeType.Sell);
            
                       
        
          Print (" Mac last value " , _MACD.MACD.LastValue, " MAC sinal   " , _MACD.Signal.LastValue, "  Histogram  " , _MACD.Histogram.LastValue);  
      
 
            if (_MACD.MACD.HasCrossedAbove(_MACD.Signal.LastValue,1))
    
            {
                if (downposition != null)
                {
                    ClosePosition(downposition);
                }

                if (upposition == null)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, VolumeToOpenOrders, label, StopLoss, TakeProfitInPips);

                }
            }
            
            if (_MACD.MACD.HasCrossedBelow(_MACD.Signal.LastValue,1))
            {
                if (upposition != null)
                {
                    ClosePosition(upposition);
                }

                if (downposition == null)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, VolumeToOpenOrders, label, StopLoss, TakeProfitInPips);
                   }
            }
           }
        }
    }

 

Hi there,

You seem to use the LastValue on bar opening. This value changes by the time the bar is closed. Use Last(1) instead.

Best regards,

Panagiotis


@PanagiotisCharalampous