Increase Order volume on every 4th position

Created at 20 Oct 2021, 14:01
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!
VE

velu130486

Joined 08.11.2019 Blocked

Increase Order volume on every 4th position
20 Oct 2021, 14:01


Dear All,

I am not a developer, however I am learning Cbot coding based on the sample bots available in this forum. With support of Ahmad, I able to put the limitation of Equity % for opening the Buy or sell orders and it is working fine. 

Below is the extract of code from my Cbot which opens the order based on buy or sell signal similar to Grid Bot.

private void tradeExecution(TradeType type, string sym, string label)
        {
            var equityLossPercentage = (Account.Equity - Account.Balance) / Account.Balance * 100;
            double close = lowBar[sym].ClosePrices.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;
            double atr = ATR[sym].Result.LastValue;
            double gap = ATR[sym].Result.LastValue * (1 / Symbols.GetSymbol(sym).PipSize);
                   
            if (autoMode2 && equityLossPercentage >= -10)
            {
                if (type == TradeType.Buy)
                {
 automode2PositionCount(sym, TradeType.Buy, close + atr, close - atr);
                    if (pcount > 0)
                    {
                        if (close > pAboveprice && pcountwithin == 0)
                            ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
                        if (close < pBelowprice && pcountwithin == 0)
                            ExecuteMarketOrder(TradeType.Buy, sym, minvolume * (pcount + 1), "EA", 0, 0);
                    }
                    else
                    {
                        ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
                    }
                    Print(sym + " : BUY" + " ATR : " + gap + "  Count: " + pcount + "   Within: " + pcountwithin);
                }
                if (type == TradeType.Sell)
                {
                    automode2PositionCount(sym, TradeType.Sell, close + atr, close - atr);
                    if (pcount > 0)
                    {
                        if (close > pAboveprice && pcountwithin == 0)
                            ExecuteMarketOrder(TradeType.Sell, sym, minvolume * (pcount + 1), "EA", 0, 0);
                        if (close < pBelowprice && pcountwithin == 0)
                            ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
                    }
                    else
                    {
                        ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
                    }
                    Print(sym + " : SELL" + " ATR : " + gap + "  Count : " + pcount + "   Within : " + pcountwithin);
                }
            }
            else
            {
                Print(sym + " : Max Equity Drawdown Trade not allowed " + losspercentage);
            }
        }

int pcount = 0;
int pcountwithin = 0;
double pAboveprice = 0;
double pBelowprice = 0;
        
void automode2PositionCount(string sym, TradeType type, double above, double below)
        {
            pcount = 0;
            pcountwithin = 0;
            volume_ = 0;
            pAboveprice = 0;
            pBelowprice = 9999999999L;
            foreach (var pos in Positions)
            {
                var sym_ = pos.SymbolName;
                var type_ = pos.TradeType;
                if (sym == sym_ && type == type_)
                {
                    pcount++;
                    pAboveprice = pos.EntryPrice > pAboveprice ? pos.EntryPrice : pAboveprice;
                    pBelowprice = pos.EntryPrice < pBelowprice ? pos.EntryPrice : pBelowprice;
                    pcountwithin += pos.EntryPrice >= below && pos.EntryPrice <= above ? 1 : 0;
                }
            }
        }

Actually the bot check the no of open position above the close and increase the volume for next Buy position and vice versa for Sell position. 

For ex Initial order was opened with 0.01 Lot, next order when the price is below opened position it will increase the volume to 0.02, 0.03, 0.04, 0.05 etc., and if it is above volume is not increased. But in certain market conditions it leads to equity drawdown

My query is how to modify this code to increase the volume only on every multiple of 4th position like below

0.01,0.01,0.01,0.02,0.01,0.01,0.01,0.03 etc like this.

Could you please help me to modify the code.

Thanks and Regards

R. Vadivelan


Replies

amusleh
21 Oct 2021, 09:43

Hi,

You can use timer, set the interval to 4 hours and double the volume whenever the timer elapsed:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class HighVolumeHours : Robot
    {
        private double _volume;

        protected override void OnStart()
        {
            // Here we set the initial volume to symbol minimum volume
            _volume = Symbol.VolumeInUnitsMin;

            // Here we start the timer and we set the interval to 4 hours
            Timer.Start(TimeSpan.FromHours(4));
        }

        protected override void OnTimer()
        {
            // double the volume whenever OnTimer is called
            _volume *= 2;

            // Stop the timer if we reached the symbol maximum volume
            if (_volume >= Symbol.VolumeInUnitsMax)
            {
                _volume = Symbol.VolumeInUnitsMax;

                Timer.Stop();
            }
        }
    }
}

 


@amusleh

velu130486
21 Oct 2021, 10:49 ( Updated at: 22 Oct 2021, 14:28 )

RE:

Hi Ahmad,

Thanks for your reply. Actually my request is to double the volume on every 4th position count not based on time.

Because sometimes my bot create more than 4 - 6 positions within a hour depends on moving average signal. So the volume is increased on each and every position which I would like to control it, so I would like to increase the volume only on 4,8,12,16 position for both buy and sell positions etc.,

Thanks and Regards

R. Vadivelan

amusleh said:

Hi,

You can use timer, set the interval to 4 hours and double the volume whenever the timer elapsed:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class HighVolumeHours : Robot
    {
        private double _volume;

        protected override void OnStart()
        {
            // Here we set the initial volume to symbol minimum volume
            _volume = Symbol.VolumeInUnitsMin;

            // Here we start the timer and we set the interval to 4 hours
            Timer.Start(TimeSpan.FromHours(4));
        }

        protected override void OnTimer()
        {
            // double the volume whenever OnTimer is called
            _volume *= 2;

            // Stop the timer if we reached the symbol maximum volume
            if (_volume >= Symbol.VolumeInUnitsMax)
            {
                _volume = Symbol.VolumeInUnitsMax;

                Timer.Stop();
            }
        }
    }
}

 

 


amusleh
25 Oct 2021, 09:51

RE: RE:

velu130486 said:

Hi Ahmad,

Thanks for your reply. Actually my request is to double the volume on every 4th position count not based on time.

Because sometimes my bot create more than 4 - 6 positions within a hour depends on moving average signal. So the volume is increased on each and every position which I would like to control it, so I would like to increase the volume only on 4,8,12,16 position for both buy and sell positions etc.,

Thanks and Regards

R. Vadivelan

amusleh said:

Hi,

You can use timer, set the interval to 4 hours and double the volume whenever the timer elapsed:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class HighVolumeHours : Robot
    {
        private double _volume;

        protected override void OnStart()
        {
            // Here we set the initial volume to symbol minimum volume
            _volume = Symbol.VolumeInUnitsMin;

            // Here we start the timer and we set the interval to 4 hours
            Timer.Start(TimeSpan.FromHours(4));
        }

        protected override void OnTimer()
        {
            // double the volume whenever OnTimer is called
            _volume *= 2;

            // Stop the timer if we reached the symbol maximum volume
            if (_volume >= Symbol.VolumeInUnitsMax)
            {
                _volume = Symbol.VolumeInUnitsMax;

                Timer.Stop();
            }
        }
    }
}

 

 

Hi,

You can do that by using position label to differentiate your cBot positions from other positions, then use Positions collection to count the number of your cBot open positions.

Then you can multiply the number of positions to volume or any other number you want to, for example if your cBot had 4 open positions you can multiply the volume by 2.


@amusleh

velu130486
25 Oct 2021, 10:22

RE: RE: RE:

Thanks Ahmad for your reply. I will try to play with position labels. By the way is there any possibility to change the Position label or comment once the position is opened. I am ok to do the modification manually also.

Thanks and Regards,

R. Vadivelan.

amusleh said:

velu130486 said:

Hi Ahmad,

Thanks for your reply. Actually my request is to double the volume on every 4th position count not based on time.

Because sometimes my bot create more than 4 - 6 positions within a hour depends on moving average signal. So the volume is increased on each and every position which I would like to control it, so I would like to increase the volume only on 4,8,12,16 position for both buy and sell positions etc.,

Thanks and Regards

R. Vadivelan

amusleh said:

Hi,

You can use timer, set the interval to 4 hours and double the volume whenever the timer elapsed:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class HighVolumeHours : Robot
    {
        private double _volume;

        protected override void OnStart()
        {
            // Here we set the initial volume to symbol minimum volume
            _volume = Symbol.VolumeInUnitsMin;

            // Here we start the timer and we set the interval to 4 hours
            Timer.Start(TimeSpan.FromHours(4));
        }

        protected override void OnTimer()
        {
            // double the volume whenever OnTimer is called
            _volume *= 2;

            // Stop the timer if we reached the symbol maximum volume
            if (_volume >= Symbol.VolumeInUnitsMax)
            {
                _volume = Symbol.VolumeInUnitsMax;

                Timer.Stop();
            }
        }
    }
}

 

 

Hi,

You can do that by using position label to differentiate your cBot positions from other positions, then use Positions collection to count the number of your cBot open positions.

Then you can multiply the number of positions to volume or any other number you want to, for example if your cBot had 4 open positions you can multiply the volume by 2.

 


amusleh
25 Oct 2021, 11:47

Hi,

No, you can't change the position label or comment once you set it.

If it was possible then you couldn't rely on it.


@amusleh