GitHub で見る

I-Trend戦略

概要

I-Trend戦略は、オリジナルのMQL5エキスパートExp_i_Trendから変換されたトレンドフォロー型取引アルゴリズムです。移動平均とボリンジャーバンドを組み合わせてモメンタムの転換を識別します。戦略はカスタムのiTrend値と対応するシグナルラインを計算し、クロスオーバーが発生したときにポジションを開閉します。

仕組み

  1. インジケーターの設定
    • 設定可能な期間で指数移動平均(EMA)を計算します。
    • 同じ時間軸と偏差パラメーターを使用してボリンジャーバンドを構築します。
    • 選択した価格と選択したボリンジャーバンドライン(上限、下限、中央)の差としてiTrend値を導出します。
    • シグナルラインを2 * MA - (High + Low)として計算します。
  2. シグナル生成
    • iTrendがシグナルラインを上から下へクロスすると、戦略はショートポジションを決済してロングポジションを開設します。
    • iTrendがシグナルラインを下から上へクロスすると、戦略はロングポジションを決済してショートポジションを開設します。
  3. 注文執行
    • エントリーとエグジットは成行価格で実行されます。
    • ポジションサイズは戦略パラメーターVolumeで定義されます。

パラメーター

名前 説明
MaPeriod 計算に使用する移動平均の期間。
BbPeriod ボリンジャーバンドの期間。
BbDeviation ボリンジャーバンドの標準偏差。
PriceType iTrend値の計算に使用する価格タイプ(Close, Open, High, Low, Median, Typicalなど)。
BbMode 使用するボリンジャーバンドラインを選択(Upper, Lower, Middle)。
CandleType 戦略に供給されるローソク足の時間軸。
Volume エントリーの注文ボリューム。

備考

  • 戦略は完了したローソク足のみで動作します。未完了のローソク足は無視されます。
  • 教育目的で設計されており、ライブ取引には調整が必要な場合があります。
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>
/// i_Trend strategy built on Bollinger Bands and Moving Average.
/// Generates buy/sell signals when the iTrend value crosses the signal line.
/// </summary>
public class ITrendStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<int> _bbPeriod;
	private readonly StrategyParam<decimal> _bbDeviation;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevInd;
	private decimal _prevSign;
	private bool _isInitialized;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public int BbPeriod { get => _bbPeriod.Value; set => _bbPeriod.Value = value; }
	public decimal BbDeviation { get => _bbDeviation.Value; set => _bbDeviation.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ITrendStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 13)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Moving average length", "Indicator");

		_bbPeriod = Param(nameof(BbPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("BB Period", "Bollinger Bands period", "Indicator");

		_bbDeviation = Param(nameof(BbDeviation), 2.0m)
			.SetDisplay("BB Deviation", "Standard deviation for Bollinger Bands", "Indicator");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevInd = 0m;
		_prevSign = 0m;
		_isInitialized = false;
	}

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

		var ma = new ExponentialMovingAverage { Length = MaPeriod };
		var bb = new BollingerBands { Length = BbPeriod, Width = BbDeviation };

		Indicators.Add(ma);

		var subscription = SubscribeCandles(CandleType);

		subscription
			.BindEx(bb, (candle, bbValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!bbValue.IsFormed)
					return;

				var maResult = ma.Process(candle.ClosePrice, candle.OpenTime, true);
				if (!maResult.IsFormed)
					return;

				var maVal = maResult.ToDecimal();
				var bbVal = (BollingerBandsValue)bbValue;
				if (bbVal.UpBand is not decimal upperBand)
					return;

				ProcessCandle(candle, maVal, upperBand);
			})
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, decimal maValue, decimal band)
	{
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var price = candle.ClosePrice;

		var ind = price - band;
		var sign = 2m * maValue - (candle.LowPrice + candle.HighPrice);

		if (!_isInitialized)
		{
			_prevInd = ind;
			_prevSign = sign;
			_isInitialized = true;
			return;
		}

		var crossUp = _prevInd <= _prevSign && ind > sign;
		var crossDown = _prevInd >= _prevSign && ind < sign;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevInd = ind;
		_prevSign = sign;
	}
}