GitHub で見る

Liquidex V1戦略

Liquidex V1は元のMQLエキスパートアドバイザーから変換されたブレイクアウトスキャルピング戦略です。レンジフィルターと**加重移動平均(WMA)**を組み合わせて短期的な機会を特定します。

トレードロジック

  1. 完成した各ローソク足のレンジ(high - low)を計測します。
  2. ローソク足のレンジがRangeFilterより小さい場合、そのローソク足は無視されます。
  3. 終値を使用して期間MaPeriodのWMAを計算します。
  4. ローソク足がWMAの下で始まりWMAの上で終わった場合、買い成行注文が送信されます。
  5. ローソク足がWMAの上で始まりWMAの下で終わった場合、売り成行注文が送信されます。
  6. 各ポジションはStopLossで定義されたストップロスで保護されます。

パラメーター

  • RangeFilter – 取引に必要な価格単位での最小ローソク足レンジ。
  • MaPeriod – 加重移動平均の期間数。
  • StopLoss – ポイント単位の保護ストップロス。
  • CandleType – 分析に使用するローソク足シリーズ。

戦略は注文サイズとしてStrategy.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>
/// WMA crossover strategy with range filter.
/// </summary>
public class LiquidexV1Strategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevClose;
	private decimal _prevWma;
	private bool _hasPrev;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LiquidexV1Strategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "WMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevClose = 0;
		_prevWma = 0;
		_hasPrev = false;
	}

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

		var wma = new WeightedMovingAverage { Length = MaPeriod };

		SubscribeCandles(CandleType)
			.Bind(wma, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevClose = candle.ClosePrice;
			_prevWma = wmaVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevClose <= _prevWma && candle.ClosePrice > wmaVal;
		var crossDown = _prevClose >= _prevWma && candle.ClosePrice < wmaVal;

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

		_prevClose = candle.ClosePrice;
		_prevWma = wmaVal;
	}
}