How to make several OCO for stop_orders and limit_orders

Questions about MultiCharts .NET and user contributed studies.
d0ke
Posts: 6
Joined: 05 May 2014
Been thanked: 1 time

How to make several OCO for stop_orders and limit_orders

Postby d0ke » 27 Feb 2017

I use MultiCharts .NET64 Version 10.0 and use your From_broker_to_strategy_synchronizer to make my own strategy see when I go long or short while I'm trading in IB TWS. Then my strategy manages the trade for me, and I want it to take off a part of my position. However, it seems MultiCharts place orders in the way that makes MultiCharts useless for me. To not over complicate things with my own strategy logic, I prepared very simple logic here for you:

protected override void CalcBar(){

if (StrategyInfo.MarketPositionAtBroker > 0)
{
serLongPos.Value = Math.Max(serLongPos[1], StrategyInfo.MarketPositionAtBroker);
if (Bars.Status == EBarState.Close)
{
serLongInitBarsAgo.Value = serLongInitBarsAgo[1] + 1;
}
Output.WriteLine("Long: {0}/{1} sh, {2} bars ago", StrategyInfo.MarketPositionAtBroker, serLongPos[0], serLongInitBarsAgo[0]);

sell_stop_market1OCO.Send("S1_stop", Bars.Low[1], serLongPos[0] / 3); // into one OCO group
sell_limit1OCO.Send("S1_takeprofit", Bars.Close[serLongInitBarsAgo[0]] + 0.15, serLongPos[0] / 3); //

sell_stop_market2OCO.Send("S2_stop", Bars.Low[2], serLongPos[0] / 3); // into another OCO group
sell_limit2OCO.Send("S2_takeprofit", Bars.Close[serLongInitBarsAgo[0]] + 0.3, serLongPos[0] / 3); //

sell_stop_market3.Send("S3_stop", Bars.Low[3], serLongPos[0] / 3); //no OCO order for this stop
}

so, if I'm long then place:
sell stop(1/3size, trigger Low[1]) and sell limit(1/3 size, +15cents from close),
sell stop(1/3size, trigger Low[2]) and sell limit(1/3size, +30cents from close),
sell stop(1/3size, trigger Low[3]).

MultiCharts just place all orders into one group. But I need first sell stop order to be OCO with first sell limit (to take profit partly 15cents higher, and if it happens - cancel one stop, or if I get a partial fill then IB adjust sell stop size on back-end side). The same for the second stop and second limit.
Are there any ways to do that properly, so my stops and take profits are in proper OCO groups and all orders stored at IB back-end to eliminate connection risks?

1. It's impossible to use GenerateProfitTarget() since no option there to take profit partly.
2. It's impossible to create 3 strategies to break groups apart - the orders created at one tick go to one group.
3. You can't emulate profit taking by implementing the condition "if the price goes 15cents higher, then sell market 1/3size", because that taking profit order won't be stored at IB back-end.

How to manage this properly?
2017-02-27_232613.jpg
(233.23 KiB) Downloaded 905 times
zzTrailHiLoStopsWithOCO.pln
(2.58 KiB) Downloaded 481 times

d0ke
Posts: 6
Joined: 05 May 2014
Been thanked: 1 time

Re: How to make several OCO for stop_orders and limit_orders

Postby d0ke » 02 Mar 2017

5 day up

User avatar
Angelina MultiСharts
Posts: 260
Joined: 28 Dec 2016
Has thanked: 28 times
Been thanked: 66 times

Re: How to make several OCO for stop_orders and limit_orders

Postby Angelina MultiСharts » 05 Mar 2017

Hello d0ke,

Groups of orders have to be sent one by one to form separate OCO-groups. Send the first group, when it is processed - send the next one, etc.

In PLE.NET you can send unmanaged orders (bypassing the autotrading engine). Using this mechanism it is possible to group the orders by OCO-groups. But such orders will not be taken into account in the autotrading.

Unmanaged orders can be sent using ITradingProfile.PlaceOCOOrders() method:

Code: Select all

int[] PlaceOCOOrders( MTPA_OCOGroupType oco_type, params OrderParams[] orders);

d0ke
Posts: 6
Joined: 05 May 2014
Been thanked: 1 time

Re: How to make several OCO for stop_orders and limit_orders

Postby d0ke » 05 Mar 2017

TY Angelina, while the things you said make some sens, I guess it's too hard for my current coding level. Simple strategy script would be very helpful.


Return to “MultiCharts .NET”