GitHub で見る

CCI VWAP 戦略

CCI VWAPのアプローチは、モメンタムと価格が出来高加重平均価格から乖離したときのイントラデイのリバーサルを捉えようとします。VWAPレベルと並べてCommodity Channel Indexを観察することで、システムは公正価値のベンチマークに対する最近のスイングの強さを測定します。

テストでは年間平均リターン約70%を示しています。株式市場で最もパフォーマンスが高いです。

CCIが-100を下回り、市場がVWAPの下で取引されている場合に買いセットアップが生まれ、売り圧力が枯渇した可能性を示します。CCIが+100を超えて上昇し、価格がVWAPの上にある場合にショートが発生し、反落に脆弱な伸びすぎたラリーを示します。価格が反対方向にVWAPを回復すると、ポジションは閉じられます。

この戦略は、極端な位置でフェードを好むが出口として客観的なレベルに頼るデイトレーダー向けに設計されています。定義されたストップロスは、モメンタムが素早く平均に回帰しない場合のリスク管理に役立ちます。

詳細

  • エントリー条件:
    • ロング: CCI < -100 && Price < VWAP (oversold below VWAP)
    • ショート: CCI > 100 && Price > VWAP (overbought above VWAP)
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 価格がVWAPを上回ったときロングを終了
    • ショート: 価格がVWAPを下回ったときショートを終了
  • ストップ: はい。
  • デフォルト値:
    • CciPeriod = 20
    • StopLossPercent = 2m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: 混合
    • 方向: 両方
    • インジケーター: CCI VWAP
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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 uses CCI and VWAP indicators to identify oversold and overbought conditions.
/// Enters long when CCI is below -100 and price is below VWAP.
/// Enters short when CCI is above 100 and price is above VWAP.
/// </summary>
public class CciVwapStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;
	
	private CommodityChannelIndex _cci;
	private int _cooldown;
	private DateTime _vwapDate;
	private decimal _vwapCumPv;
	private decimal _vwapCumVol;
	
	/// <summary>
	/// CCI period parameter.
	/// </summary>
	public int CciPeriod
	{
		get => _cciPeriod.Value;
		set => _cciPeriod.Value = value;
	}
	
	/// <summary>
	/// Bars to wait between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Stop-loss percentage parameter.
	/// </summary>
	public decimal StopLossPercent
	{
		get => _stopLossPercent.Value;
		set => _stopLossPercent.Value = value;
	}
	
	/// <summary>
	/// Candle type parameter.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}
	
	/// <summary>
	/// Strategy constructor.
	/// </summary>
	public CciVwapStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("CCI period", "CCI indicator period", "Indicators")
			
			.SetOptimize(10, 30, 5);

		_cooldownBars = Param(nameof(CooldownBars), 60)
			.SetRange(1, 200)
			.SetDisplay("Cooldown Bars", "Bars between trades", "General");
			
		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Stop-loss %", "Stop-loss as percentage of entry price", "Risk Management")
			
			.SetOptimize(1m, 3m, 0.5m);
			
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();

		_cci = null;
		_cooldown = 0;
		_vwapDate = default;
		_vwapCumPv = 0m;
		_vwapCumVol = 0m;
	}

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

		// Initialize CCI indicator
		_cci = new CommodityChannelIndex
		{
			Length = CciPeriod
		};

		var dummyEma = new ExponentialMovingAverage { Length = 10 };

		// Bind CCI to candle subscription
		var candlesSubscription = SubscribeCandles(CandleType)
			.Bind(_cci, dummyEma, ProcessCandle)
			.Start();
		
		// Setup chart if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, candlesSubscription);
			DrawIndicator(area, _cci);
			DrawOwnTrades(area);
		}
	}
	
	private void ProcessCandle(ICandleMessage candle, decimal cci, decimal dummyValue)
	{
		// Skip unfinished candles
		if (candle.State != CandleStates.Finished)
			return;
			
		var date = candle.ServerTime.Date;
		if (_vwapDate != date)
		{
			_vwapDate = date;
			_vwapCumPv = 0m;
			_vwapCumVol = 0m;
		}

		_vwapCumPv += candle.ClosePrice * candle.TotalVolume;
		_vwapCumVol += candle.TotalVolume;
		if (_vwapCumVol <= 0m)
			return;

		var currentVwap = _vwapCumPv / _vwapCumVol;
		if (currentVwap == 0)
			return;

		if (_cooldown > 0)
			_cooldown--;
			
		// Long signal: CCI below -100 and price below VWAP
		if (_cooldown == 0 && cci < -100 && candle.ClosePrice < currentVwap && Position <= 0)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (_cooldown == 0 && cci > 100 && candle.ClosePrice > currentVwap && Position >= 0)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && candle.ClosePrice > currentVwap)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position < 0 && candle.ClosePrice < currentVwap)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
	}
}