Parameter for max open trades quantity

Created at 25 Feb 2019, 08:51
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!
jamespeterwilkinson's avatar

jamespeterwilkinson

Joined 23.02.2019

Parameter for max open trades quantity
25 Feb 2019, 08:51


Looking for code to set parameter for maximum amount of open trades,

Want to set a limit that can be changed for maximum allowable open trades.

 cTrader version 3.3

Thank you in advance


@jamespeterwilkinson
Replies

PanagiotisCharalampous
25 Feb 2019, 11:02

Hi jpwtrading,

See an example below

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.UTC, AccessRights = AccessRights.None)]
    public class CustomcBot : Robot
    {
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int MaxPositions { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            if (Positions.Count < MaxPositions)
            {
                // Do somehting
            }
        }

        protected override void OnStop()
        {

        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

jamespeterwilkinson
25 Feb 2019, 12:10 ( Updated at: 21 Dec 2023, 09:21 )

RE: Thank you

Panagiotis Charalampous said:

Hi jpwtrading,

See an example below

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.UTC, AccessRights = AccessRights.None)]
    public class CustomcBot : Robot
    {
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int MaxPositions { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            if (Positions.Count < MaxPositions)
            {
                // Do somehting
            }
        }

        protected override void OnStop()
        {

        }
    }
}

Best Regards,

Panagiotis

Thank you for the code.

Is my application of the code correct? added another &&

if (Functions.IsRising(AwesomeOscillator.Result) && Functions.HasCrossedAbove(AwesomeOscillator.Result, MarketSeries.Close, 10) && (Positions.Count < MaxPositions))
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, "cBot", StopLoss, TakeProfit, Slippage, "this is a comment");

also was surprised not to find MaxPositions in the Reference Editor

Regards + thank you

James


@jamespeterwilkinson

jamespeterwilkinson
25 Feb 2019, 19:03

RE:

Panagiotis Charalampous said:

Hi jpwtrading,

See an example below

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.UTC, AccessRights = AccessRights.None)]
    public class CustomcBot : Robot
    {
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int MaxPositions { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            if (Positions.Count < MaxPositions)
            {
                // Do somehting
            }
        }

        protected override void OnStop()
        {

        }
    }
}

Best Regards,

Panagiotis

@Panagiotis Working Thank you!

 


@jamespeterwilkinson

Boring
11 Aug 2022, 14:08 ( Updated at: 11 Aug 2022, 14:09 )

Parameter code for limiting max open trades not working

I need help, please :)

I was glad I found this and thought it would be simple to implement but I cannot get it to work in the following :

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.UTC, AccessRights = AccessRights.None)]
    public class TestcBOT : Robot
    {
        [Parameter(DefaultValue = 1, MinValue = 1)]
        public int MaxPositions { get; set; }
        
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1000, Step = 1000)]
        
        public int Volume { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            if (Positions.Count < MaxPositions)
            {
                // Do somehting
            }
        }

        protected override void OnStop()
        {

        }
        
        protected override void OnBar()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            foreach (var position in Positions)
            {
                if (position.Pips > 2)
                {
                    ClosePosition(position);}
                }
         }
    }
}

 


@Boring

PanagiotisCharalampous
11 Aug 2022, 14:10

Hi Liquidity,

You need to put the code inside the condition.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

Boring
11 Aug 2022, 14:13

RE:

PanagiotisCharalampous said:

Hi Liquidity,

You need to put the code inside the condition.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 

I have to be honest, sorry. I have no idea what that means "inside the condition".

If I know what the "condition" is, I can place the code inside it.


@Boring

PanagiotisCharalampous
11 Aug 2022, 14:24

Hi Liquidity,

Here you go

            if (Positions.Count < MaxPositions)
            {
              ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
              ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
              foreach (var position in Positions)
              {
                if (position.Pips > 2)
                {
                    ClosePosition(position);
                 }
              }
            }

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

Boring
11 Aug 2022, 14:51

RE:

PanagiotisCharalampous said:

Hi Liquidity,

Here you go

            if (Positions.Count < MaxPositions)
            {
              ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
              ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
              foreach (var position in Positions)
              {
                if (position.Pips > 2)
                {
                    ClosePosition(position);
                 }
              }
            }

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Thanks! There seems to be a difference in the backtesting results after adding the above code into protected override void OnTick()

 


@Boring

firemyst
12 Aug 2022, 02:43

RE: RE:

Liquidity said:

 

Thanks! There seems to be a difference in the backtesting results after adding the above code into protected override void OnTick()

 

Of course there is.

That's because in OnTick, the code is checked with every "tick" that comes in; in "OnBar", the code is only run once when a new bar is started.


@firemyst