在 GitHub 上查看

IsConnected 策略

摘要

  • 来源:由 MetaTrader 5 脚本 IsConnected.mq5(目录 MQL/35056)转换而来。
  • 目标:持续监控连接器的在线状态,在状态切换时记录时间戳以及在线/离线持续时间。
  • 类型:运维监控型策略,不包含交易信号与下单逻辑。

行为流程

  1. 启动时输出一条初始化日志,并获取当前连接状态。
  2. 后台定时器每隔 CheckIntervalSeconds 秒(默认 1 秒)读取一次 Connector.IsConnected 标志。
  3. 当检测到状态变化时,策略会:
    • 使用策略的 CurrentTime 记录切换时刻。
    • 输出新的状态(OnlineOffline)。
    • 给出上一状态持续的时间长度(断线前在线多久,或恢复前离线多久)。
  4. 策略停止时取消定时器,并报告结束时的连接状态,便于追踪停机时是否在线。

参数

名称 类型 默认值 说明
CheckIntervalSeconds int 1 连接状态检查周期(秒)。必须大于零。

日志特性

  • 所有信息通过 LogInfo 以英文输出,以保持与原始脚本 Print 行为一致。
  • 输出同时包含切换时间与上一阶段持续时间,便于审计。

与原脚本的差异

  • MQL5 中的忙等待循环被托管定时器替代,避免阻塞策略主线程。
  • 去除了重复输出,改为结构化地报告在线/离线持续时长。
  • OnStoppedOnReseted 中显式关闭定时器,确保不会遗留后台任务。
namespace StockSharp.Samples.Strategies;

using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;

/// <summary>
/// IsConnected strategy: Parabolic SAR trend following.
/// Buys when close above SAR, sells when close below SAR.
/// </summary>
public class IsConnectedStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _acceleration;
	private readonly StrategyParam<decimal> _accelerationMax;

	private decimal _prevSar;
	private decimal _prevClose;
	private bool _hasPrev;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public decimal Acceleration { get => _acceleration.Value; set => _acceleration.Value = value; }
	public decimal AccelerationMax { get => _accelerationMax.Value; set => _accelerationMax.Value = value; }

	public IsConnectedStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(60).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
		_acceleration = Param(nameof(Acceleration), 0.01m)
			.SetDisplay("Acceleration", "SAR acceleration factor", "Indicators");
		_accelerationMax = Param(nameof(AccelerationMax), 0.1m)
			.SetDisplay("Acceleration Max", "SAR max acceleration", "Indicators");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSar = 0;
		_prevClose = 0;
		_hasPrev = false;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_prevSar = 0;
		_prevClose = 0;
		_hasPrev = false;
		var sar = new ParabolicSar { Acceleration = Acceleration, AccelerationMax = AccelerationMax };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(sar, ProcessCandle).Start();
	}

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

		if (_hasPrev)
		{
			if (_prevClose <= _prevSar && candle.ClosePrice > sarValue && Position <= 0)
				BuyMarket();
			else if (_prevClose >= _prevSar && candle.ClosePrice < sarValue && Position >= 0)
				SellMarket();
		}

		_prevClose = candle.ClosePrice;
		_prevSar = sarValue;
		_hasPrev = true;
	}
}