Стратегия основана на пробое индекса товарного канала (CCI).
Тестирование показывает среднегодичную доходность около 94%. Стратегию лучше запускать на фондовом рынке.
CCI Breakout использует индикатор Commodity Channel Index для выявления сильных движений. Всплески выше положительного или ниже отрицательного порога CCI формируют входы. Выход происходит, когда CCI возвращается к нулю или появляется противоположный сигнал.
Так как CCI измеряет отклонение от скользящей средней, экстремальные значения подразумевают неустойчивые цены. Система ждёт этих экстремумов, пытаясь заработать на продолжении движения.
Подробности
Критерии входа: сигналы на основе CCI и Momentum.
Длинные/короткие: оба направления.
Критерии выхода: противоположный сигнал или стоп.
Стопы: да.
Параметры по умолчанию:
CciPeriod = 20
StopLossPercent = 2м
CandleType = TimeSpan.FromMinutes(5)
Фильтры:
Категория: Пробой
Направление: Оба
Индикаторы: CCI, Momentum
Стопы: Да
Сложность: Базовая
Таймфрейм: Внутридневной (5м)
Сезонность: Нет
Нейронные сети: Нет
Дивергенция: Нет
Уровень риска: Средний
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>
/// Strategy based on CCI (Commodity Channel Index) breakout.
/// Buys when CCI crosses above +100, sells when CCI crosses below -100.
/// </summary>
public class CciBreakoutStrategy : Strategy
{
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevCci;
private bool _hasPrevValues;
private int _cooldown;
/// <summary>
/// CCI period.
/// </summary>
public int CciPeriod
{
get => _cciPeriod.Value;
set => _cciPeriod.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="CciBreakoutStrategy"/>.
/// </summary>
public CciBreakoutStrategy()
{
_cciPeriod = Param(nameof(CciPeriod), 20)
.SetDisplay("CCI Period", "Period for CCI calculation", "Indicators")
.SetOptimize(14, 30, 4);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevCci = default;
_hasPrevValues = default;
_cooldown = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(cci, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, cci);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue cciInd)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (cciInd.IsEmpty)
return;
decimal cciValue;
try
{
cciValue = cciInd.GetValue<decimal>();
}
catch (IndexOutOfRangeException)
{
return;
}
if (!_hasPrevValues)
{
_hasPrevValues = true;
_prevCci = cciValue;
return;
}
if (_cooldown > 0)
{
_cooldown--;
_prevCci = cciValue;
return;
}
// CCI crosses above +100 - buy signal
if (_prevCci <= 100 && cciValue > 100 && Position <= 0)
{
var volume = Volume + Math.Abs(Position);
BuyMarket(volume);
_cooldown = 2;
}
// CCI crosses below -100 - sell signal
else if (_prevCci >= -100 && cciValue < -100 && Position >= 0)
{
var volume = Volume + Math.Abs(Position);
SellMarket(volume);
_cooldown = 2;
}
_prevCci = cciValue;
}
}
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_breakout_strategy(Strategy):
"""
Strategy based on CCI (Commodity Channel Index) breakout.
Buys when CCI crosses above +100, sells when CCI crosses below -100.
"""
def __init__(self):
super(cci_breakout_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 20) \
.SetDisplay("CCI Period", "Period for CCI calculation", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_cci = 0.0
self._has_prev_values = False
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(cci_breakout_strategy, self).OnReseted()
self._prev_cci = 0.0
self._has_prev_values = False
self._cooldown = 0
def OnStarted2(self, time):
super(cci_breakout_strategy, self).OnStarted2(time)
cci = CommodityChannelIndex()
cci.Length = self._cci_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(cci, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, cci)
self.DrawOwnTrades(area)
def _process_candle(self, candle, cci_ind):
if candle.State != CandleStates.Finished:
return
if cci_ind.IsEmpty:
return
try:
cci_value = float(cci_ind)
except:
return
if not self._has_prev_values:
self._has_prev_values = True
self._prev_cci = cci_value
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_cci = cci_value
return
if self._prev_cci <= 100 and cci_value > 100 and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
self._cooldown = 2
elif self._prev_cci >= -100 and cci_value < -100 and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
self._cooldown = 2
self._prev_cci = cci_value
def CreateClone(self):
return cci_breakout_strategy()