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!
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:
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.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.