100% time onmarket long and short strategy – unwanted partial close problem
Forums › ProRealTime English forum › ProOrder support › 100% time onmarket long and short strategy – unwanted partial close problem
- This topic has 6 replies, 3 voices, and was last updated 5 years ago by Finning.
-
-
04/02/2019 at 1:56 AM #95270
Hi all,
I have a strategy that i want to stay on market 100% of the time. It’s either long or short – and switches from long to short in a single trade, by cancelling/netting off the entire of the previous position.
The original code that I’ve been using is below to do this:
123456789if positionsize+countoflongshares>3150 thenpositionsize = 3150-countoflongsharesendifif shortonmarket and countofshortshares+positionsize>6300 thenpositionsize = 6300endifBUY PositionSize CONTRACT AT MARKETThe intent of the above code was that if I was not onmarket, buy positionsize shares to start with – and – if I was shortonmarket – buy out my short position with up to 6300 contracts to have 3150 contracts long net. Using cumulating orders.
This code works well, and gives me the 100% onmarket switch that I wanted up to my max net positionsize of 3150 long or short – the code does what I want it to do.
The problem I’m having, is that I’m right at the end of my testing phase, and went to launch into an onmarket test then live trading, and I got this message – see attachment.
I am well aware of the partial close limitation problem with PRT. The thing is I’m not trying to partially close a position here – I’m trying to close all of my short/long position, and then trade in the opposite direction – all in the same move.
I have tried the following code to try and exit shorts (this example) totally first , to try and eliminate the partial closures problem. This didn’t seem to work but?
12345678910if shortonmarket thenexitshort at marketendifif positionsize+countoflongshares>3150 thenpositionsize = 3150-countoflongsharesendifBUY PositionSize CONTRACT AT MARKETBear in mind that the above code, like the first one listed above “if shortonmarket” comes after an “if” condition above has been met – not shown/listed.
Any ideas on how I can beat this unwanted partial closures type problem? It is a bit annoying because I knew about no partial closures – I was trying to sell off entirely, and then re-enter the market in the new direction.
I have found that the instantaneous direction change from long to short is very useful in backtest – it is a feature that I want to keep – IE – if possible I don’t want to have to exit the market, wait a bar, to then re-enter.
Am really hoping to be able to keep this functionality – and would appreciate any ideas so I can make this happen.
Thanks,
Finning.
04/02/2019 at 7:09 AM #95279I think that the quantity of shares could be slightly different from the ones that want to be set at market (because of rounding..). So inverting position by buying a new calculated amount of shares could not lead to close all active positions at market. I suggest you close ALL ORDERS with EXISHORT AT MARKET (or SELL AT MARKET ) before inverting the direction of your orders.
04/02/2019 at 11:46 AM #95313Hi Nicolas,
thanks for your reply.
I will double check to make sure that positions are balanced because of rounding, and that this hasn’t caused a problem somehow.
I do know what you mean about closing all orders, and I will try different variations of this.
As I mentioned in the second block of code snippet in my initial post above , with my first attempt to fix this problem I did try to exit out of all positions first, before trading in the opposite direction – market exit code used as per below (for shorts):
123if shortonmarket thenexitshort at marketendifand then placed a buy order for a positionsize directly after it.
The 2nd block of code in my initial post (with exitshort) works fine/gives the exact same backtest result as my initial code, but again when I try to set it up for live trading, the error message is displayed.
In theory, this should counteract any out of balance positions, because you are starting with a clean book with no positions before you take on a new order, right?
And by just saying “exitshort at market”, you are not specifying a quantity in the instructions for disposal – which seems to be the initial problem that I was dealing with. So this problem should be solved, but it still doesn’t seem to be?
Is it possible – to exitshort at market – and then – buy new positions all in the same bar/candle/same run of code? This is what I’m aiming for.
Many thanks,
Finning.
04/02/2019 at 12:07 PM #95315Is it possible – to exitshort at market – and then – buy new positions all in the same bar/candle/same run of code? This is what I’m aiming for.
If you use below as the first line of your code then this will achieve what you want, but maybe not in the way you want?
1DEFPARAM CumulateOrders = False04/02/2019 at 12:20 PM #95316Hi GraHal,
Yes I have cumulate orders as true. I really do need to keep it that way as it greatly helps system performance.
I will try with this setting as false and see what difference it makes.
There aren’t any other ways that you can think of to get around this problem?
Many thanks,
Finning
04/02/2019 at 12:46 PM #9532004/04/2019 at 4:19 AM #95442Hi all,
thanks for your help with this.
I did manage to solve this problem – though the problem wasn’t where I was initially looking/what I first asked for help with.
Later on in my code, I had a device to exit over leveraged, under-performing positions. I didn’t look at this initially, because I didn’t have any numbers associated with it. Turns out though that this is where the problem was.
The below code is what gave the error:
1234If EQUITYCURVE < 1000 and (abs(countofposition))>1 and positionperf<-0.004 thensell Countoflongshares shares at marketexitshort countofshortshares shares at marketendifAnd by just getting rid of the “countoflongshares shares/countofshortshares shares” commands the error problem has now gone away.
1234If EQUITYCURVE < 1000 and (abs(countofposition))>1 and positionperf<-0.004 thensell at marketexitshort at marketendifI didn’t think that countoflongshares/countofshortshares was a numerical command (though it deals with numbers) – more of a “total” command – so I didn’t look there to start with.
By simplifying the code and getting rid of something I probably didn’t need in the first place the problem was eliminated.
Cheers,
Finning.
-
AuthorPosts
Find exclusive trading pro-tools on