Hi everyone,
I’m working on an EMA crossover strategy in Python and need some guidance on validating trade states.
My questions are:
- How can I correctly identify whether an EMA crossover is in PCO (Positive Crossover) or NCO (Negative Crossover) state using candle data?
- Once the crossover is confirmed, what is the proper way to exit the trade on the next candle?
- Should the crossover validation be based on previous candle close vs current candle close, or is intraday candle data sufficient?
If possible, I’d really appreciate a Python example or logic explanation for handling crossover state validation and next-candle exit logic.
currentlly using pine script and working as expected how to do same in python ?
Hi @mahendra.beit ,
- PCO / NCO crossover condition
# PCO: Price crosses above EMA (prev below, current above)
PCO = (current_candle['close'] > current_candle['ema']) and (previous_candle['close'] < previous_candle['ema'])
# NCO: Price crosses below EMA (prev above, current below)
NCO = (current_candle['close'] < current_candle['ema']) and (previous_candle['close'] > previous_candle['ema'])
- Exit logic (rephrased)
You can handle this through your exit conditions by adding a time-based exit, for example:
exit_condition = candle_time > status['entry_time'] + datetime.timedelta(minutes=5)
# This forces an exit within the next 5 minutes (i.e., the next candle on a 5-min timeframe)
- Crossover validation
Yes — crossover confirmation should always be done using both the previous candle and the current candle (not just current candle alone).