How to add to an open position

Questions about MultiCharts and user contributed studies.
Yohyon
Posts: 10
Joined: 03 Dec 2018
Has thanked: 1 time

How to add to an open position

Postby Yohyon » 25 Feb 2021

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

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: How to add to an open position

Postby MAZINGUER » 25 Feb 2021

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:

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;
The code I have not tested but it must be to look for something like this

Regards.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: How to add to an open position

Postby MAZINGUER » 26 Feb 2021

Hi
I have edited and changed the above code because there was something wrong.

Yohyon
Posts: 10
Joined: 03 Dec 2018
Has thanked: 1 time

Re: How to add to an open position

Postby Yohyon » 26 Feb 2021

Thank you Mazinguer. That is very helpful. I will implement and test it.

Mydesign
Posts: 177
Joined: 15 Feb 2017
Has thanked: 32 times
Been thanked: 39 times

Re: How to add to an open position

Postby Mydesign » 26 Feb 2021

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.

Yohyon
Posts: 10
Joined: 03 Dec 2018
Has thanked: 1 time

Re: How to add to an open position

Postby Yohyon » 26 Feb 2021

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.

Yohyon
Posts: 10
Joined: 03 Dec 2018
Has thanked: 1 time

Re: How to add to an open position

Postby Yohyon » 20 Oct 2023

I tried the following code, but it does not work.

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;
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.

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to add to an open position

Postby rrams » 06 Nov 2023

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:

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;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: How to add to an open position

Postby TJ » 06 Nov 2023

I tried the following code, but it does not work.

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;
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.
See post #5
viewtopic.php?t=11713


Return to “MultiCharts”