Market order opens a ton of positions!!!!!!

Created at 26 Jun 2019, 19:54
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!
RY

ryanoia@gmail.com

Joined 09.03.2018

Market order opens a ton of positions!!!!!!
26 Jun 2019, 19:54


if ((Positions.Find("order 1", Symbol, TradeType.Buy) == null) && (Positions.Find("order 1", Symbol, TradeType.Sell) == null))
{            
ExecuteMarketOrderAsync(TradeType.Buy, Symbol, Volume, "order 1", StopLossInPips, TakeProfitInPips, null);
ExecuteMarketOrderAsync(TradeType.Sell, Symbol, Volume, "order 1", StopLossInPips, TakeProfitInPips, null);
}            

Hello, I have this code above where the logic is that, if the bot detects no position for the particular symbol, then it creates one buy and one sell position. 

In backtesting it runs as coded. However, when I forward tested it and ran it in demo, it created 5 buys and 5 sell open positions simultaneously!

I put this under OnTick and OnBar (1 minute timeframe) and it still produced the same issues. Preferably though, I would run this ontick.

The problem I think, is that once bot detects there is no position on the chart, it sends the buy and sell market orders to the server, however, the server would take some time to process the order (maybe 1 or 2 seconds?) but during the time that the order has not been processed yet, the bot will keep detecting that there is no position as the pair still keeps on "tick-ing" and so the bot just keeps sending the market orders until the server is able to fulfill the orders and creates buy and sell positions. 

My questions:

1. Do you think my theory above is the reason why so many buy and sell positions are opened?

2. If yes, then what can I do code-wise to solve this issue? Is there a work-around for this?

3. Anyone encounter the same issue?

Any assistance will be highly appreciated.

Thanks. 


@ryanoia@gmail.com
Replies

ryanoia@gmail.com
26 Jun 2019, 19:58

One solution that I think would work is that the bot just runs the functions just once and then waits about 1 minute before running it again, by which time the server has probably already processed the order and so bot would not have to send new market orders since the symbol would have positions by that time. However, I have no idea how to code that. If you can, please advise.

On the other hand, if you have a solution different from the one above but would work as well, please feel free to share it.  


@ryanoia@gmail.com

PanagiotisCharalampous
27 Jun 2019, 09:41

Hi ryanoia@gmail.com,

Try using ExecuteMarketOrder instead of ExecuteMarketOrderAsync.

Best Regards,

Panagiotis


@PanagiotisCharalampous

ryanoia@gmail.com
27 Jun 2019, 18:39

The moment I read it, I knew this would work, and it did. 

Thanks. You're the best mate!


@ryanoia@gmail.com