If I’m in a trade and price moves favourably, I am trying to close half the position and set a stop so that the trade can’t lose. I’ve written this simple code to help me debug it. It does’t seem to work on my spread betting platform. Could someone please tell me whether it should be possible and I’ve done something wrong in the code, or if there’s another way to do it? If it’s not possible on a spread betting platform, is it possible with CFDs?
My screenshot shows that the code runs; Stopmoved = 1 and Soldhalf = 1, but there is no indication that the buy is executed and COUNTOFSHARES never changes to 1.
IF NOT ONMARKET AND close < close[1] THEN // conditions to enter a short trade
SELLSHORT 2 perpoint at market
ENDIF
IF SHORTONMARKET AND close < tradeprice - 10 THEN // if the price moves 10pts in my favour...
breakevenLevel = tradeprice - 5 //...calculate the level for a stop 5pts below the trade open price...
SELL AT breakevenLevel STOP //...and place a new stop order on market at breakevenLevel, ie 5pts below the trade open price
Stopmoved = 1
if countofshortshares = 2 then
buy 1 PERPOINT at market //close half of the trade
SoldHalf = 1
endif
ENDIF
graph stopmoved
graph soldhalf
graph countofshortshares
IF SHORTONMARKET AND CLOSE > TRADEPRICE + 2 THEN
EXITSHORT AT MARKET
ENDIF
Many thanks in advance.
Partial closure of positions is not allowed in automated trading, as of this writing. We all hope it will be in the future.
Still, it is allowed in backtesting.
Partial closure is possible in backtesting even if IG don’t allow it in live trading but also your code at line 10 is
buy 1 PERPOINT at market //close half of the trade
If you want to partially close a short position then you must use EXITSHORT and not BUY. BUY will reverse the position and all short positions will be closed as it is not possible to be long and short at the same time in a ProOrder strategy.
Thanks, robertogozzi and Vonasi, and I take the point that I should have used EXITSHORT.
Nicolas’ post here made me think that I could close half the position: https://www.prorealcode.com/topic/bollinger-band-coding-help-please/#post-80959