CCI Woodies Strategy
Overview
This strategy trades based on the crossover of two Commodity Channel Index (CCI) lines derived from the Woodies CCI method. A fast CCI and a slow CCI are calculated on the specified timeframe. When the fast line crosses below the slow line, a long position is opened and any short position is closed. When the fast line crosses above the slow line, a short position is opened and any long position is closed.
Parameters
- FastPeriod – length of the fast CCI indicator.
- SlowPeriod – length of the slow CCI indicator.
- CandleType – timeframe of candles used for calculations.
- InvertSignals – if enabled, buy and sell rules are swapped.
- TakeProfitPoints – profit target in price points.
- StopLossPoints – loss limit in price points.
Notes
The strategy uses the high-level StockSharp API. Indicators are bound via Bind, and risk control is handled with StartProtection using stop-loss and take-profit levels.
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 Woodies crossover strategy.
/// Buys when the fast CCI crosses below the slow CCI and sells on the opposite crossover.
/// </summary>
public class CciWoodiesStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private bool _isInitialized;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public CciWoodiesStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 6)
.SetDisplay("Fast CCI Period", "Period for fast CCI", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 14)
.SetDisplay("Slow CCI Period", "Period for slow CCI", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_isInitialized = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevFast = 0;
_prevSlow = 0;
_isInitialized = false;
var fastCci = new CommodityChannelIndex { Length = FastPeriod };
var slowCci = new CommodityChannelIndex { Length = SlowPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fastCci, slowCci, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastCci);
DrawIndicator(area, slowCci);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (!_isInitialized)
{
_prevFast = fast;
_prevSlow = slow;
_isInitialized = true;
return;
}
var crossDown = _prevFast > _prevSlow && fast <= slow;
var crossUp = _prevFast < _prevSlow && fast >= slow;
if (crossDown && Position <= 0)
BuyMarket();
else if (crossUp && Position >= 0)
SellMarket();
_prevFast = fast;
_prevSlow = slow;
}
}
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
from StockSharp.Algo.Strategies import Strategy
class cci_woodies_strategy(Strategy):
def __init__(self):
super(cci_woodies_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 6) \
.SetDisplay("Fast CCI Period", "Period for fast CCI", "Indicators")
self._slow_period = self.Param("SlowPeriod", 14) \
.SetDisplay("Slow CCI Period", "Period for slow CCI", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._is_initialized = False
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(cci_woodies_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._is_initialized = False
def OnStarted2(self, time):
super(cci_woodies_strategy, self).OnStarted2(time)
self._prev_fast = 0.0
self._prev_slow = 0.0
self._is_initialized = False
fast_cci = CommodityChannelIndex()
fast_cci.Length = self.fast_period
slow_cci = CommodityChannelIndex()
slow_cci.Length = self.slow_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_cci, slow_cci, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_cci)
self.DrawIndicator(area, slow_cci)
self.DrawOwnTrades(area)
def process_candle(self, candle, fast, slow):
if candle.State != CandleStates.Finished:
return
fast = float(fast)
slow = float(slow)
if not self._is_initialized:
self._prev_fast = fast
self._prev_slow = slow
self._is_initialized = True
return
cross_down = self._prev_fast > self._prev_slow and fast <= slow
cross_up = self._prev_fast < self._prev_slow and fast >= slow
if cross_down and self.Position <= 0:
self.BuyMarket()
elif cross_up and self.Position >= 0:
self.SellMarket()
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return cci_woodies_strategy()