GitHub で見る

注文クローズ時ステータスメール・アラート戦略

この戦略はアカウントを監視し、重要なイベントを報告します。

  • 指定した分に毎日ステータス通知を送信します。
  • 各クローズされた注文について基本的な取引情報とともに報告します。

MQLエキスパートStatusMailandAlertOnOrderClose.mq4をベースにしており、StockSharpでの通知処理の方法を示しています。

パラメーター

名前 説明
SendReportEmail 毎日のステータス通知を有効にします。
StatusEmailMinute ステータスメッセージを送信する時刻の分。
SendClosedEmail 注文がクローズされた際の通知を有効にします。
StartBalance 利益計算に使用するアカウントの初期残高。
CandleType 時刻確認に使用する時間軸。通常は1分に設定。

ロジック

  1. 選択した時間軸のローソク足を購読します。
  2. ローソク足が終了したら、指定した分かどうかを確認してレポートメッセージを送信します。
  3. 新しい約定ごとに、注文がクローズされていれば通知します。

これらのメッセージはAddInfoを通じてログに記録されますが、任意の通知メカニズムに置き換えることができます。

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>
/// EMA crossover with status tracking.
/// </summary>
public class StatusMailAndAlertOnOrderCloseStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

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

	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 StatusMailAndAlertOnOrderCloseStrategy()
	{
		_fastLength = Param(nameof(FastLength), 9)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
		_slowLength = Param(nameof(SlowLength), 26)
			.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 = FastLength };
		var slow = new ExponentialMovingAverage { Length = SlowLength };
		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;
	}
}