Can some expert help me on code Martingale?

Created at 23 Aug 2023, 18:33
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!
KA

kanapon

Joined 04.05.2019

Can some expert help me on code Martingale?
23 Aug 2023, 18:33


Can expert help to add to below code if i need to martingale for next order when previous position is closed with negative profit?

Thank you very much,  i try to see sample code of martingale and try to make it for two day but not success.

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MovingAverageMomentumStrategy : Robot
    {
        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossPips { get; set; }

        [Parameter("short MA Period", DefaultValue = 50, MinValue = 1)]
        public int masPeriod { get; set; }

        [Parameter("long MA Period", DefaultValue = 200, MinValue = 1)]
        public int malPeriod { get; set; }

        [Parameter("RSI Period", DefaultValue = 14, MinValue = 1)]
        public int rsiPeriod { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 40, MinValue = 1)]
        public int TakeProfitPips { get; set; }

        [Parameter("Volume", DefaultValue = 10000)]
        public int volume { get; set; }


        private MovingAverage ma50;
        private MovingAverage ma200;
        private RelativeStrengthIndex rsi;
        private MacdCrossOver macd;

        protected override void OnStart()
        {
            ma50 = Indicators.MovingAverage(Bars.ClosePrices, masPeriod, MovingAverageType.Exponential);
            ma200 = Indicators.MovingAverage(Bars.ClosePrices, malPeriod, MovingAverageType.Exponential);
            rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, rsiPeriod);
            macd = Indicators.MacdCrossOver(Bars.ClosePrices, 12, 26, 9);
        }

        protected override void OnBar()
        {

           

            bool isPriceAboveMa50 = Bars.ClosePrices.Last(1) > ma50.Result.Last(1);
            bool isMa50AboveMa200 = ma50.Result.Last(1) > ma200.Result.Last(1);
            bool isRsiAbove50 = rsi.Result.Last(1) > 50;
            bool isMacdAboveSignal = macd.MACD.Last(1) > macd.Signal.Last(1);

            bool isPriceBelowMa50 = Bars.ClosePrices.Last(1) < ma50.Result.Last(1); ;
            bool isMa50BelowMa200 = ma50.Result.Last(1) < ma200.Result.Last(1);
            bool isRsiBelow50 = rsi.Result.Last(1) < 50;
            bool isMacdBelowSignal = macd.MACD.Last(1) < macd.Signal.Last(1);

            if (Positions.Count != 0)
            {
                foreach (var position in Positions)
                {
                    if (position.TradeType == TradeType.Sell && isPriceAboveMa50)
                    {
                        position.Close();
                    }
                    else if (position.TradeType == TradeType.Buy && isPriceBelowMa50)
                    {
                        position.Close();

                    }
                }
            }

            if (isPriceAboveMa50 && isMa50AboveMa200 && isRsiAbove50 && isMacdAboveSignal && Positions.Count ==0)
            {
   


                ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "MovingAverageMomentum", StopLossPips, TakeProfitPips);
            }
            else if (isPriceBelowMa50 && isMa50BelowMa200 && isRsiBelow50 && isMacdBelowSignal && Positions.Count == 0)
            {



                ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "MovingAverageMomentum", StopLossPips, TakeProfitPips);
            }
        }


    }
}


@kanapon
Replies

rob.p.marshall
23 Aug 2023, 21:42

Try Sascha at sascha.coding@gmail.com


@rob.p.marshall

PanagiotisChar
24 Aug 2023, 05:12 ( Updated at: 24 Aug 2023, 05:25 )

Hi there,

If you need professional help, contact me at development@clickalgo.com

Aieden Technologies

Need help? Join us on Telegram


 


@PanagiotisChar