- ago
Hi everyone,

In Wealth-Lab, I've utilized the OCO option effectively in strategies with fixed price take profit and stop loss. However, with mean reversion strategies involving a stop loss and a timeout order after X days at market close, OCO doesn't trigger (The chain symbol in the order manager doesn't appear).

Twice in the last month, my stop loss was hit, but the OCO didn't cancel the market close order, leading to double sells.

Is there a way to programmatically link orders to enforce OCO rules ? How might this be incorporated into the strategy code?

I also read this post but : https://www.wealth-lab.com/Discussion/IB-broker-OCO-usage-Order-Take-Profit-Stop-Loss-7934 where Cone said
QUOTE:
NOTE!
OCO only works for orders whose prices do not change. If the strategy changes stop/limit price levels, at least for now, do not check the OCO option. (We're working on this, but brokers treat this replace operation differently.)

This is probably the reason why the market close order doesn't work with OCO but you said you guys were working on it. Any news on this ?

thx
Spacious
0
380
Solved
5 Replies

Reply

Bookmark

Sort
Cone8
 ( 4.72% )
- ago
#1
To link exit orders as a group, you need to assign the same code (any integer) to Backtester.CancelationCode.
Then when one of the orders in the group fills, the other orders will be canceled.

Example -
CODE:
            if (someCondition)             {                Backtester.CancelationCode = 123;                ClosePosition(foundPosition0, OrderType.Market, 0, "");             }                          Backtester.CancelationCode = 123;                         ClosePosition(foundPosition0, OrderType.Stop, val * 0.98, "Stp");                          Backtester.CancelationCode = 123;             val2 = multSource2[idx];             ClosePosition(foundPosition0, OrderType.Limit, val * 1.01, "Tgt");
2
Best Answer
- ago
#2

Hello Cone,

I incorporated the cancellation code in my strategy, incrementing it with a static variable.

CODE:
static int CancelationCode = 0; ... Backtester.CancelationCode = CancelationCode; ClosePosition(p, OrderType.Stop, stop, "Stop Loss")); Double profitPercent = p.ProfitPctAsOf(idx); if (profitPercent > profitPctLimit) { Backtester.CancelationCode = CancelationCode; ClosePosition(p, OrderType.MarketClose, 0.0, "Take Profit")); } if (idx - p.EntryBar >= 3) { Backtester.CancelationCode = CancelationCode; ClosePosition(p, OrderType.MarketClose, 0.0, "Timeout"); } CancelationCode++;


After placing the orders in the order manager, I noticed the chain link icon isn't visible next to the order. Is it supposed to appear? Also, does this work with "cover" orders in short selling scenarios?

thx
0
Cone8
 ( 4.72% )
- ago
#3
OCO applies only to stop and limit exit orders that are placed at the same time.

Backtester.CancelationCode works for all exit orders in the group with the same code.
0
- ago
#4
Sorry but I don't understand exactly how it works.

The Backtester.CancelationCode is a "global" variable of the Backtester, so reassigning it before each ClosePosition appears unnecessary. Does the Backtester.CancelationCode value is assigned to the order when calling ClosePosition method ?

I'm dispatching all orders simultaneously to the order manager, but I need to group them by symbol, not all together. That's why I create a static variable to increment the cancelation code for each symbol.

Wouldn't it be more flexible to assign a cancellation code directly to ClosePosition, allowing for grouped management instead of using a broad variable for a single group?

thx
0
Cone8
 ( 4.72% )
- ago
#5
If you're sure that your code doesn't set it to a different value somewhere else for another set of exits, then sure it's not necessary to assign it before each order - but you need to assign it at least once. And, it doesn't hurt to reinforce the idea that the orders are part of the same group.

QUOTE:
That's why I create a static variable to increment the cancelation code for each symbol.
That's not required. The group is already unique by symbol. I'll make sure to add that to the doc.

QUOTE:
Wouldn't it be more flexible
More flexible, how? In both cases the code is assigned to the transaction. As I recall it was just easier to add it this way than to break a lot of method signatures.
1

Reply

Bookmark

Sort