I want to use 2 macd 15 and 60 min.

Created at 28 Oct 2022, 23:31
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

I want to use 2 macd 15 and 60 min.
28 Oct 2022, 23:31


good afternoon.
i am trying to use 2 macd in this bot. I just can't get out now.
The command should be when both macd 1 current chart is 15min time frame and the other on the 60 min time frame and both are above the zero line. then open a buy position and a sell position 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

PanagiotisChar
31 Oct 2022, 11:40

Hi there,

Are you asking from somebody to do the job for you or are you missing some information to do this yourself? If you are missing information, please let us know what it is and we will be happy to help you.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

firemyst
31 Oct 2022, 13:45

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