GitHub で見る

ForexLine戦略

ForexLine戦略は、MetaTraderインジケーター「ForexLine」から派生したトレンドフォローシステムです。価格に2段階の加重移動平均を適用して高速線と低速線を構築します。これらの二重平滑化線間のクロスオーバーがエントリーシグナルの決定に使用されます。

高速線が低速線を上抜けすると買い、高速線が低速線を下抜けすると売りです。各移動平均は市場ノイズをフィルタリングするのに役立つ2ステップの平滑化プロセスを使用します。

詳細

  • エントリー条件:
    • ロング: 高速二重平滑化WMAが低速二重平滑化WMAを上抜け。
    • ショート: 高速二重平滑化WMAが低速二重平滑化WMAを下抜け。
  • ロング/ショート: 両方向。
  • エグジット条件:
    • 反対のクロスオーバーが既存のポジションをクローズ。
  • ストップ: 含まれていない。外部で追加可能。
  • デフォルト値:
    • FastLength1 = 5
    • FastLength2 = 10
    • SlowLength1 = 20
    • SlowLength2 = 20
    • CandleType = 8時間の時間軸
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: 加重移動平均
    • ストップ: いいえ
    • 複雑さ: 中程度
    • 時間軸: 中期
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Forex Line strategy using fast/slow WMA crossover.
/// </summary>
public class ForexLineStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ForexLineStrategy()
	{
		_fastLength = Param(nameof(FastLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast WMA Length", "Fast line period", "Parameters");

		_slowLength = Param(nameof(SlowLength), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow WMA Length", "Slow line period", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = _prevSlow = null;
	}

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

		var fast = new WeightedMovingAverage { Length = FastLength };
		var slow = new WeightedMovingAverage { Length = SlowLength };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevFast = fast;
			_prevSlow = slow;
			return;
		}

		if (_prevFast is decimal pf && _prevSlow is decimal ps)
		{
			if (pf <= ps && fast > slow && Position <= 0)
				BuyMarket();
			else if (pf >= ps && fast < slow && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}