Martingale following strategy

Created at 01 Sep 2020, 09:19
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!
LU

luciancastro89

Joined 07.07.2020

Martingale following strategy
01 Sep 2020, 09:19


Hi there, hope anyone can help me..

Is it possible to double position on every loss placing the code inside OnBar?     

 

 protected override void OnBar()

{

Some code to double position on every loss here?

}

 

I got here the regular Martingale working with the code inside OnPositionClosed but it does not follow the strategy..

 

private void OnPositionsClosed(PositionClosedEventArgs args)

{

Working MG but does not follow the strategy

}

 

 

Many thanksss


@luciancastro89
Replies

PanagiotisCharalampous
01 Sep 2020, 09:21

Hi Lucian,

Can you post your cBot's code and explain to us why do you think it is not working?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

luciancastro89
01 Sep 2020, 12:07

Hi Panagiotis, just spent more time with my code, I think Im almost there, the issue now is that MG is getting double orders.. here is the code

 

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class rsimg : Robot
    {


        [Parameter("Instance Name", DefaultValue = 4, MinValue = 1, Step = 1)]
        public string InstanceName { get; set; }

        [Parameter("Stop Loss", DefaultValue = 50)]
        public double SL { get; set; }

        [Parameter("Take Profit", DefaultValue = 150)]
        public double TP { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double ParamVol { get; set; }

        [Parameter("Long Trades", Group = "Positions", DefaultValue = 1, MinValue = 0)]
        public int MaxLongTrades { get; set; }

        [Parameter("Short Trades", Group = "Positions", DefaultValue = 1, MinValue = 0)]
        public int MaxShortTrades { get; set; }

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

        [Parameter("Periods", Group = "RSI", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("RSI Overbought Level", Group = "RSI", DefaultValue = 80, MinValue = 1, Step = 1)]
        public int RSIOverB { get; set; }

        [Parameter("RSI Oversold Level", Group = "RSI", DefaultValue = 40, MinValue = 1, Step = 1)]
        public int RSIOverS { get; set; }


        private RelativeStrengthIndex rsi;

        private int LongPositions = 0, ShortPositions = 0, MaxLong = 0, MaxShort = 0;


        protected override void OnStart()
        {

            rsi = Indicators.RelativeStrengthIndex(Source, Periods);

            MaxLong = MaxLongTrades;
            MaxShort = MaxShortTrades;
        }
        protected override void OnBar()
        {

            var LongPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Buy);
            var ShortPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Sell);
            double v = Symbol.QuantityToVolumeInUnits(ParamVol);
            HistoricalTrade lastClosed = History.FindLast(InstanceName, Symbol);


            if (LongPositions.Length < MaxLong && rsi.Result.LastValue < RSIOverS)
            {


      
                }
                ExecuteMarketOrder(TradeType.Buy, SymbolName, v, InstanceName, SL, TP);
                Close(TradeType.Sell);
            }

            if (ShortPositions.Length < MaxShort && rsi.Result.LastValue > RSIOverB)
            {

                }
                ExecuteMarketOrder(TradeType.Sell, SymbolName, v, InstanceName, SL, TP);
                Close(TradeType.Buy);
            }

        }
        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(InstanceName, SymbolName, tradeType))
                ClosePosition(position);
        }

    }
}


@luciancastro89

PanagiotisCharalampous
01 Sep 2020, 15:08

Hi Lucian,

What do you mean when you say it is getting "double orders"?

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luciancastro89
01 Sep 2020, 15:16 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi Lucian,

What do you mean when you say it is getting "double orders"?

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis,

Instead open only the first trade with double volume after a loss, it is opening the first and the second trade... check image please

 

 

 


@luciancastro89

PanagiotisCharalampous
01 Sep 2020, 15:28

Hi Lucian,

This happens because you open positions before the last one is closed but you use the last closed volume. If you want subsequent trades to double the volume, then you need to use the last opened volume and not the last closed volume.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luciancastro89
01 Sep 2020, 16:06

RE:

Thanks


@luciancastro89