Need help to add Martingale

Created at 16 Jul 2020, 16:42
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

Need help to add Martingale
16 Jul 2020, 16:42


Hi guys, I tried to add a simple MG on this bot but no success, anyone can help me? I think could be a good bot

 

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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 RickyMoney : Robot
    {
        [Parameter("Max No of Positions", DefaultValue = 1, MinValue = 1, MaxValue = 1000, Step = 1)]
        public int NoOfPositions { get; set; }

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

        [Parameter("SL", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("TP", DefaultValue = 20)]
        public int TakeProfit { get; set; }
        int newprofit;

        private Random random = new Random();
        bool ganada;
        bool compra;
        bool permiso;

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

            ExecuteOrder(InitialQuantity, GetRandomTradeType(), StopLoss, TakeProfit);
        }

        private void ExecuteOrder(double quantity, TradeType tradeType, int stop, int take)
        {

            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Robot", stop, take);

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

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

            if (position.Label != "Robot" || position.SymbolCode != Symbol.Code)
                return;
            if (position.NetProfit > 0)
            {
                ganada = true;
                if (position.TradeType == TradeType.Buy)
                {
                    compra = true;
                }
                else
                {
                    compra = false;
                }
            }
            else
            {
                ganada = false;
                if (position.TradeType == TradeType.Buy)
                {
                    compra = true;
                }
                else
                {
                    compra = false;
                }
            }
        }

        protected override void OnBar()
        {
            if (Positions.Count < NoOfPositions)
                if (permiso)
                {
                    permiso = false;
                    if (ganada)
                    {
                        newprofit = TakeProfit;
                        if (compra)
                        {
                            ExecuteOrder(InitialQuantity, TradeType.Sell, StopLoss, TakeProfit);
                        }
                        else
                        {
                            ExecuteOrder(InitialQuantity, TradeType.Buy, StopLoss, TakeProfit);
                        }
                    }
                    else
                    {
                        if (compra)
                        {

                            ExecuteOrder(InitialQuantity, TradeType.Buy, StopLoss / 1, newprofit);
                        }
                        else
                        {
                            ExecuteOrder(InitialQuantity, TradeType.Sell, StopLoss / 1, newprofit);
                        }
                    }
                }
        }

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


@luciancastro89
Replies

PanagiotisCharalampous
16 Jul 2020, 16:45

Hi luciancastro89,

Can you explain what is the problem?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

luciancastro89
16 Jul 2020, 23:47

nevermind, fixed already

 

 

    

Thanks again Panagiotis


@luciancastro89