Good evening,
I have set up my code to enter a long trade every time a 4hour candle closes green. At the end of the next 4h candle the trade should close and a new trade should be entered based on the same rules.
If the 4h candle closes red it should enter a short.
Currently all works fine except if a short trade is open, once the candle ends, the trade closes BUT a new trade is not entered. It does perfectly work with a long, just not a short.
Could anyone please have a look at my code and let me know why this is happening? I would greatly appreciate it!
Kind regards,
Anton
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
ONCE SL = 300
ONCE Capital = 20000
ONCE Risk = 0.10
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
//PositionSize = abs(round((maxrisk/SL)/2))
PositionSize = 5
SET STOP pLOSS SL
// Conditions to enter long positions
c1 = (close >= close[1])
IF c1 THEN
BUY PositionSize CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
IF Longonmarket then
IF open<close Then
SELL AT MARKET
ENDIF
ENDIF
// Conditions to enter short positions
c3 = (close < close[1])
IF c3 THEN
SELLSHORT PositionSize CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
IF Shortonmarket then
IF open>close Then
ExitShort AT MARKET
ENDIF
ENDIF
Actually you are not trying to detect GREEN (close > open) and RED (close < open) candlesticks, rather those closing higher than the previous one (it could be a RED candle that opened in a gap, then closed still higher than the previous one, or the other way round).
Anyway, the code seems to work as expected. Keep in mind that it takes a whole bar for the strategy to be aware of a new status (from OnMarket to Not OnMarket and viceversa, just like LongOnMarket and ShortOnMarket with their NOT counterpart).
This version simply does a Stop & Reverse, thus not waiting for a NOT OnMarket condition. Try if that is what you are looking for:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
ONCE SL = 300
ONCE Capital = 20000
ONCE Risk = 0.10
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
//PositionSize = abs(round((maxrisk/SL)/2))
PositionSize = 5
SET STOP pLOSS SL
// Conditions to enter long positions
c1 = (close >= close[1])
IF c1 THEN
BUY PositionSize CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
//IF Longonmarket then
//IF open<close Then
//SELL AT MARKET
//ENDIF
//ENDIF
// Conditions to enter short positions
c3 = (close < close[1])
IF c3 THEN
SELLSHORT PositionSize CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
//IF Shortonmarket then
//IF open>close Then
//ExitShort AT MARKET
//ENDIF
//ENDIF
//graph c1 coloured(0,128,0,155)
//graph c3 coloured(255,0,0,255)
Thank you Roberto, you have just answered two big questions for me. Using (close[1] > open[1]) just solved another issue I had with a different code I’m working on.
Thanks again, I really appreciate it!