I need a simple help

Created at 19 Feb 2016, 06:30
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!
Rotterdam's avatar

Rotterdam

Joined 19.02.2016

I need a simple help
19 Feb 2016, 06:30


 

do not know if this will be the right place because I am new to the forum. I appreciate who could help me with this issue: I need to modify the CBOT Sample Martingale just open positions for the opening of the next candle. because it opens an operation after closing the previous one, and I need to wait for the opening of the next candle to open a new order. someone could help me?

This is the code:

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { 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();

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;

            ExecuteOrder(InitialQuantity, GetRandomTradeType());
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

 

 

 

 

 

 

thanks for the help Estefano Gigli Buenos Aires. Argentina


@Rotterdam
Replies

Spotware
24 Feb 2016, 14:53

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware

solark
28 Mar 2016, 21:55

Did you know if you flip a coin 100 times there's a ~0.80 chance of 5 consecutive heads? Or that if you could fold a piece of paper 42 times it would reach from the earth to the moon? Anyway, though I avoid this approach to risk/money management,  I'm not gonna assume you don't know the risks involved in martingale 'betting' and hopefully provide some help.
 

Simplest thing is to change where it's currently submitting an order to modifying some state variables, then use the state variables "OnBar" to decide on if and what order to submit. This is untested but the general idea:

 

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { 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 double tradeQuantity;
        private TradeType currentTradeType;
        private bool shouldEnter;

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
            currentTradeType = GetRandomTradeType();
            tradeQuantity = InitialQuantity;
            shouldEnter = true;
            //ExecuteOrder(InitialQuantity, currentTradeType);
        }

        protected override void OnBar()
        {
            if (shouldEnter)
            {
                shouldEnter = false;
                ExecuteOrder(tradeQuantity, currentTradeType);
            }
        }
        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;

            if (position.GrossProfit > 0)
            {
                shouldEnter = true;
                tradeQuantity = InitialQuantity;
                currentTradeType = GetRandomTradeType();
                //ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                shouldEnter = true;
                tradeQuantity = position.Quantity * 2;
                currentTradeType = position.TradeType;
                //ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

 


@solark