how do you write this in c#

Created at 05 Nov 2022, 19:29
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 do you write this in c#
05 Nov 2022, 19:29


 

I want to open a position when the EMA 8 is below the EMA 20 and the EMA 20 is below 50 as shown in the picture.

the tricker candle is the candle that closes between the ema 20 and ema 50. The entry is closed below the EMA 8 after the 2nd candle
And as an extra the EMA 50 must fall

 

I've been trying to understand the rules around IsFalling and IsRising, as well as the HasCrossedAbove etc. But I can't figure out how to make this entry.

 

Tanks for hour Hulp!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Runtime.Intrinsics.X86;
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.None)]
    public class PullbackStrategy : Robot
    {

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

        [Parameter("Take Profit", Group = "Protect", DefaultValue = 50, MaxValue = 50, 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("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("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 = 19, MaxValue = 20, MinValue = 12, Step = 1)]
        public int Periods { get; set; }

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

        [Parameter("Oversold", Group = "RSI", DefaultValue = 30, MaxValue = 30, MinValue = 10, Step = 10)]
        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; }


        private Position position;
        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()

        {
            macd = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
            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_2 = Indicators.MacdCrossOver(_marketSeries2.ClosePrices, LongPeriod2, ShortPeriod2, SignalPeriod2);
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            
         
        }
        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            position = null;
        }

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

        [Obsolete]
        protected override void OnBar()
        {
            if (position != null)
                return;

            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 (Trade.IsExecuting) return;


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


            {
                int index = MarketSeries.Close.Count;


                if (MarketSeries.Close.HasCrossedBelow(i_MA_standart.Result, Backstep)
                   && i_MA_slow.Result.LastValue > i_MA_standart.Result.LastValue
                   && i_MA_fast.Result[index - 2] < MarketSeries.Close[index - 2]
                   && i_MA_standart.Result[index - 1] > MarketSeries.Close[index - 2])
                 //  && MACDLine > Signal & PrevMACDLine > PrevSignal
                //   && MACDLine_2 > Signal_2 & PrevMACDLine_2 > PrevSignal_2)

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

                else if (MarketSeries.Close.HasCrossedAbove(i_MA_standart.Result, Backstep)
                         && i_MA_slow.Result.LastValue < i_MA_standart.Result.LastValue
                         && i_MA_fast.Result[index - 2] > MarketSeries.Close[index - 2]
                         && i_MA_standart.Result[index - 1] < MarketSeries.Close[index - 2])
                    //     && MACDLine < Signal & PrevMACDLine < PrevSignal
                    //     && MACDLine_2 < Signal_2 & PrevMACDLine_2 < PrevSignal_2)

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

        }

        protected override void OnStop()
        {

        }
    }
}

 


@Alwin123