controlling the martingale

Created at 16 May 2013, 00:50
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!
TR

tradermatrix

Joined 24.07.2012

controlling the martingale
16 May 2013, 00:50


hello
on martingale:

the robot multiplied by 2 to the infinite
I would like to find a solution to stop the martingale and thus reduce the risk of exponential loss.
for example:
10000 ... 20000 ... 40000 ...
40000 when the stop loss triggers, back to 10000
cordially

 

using System;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleMartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }


        private Random random = new Random();
        private Position position;

        protected override void OnStart()
        {
            ExecuteOrder(InitialVolume, GetRandomTradeCommand());
        }

        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            var request = new MarketOrderRequest(tradeType, volume)
                {
                    Label = "SampleMartingaleRobot",
                    StopLossPips = StopLoss,
                    TakeProfitPips = TakeProfit
                };
            Trade.Send(request);

        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            if (closedPosition.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetRandomTradeCommand());
            }
            else
            {
                ExecuteOrder((int) position.Volume * 2, position.TradeType);
            }
        }

        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }

        private TradeType GetRandomTradeCommand()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;

 


        }
    }
}

 

 


@tradermatrix
Replies

lec0456
16 May 2013, 02:32

 

I did that but it didn't give great results.  You just use an if statement:

if (closedPositon<InitialVolume*4)

ExecuteOrder((int) position.Volume * 2, position.TradeType);

else 

ExecuteOrder((int) position.Volume, position.TradeType);


@lec0456

tradermatrix
16 May 2013, 13:06

  protected override void OnPositionClosed(Position closedPosition)
        {
       if (closedPosition <InitialVolume*4)

ExecuteOrder((int) position.Volume * 2, position.TradeType);

else 

ExecuteOrder((int) position.Volume, position.TradeType);
}

thank you
but the robot does not work

I have added if (closedPosition.GrossProfit <InitialVolume*4)

but the martingale does not work.
may be that I did not understand your method.
cordially


@tradermatrix