I made a test script with a stoploss and a profittarget. I am using the autotrader.
What it seems like is this:
1) stoploss get hit and say partial fill of 200 out of 1000 stocks.
2) MC Autotrader is "locking" the stop order because it is in the process of selling/buying.
3) MC Autotrader is syncing (reducing) the amount of stock with 200 of the stoploss and updates IB TWS.
4) MC Autotrader is not "locking" the profit order and with the syncing (reducing) the amount of stock with 200. The profittarget is replaced with the same target but with a new outstanding amount of stock of 800.
5) Because the profittarget is replaced (and therefore cancelled) also the stop order is cancelled. because this is part of the OCO order.
6) Stop order is replaced with the same target and needs to be hit again. When hit again, this results in multiple orders with additional costs.
I tried the test script again but now without the profittarget. And then the stoporder is not replaced but it updates the amount of stock on IB TWS and everything works as it is intended. That's why I think the profit target is not "locked" (target price is not changed, only the stock amount).
This is the testscript I used:
Code: Select all
// Signal: LSI TEST SCRIPT
// Version: 0.1
// Date: 14 november 2012
// Last backup made :
// DESCRIPTION:
// Script to test the stoploss, profit target and trailing stop
[IntrabarOrderGeneration = True]
[LegacyColorValue = TRUE];
inputs:
// time inputs
Begin_time_Trade (900), // start time for engaging trades
End_time_Trade (2300), // end time for trading
Begin_time_plus_minutes(5), // Begin_time_trade + the input of this in minutes
End_time_minus_minutes(0), // end time for for trading -/- the input of this in minutes
Begin_time_Targets (800), // start time for enabling profit/stoploss targets
End_time_Targets(2300), // end time for disabling profit/stoploss targets. Usefull for end of market when price are crazy
// Price inputs
Price (Close), // price = close of bar
// profit targets and stoploss inputs
StockAmt (1000), // amount of stocks
ProfittargetAmt (500), // profittarget
RiskRewardAmt (2); // riskreward
variables:
ResultStoploss(0),
Stoploss_Fixed (0),
Stoploss_Fixed_Long (0),
Stoploss_Fixed_Short (0),
Profit_Fixed (0),
Profit_Fixed_Long(0),
Profit_Fixed_Short(0),
Begin_time_trigger(0),
End_time_trigger(0);
// TIME SETTINGS
Begin_time_trigger = calctime(begin_time_trade, begin_time_plus_minutes);
End_time_trigger = calctime(end_time_trade, end_time_minus_minutes);
// DETERMINE PROFIT AND STOPLOSS TARGETS
Profit_Fixed = ProfittargetAmt/StockAmt;
Stoploss_Fixed = (profittargetAmt/RiskRewardAmt)/StockAmt;
ResultStoploss = Profit_Fixed / stoploss_fixed;
// TEST CONDITIONS
Condition1 = price > price[1];
Condition2 = price < price[1];
// ENTRY CONDITIONS
If (barstatus(1) = 2)then begin;
If time > Begin_time_trigger and time < End_time_trigger then begin
If resultstoploss >= 2 and Condition1 then
if MarketPosition = 0 then begin
Buy ("Long") next bar market;
end;
{ If resultstoploss >= 2 and Condition2 then
if MarketPosition = 0 then begin
Sellshort ("Short") next bar market;
end;}
end;
End;
// EXIT CONDITIONS ---------------------------------------------------------------------------------------
// STOPLOSS AND PROFIT TARGET CALCULATING PER STOCK
If time > Begin_time_targets and time < End_time_Targets then begin
If marketposition <> 0 then begin
Profit_Fixed_Long = AvgEntryprice_at_broker + Profit_Fixed;
Profit_Fixed_Short = AvgEntryprice_at_broker - Profit_Fixed;
Stoploss_Fixed_Long = AvgEntryprice_at_broker - Stoploss_Fixed;
Stoploss_Fixed_Short = AvgEntryprice_at_broker + Stoploss_Fixed;
end;
// PROFIT TARGET FIXED
// Long exit:
if marketposition = 1 then begin
Sell ("CloseProfitLong") next bar Profit_Fixed_Long limit;
end;
// Short exit
if marketposition = -1 then begin
BuytoCover ("CloseProfitShort") next bar Profit_Fixed_Short limit;
end;
// STOPLOSS FIXED --------------------------------------------------------------------------------------------
// Long exit:
if marketposition = 1 then begin
Sell ("StopLossLong") next bar Stoploss_Fixed_Long stop;
end;
// Short exit:
if marketposition = -1 then begin
Buytocover ("StopLossshort") next bar Stoploss_Fixed_Short stop;
end;
End;
Correct me if I am wrong and made a coding error or something. But if this is the way OCO is intended to work, you can not use OCO with stop and limit orders because always one or the other order is cancelled with partial fill because of the change in position size. Resulting in additional costs and possible loss of place in the order queue. Could also be a issue if you do pyramid trading.
Should MC also "lock" the profittarget order (in this case) or whole OCO order when the stoploss order is in the process of selling/buying?
Workaround could be to cancel the stoploss or the profitorder of your OCO order when one or the other is in the neighbourhood of the target. So you can code a cancellation of the profittarget when the stoploss is 2 cents away from getting hit for example.
PS.: sorry for the long posts by the way