Opening a single position for critera

Created at 17 Oct 2018, 15:28
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!
TI

tim_d2

Joined 17.10.2018

Opening a single position for critera
17 Oct 2018, 15:28


Hi,

I am rather new to codeing cbots with my experience limited to VBA and MathLab in the past.

During backtesting, I noticed in my trade history multiple trades opening on the same time period when critera is met, skewing my results.

I would like only 1 trade to be placed when the critera is met.

My setup is:

if (Criteria1 && Positions.Count == 0 && Criteria2)
            {
                PlaceLimitOrder(TradeType.Buy, Symbol, VolumeInUnits, Limit, label, SL, boll2.TP);
            }
            else if (Criteria1 && Positions.Count == 0 && Criteria2)
            {
                PlaceLimitOrder(TradeType.Sell, Symbol, VolumeInUnits, Limit, label, SL, TP);
            }

Criteria1 and Criteria2 are based on movement in bollingerbands.
The if statement is run in the OnTick() instance.

besides Positions.Count I have also tried

longPosition == null

with

             longPosition = Positions.Find(label, Symbol, TradeType.Buy);

Any advise would be much appreciated.


@tim_d2
Replies

freemangreat
17 Oct 2018, 15:59

Count	Property	The total number of open positions.

limit order is not an open position.

Are you sure the label is defined?

Print("Positions {0}: {1}", label, Positions.FindAll(label));

 


@freemangreat

tim_d2
17 Oct 2018, 23:44

Ahh, so my code is checking open positions, which in effect allows multiple limit orders to be placed at once. How would I reference limit orders that are pending in the market?
@tim_d2

freemangreat
18 Oct 2018, 02:29

Maybe PendingOrders?


@freemangreat

PanagiotisCharalampous
18 Oct 2018, 09:42

Hi tim_d2,

Yo get the pending limit orders. you can try the following

PendingOrders.Where(x => x.OrderType == PendingOrderType.Limit);

Best Regards,

Panagiotis


@PanagiotisCharalampous