Hi, I try to build some code that will add to my position under cetrain conditions to a maximum of 5 contracts, but only when I have a partial fill. So the scenario is: I try to buy 5 contracts at a limitprice, I get 3 and I would like to add the other 2 contracts as soon as the close is lower than or equal to my entryprice. I tried something like this:
If [Long condition is met] and Marketposition>0 and Marketposition<5 and Close<=Entryprice then buy.
If [Short condition is met] and Marketposition<0 and Marketposition>-5 and Close>=Entryprice then sell short.
It doesn't work reliably. Especially when a position is reversed I end up in a position of 4. I can't explain why.
I truly would appreciate feedback.
Thanks, Yohyon
How to add to an open position
Re: How to add to an open position
Hi,
The problem you have is that you do not use the definition of the Marketposition keyword correctly.
Marketposition can only have the values -1, zero or 1 for a candle ... and it means that the position is short, none or long on that candle.
That is, if Marketposition = 1 it means that in the current candle the position is long.
It can also refer to previous candles, such as marketposition (6) = - 1 which means that the position six candles ago is / was short.
Regarding what the code asks, perhaps it could be something like this:
The code I have not tested but it must be to look for something like this
Regards.
The problem you have is that you do not use the definition of the Marketposition keyword correctly.
Marketposition can only have the values -1, zero or 1 for a candle ... and it means that the position is short, none or long on that candle.
That is, if Marketposition = 1 it means that in the current candle the position is long.
It can also refer to previous candles, such as marketposition (6) = - 1 which means that the position six candles ago is / was short.
Regarding what the code asks, perhaps it could be something like this:
Code: Select all
Variable: SizePosition (5);
If {Long Condition} and Marketposition = 0 then
Buy SizePosition next bar at market;
If MarketPosition=1 and close <EntryPrice and then begin
If SizePosition<5 then begin
SizePosition = 5 - SizePosition;
Buy SizePosition next Bar at market;
end else
SizePosition=5;
end;
end;
Regards.
Re: How to add to an open position
Thank you Mazinguer. That is very helpful. I will implement and test it.
Re: How to add to an open position
Alternatively, you could check the "Convert partially Filled orders" option in Strategy properties > Auto Trading tab. But this converts the remaining to market order, which is not exactly want you want.
Also make sure to check "Allow up to " and "Sync" mode in properties.
Also make sure to check "Allow up to " and "Sync" mode in properties.
Re: How to add to an open position
Thank you MyDesign. Yes indeed, I prefer to avoid MKT orders. In the mean time I also found MarketPosition_at_Broker as an alternative that I am experminenting wth right now. That won't work in backtest, but it could do the job in realtime.
Re: How to add to an open position
I tried the following code, but it does not work.
Could anyone be so kind to scrutinize it for me. It doesn't do anything, But I don't see what's missing.
Thanks in advance.
Code: Select all
NumContracts = 5;//The amount of contracts in a position
MyPosition = MarketPosition_at_Broker;
Myentryprice=entryprice_checked;
Condition5 = MyPosition>0 and barstatus=1 and MyPosition< NumContracts and C<Myentryprice;//Long positions
Condition6 = MyPosition <0 and barstatus=1 and MyPosition> NumContracts *-1 and C>Myentryprice;//Short positions
If condition5 then Buy ("Add long") (NumContracts -MyPosition) share next bar C limit;
If condition6 then Sellshort ("Add short") (NumContracts +MyPosition) shares next bar C limit;
Thanks in advance.
- rrams
- Posts: 129
- Joined: 10 Feb 2011
- Location: USA
- Has thanked: 7 times
- Been thanked: 70 times
- Contact:
Re: How to add to an open position
Yohyon,
It is good to not be afraid to try to implement the strategy you want; but learning can be much less expensive using only one contract until you get the hang of it. Scaling into a position with multiple limit orders is going to be daunting to code the way you expect it to work. The buy order with a sellshort order as both entry and exit can backtest one way and trade in real life a shockingly different way.
To answer your original question I think this is more what you had in mind:
It is good to not be afraid to try to implement the strategy you want; but learning can be much less expensive using only one contract until you get the hang of it. Scaling into a position with multiple limit orders is going to be daunting to code the way you expect it to work. The buy order with a sellshort order as both entry and exit can backtest one way and trade in real life a shockingly different way.
To answer your original question I think this is more what you had in mind:
Code: Select all
input: NumContracts(5); //The amount of contracts in a position
vars: MyPosition(0);
MyPosition=MarketPosition_at_Broker;
condition5=MyPosition>=0 and CurrentContracts<NumContracts and C<EntryPrice; // Long
condition6=MyPosition<=0 and CurrentContracts<NumContracts and C>EntryPrice; // Short
if condition5 then Buy("Add long") NumContracts-CurrentContracts contracts next bar C limit;
if condition6 then Sellshort("Add short") NumContracts-CurrentContracts contracts next bar C limit;
- TJ
- Posts: 7745
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2224 times
Re: How to add to an open position
See post #5I tried the following code, but it does not work.
Could anyone be so kind to scrutinize it for me. It doesn't do anything, But I don't see what's missing.Code: Select all
NumContracts = 5;//The amount of contracts in a position MyPosition = MarketPosition_at_Broker; Myentryprice=entryprice_checked; Condition5 = MyPosition>0 and barstatus=1 and MyPosition< NumContracts and C<Myentryprice;//Long positions Condition6 = MyPosition <0 and barstatus=1 and MyPosition> NumContracts *-1 and C>Myentryprice;//Short positions If condition5 then Buy ("Add long") (NumContracts -MyPosition) share next bar C limit; If condition6 then Sellshort ("Add short") (NumContracts +MyPosition) shares next bar C limit;
Thanks in advance.
viewtopic.php?t=11713