订单关闭状态邮件与警报策略
该策略监控账户并在关键事件发生时发送通知:
- 在指定的分钟发送每日状态报告。
- 在每个订单关闭时发送通知。
该策略基于 MQL 专家 StatusMailandAlertOnOrderClose.mq4,演示如何在 StockSharp 中处理通知。
参数
| 名称 | 说明 |
|---|---|
SendReportEmail |
是否在每日指定时间发送状态报告。 |
StatusEmailMinute |
发送状态信息的分钟。 |
SendClosedEmail |
是否在订单关闭时发送通知。 |
StartBalance |
用于计算收益的初始账户余额。 |
CandleType |
用于检查时间的K线周期,通常为1分钟。 |
逻辑
- 订阅所选时间框架的K线。
- 当K线收盘时检查是否到达指定分钟并发送报告。
- 在每个新成交产生时,如果订单已关闭则发送通知。
通知通过 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;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class status_mail_and_alert_on_order_close_strategy(Strategy):
def __init__(self):
super(status_mail_and_alert_on_order_close_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 9) \
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators")
self._slow_length = self.Param("SlowLength", 26) \
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
@property
def fast_length(self):
return self._fast_length.Value
@property
def slow_length(self):
return self._slow_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(status_mail_and_alert_on_order_close_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(status_mail_and_alert_on_order_close_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_length
slow = ExponentialMovingAverage()
slow.Length = self.slow_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast, slow, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, fast, slow):
if candle.State != CandleStates.Finished:
return
if not self._has_prev:
self._prev_fast = fast
self._prev_slow = slow
self._has_prev = True
return
if self._prev_fast <= self._prev_slow and fast > slow:
if self.Position < 0:
self.BuyMarket()
if self.Position <= 0:
self.BuyMarket()
elif self._prev_fast >= self._prev_slow and fast < slow:
if self.Position > 0:
self.SellMarket()
if self.Position >= 0:
self.SellMarket()
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return status_mail_and_alert_on_order_close_strategy()