How to use 2 macd different timeframe

Created at 30 Oct 2022, 14:45
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!
AL

Alwin123

Joined 28.10.2022

How to use 2 macd different timeframe
30 Oct 2022, 14:45


How to use 2 macd different timeframe

 

I can't get past this.

anyone who can improve the script?
Both macd must indicate the same sentiment before opening a trade.

Preferably above or below the zero line

 

 

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


namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class PullbackStrategy : Robot
    {

        [Parameter("Sentiment: Buy", Group = "Sentiment", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", Group = "Sentiment", DefaultValue = true)]
        public bool Sell { get; set; }

        [Parameter("Another Time Frame MACD ", Group = "Strategy", DefaultValue = "Hour")]
        public TimeFrame Multitimeframe { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries SourceRSI { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 19)]
        public int PeriodsRSI { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss ", Group = "Risk Managment", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Risk Managment", DefaultValue = 100)]
        public int TakeProfit { get; set; }


        public ExponentialMovingAverage i_MA_slow;
        public ExponentialMovingAverage i_MA_standart;
        public ExponentialMovingAverage i_MA_fast;
        private RelativeStrengthIndex rsi;
        private MacdCrossOver macd;
        private MacdCrossOver MultitimeframeMACD;
        private double volumeInUnits;
        private Position position;


        protected override void OnStart()

        {
            i_MA_slow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 50);
            i_MA_standart = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
            i_MA_fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 8);
            rsi = Indicators.RelativeStrengthIndex(SourceRSI, PeriodsRSI);
            macd = Indicators.MacdCrossOver(26, 12, 9);
            MultitimeframeMACD = Indicators.MacdCrossOver(26, 12, 9);
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            position = null;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            position = obj.Position;
        }
                    
                                  
        [Obsolete]
        protected override void OnBar()
        {
        var MACDLine  = macd.MACD.Last(1);
        var PrevMACDLine = macd.MACD.Last(2);
        var Signal  = macd.Signal.Last(1);
        var PrevSignal= macd.Signal.Last(2);

            // both macd must be above the zero line for an entry buy or sell
            var MultitimeframeMACD = MarketData.GetSeries(Multitimeframe);
            var Multitimeframe = MultitimeframeMACD.MACD.Last(1);
            var PrevMACDLineMultiTF = MultitimeframeMACD.MACD.Last(2);
            var SignalMultiTF = MultitimeframeMACD.Signal.Last(1);
            var PrevSignalMultiTF = MultitimeframeMACD.Signal.Last(2);

            if (position != null)
                return;
            {
                if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
                {
                    
                        int index = MarketSeries.Close.Count;

                    if   (MACDLine > Signal & PrevMACDLine <PrevSignal & default==Sell 
                        & Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Sell
                        & i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] < MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue)
               {         
                        
                      ExecuteMarketOrder( TradeType.Buy, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  }
                        
                        else if (MACDLine < Signal & PrevMACDLine >PrevSignal & default== Buy
                        & Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Buy
                        & i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] > MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue)

                        {
                                        
                     ExecuteMarketOrder( TradeType.Sell, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  
                            }
                }
               

                } 
           
         }
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     


        
   
    
    


@Alwin123
Replies

firemyst
31 Oct 2022, 13:51 ( Updated at: 21 Dec 2023, 09:23 )

RE:

Alwin123 said:

How to use 2 macd different timeframe

 

I can't get past this.

anyone who can improve the script?
Both macd must indicate the same sentiment before opening a trade.

Preferably above or below the zero line

 

 

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


namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class PullbackStrategy : Robot
    {

        [Parameter("Sentiment: Buy", Group = "Sentiment", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", Group = "Sentiment", DefaultValue = true)]
        public bool Sell { get; set; }

        [Parameter("Another Time Frame MACD ", Group = "Strategy", DefaultValue = "Hour")]
        public TimeFrame Multitimeframe { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries SourceRSI { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 19)]
        public int PeriodsRSI { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss ", Group = "Risk Managment", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Risk Managment", DefaultValue = 100)]
        public int TakeProfit { get; set; }


        public ExponentialMovingAverage i_MA_slow;
        public ExponentialMovingAverage i_MA_standart;
        public ExponentialMovingAverage i_MA_fast;
        private RelativeStrengthIndex rsi;
        private MacdCrossOver macd;
        private MacdCrossOver MultitimeframeMACD;
        private double volumeInUnits;
        private Position position;


        protected override void OnStart()

        {
            i_MA_slow = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 50);
            i_MA_standart = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
            i_MA_fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 8);
            rsi = Indicators.RelativeStrengthIndex(SourceRSI, PeriodsRSI);
            macd = Indicators.MacdCrossOver(26, 12, 9);
            MultitimeframeMACD = Indicators.MacdCrossOver(26, 12, 9);
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            position = null;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            position = obj.Position;
        }
                    
                                  
        [Obsolete]
        protected override void OnBar()
        {
        var MACDLine  = macd.MACD.Last(1);
        var PrevMACDLine = macd.MACD.Last(2);
        var Signal  = macd.Signal.Last(1);
        var PrevSignal= macd.Signal.Last(2);

            // both macd must be above the zero line for an entry buy or sell
            var MultitimeframeMACD = MarketData.GetSeries(Multitimeframe);
            var Multitimeframe = MultitimeframeMACD.MACD.Last(1);
            var PrevMACDLineMultiTF = MultitimeframeMACD.MACD.Last(2);
            var SignalMultiTF = MultitimeframeMACD.Signal.Last(1);
            var PrevSignalMultiTF = MultitimeframeMACD.Signal.Last(2);

            if (position != null)
                return;
            {
                if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
                {
                    
                        int index = MarketSeries.Close.Count;

                    if   (MACDLine > Signal & PrevMACDLine <PrevSignal & default==Sell 
                        & Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Sell
                        & i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] < MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue)
               {         
                        
                      ExecuteMarketOrder( TradeType.Buy, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  }
                        
                        else if (MACDLine < Signal & PrevMACDLine >PrevSignal & default== Buy
                        & Multitimeframe > SignalMultiTF & PrevMACDLineMultiTF < PrevSignalMultiTF & default == Buy
                        & i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] > MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue)

                        {
                                        
                     ExecuteMarketOrder( TradeType.Sell, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  
                            }
                }
               

                } 
           
         }
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     


        
   
    
    

HI @Alwin123:

Your code isn't going to work, because you have to pass in a different time frame series when you create the MACD indicator.

It'll be something like this:
 

private Bars _marketSeriesH1;
MacdCrossOver macd;

//In the OnStart method, do something like this. This will get the data for the H1 timeframe:
_marketSeriesH1 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);

//This gets the MACD with the above time frame set (eg, H1 in this case):
macd = Indicators.MacdCrossOver(_marketSeriesH1.ClosePrices, LongPeriod, ShortPeriod, SignalPeriod);



@firemyst

Alwin123
31 Oct 2022, 19:45

RE: RE:
Thanks for your help.
I just can't get it without errors. maybe i'm doing it wrong. of course I haven't been working on C# for that long

 

HI @Alwin123:

Your code isn't going to work, because you have to pass in a different time frame series when you create the MACD indicator.

It'll be something like this:
 

private Bars _marketSeriesH1;
MacdCrossOver macd;

//In the OnStart method, do something like this. This will get the data for the H1 timeframe:
_marketSeriesH1 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);

//This gets the MACD with the above time frame set (eg, H1 in this case):
macd = Indicators.MacdCrossOver(_marketSeriesH1.ClosePrices, LongPeriod, ShortPeriod, SignalPeriod);


 


@Alwin123

Alwin123
31 Oct 2022, 21:36 ( Updated at: 31 Oct 2022, 23:09 )

i have now this can you check this? is this wrong with the both MACD

//both macd must be above the zero line for an entry buy or sell

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
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)]
    public class PullbackStrategy : Robot
    {

        [Parameter("Sentiment: Buy", Group = "Sentiment", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", Group = "Sentiment", DefaultValue = true)]
        public bool Sell { get; set; }

        [Parameter("First MACD ", Group = "First MACD", DefaultValue = "Minute15")]
        public TimeFrame macd1 { get; set; }

        [Parameter("MACD LongCycle", Group = "First MACD ", DefaultValue = 26, MinValue = 1)]
        public int LongPeriod1 { get; set; }

        [Parameter("MACD ShortCycle", Group = "First MACD", DefaultValue = 12, MinValue = 1)]
        public int ShortPeriod1 { get; set; }

        [Parameter("MACD Period", Group = "First MACD", DefaultValue = 9, MinValue = 1)]
        public int SignalPeriod1 { get; set; }

        [Parameter("Second MACD ", Group = "Second MACD", DefaultValue = "Hour")]
        public TimeFrame macd2 { get; set; }

        [Parameter("MACD LongCycle", Group = "Second MACD", DefaultValue = 26, MinValue = 1)]
        public int LongPeriod2 { get; set; }

        [Parameter("MACD ShortCycle", Group = "Second MACD", DefaultValue = 12, MinValue = 1)]
        public int ShortPeriod2 { get; set; }

        [Parameter("MACD Period", Group = "Second MACD", DefaultValue = 9, MinValue = 1)]
        public int SignalPeriod2 { get; set; } 

        [Parameter("MME Slow", Group = "MA Type", DefaultValue = 50, MinValue = 45, MaxValue = 220, Step = 1)]
        public int mmeSlow { get; set; }

        [Parameter("MME Standart", Group = "MA Type", DefaultValue = 20, MinValue = 15, MaxValue = 120, Step = 1)]
        public int mmeStandart { get; set; }

        [Parameter("MME Fast", Group = "MA Type", DefaultValue = 8, MinValue = 4, MaxValue = 20, Step = 1)]
        public int mmeFast { get; set; }
       
       
       [Parameter("Source", Group = "RSI")]
        public DataSeries SourceRSI { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 19, MinValue = 4, MaxValue = 20, Step = 1)]
        public int PeriodsRSI { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss ", Group = "Risk Managment", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Risk Managment", DefaultValue = 100)]
        public int TakeProfit { get; set; }


        public MovingAverage i_MA_slow;
        public MovingAverage i_MA_standart;
        public MovingAverage i_MA_fast;
        private RelativeStrengthIndex rsi;
        private MacdCrossOver macd_1;
        private MacdCrossOver macd_2;
        private double volumeInUnits;
        private Position position;
        private Bars _marketSeries2;
        private Bars _marketSeries1;


        protected override void OnStart()

        {
           
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, mmeSlow, MovingAverageType.Exponential);
            i_MA_standart = Indicators.MovingAverage(Bars.ClosePrices, mmeStandart, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, mmeFast, MovingAverageType.Exponential);
            rsi = Indicators.RelativeStrengthIndex(SourceRSI, PeriodsRSI);
            macd_1 = Indicators.MacdCrossOver(LongPeriod1, ShortPeriod1, SignalPeriod1);
            macd_2 = Indicators.MacdCrossOver(LongPeriod2, ShortPeriod2, SignalPeriod2);
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            _marketSeries2 = MarketData.GetBars(macd2, Symbol.Name);
            _marketSeries1 = MarketData.GetBars(macd1, Symbol.Name);
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            position = null;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            position = obj.Position;
        }
                    
                                  
        [Obsolete]
        protected override void OnBar()
        {
            //First MACD
            var _marketSeries1 = MarketData.GetBars(macd1);
            var macd_1 = Indicators.MacdCrossOver(_marketSeries1.ClosePrices, LongPeriod1, ShortPeriod1, SignalPeriod1);
            var MACDLine1  = macd_1.MACD.Last(1);
            var PrevMACDLine1 = macd_1.MACD.Last(2);
            var Signal1  = macd_1.Signal.Last(1);
            var PrevSignal1 = macd_1.Signal.Last(2);

            // both macd must be above the zero line for an entry buy or sell
            // Second MACD
            var _marketSeries2 = MarketData.GetBars(macd2);
            var macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);
            var MACDLine_2 = macd_2.MACD.Last(1);
            var PrevMACDLine_2 = macd_2.MACD.Last(2);
            var Signal_2 = macd_2.Signal.Last(1);
            var PrevSignal_2 = macd_2.Signal.Last(2);

            if (position != null)
                return;
            {
                if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
                {
                    
                        int index = MarketSeries.Close.Count;

                    if   (MACDLine1 > Signal1 & PrevMACDLine1 <PrevSignal1 & default==Sell 
                        & MACDLine_2 > Signal_2 & PrevMACDLine_2 < PrevSignal_2 & default == Sell
                        & i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] < MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue)
               {         
                        
                      ExecuteMarketOrder( TradeType.Buy, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  }
                        
                        else if (MACDLine1 < Signal1 & PrevMACDLine1 >PrevSignal1 & default== Buy
                        & MACDLine_2 > Signal_2 & PrevMACDLine_2 < PrevSignal_2 & default == Buy
                        & i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] > MarketSeries.Close[index - 1]
                        & i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue
                        & i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue
                        & i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue)

                        {
                                        
                     ExecuteMarketOrder( TradeType.Sell, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  
                            }
                }
               

                } 
           
         }
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     


        
   
    
    

 


@Alwin123

firemyst
01 Nov 2022, 00:59

Your code isn't going to work, because you're not creating the marketseries variables correctly:

 

_marketSeries2 = MarketData.GetBars(macd2, Symbol.Name);
            _marketSeries1 = MarketData.GetBars(macd1, Symbol.Name);

 

The method is "GetBars", so why are you passing in the macd indicators? The first parameter of the GetBars method is the TIMEFRAME, not an indicator.

 

You are also not creating/initializing the MACD indicator as I showed you in my example -- the first parameter should be the data series, not the long period.

Please go back and reread my example making sure you understand it.

Spotware also has an example on their website which should help:

https://help.ctrader.com/ctrader-automate/indicator-code-samples/#multiple-timeframes

 


@firemyst

Alwin123
02 Nov 2022, 21:24

RE: thanks for your response. I'll try to figure out how this works. your explanation will be perfect. my knowledge of C# is not that far yet.

firemyst said:

Your code isn't going to work, because you're not creating the marketseries variables correctly:

 

_marketSeries2 = MarketData.GetBars(macd2, Symbol.Name);
            _marketSeries1 = MarketData.GetBars(macd1, Symbol.Name);

 

The method is "GetBars", so why are you passing in the macd indicators? The first parameter of the GetBars method is the TIMEFRAME, not an indicator.

 

You are also not creating/initializing the MACD indicator as I showed you in my example -- the first parameter should be the data series, not the long period.

Please go back and reread my example making sure you understand it.

Spotware also has an example on their website which should help:

https://help.ctrader.com/ctrader-automate/indicator-code-samples/#multiple-timeframes

 

 


@Alwin123

Alwin123
02 Nov 2022, 22:22

is this correct the adjustment in the MACD?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
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)]
    public class PullbackStrategy : Robot
    {

        [Parameter("Sentiment: Buy", Group = "Sentiment", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", Group = "Sentiment", DefaultValue = true)]
        public bool Sell { get; set; }

        //[Parameter("First MACD ", Group = "First MACD", DefaultValue = "Minute15")]
       // public TimeFrame macd1 { get; set; }

        [Parameter("MACD LongCycle", Group = "First MACD ", DefaultValue = 26, MinValue = 1)]
        public int LongPeriod1 { get; set; }

        [Parameter("MACD ShortCycle", Group = "First MACD", DefaultValue = 12, MinValue = 1)]
        public int ShortPeriod1 { get; set; }

        [Parameter("MACD Period", Group = "First MACD", DefaultValue = 9, MinValue = 1)]
        public int SignalPeriod1 { get; set; }

        //[Parameter("Second MACD ", Group = "Second MACD", DefaultValue = "Hour")]
        //public TimeFrame macd2 { get; set; }

        [Parameter("MACD LongCycle", Group = "Second MACD", DefaultValue = 26, MinValue = 1)]
        public int LongPeriod2 { get; set; }

        [Parameter("MACD ShortCycle", Group = "Second MACD", DefaultValue = 12, MinValue = 1)]
        public int ShortPeriod2 { get; set; }

        [Parameter("MACD Period", Group = "Second MACD", DefaultValue = 9, MinValue = 1)]
        public int SignalPeriod2 { get; set; } 

        [Parameter("MME Slow", Group = "MA Type", DefaultValue = 50, MinValue = 45, MaxValue = 220, Step = 1)]
        public int mmeSlow { get; set; }

        [Parameter("MME Standart", Group = "MA Type", DefaultValue = 20, MinValue = 15, MaxValue = 120, Step = 1)]
        public int mmeStandart { get; set; }

        [Parameter("MME Fast", Group = "MA Type", DefaultValue = 8, MinValue = 4, MaxValue = 20, Step = 1)]
        public int mmeFast { get; set; }
       
       
       [Parameter("Source", Group = "RSI")]
        public DataSeries SourceRSI { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 19, MinValue = 4, MaxValue = 20, Step = 1)]
        public int PeriodsRSI { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss ", Group = "Risk Managment", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Risk Managment", DefaultValue = 100)]
        public int TakeProfit { get; set; }


        public MovingAverage i_MA_slow;
        public MovingAverage i_MA_standart;
        public MovingAverage i_MA_fast;
        private RelativeStrengthIndex rsi;
        private double volumeInUnits;
        private Position position;
        private Bars _marketSeries2;
        private Bars _marketSeries1;
        private MacdCrossOver macd_1;
        private MacdCrossOver macd_2;


        protected override void OnStart()

        {
           
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, mmeSlow, MovingAverageType.Exponential);
            i_MA_standart = Indicators.MovingAverage(Bars.ClosePrices, mmeStandart, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, mmeFast, MovingAverageType.Exponential);
            rsi = Indicators.RelativeStrengthIndex(SourceRSI, PeriodsRSI);
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            _marketSeries2 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
            _marketSeries1 = MarketData.GetBars(TimeFrame.Minute15, Symbol.Name);
            macd_1 = Indicators.MacdCrossOver(_marketSeries1.ClosePrices, LongPeriod1, ShortPeriod1, SignalPeriod1);
            macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);
        }

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            position = null;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            position = obj.Position;
        }
                    
                                  
        [Obsolete]
        protected override void OnBar()
        {
            //First MACD
         
            var MACDLine1  = macd_1.MACD.Last(1);
            var PrevMACDLine1 = macd_1.MACD.Last(2);
            var Signal1  = macd_1.Signal.Last(1);
            var PrevSignal1 = macd_1.Signal.Last(2);

            // both macd must be above the zero line for an entry buy or sell
            // Second MACD
          
            var MACDLine_2 = macd_2.MACD.Last(1);
            var PrevMACDLine_2 = macd_2.MACD.Last(2);
            var Signal_2 = macd_2.Signal.Last(1);
            var PrevSignal_2 = macd_2.Signal.Last(2);

            if (position != null)
                return;
            {
                if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
                {
                    
                        int index = MarketSeries.Close.Count;

                    if   (MACDLine1 > Signal1 & PrevMACDLine1 <PrevSignal1 & default==Sell 
                        & MACDLine_2 > Signal_2 & PrevMACDLine_2 < PrevSignal_2 & default == Sell
                        & i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] < MarketSeries.Close[index - 1])
                     //   & i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue
                     //   & i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue
                     //   & i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue)
               {         
                        
                      ExecuteMarketOrder( TradeType.Buy, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  }
                        
                        else if (MACDLine1 < Signal1 & PrevMACDLine1 >PrevSignal1 & default== Buy
                        & MACDLine_2 > Signal_2 & PrevMACDLine_2 < PrevSignal_2 & default == Buy
                        & i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2] 
                        & i_MA_fast.Result[index - 1] > MarketSeries.Close[index - 1])
                      //  & i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue
                      //  & i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue
                       // & i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue)

                        {
                                        
                     ExecuteMarketOrder( TradeType.Sell, SymbolName, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                  
                            }
                }
               

                } 
           
         }
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     


        
   
    
    

 


@Alwin123

firemyst
03 Nov 2022, 01:45

Hi there,

Looks like you've made good progress!

This is correct - well done!

_marketSeries2 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
_marketSeries1 = MarketData.GetBars(TimeFrame.Minute15, Symbol.Name);
macd_1 = Indicators.MacdCrossOver(_marketSeries1.ClosePrices, LongPeriod1, ShortPeriod1, SignalPeriod1);
macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);

 

If you're unsure that you're getting the values you are expecting, then put Print statements in your code to print out the values and you can compare against having MACD indicators on your charts.

Example Print statement:

Print("MACD1 Value {0}, MACD1 Prev {1}", MACDLine1, PrevMACDLine1);


@firemyst

Alwin123
03 Nov 2022, 13:01

RE: thanks for your feedback

firemyst said:

Hi there,

Looks like you've made good progress!

This is correct - well done!

_marketSeries2 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
_marketSeries1 = MarketData.GetBars(TimeFrame.Minute15, Symbol.Name);
macd_1 = Indicators.MacdCrossOver(_marketSeries1.ClosePrices, LongPeriod1, ShortPeriod1, SignalPeriod1);
macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);

 

If you're unsure that you're getting the values you are expecting, then put Print statements in your code to print out the values and you can compare against having MACD indicators on your charts.

Example Print statement:

Print("MACD1 Value {0}, MACD1 Prev {1}", MACDLine1, PrevMACDLine1);

 


@Alwin123

Alwin123
06 Nov 2022, 23:16

RE:

firemyst said:

Hi there,

Looks like you've made good progress!

This is correct - well done!

_marketSeries2 = MarketData.GetBars(TimeFrame.Hour, Symbol.Name);
_marketSeries1 = MarketData.GetBars(TimeFrame.Minute15, Symbol.Name);
macd_1 = Indicators.MacdCrossOver(_marketSeries1.ClosePrices, LongPeriod1, ShortPeriod1, SignalPeriod1);
macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);

 

If you're unsure that you're getting the values you are expecting, then put Print statements in your code to print out the values and you can compare against having MACD indicators on your charts.

Example Print statement:

Print("MACD1 Value {0}, MACD1 Prev {1}", MACDLine1, PrevMACDLine1);

Hi firemyst can you check this both macd? 

using System;
using System.Security.Cryptography;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class PullbackStrategy : Robot
    {

        [Parameter("Stop Loss ", Group = "Protect", DefaultValue = 100, MaxValue = 100, MinValue = 5, Step = 0.01)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protect", DefaultValue = 100, MaxValue = 100, MinValue = 3, Step = 0.01)]
        public int TakeProfit { get; set; }

        [Parameter("Period", Group = "First MACD", DefaultValue = 9, MaxValue = 9, MinValue = 9, Step = 1)]
        public int Period { get; set; }

        [Parameter("Long Cycle", Group = "First MACD", DefaultValue = 26, MaxValue = 26, MinValue = 26, Step = 1)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", Group = "First MACD", DefaultValue = 12, MaxValue = 12, MinValue = 12, Step = 1)]
        public int ShortCycle { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", Group = "First MACD", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }

        [Parameter("TimeFrame", Group = "Second MACD", DefaultValue = "Hour")]
        public TimeFrame _TimeFrame { get; set; }

        [Parameter("MACD Period", Group = "Second MACD", DefaultValue = 9, MaxValue = 9, MinValue = 9, Step = 1)]
        public int SignalPeriod2 { get; set; }

        [Parameter("MACD LongCycle", Group = "Second MACD", DefaultValue = 26, MaxValue = 26, MinValue = 26, Step = 1)]
        public int LongPeriod2 { get; set; }

        [Parameter("MACD ShortCycle", Group = "Second MACD", DefaultValue = 12, MaxValue = 12, MinValue = 12, Step = 1)]
        public int ShortPeriod2 { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", Group = "Second MACD", DefaultValue = true)]
        public bool IsSignalLineCrossover_2 { get; set; }

        [Parameter("MME Slow", Group = "MA", DefaultValue = 50, MaxValue = 50, MinValue = 50, Step = 0.01)]
        public int MmeSlow { get; set; }

        [Parameter("MME Standart", Group = "MA", DefaultValue = 20, MinValue = 20, MaxValue = 20, Step = 1)]
        public int MmeStandart { get; set; }

        [Parameter("MME Fast", Group = "MA", DefaultValue = 8, MaxValue = 8, MinValue = 8, Step = 1)]
        public int MmeFast { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 14, MaxValue = 20, MinValue = 12, Step = 1)]
        public int Periods { get; set; }

        [Parameter("Overbold", Group = "RSI", DefaultValue = 70, MaxValue = 90, MinValue = 70, Step = 5)]
        public int Overbold { get; set; }

        [Parameter("Oversold", Group = "RSI", DefaultValue = 30, MaxValue = 30, MinValue = 10, Step = 5)]
        public int Oversold { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("BackStep", Group = "Pullback", DefaultValue = 5, MinValue = 1, MaxValue = 5, Step = 1)]
        public int Backstep { get; set; }


        public MovingAverage i_MA_slow, i_MA_standart, i_MA_fast;
        public double volumeInUnits;
        public RelativeStrengthIndex rsi;
        public Bars _marketSeries2;
        public MacdCrossOver macd, macd_2;


        protected override void OnStart()

        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, MmeSlow, MovingAverageType.Exponential);
            i_MA_standart = Indicators.MovingAverage(Bars.ClosePrices, MmeStandart, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, MmeFast, MovingAverageType.Exponential);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            _marketSeries2 = MarketData.GetBars(_TimeFrame, Symbol.Name);
            macd = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
            macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);

        }

        protected override void OnBar()
        {

            var MACDLine_2 = macd_2.MACD.Last(1);
            var PrevMACDLine_2 = macd_2.MACD.Last(2);
            var Signal_2 = macd_2.Signal.Last(1);
            var PrevSignal_2 = macd_2.Signal.Last(2);

            var MACDLine = macd.MACD.Last(1);
            var PrevMACDLine = macd.MACD.Last(2);
            var Signal = macd.Signal.Last(1);
            var PrevSignal = macd.Signal.Last(2);


           if (rsi.Result.LastValue > Oversold && rsi.Result.LastValue < Overbold)


            {

                if (IsSignalLineCrossover && IsSignalLineCrossover_2)
                {


                    if (PrevMACDLine < PrevSignal && MACDLine > Signal && PrevMACDLine_2 < PrevSignal_2 && MACDLine_2 > Signal_2)
                    {
                        var position = Positions.Find("PullbackStrategy, RSI, MACD");
                        if (position != null && position.TradeType == TradeType.Sell)
                        {
                            position.Close();
                        }
                  

                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                    }

                    if (PrevMACDLine > PrevSignal && MACDLine < Signal && PrevMACDLine_2 > PrevSignal_2 && MACDLine_2 < Signal_2)
                    {
                        var position = Positions.Find("PullbackStrategy, RSI, MACD");
                        if (position != null && position.TradeType == TradeType.Buy)
                        {
                            position.Close();
                        }
                       
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                    }
                }
                //Zero cross over
                else
                {
                    if (MACDLine > 0 && PrevMACDLine < 0 && MACDLine_2 > 0 && PrevMACDLine_2 < 0)
                    {
                        //up
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                    }
                    else if (MACDLine < 0 && PrevMACDLine > 0 && MACDLine_2 < 0 && PrevMACDLine_2 > 0)
                    {
                        //Down
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumeInUnits, "PullbackStrategy, RSI, MACD", StopLoss, TakeProfit);
                    }
                }
            }
        }

    }
        
    }


        


 

 


@Alwin123