Replies

thamimsibi3
25 Mar 2019, 15:28

Hi Mr Charalampous. the cBot works very well. i did sme tests and I figured out i need one more tweek in the lot multiplication. I need to multiply the lot by 110%. which will give a result that has may numbers after the coma. then i need to round that result into 2 decimal places before sending the order to the broker otherwise it gets rejected.

here is the part im talking about...............

here i need the *2 to be *1.1

if previous lot was 0.12, then we multiply by 1.1 the answer will be 1.132... but i need to have 1.13 to send it as a lot size to the broker.

then i need to take only 2 numbers after the coma to make a valid Lot size

kind regards.

           if (position.GrossProfit > 0)

            {

                ExecuteOrder(InitialQuantity, position.TradeType);

            }

            else

            {

                if (position.TradeType == TradeType.Sell)

                    ExecuteOrder(position.Quantity * 2, TradeType.Buy);

                else

                    ExecuteOrder(position.Quantity * 2, TradeType.Sell);


@thamimsibi3

thamimsibi3
25 Mar 2019, 15:18

RE: RE:

thamimsibi3 said:

Panagiotis Charalampous said:

Hi thamimsibi3,

Thanks for the clarification, that was not very clear.

See below the cBot based on your clarification

// -------------------------------------------------------------------------------------------------
//
//    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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 3)]
        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.QuantityToVolumeInUnits(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, position.TradeType);
            }
            else
            {
                if (position.TradeType == TradeType.Sell)
                    ExecuteOrder(position.Quantity * 2, TradeType.Buy);
                else
                    ExecuteOrder(position.Quantity * 2, TradeType.Sell);
            }
        }

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

Best Regards,

Panagiotis

Thank you very much it works. straight perfectly.

Thank You.

Hi Mr Charalampous. the cBot works very well. i did sme tests and I figured out i need one more tweek in the lot multiplication. I need to multiply the lot by 110%. which will give a result that has may numbers after the coma. then i need to round that result into 2 decimal places before sending the order to the broker otherwise it gets rejected.

here is the part im talking about...............

here i need the *2 to be *1.1

if previous lot was 0.12, then we multiply by 1.1 the answer will be 1.132... but i need to have 1.13 to send it as a lot size to the broker.

then i need to take only 2 numbers after the coma to make a valid Lot size

kind regards.

        if (position.TradeType == TradeType.Sell)
                    ExecuteOrder(position.Quantity * 2, TradeType.Buy);
                else
                    ExecuteOrder(position.Quantity * 2, TradeType.Sell);

@thamimsibi3

thamimsibi3
12 Mar 2019, 17:23

RE:

Panagiotis Charalampous said:

Hi thamimsibi3,

Thanks for the clarification, that was not very clear.

See below the cBot based on your clarification

// -------------------------------------------------------------------------------------------------
//
//    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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 3)]
        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.QuantityToVolumeInUnits(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, position.TradeType);
            }
            else
            {
                if (position.TradeType == TradeType.Sell)
                    ExecuteOrder(position.Quantity * 2, TradeType.Buy);
                else
                    ExecuteOrder(position.Quantity * 2, TradeType.Sell);
            }
        }

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

Best Regards,

Panagiotis

Thank you very much it works. straight perfectly.

Thank You.


@thamimsibi3

thamimsibi3
12 Mar 2019, 15:35

RE:

Panagiotis Charalampous said:

Hi thamimsibi3,

Thanks for posting in our forum. See below

// -------------------------------------------------------------------------------------------------
//
//    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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 3)]
        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.QuantityToVolumeInUnits(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, position.TradeType);
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

Best Regards,

Panagiotis

Thank you Panagiotis i just tested it. i notice the difference it only opens sell trades all the time, which is not what im looking for.  i need it to open the next trade on this condition ( if previous SL OR TP was upwards, then the next trade is upwards(BUY). If previous SL or TP closed downwards, the the next trade is downward(sell)

kind regards.


@thamimsibi3