BracketOrder or 2 pendingorders (SL & TP)

Created at 16 Nov 2016, 12:45
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!
DE

DELETED_USER

Joined 08.04.2016 Blocked

BracketOrder or 2 pendingorders (SL & TP)
16 Nov 2016, 12:45


Hello, 

Is it possible to link 2 orders together into a bracketorder whereas if 1 of the orders is filled the 2nd one is cancelled?

I tried the following logic which didn't work. 

I am opening a position with a limitorder (for example buy1m eur/usd). 

I then on private void OnPositionOpened(PositionOpenedEventArgs args) Event send 2 pending orders, 1 limit (T/P) and 1 stop. 

This logic doesn't work since when either orders are filled a new position is opened rather than taking profit or stopping out of the already existing position. 

What would be the correct implementation of this logic?

Thanks


Replies

... Deleted by UFO ...

DELETED_USER
04 Dec 2016, 22:35

I am still struggeling to get this working since the stoploss and limit order actually open a new position rather than closing the actual position. 

For example: OnBarClose I am sending a buylimitorder to open a position. 

var buyLmtOrder = PlaceLimitOrder(TradeType.Buy, Symbol, size, buyLimit, "label");

OnPositionOpened I then send a stop and limit order based on the entryPrice of the position once the buyLmtOrder is filled. 

   private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            Print("Opened");
            var position = args.Position;
            Print("Position opened at {0}", position.EntryPrice);

            if (position.TradeType == TradeType.Buy)
            {
                PlaceLimitOrder(TradeType.Sell, Symbol, (long)position.Quantity, TPBuy, "label");
                PlaceStopOrder(TradeType.Sell, Symbol, (long)position.Quantity, sellSL, "label");
            }
        }

OnPositionClosed I then cancel the 2nd outstanding order so if stoploss is filled I cancel the limit and vice versa. 

  private void OnPositionClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.SymbolCode == Symbol.Code && position.NetProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }

            else if (position.SymbolCode == Symbol.Code && position.NetProfit < 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
        }

Any help would be much appreciated. 

 

 

 


cyfer
04 Dec 2016, 23:32

I can't follow what's going on here 

why are you opening 2 pending orders after the initial order is filled ??

are you trying to set Stop Loss and Take profit for the "Just opened" Position ? .. in that case you should set those parameters in the call back function

private void OnPositionOpened(PositionOpenedEventArgs args)
{
    var position = args.Position;
    var stopLoss = The Stop Loss you want to set;
    var takeProfit= the Take Profit you want to set;
    ModifyPosition(position, stopLoss, takeProfit);

}

 

 


@cyfer

DELETED_USER
06 Dec 2016, 21:16

RE:

Yes thats it cyfer, set it for the just opened position. 

 

Thanks!

 

cyfer said:

I can't follow what's going on here 

why are you opening 2 pending orders after the initial order is filled ??

are you trying to set Stop Loss and Take profit for the "Just opened" Position ? .. in that case you should set those parameters in the call back function

private void OnPositionOpened(PositionOpenedEventArgs args)
{
    var position = args.Position;
    var stopLoss = The Stop Loss you want to set;
    var takeProfit= the Take Profit you want to set;
    ModifyPosition(position, stopLoss, takeProfit);

}