Delay new position after closed position

Created at 23 Oct 2013, 00: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!
OL

Old Account

Joined 14.10.2013

Delay new position after closed position
23 Oct 2013, 00:01


Hi

What code should i use to stop the robot form opening a new order after a position is closed. So it doesn't open a new position whitin 10 bars or so.

Thanks


@Old Account
Replies

Spotware
23 Oct 2013, 12:52

You can set a boolean field in the OnPositionClosed method and then check if you can open positions.

Set the boolean field:

         private bool isOpenPositionsPermitted;

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

        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            isOpenPositionsPermitted = false;
        }

Check if the position is closed in the OnTick/OnBar events or your user defined method which opens positions:

if(isOpenPositionsPermitted)
{
  // Your logic to open positions
}

Reset the boolean field to false according to your algorithm logic. If you want to count 10 bars then set a variable in the  OnPositionsClosed to the bar count:

        private int barCountSinceLastPosition;

        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            isOpenPositionsPermitted = false;
            barCountSinceLastPosition = MarketSeries.Close.Count;
        }

Check if the bar count value against the current bar in the OnTick/OnBar events or your user defined method which opens positions:

if (MarketSeries.Close.Count - barCountSinceLastPosition >= 10)
{
    isOpenPositionspermitted = true;
}

 


@Spotware

Old Account
23 Oct 2013, 15:12

Thanks

 


@Old Account

WinningTrader
19 May 2014, 14:32

Has Anyone tried delaying by a fixed interval of minutes ?

 

say gap between 2 consecutive positions = 10 Minutes ?

 

 


@WinningTrader

Spotware
19 May 2014, 15:08

Fixed interval in minutes:

        protected override void OnTick()
        {
            DateTime? lastPositionClosingTime = null;
            if (History.Count > 0)
            {
                lastPositionClosingTime = History[History.Count - 1].ClosingTime;
            }
            if (lastPositionClosingTime == null || lastPositionClosingTime.Value - Server.Time >= TimeSpan.FromMinutes(10))
            {
                // Your logic to open positions
            }
        }

 


@Spotware

WinningTrader
19 May 2014, 15:10

RE:

Spotware said:

Fixed interval in minutes:

        protected override void OnTick()
        {
            DateTime? lastPositionClosingTime = null;
            if (History.Count > 0)
            {
                lastPositionClosingTime = History[History.Count - 1].ClosingTime;
            }
            if (lastPositionClosingTime == null || lastPositionClosingTime.Value - Server.Time >= TimeSpan.FromMinutes(10))
            {
                // Your logic to open positions
            }
        }

 

thanks


@WinningTrader

Drummond360
17 Jan 2018, 17:03

RE:

Spotware said:

You can set a boolean field in the OnPositionClosed method and then check if you can open positions.

Set the boolean field:

         private bool isOpenPositionsPermitted;

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

        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            isOpenPositionsPermitted = false;
        }

Check if the position is closed in the OnTick/OnBar events or your user defined method which opens positions:

if(isOpenPositionsPermitted)
{
  // Your logic to open positions
}

Reset the boolean field to false according to your algorithm logic. If you want to count 10 bars then set a variable in the  OnPositionsClosed to the bar count:

        private int barCountSinceLastPosition;

        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            isOpenPositionsPermitted = false;
            barCountSinceLastPosition = MarketSeries.Close.Count;
        }

Check if the bar count value against the current bar in the OnTick/OnBar events or your user defined method which opens positions:

if (MarketSeries.Close.Count - barCountSinceLastPosition >= 10)
{
    isOpenPositionspermitted = true;
}

 

Typo in last line: 'isOpenPositionspermitted' should be 'isOpenPositionsPermitted'... Had me confused for a wee while so thought I'd share...


@Drummond360

PanagiotisCharalampous
17 Jan 2018, 17:07

Hi Drummond360,

Indeed it is a typo.

Best Regards,

Panagiotis


@PanagiotisCharalampous