How to club stop order and limit order in to one group

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
In the custom strategy i am using the limit stop order and limit sell order. But the issue is that both are not clubbed.If stop loss hit then still that sell order is active.
Need to club both orders in the 1 group . If any of the sell / stop loss is hit then the other order should be cancelled.

Basically need create orders like bracket order.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
480
Likes
192
Does your broker allow hedging ? That could be a possible reason why orders aren't grouped.

Look in the code of MW's TradeManager to see how to handle such a case:
Java:
 // Special Case:  If hedging is enabled we need to create separate position orders for each exit target
    Instrument instr = ctx.getInstrument();
    if (ctx.supportsHedging() && getSettings().getExitPoint(SECOND_EXIT).isEnabled()) {
      var orders = new ArrayList<Order>();
      var ep = getSettings().getExitPoint(FIRST_EXIT);
      entryOrder1 = createMktEntry(ctx, isLong(), ep.getLots() * instr.getDefaultQuantityAsFloat());
      orders.add(entryOrder1);
      ep = getSettings().getExitPoint(SECOND_EXIT);
      if (ep.isEnabled()) {
        entryOrder2 = createMktEntry(ctx, isLong(), ep.getLots() * instr.getDefaultQuantityAsFloat());
        orders.add(entryOrder2);
      }
      ep = getSettings().getExitPoint(THIRD_EXIT);
      if (ep.isEnabled()) {
        entryOrder3 = createMktEntry(ctx, isLong(), ep.getLots() * instr.getDefaultQuantityAsFloat());
        orders.add(entryOrder3);
      }
      ctx.submitOrders(orders);
    }
    else {
      entryOrder = createMktEntry(ctx, isLong(), getSettings().getInteger(TRADE_LOTS) * instr.getDefaultQuantityAsFloat());
      ctx.submitOrders(entryOrder);
    }


Could be due to another reason as well, but try to exclude this first ;)
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
Hi @Spin
Thanks for the reply . My broker supports the bracket order and if i am placing the order from the Trande Pannel as the Bracket 1 or 2 or 3 then the stop loss and limit sell / buy are clubbed as OCC orders . If one is hit the other will be cancelled automatically. In the MW chat section the stop loss and limit sell are connected with line and MW account section also it is displaying the both orders are grouped and marked with light blue color.
[MW Chart Screen]
383
384

I am using the similar kind of code present in Trade Manager. Once my order is placed then i am placing the my stop loss and limit sell / buy . But these 2 are triggered as individual orders . Once 1 is filled the other is still remaining there. In the account section the group is not colored
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
Though i am not able to place the OCC orders which will appear the connected in the link Chart but able to achieve my functionality by changing the approach.

if (ctx.getPosition()> 0 || ctx.getPosition()< 0 ) then placing the stop loss order and Limit profit order
else ctx.cancelOrders();

As of now my purpose is being solved but still need to know how to place OCC orders linked with each other chat and Order tab.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
480
Likes
192
Ok, I see what you mean, @sas7085.

There is a way to create orders with a 'reference', so you can in fact attach them to an already existing order:

(from the SDK API overview found here: createLimitOrder)
385

So my next steps would be:
  • make sure that the initial Order I create has a unique reference
  • print that reference in a debug statement to make 100% sure that what I am doing, is working
  • attach my Stop Loss / Take Profit Orders to that already existing Order (using the 'createLimitOrder' above, or one of its lookalikes: createMarketOrder or createStopOrder)
  • check whether stuff worked by means of a replay session test :)
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
I was trying to implement in that way but in onOrderFilled() the reference id of order is always null . Not sure why it is null . It should have some unique reference id.
There should be something groupOrder() where you can fire the multiple linked order including the initial order + stop loss + limit profit order. This is the easy way :)
 

Spin

Well-known member
Joined
May 22, 2019
Posts
480
Likes
192
Well, when you create the initial order, do you send a unique reference with it ?
If you do, it should not be 'null', and it should have a certain value.

Please try again, but make sure to send a unique 'RefID' with the initial order :)
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
I tried initial order in two ways ctx.buy(qty); & createMarketOrder(Instrument instr, Enums.OrderAction action, float qty) .
I guess the reference id is something the framework should return when placed one order and that should be associated with the broker ticket no Or order Id.

The MW API -
public java.lang.Object getReferenceId()
Gets the reference ID for this execution.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
480
Likes
192
So the problem is solved ?

Would you be so kind to post the code, so that others may benefit from it ? :)
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
As of now am using the work around way
onOrderFilled() method cancelling the all previous open orders by using the ctx.cancelOrders(); with the condition if (!(ctx.getPosition() > 0 || ctx.getPosition() < 0)).

Then creating the fresh limit and stop orders for the recently filled order.

Note - I am trading and testing for 1 instrument. I guess ctx.cancelOrders(); will cancel all open Limit orders irrespective of instruments.

Still i am looking for solution to place as OCC order.
 

sas7085

Active member
Joined
Jul 7, 2020
Posts
33
Likes
7
@Spin After you message i tried once again and it is working fine now. The Stop Loss + Limit orders are grouped in chart.

You are right that hedging account thing.

This time i tried with by passing the order id [entryOrder.getOrderId()] as reference id. Not sure earlier it was null everytime but today it is returning the value :)
Java:
Order createStopOrder(Instrument instr,
java.lang.Object refId,
Enums.OrderAction action,
Enums.TIF tif,
float qty,
float stopPrice)
 
Last edited:

Spin

Well-known member
Joined
May 22, 2019
Posts
480
Likes
192
Very nice work !! Glad you figured it out (y)
 
Top