Hi All,
I am trying to code a reduction of the tradesize by half when being long/short and the current high/low reaches 20(before 15:00hr) or 40 (after 15:00hr) points. The remaining half will stay in until price closes below/above the lowest/highest point of the last 12 candlesticks. If the required points aren’t reached, but it does close on or above 10 points I want the stop loss to move to break even.
Additionally, I always want to exit when price closes below/above the lowest/highest point of the last 12 candlesticks or at a loss of 15 points. Whichever is sooner.
So far, I have come up with this, but it’s a mess:
AM = time < 150000
PM = time >= 1500000
// Condiciones de salida de posiciones largas
c3 = (close < lowest[12](low[1]))
c17 = High >= (TradePrice + 20)
c18 = High >= (Tradeprice + 40)
c21 = close >= (tradeprice + 10)
IF LongOnMarket Then
Set Stop ploss 15
Endif
IF LongOnMarket Then
If c21 then
Set Stop ploss tradeprice
Endif
If (AM AND c17) OR (PM AND c18) Then
Sell CountOfLongShares/2 contracts at market
Elsif c3 THEN
SELL AT MARKET
Endif
Endif
// Condiciones de salida de posiciones cortas
c6 = (close > highest[12](high[1]))
c19 = low <= (TradePrice - 20)
c20 = low <= (Tradeprice - 40)
c22 = close <= (tradeprice - 10)
IF ShortOnMarket Then
Set Stop ploss 15
Endif
IF ShortOnMarket Then
If c22 then
Set Stop ploss tradeprice
Endif
If (AM AND c19) OR (PM AND c20) Then
Buy CountOfShortShares/2 contracts at market
Elsif c6 THEN
EXITSHORT AT MARKET
Endif
Endif
I would appreciate some help.
Thanks
Are you aware that currently it is not possible to partially close a position using ProOrder when live trading? It is possible to test with partial closure when running a back test but you cannot put that strategy live in either demo or real trading.
Oh, that’s unfortunate. I have removed that part from the code and replaced it with just a profit target. However, it still doesn’t always do what I want it to do. That is exiting when price closes above or below the highest or lowest price of the last 12 candlesticks and moving the stop loss to break even after closing 10 points or higher from the entry price. Do you have any tips?
// Condiciones de salida de posiciones largas
c3 = (close < lowest[12](low[1]))
c21 = close >= (tradeprice + 10)
IF LongOnMarket Then
If c21 then
Set Stop ploss tradeprice
Elsif c3 THEN
SELL AT MARKET
Endif
Endif
// Condiciones de salida de posiciones cortas
c6 = (close > highest[12](high[1]))
c22 = close <= (tradeprice - 10)
IF ShortOnMarket Then
If c22 then
Set Stop ploss tradeprice
Elsif c6 THEN
EXITSHORT AT MARKET
Endif
Endif
SET TARGET PPROFIT 50
SET STOP PLOSS requires a distance and not a price.
Try this (not tested):
// Condiciones de salida de posiciones largas
c3 = (close < lowest[12](low[1]))
c21 = close >= (tradeprice + 10)
IF LongOnMarket Then
If c21 or c3 then
sell at market
Endif
// Condiciones de salida de posiciones cortas
c6 = (close > highest[12](high[1]))
c22 = close <= (tradeprice - 10)
IF ShortOnMarket Then
If c22 or c6 then
EXITSHORT AT MARKET
Endif
SET TARGET PPROFIT 50
This immediately exits my position, I just want to move the stop loss to break even. However, on your advice that set stop ploss requires a distance I changed tradeprice with just 0.1 that seems to work. It doesn’t work with 0 for some reason though. Anyway, thanks for your help.
SET STOP LOSS 0 cancels the stop order.
When trying to code break even stop losses you have to be aware of how close to price your broker will let you place the stop – and this can change at times of high volatility. So if you are only a few pips in profit and try to move your stop to break even then it is likely that your broker will reject the order – if your strategy tries too many candles in a row then they will stop the strategy and either close the trade or orphan it depending on your settings.
Ok, I’ll keep that in mind. Thank you