CCI COMA
Uses Commodity Channel Index and multi-timeframe moving averages to follow the prevailing trend.
Details
- Data: Price candles from multiple timeframes.
- Entry: Long when CCI is above zero, RSI above 50, candle closes above open, and all monitored timeframes show an uptrend; short when opposite.
- Exit: Position closes on the opposite signal.
- Instruments: Any instruments.
- Risk: No explicit stop loss or take profit.
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// CCI and RSI crossover strategy with EMA trend filter.
/// </summary>
public class CciComaStrategy : Strategy
{
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevCci;
private bool _hasPrev;
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public CciComaStrategy()
{
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "CCI length", "Indicators");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI length", "Indicators");
_emaPeriod = Param(nameof(EmaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "Trend EMA period", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevCci = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var cci = new CommodityChannelIndex { Length = CciPeriod };
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
SubscribeCandles(CandleType)
.Bind(cci, rsi, ema, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal cciVal, decimal rsiVal, decimal emaVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevCci = cciVal;
_hasPrev = true;
return;
}
var bullish = cciVal > 0 && rsiVal > 50m && candle.ClosePrice > emaVal;
var bearish = cciVal < 0 && rsiVal < 50m && candle.ClosePrice < emaVal;
// CCI crossing zero
var cciCrossUp = _prevCci <= 0 && cciVal > 0;
var cciCrossDown = _prevCci >= 0 && cciVal < 0;
if (cciCrossUp && bullish && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (cciCrossDown && bearish && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevCci = cciVal;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import CommodityChannelIndex, RelativeStrengthIndex, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class cci_coma_strategy(Strategy):
def __init__(self):
super(cci_coma_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 14) \
.SetDisplay("CCI Period", "CCI length", "Indicators")
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI length", "Indicators")
self._ema_period = self.Param("EmaPeriod", 20) \
.SetDisplay("EMA Period", "Trend EMA period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev_cci = 0.0
self._has_prev = False
@property
def cci_period(self):
return self._cci_period.Value
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def ema_period(self):
return self._ema_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(cci_coma_strategy, self).OnReseted()
self._prev_cci = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(cci_coma_strategy, self).OnStarted2(time)
cci = CommodityChannelIndex()
cci.Length = self.cci_period
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_period
ema = ExponentialMovingAverage()
ema.Length = self.ema_period
self.SubscribeCandles(self.candle_type) \
.Bind(cci, rsi, ema, self.process_candle) \
.Start()
def process_candle(self, candle, cci_val, rsi_val, ema_val):
if candle.State != CandleStates.Finished:
return
cci_val = float(cci_val)
rsi_val = float(rsi_val)
ema_val = float(ema_val)
if not self._has_prev:
self._prev_cci = cci_val
self._has_prev = True
return
bullish = cci_val > 0 and rsi_val > 50.0 and float(candle.ClosePrice) > ema_val
bearish = cci_val < 0 and rsi_val < 50.0 and float(candle.ClosePrice) < ema_val
cci_cross_up = self._prev_cci <= 0 and cci_val > 0
cci_cross_down = self._prev_cci >= 0 and cci_val < 0
if cci_cross_up and bullish and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cci_cross_down and bearish and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_cci = cci_val
def CreateClone(self):
return cci_coma_strategy()