I am using the setstoploss and setprofittarget in my strategy with autotrade. The stoploss and profittarget is send to TWS IB at opening of the position. That is good.
When the stoploss or profittarget is hit, I often have that it's partially filled, leaving me with a position. I assume that with both, it should be the case, that as soon as the pricelevel of one of them is hit, the order that is already there in TWS will activate at market and get filled eventually also during pricechanges without looking again at conditions in the strategy if there met or not.
But it seems like that MC is cancelling market orders, when price changes and conditions are not met anymore in the strategy. Resulting in partial fills of my stoploss or profittarget.
I don't use IOG and the stoploss and profittarget is set outside the conditions in the strategy.
Probably this is a programming fault on my side. What I want to do is set once a stoploss or profittarget level (at position opening) and when that level is reached it should buy or sell the total position. So not buy or sell at only the set pricelevel. Should I use other keywords?
Setstoploss / setprofittarget
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 124 times
Re: Setstoploss / setprofittarget
In addition to my earlier post.
I use no IOG and work with a minute bar chart. I use the SetProfitTarget reserveword. I just saw the described issue in my earlier post happen:
The profit target (limit order) got hit on the price level. And the broker started selling the position. But the filling took longer than 1 minute. So my strategy recalculated the profit target with the remaining amount of stock. Resulting in a different higher profit target and therefore leaving me with a part of the position. Example: 5000 stocks at profittarget of 2500 will give a profittarget on the entryprice of 0,50. If 2000 stocks were sold the remaining position is now having a profit target of 2500/3000 = 0,8333 on the price
Part of the code:
How can I use the setprofittarget that it will not change the profit target limit price after partial fill?
I use no IOG and work with a minute bar chart. I use the SetProfitTarget reserveword. I just saw the described issue in my earlier post happen:
The profit target (limit order) got hit on the price level. And the broker started selling the position. But the filling took longer than 1 minute. So my strategy recalculated the profit target with the remaining amount of stock. Resulting in a different higher profit target and therefore leaving me with a part of the position. Example: 5000 stocks at profittarget of 2500 will give a profittarget on the entryprice of 0,50. If 2000 stocks were sold the remaining position is now having a profit target of 2500/3000 = 0,8333 on the price
Part of the code:
Code: Select all
[IntrabarOrderGeneration = FALSE]
inputs:
Begin_time (900),
End_time (1700),
ProfittargetAmt (2500),
StoplossAmt (1250);
Variables:
MP = marketposition;
If time > begin_time and time < end_time then begin
If Condition3 and Condition6 and condition7 and condition10 and condition12 then
if MarketPosition = 0 then begin
Sellshort ("Short") next bar at market;
if ProfitTargetAmt > 0 then
SetProfitTarget(ProfitTargetAmt);
if StopLossAmt > 0 then
SetStopLoss(StopLossAmt);
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 124 times
Re: Setstoploss / setprofittarget
Hi all,
After some reading and testing I found out the way to solve my question in the earlier posts. So for anyone who needs to solve this problem too. Have a look at the following code. By creating a stoploss / profittarget value which is calculated per contract and not per total amount, I think the remaining stock will also get filled eventually against the set stoploss/profit value.
This should work, but don't shoot the messenger if it doesn't work for you
After some reading and testing I found out the way to solve my question in the earlier posts. So for anyone who needs to solve this problem too. Have a look at the following code. By creating a stoploss / profittarget value which is calculated per contract and not per total amount, I think the remaining stock will also get filled eventually against the set stoploss/profit value.
This should work, but don't shoot the messenger if it doesn't work for you
Code: Select all
[IntrabarOrderGeneration = FALSE]
inputs:
Begin_time (900),
End_time (1700),
ProfittargetAmt (2500),
StoplossAmt (1250),
Stockamt(5000);
variables:
MP(0),
Stoploss_value(0),
Profit_value(0);
MP = marketposition;
Stoploss_value = stoplossAmt / stockamt;
Profit_value = profittargetAmt / Stockamt;
// make sure the stockamount in the inputs is the same as the position you want to take at the broker
If time > begin_time and time < end_time then begin
If MyCondition1 then
if MP = 0 then begin
Sellshort ("Short") next bar at market;
setstopcontract;
if ProfitTargetAmt > 0 then
SetProfitTarget(profit_value);
if StopLossAmt > 0 then
SetStopLoss(Stoploss_value);
end;
end;