GitHub で見る

CaiChannel システムデジット戦略

この戦略はMetaTraderエキスパート i-CAiChannel System Digit をStockSharpに簡略移植したものです。

アルゴリズムは移動平均と標準偏差(ボリンジャーバンド)で構築されたボラティリティチャンネルを監視します。 ローソク足がチャンネル外でクローズし、次のローソク足が内側に戻った場合、再参入の方向に取引します。

パラメーター

  • Length – 移動平均の期間。
  • Width – 標準偏差の乗数。
  • Candle Type – 処理する時間軸。

取引ロジック

  1. 選択した時間軸のローソク足をサブスクライブする。
  2. 指定したパラメーターでボリンジャーバンドを計算する。
  3. 前のローソク足が上限バンドの上でクローズし、現在のローソク足が内側に戻った場合、ロングエントリー。
  4. 前のローソク足が下限バンドの下でクローズし、現在のローソク足が内側に戻った場合、ショートエントリー。
  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>
/// CAiChannel System Digit strategy using Bollinger Bands.
/// Buys when price returns inside from above the upper band.
/// Sells when price returns inside from below the lower band.
/// </summary>
public class CaiChannelSystemDigitStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _width;
	private readonly StrategyParam<DataType> _candle;
	private bool _prevUp;
	private bool _prevDown;

	public int Length { get => _length.Value; set => _length.Value = value; }
	public decimal Width { get => _width.Value; set => _width.Value = value; }
	public DataType CandleType { get => _candle.Value; set => _candle.Value = value; }

	public CaiChannelSystemDigitStrategy()
	{
		_length = Param(nameof(Length), 12).SetGreaterThanZero();
		_width = Param(nameof(Width), 2m).SetGreaterThanZero();
		_candle = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame());
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevUp = false;
		_prevDown = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevUp = false;
		_prevDown = false;

		var bb = new BollingerBands { Length = Length, Width = Width };

		var sub = SubscribeCandles(CandleType);
		sub.BindEx(bb, Process).Start();

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

	private void Process(ICandleMessage candle, IIndicatorValue bbVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (bbVal is not IBollingerBandsValue bb || bb.UpBand is not decimal up || bb.LowBand is not decimal down)
			return;

		if (_prevUp && candle.ClosePrice <= up && Position <= 0)
			BuyMarket();
		else if (_prevDown && candle.ClosePrice >= down && Position >= 0)
			SellMarket();

		_prevUp = candle.ClosePrice > up;
		_prevDown = candle.ClosePrice < down;
	}
}