GitHub で見る

マルチカレンシー・トレーディングパネル戦略

この戦略は、オリジナルのMQL「Multicurrency trading panel」エキスパートアドバイザーの動作をエミュレートします。3つの通貨ペア(EURUSD、USDJPY、GBPUSD)を監視し、最新のローソク足を7つの単純な指標(始値、高値、安値、(高値+安値)/2、終値、(高値+安値+終値)/3、(高値+安値+終値+終値)/4)を使って前のローソク足と比較します。 各比較でBUYまたはSELLスコアが増加します。自動取引が有効な場合、BUYスコアがSELLスコアを上回るか、またはその逆の場合に、ペアでポジションを開設または反転させます。

パラメーター

  • EURUSD – 最初の証券。
  • USDJPY – 2番目の証券。
  • GBPUSD – 3番目の証券。
  • Candle Type – ローソク足の時間軸。
  • Auto Trade – 自動注文発注の切り替え。

この戦略は簡略化されたデモであり、実際の取引を目的としていません。

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>
/// Strategy that compares consecutive candle metrics and trades based on majority direction.
/// </summary>
public class MulticurrencyTradingPanelStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleTypeParam;

	private ICandleMessage _prev;

	/// <summary>
	/// Candle type used for signal calculation.
	/// </summary>
	public DataType CandleType
	{
		get => _candleTypeParam.Value;
		set => _candleTypeParam.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public MulticurrencyTradingPanelStrategy()
	{
		_candleTypeParam = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for analysis", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		if (CandleType != null)
			yield return (Security, CandleType);
	}

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

		var warmup = new ExponentialMovingAverage { Length = 5 };
		SubscribeCandles(CandleType)
			.Bind(warmup, ProcessCandle)
			.Start();
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prev = null;
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prev == null)
		{
			_prev = candle;
			return;
		}

		var buy = 0;
		var sell = 0;

		void Compare(decimal current, decimal previous)
		{
			if (current > previous)
				buy++;
			else
				sell++;
		}

		Compare(candle.OpenPrice, _prev.OpenPrice);
		Compare(candle.HighPrice, _prev.HighPrice);
		Compare(candle.LowPrice, _prev.LowPrice);
		Compare((candle.HighPrice + candle.LowPrice) / 2m, (_prev.HighPrice + _prev.LowPrice) / 2m);
		Compare(candle.ClosePrice, _prev.ClosePrice);
		Compare((candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m,
			(_prev.HighPrice + _prev.LowPrice + _prev.ClosePrice) / 3m);
		Compare((candle.HighPrice + candle.LowPrice + candle.ClosePrice + candle.ClosePrice) / 4m,
			(_prev.HighPrice + _prev.LowPrice + _prev.ClosePrice + _prev.ClosePrice) / 4m);

		if (buy > sell && Position <= 0)
			BuyMarket();
		else if (sell > buy && Position >= 0)
			SellMarket();

		_prev = candle;
	}
}