GitHub で見る

CCI 自動化戦略

CCI 自動化戦略は、商品チャネル指数(CCI)の閾値クロスに反応するリバーサル戦略です。CCIが−90を下回った後に−80を上回るとロングに入り、90を超えた後に80を下回るとショートに入ります。システムはユーザー定義の上限までトレードを複製し、固定のテイクプロフィットとストップロスレベルでリスクを管理し、設定可能なトレーリングストップで利益を追います。

このアプローチは、売られすぎまたは買われすぎの状態の後に早期のモメンタム変化を捉えることを目指しています。複数のポジションを積み重ね、価格が進むにつれてストップを移動させることで、リスクを制限しながら持続的なリバーサルを活用しようとします。

詳細

  • エントリー条件: CCIが-90を下回った後に-80を上回るとロング;90を超えた後に80を下回るとショート。
  • ロング/ショート: 両方向。
  • エグジット条件: ストップロス、テイクプロフィット、またはトレーリングストップ。
  • ストップ: はい。
  • デフォルト値:
    • CciPeriod = 9
    • TradesDuplicator = 3
    • Volume = 0.03
    • StopLoss = 50
    • TakeProfit = 200
    • TrailingStop = 50
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: CCI
    • ストップ: はい
    • 複雑さ: 基本
    • 時間軸: イントラデイ (5m)
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy that trades based on CCI crossing specific thresholds.
/// </summary>
public class CciAutomatedStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _tradesDuplicator;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _trailingStop;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevCci;
	private decimal? _trailPrice;

	/// <summary>
	/// CCI calculation period.
	/// </summary>
	public int CciPeriod
	{
		get => _cciPeriod.Value;
		set => _cciPeriod.Value = value;
	}

	/// <summary>
	/// Maximum number of duplicated trades.
	/// </summary>
	public int TradesDuplicator
	{
		get => _tradesDuplicator.Value;
		set => _tradesDuplicator.Value = value;
	}


	/// <summary>
	/// Stop loss distance in price units.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Take profit distance in price units.
	/// </summary>
	public decimal TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in price units.
	/// </summary>
	public decimal TrailingStop
	{
		get => _trailingStop.Value;
		set => _trailingStop.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="CciAutomatedStrategy" /> class.
	/// </summary>
	public CciAutomatedStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 9)
			.SetRange(5, 50)
			.SetDisplay("CCI Period", "CCI indicator length", "Indicators")
			;

		_tradesDuplicator = Param(nameof(TradesDuplicator), 3)
			.SetRange(1, 10)
			.SetDisplay("Trades Duplicator", "Maximum number of concurrent trades", "General")
			;


		_stopLoss = Param(nameof(StopLoss), 50m)
			.SetRange(10m, 200m)
			.SetDisplay("Stop Loss", "Stop loss in price units", "Risk")
			;

		_takeProfit = Param(nameof(TakeProfit), 200m)
			.SetRange(10m, 500m)
			.SetDisplay("Take Profit", "Take profit in price units", "Risk")
			;

		_trailingStop = Param(nameof(TrailingStop), 50m)
			.SetRange(10m, 200m)
			.SetDisplay("Trailing Stop", "Trailing stop in price units", "Risk")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevCci = null;
		_trailPrice = null;
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var cci = new CommodityChannelIndex { Length = CciPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(cci, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, cci);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal cciValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var maxVolume = TradesDuplicator * Volume;

		if (_prevCci is decimal prev)
		{
			if (prev < -90m && cciValue > -80m && Position + Volume <= maxVolume)
			{
				BuyMarket();
				_trailPrice = candle.ClosePrice - TrailingStop;
			}
			else if (prev > 90m && cciValue < 80m && Position - Volume >= -maxVolume)
			{
				SellMarket();
				_trailPrice = candle.ClosePrice + TrailingStop;
			}
		}

		if (Position > 0)
		{
			var candidate = candle.ClosePrice - TrailingStop;
			if (_trailPrice is null || candidate > _trailPrice)
				_trailPrice = candidate;
			if (_trailPrice is decimal tp && candle.ClosePrice <= tp)
			{
				SellMarket();
				_trailPrice = null;
			}
		}
		else if (Position < 0)
		{
			var candidate = candle.ClosePrice + TrailingStop;
			if (_trailPrice is null || candidate < _trailPrice)
				_trailPrice = candidate;
			if (_trailPrice is decimal tp && candle.ClosePrice >= tp)
			{
				BuyMarket();
				_trailPrice = null;
			}
		}

		_prevCci = cciValue;
	}

	/// <inheritdoc />
	protected override void OnPositionReceived(Position position)
	{
		base.OnPositionReceived(position);

		if (Position == 0)
			_trailPrice = null;
	}
}