question about the if else statement on entry

Created at 27 Nov 2022, 21:06
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

question about the if else statement on entry
27 Nov 2022, 21:06


Hi everyone I am trying to make an entry when the RSI and the MACD are in the correct position and when the second to last bar closes between the ema 20 and 50 and the prevbar closes above the ema 8 the next opening is the entry.

Now I feel that I am not writing this correctly in the if else statement, because the positions that are taken and that are too few over a certain period are only when both MACD are in the same direction. it seems that the rest of the entry lines are not being read.
Is there anyone who can help me adjust the entry rules or give an example of what I should pay attention to.

Thank you for your Help.

 

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 pips
 * Target 2 : Takeprofit pips
 * 
 * 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("Stop (Pips)", DefaultValue = 10, MinValue = 0, Group = "Protection")]
        public double StopInPips { get; set; }

        [Parameter("Target (Pips)", DefaultValue = 10, MinValue = 0, Group = "Protection")]
        public double TargetInPips { 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
        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


            // 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 += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;


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

            void OnPositionsOpened(PositionOpenedEventArgs obj)
            {
                position = obj.Position;
            }
        }


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

        }

        /// <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;
            }

            #endregion

            #region Position management


            // 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);

            var stopLoss = StopInPips == 0 ? null : (double?)StopInPips;
            var takeProfit = TargetInPips == 0 ? null : (double?)TargetInPips;

            // Strategy Rules

            {
                if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
                {
                    if (MACDLine > 0 && PrevMACDLine < 0 && MACDLine_2 > 0 && PrevMACDLine_2 < 0
                    && Bars.ClosePrices.Last(3) < i_MA_standart.Result.LastValue 
                    && Bars.ClosePrices.LastValue > i_MA_fast.Result.LastValue)

                    {
                        //OpenPosition(TradeType.Buy);
                        ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, PullBackStrategy, stopLoss, takeProfit);

                    }

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

                        //OpenPosition(TradeType.Sell);
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, PullBackStrategy, stopLoss, takeProfit);

                    }
                }
            }

        }
        #endregion
    }
}


 


@Alwin123