GitHub で見る

VR MARS戦略

このサンプルはMQL4の手動取引パネル VR---MARS-EN をStockSharpに簡略化して移植したものを示します。

元のスクリプトは5つの定義済みロットサイズと、買いまたは売り注文を送信するボタンを提供していました。また、取引統計を示す複数のラベルも表示していました。このC#バージョンではビジュアルパネルは削除されていますが、5つのロットサイズのいずれかを選択して成行注文を実行するという中核的なアイデアは保持されています。

パラメーター

  • Lot1 – 最初のロットのサイズ。
  • Lot2 – 2番目のロットのサイズ。
  • Lot3 – 3番目のロットのサイズ。
  • Lot4 – 4番目のロットのサイズ。
  • Lot5 – 5番目のロットのサイズ。
  • SelectedLot – 使用するロットサイズを指定する1から5の数字。
  • Buytrue の場合、戦略開始時に成行買い注文が送信される。
  • Selltrue の場合、戦略開始時に成行売り注文が送信される。

方向フラグは一度に1つだけ有効にする必要があります。戦略が開始すると、ポジション保護を有効にし、高レベルのヘルパーメソッドを使用して対応する成行注文を送信します。

注意事項

この戦略は教育目的のためのものであり、即時の注文実行を超えた取引ロジックは実装していません。

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>
/// VR MARS strategy using EMA crossover with volume lots.
/// </summary>
public class VRMarsStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public VRMarsStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0; _prevSlow = 0; _hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType).Bind(fast, slow, ProcessCandle).Start();
	}

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

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

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

		_prevFast = fast; _prevSlow = slow;
	}
}