let both macd make the right decision together

Created at 26 Nov 2022, 15:30
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

let both macd make the right decision together
26 Nov 2022, 15:30


 

I can't figure out why both macd 1 of the chart at 15 min and the other at 1 hour don't work together. What I have tried to make is that both macd are used as a filter to determine the direction of the entry. If both macd are above or below the zero line, a position may be opened. Separately they do what I want. but not together.

in the photo the dark red and dark green areas are the areas where the macd are both above or below the zero line
  

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



/* Indicators:
 * indicator : EMA 8 periode 
 * indicator : EMA 20 periode 
 * indicator : EMA 50 periode
 * 
 * Filters: (MACDLine > 0 && PrevMACDLine < 0 == MACDLine_2 > 0 && PrevMACDLine_2 < 0) for Buy position 
 * Filter : MACD chart (15m) 26,12,9 
 * Filter : MACD Multi time frame (1H) 26,12,9
 * Filter : RSI
 * Protection:
 * Stop1 : Stoploss ATR 
 * Stop2 : Trailingstop ATR
 * Target 1 : Trailstop ATR 
 * Target 2 : Takeprofit ATR 
 * 
 * Risk per trade : 1 - 5 % account balans
 * 
 * Entry pullback behind de 20 and 50 EMA
 * 
 * 23-11-2022 checkt the Break Even and the Trailing Stop. Check!! 
 * 24-11-2022 managment positition is not ready 


*/

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PullBack_Strategy : Robot
    {
        #region User defined parameters

        [Parameter("Instance Name", DefaultValue = "PullBackStrategy")]
        public string PullBackStrategy { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = true)]// true
        public bool CalculateOnBar { get; set; }

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

        #region Protection 
      
        [Parameter("Include Break-Even", DefaultValue = true, Group = "Protection")]
        public bool IncludeBreakEven { get; set; }

        [Parameter("Break-Even Trigger (pips)", DefaultValue = 3, MinValue = 1, Group = "Protection")]
        public int BreakEvenPips { get; set; }

        [Parameter("Break-Even Extra (pips)", DefaultValue = 0.5, MinValue = 0.1, Step = 0.1, Group = "Protection")]
        public double BreakEvenExtraPips { get; set; }

        [Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
        public bool Includetrail { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 2, MinValue = 1, Group = "Protection")]
        public int Trailingstoppips { get; set; }
        #endregion

        #region Filters

        [Parameter("TimeFrame", Group = "Time Frame MACD 2, 26.12.9", DefaultValue = "Hour")]//Hour
        public TimeFrame TimeFrame2 { get; set; }

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

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

        //[Parameter("Overbold", Group = "RSI", DefaultValue = 70, MaxValue = 100, MinValue = 10, Step = 1)]// 70
        //  public int Overbold { get; set; }

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


        #endregion

        #region Indicator declarations
        // Protection
        private AverageTrueRange ATR_;
        public double volumeInUnits;
        // Filters
        public Bars _marketSeries2;
        public MacdCrossOver macd;
        public MacdCrossOver macd_2;
       
        public RelativeStrengthIndex rsi;
        // Indicators 
        public MovingAverage i_MA_slow;
        public MovingAverage i_MA_standart;
        public MovingAverage i_MA_fast;
        // Position 
        private Position position;



        #endregion

        #region cTrader events

        /// <summary>
        /// This is called when the robot first starts, it is only called once.
        /// </summary>
        protected override void OnStart()
        {
            // construct the indicators

            // Protection
            ATR_ = Indicators.AverageTrueRange(14, MovingAverageType.Simple);
            // Lotsize Lotsize - functie
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            // Filters
            _marketSeries2 = MarketData.GetBars(TimeFrame2, Symbol.Name);
            macd = Indicators.MacdCrossOver(26, 12, 9);
            macd_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, 26, 12, 9);
         
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            //Indicators
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, 50, MovingAverageType.Exponential);
            i_MA_standart = Indicators.MovingAverage(Bars.ClosePrices, 20, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, 8, MovingAverageType.Exponential);
            // Positions
            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
            Positions.Modified += Positions_Modified;



        }
        private void Positions_Modified(PositionModifiedEventArgs obj)
        {
            var modifiedPosition = obj.Position;
        }
        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            var closedPosition = obj.Position;
            var closeReason = obj.Reason;
        }
        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            var openedPosition = obj.Position;
        }

        /// <summary>
        /// This method is called every time the price changes for the symbol
        /// </summary>
        protected override void OnTick()
        {
            if (CalculateOnBar)
            {
                return;
            }

            ManagePositions();

        }

        /// <summary>
        /// This method is called at every candle (bar) close, when it has formed
        /// </summary>
        protected override void OnBar()
        {

            if (position != null)
                return;

            if (!CalculateOnBar)
            {
                return;
            }

            if (IncludeBreakEven)
            {
                BreakEvenAdjustment();
            }

            ManagePositions();

        }

        #endregion

        #region Position management

        private void ManagePositions()

        {
            // Protection
            // var ATRTP = ATR_TAKEPROFIT * ATR_.Result.LastValue * 1000;
            //var ATRSL = 3 * ATR_.Result.LastValue * 1000;

            // Filters MACD chart
            var MACDLine = macd.MACD.Last(1);
            var PrevMACDLine = macd.MACD.Last(2);

            // MACD Multi Time Frame
            var MACDLine_2 = macd_2.MACD.Last(1);
            var PrevMACDLine_2 = macd_2.MACD.Last(2);

            Print("MACD1 Value {0}, MACD1 Prev {1}", MACDLine, PrevMACDLine);
            Print("MACD2 Value {0}, MACD2 Prev {1}", MACDLine_2, PrevMACDLine_2);
            Print("Ema1: {0}, Ema2: {1}, Ema3: {2}", i_MA_fast.Result.LastValue, i_MA_standart.Result.LastValue, i_MA_slow.Result.LastValue);

            // Strategy Rules

           // if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
            {
                if (MACDLine > 0 && PrevMACDLine < 0 == MACDLine_2 > 0 && PrevMACDLine_2 < 0)
                {
                  
                    if (i_MA_standart.Result.LastValue > i_MA_slow.Result.LastValue && i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue && i_MA_fast.Result.LastValue > i_MA_standart.Result.LastValue)
                   
                    if (Bars.LowPrices.Last(2) < i_MA_standart.Result.LastValue && Bars.LowPrices.Last(2) > i_MA_slow.Result.LastValue && Bars.ClosePrices.LastValue > i_MA_fast.Result.LastValue)

                            // if there is no buy position open, open one and close any sell position that is open
                            if (!IsPositionOpenByType(TradeType.Buy))
                            {
                                OpenPosition(TradeType.Buy);
                            }
                }

                else if (MACDLine < 0 && PrevMACDLine > 0 == MACDLine_2 < 0 && PrevMACDLine_2 > 0)
                {
                  
                   if (i_MA_standart.Result.LastValue < i_MA_slow.Result.LastValue && i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue && i_MA_fast.Result.LastValue < i_MA_standart.Result.LastValue)
                   
                    if (Bars.HighPrices.Last(2) > i_MA_standart.Result.LastValue && Bars.HighPrices.Last(2) < i_MA_slow.Result.LastValue && Bars.ClosePrices.LastValue < i_MA_fast.Result.LastValue)

                            // if there is no sell position open, open one and close any buy position that is open
                            if (!IsPositionOpenByType(TradeType.Sell))
                            {
                                OpenPosition(TradeType.Sell);
                            }
                }
            }
        }
        #endregion


        #region 

        /// <summary>
        /// Opens a new long position
        /// </summary>
        /// <param name="type"></param>

        private void OpenPosition(TradeType type)
        {
            if (Positions.Count == 0)
            // open a new position
            {
                ExecuteMarketOrder(type, this.SymbolName, volumeInUnits, PullBackStrategy,  null, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        private void ClosePosition(TradeType type)
        {
            var p = Positions.Find(PullBackStrategy, this.SymbolName, type);

            if (p != null)
            {
                ClosePosition(p);
            }
        }

        #endregion

        #region Position Information

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private bool IsPositionOpenByType(TradeType type)
        {
            var p = Positions.FindAll(PullBackStrategy, SymbolName, type);

            if (p.Length >= 1)
            {
                return true;
            }

            return false;
        }

        #endregion



        #region Stops 
        /// <summary>
        /// This method is called when your robot stops, can be used to clean-up memory resources.
        /// </summary>
        protected override void OnStop()
        {
            // unused
        }
        #endregion

        #region Break Even

        // code from clickalgo.com
        private void BreakEvenAdjustment()

        {

            var allPositions = Positions.FindAll(PullBackStrategy, SymbolName);

            foreach (Position position in allPositions)
            {
                var entryPrice = position.EntryPrice;
                var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;

                // move stop loss to break even plus and additional (x) pips
                if (distance >= BreakEvenPips * Symbol.PipSize)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
                        {
                            // && position.Pips >= trailingstoppips)
                            if (Includetrail)
                            {
                                //ModifyPosition(position, position.EntryPrice);
                                position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);

                                if (position.Pips >= Trailingstoppips)
                                    position.ModifyTrailingStop(true);

                            }
                            else if (!Includetrail)
                            {
                                //ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                position.ModifyStopLossPrice(position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips));
                                Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
                            }
                        }
                    }
                    else
                    {
                        if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
                        {
                            // && position.Pips >= trailingstoppips)
                            if (Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);

                                if (position.Pips >= Trailingstoppips)
                                    position.ModifyTrailingStop(true);
                            }
                            else if (!Includetrail)
                            {
                                ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
                                Print("Stop Loss to Break Even set for SELL position {0}", position.Id);

                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}


@Alwin123
Replies

Alwin123
26 Nov 2022, 16:32

i found it following my

 if (MACDLine > 0 && PrevMACDLine < 0)
                { 
                     if( MACDLine_2 > 0 && PrevMACDLine_2 < 0)

 


@Alwin123