Avoid opening multiple positions the same second

Created at 12 Aug 2016, 03:22
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!
CR

croucrou

Joined 24.02.2016

Avoid opening multiple positions the same second
12 Aug 2016, 03:22


Hello,

how to trade with the OnTick method and not to get overwhelmed by thousands of positions opened at the very same time?

I asked a similar question time ago, but didn't get a simple solution and I am sure there must be one though.

Please advise.

Thank you.


@croucrou
Replies

tradermatrix
12 Aug 2016, 11:29

Hi,

it depends on your code.
you can use the method "open close" or just add this piece:

 

 var cBotPositions = Positions.FindAll("your label");

                    if (cBotPositions.Length >= 1)
                        return;

 


@tradermatrix

croucrou
13 Aug 2016, 01:01

Hello and thank you for responding.

The "open close" method wouldn't work, as the orders open in the same moment unforunately.

The second way is looking promissing. Possibly this has been the answer. I will be looking into it.

I also found this link referring to MetaTrader and have been hoping for a solution like this:

http://www.fxpro.co.uk/help-section/faq/fxpro-quant/why-does-my-expert-advisor-keep-opening-multiple-positions


@croucrou

tradermatrix
13 Aug 2016, 11:23

Hi,

if you have a code to improve, you can share it, there are many solutions ...

it is complicated to work in the blind ..

 

 


@tradermatrix

croucrou
13 Aug 2016, 17:26

Yes, but the question is of a general nature. It is not dependent on a particular code.

How to use the OnTick() method and not to open unlimited number of positions at that tick.

This seems to be a common issue and could be platform regulated and not require coding, actually.


@croucrou

commando1
23 Aug 2016, 23:05

             if (Positions.Count > 0 || Trade.IsExecuting)
                return;

maybe this might work


@commando1

croucrou
31 Aug 2016, 14:00

Thanks for responding. 

Inspired by first reply, to have only one position open at a time OnTick(), I now open position with:

var myPositions = Positions.FindAll("Label");

if (myPositions.Length == 0)  //to enter
return

if (myPositions.Length >= 1)  //to exit
return

I don't know, if this is the best solution, but it works!

I did not check the "count" method, but I guess it needs an additional "using" reference and seems to be too complicated, doesn't it.


@croucrou

alexgbg30@gmail.com
17 Nov 2017, 00:09

RE:

croucrou said:

Hello,

how to trade with the OnTick method and not to get overwhelmed by thousands of positions opened at the very same time?

I asked a similar question time ago, but didn't get a simple solution and I am sure there must be one though.

Please advise.

Thank you.

for example if you want to open one position or 2 or more when position closed

and there is 3 position closed at the same time, and if you let it, it will ceep duplicate until hundreds or  thousands

U can chose how many ticks it will hold opening position before let the code continue working 

         [Parameter(DefaultValue = 1)]
        public int Value { get; set; }

        [Parameter(DefaultValue = 50)]
        public int TakeProfit { get; set; }

        [Parameter(DefaultValue = 800)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 10)]
        public int MaxTick { get; set; }   // U can choose how meny ticks its going to wait befor open a new position




        int tick = 0;






//  //  //  //  //  //  // OnStart

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

        }


//  //  //  //  //  //  // OnClose
        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            if (tick == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Value, "Buy2", StopLoss, TakeProfit);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Value, "Sell2", StopLoss, TakeProfit);
                tick = 1;
            }
        }


//  //  //  //  //  //  // OnTick

        protected override void OnTick()
        {


            var BP = Positions.FindAll("Buy").Length;
            var SP = Positions.FindAll("Sell").Length;

            if (tick >= 1)
            {
                tick = tick + 1;
                if (tick >= MaxTick)
                {
                    tick = 0;
                }
            }
 

 

if there are any thing else just let me know 


@alexgbg30@gmail.com