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:
if positionsize+countoflongshares>3150 then
positionsize = 3150-countoflongshares
endif
if shortonmarket and countofshortshares+positionsize>6300 then
positionsize = 6300
endif
BUY PositionSize CONTRACT AT MARKET
The 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?
if shortonmarket then
exitshort at market
endif
if positionsize+countoflongshares>3150 then
positionsize = 3150-countoflongshares
endif
BUY PositionSize CONTRACT AT MARKET
Bear 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.