在 GitHub 上查看
IsConnected 策略
摘要
- 来源:由 MetaTrader 5 脚本
IsConnected.mq5(目录 MQL/35056)转换而来。
- 目标:持续监控连接器的在线状态,在状态切换时记录时间戳以及在线/离线持续时间。
- 类型:运维监控型策略,不包含交易信号与下单逻辑。
行为流程
- 启动时输出一条初始化日志,并获取当前连接状态。
- 后台定时器每隔
CheckIntervalSeconds 秒(默认 1 秒)读取一次 Connector.IsConnected 标志。
- 当检测到状态变化时,策略会:
- 使用策略的
CurrentTime 记录切换时刻。
- 输出新的状态(
Online 或 Offline)。
- 给出上一状态持续的时间长度(断线前在线多久,或恢复前离线多久)。
- 策略停止时取消定时器,并报告结束时的连接状态,便于追踪停机时是否在线。
参数
| 名称 |
类型 |
默认值 |
说明 |
CheckIntervalSeconds |
int |
1 |
连接状态检查周期(秒)。必须大于零。 |
日志特性
- 所有信息通过
LogInfo 以英文输出,以保持与原始脚本 Print 行为一致。
- 输出同时包含切换时间与上一阶段持续时间,便于审计。
与原脚本的差异
- MQL5 中的忙等待循环被托管定时器替代,避免阻塞策略主线程。
- 去除了重复输出,改为结构化地报告在线/离线持续时长。
- 在
OnStopped 与 OnReseted 中显式关闭定时器,确保不会遗留后台任务。
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;
}
}
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 ParabolicSar
from StockSharp.Algo.Strategies import Strategy
class is_connected_strategy(Strategy):
def __init__(self):
super(is_connected_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(60)))
self._acceleration = self.Param("Acceleration", 0.01)
self._acceleration_max = self.Param("AccelerationMax", 0.1)
self._prev_sar = 0.0
self._prev_close = 0.0
self._has_prev = False
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def Acceleration(self):
return self._acceleration.Value
@Acceleration.setter
def Acceleration(self, value):
self._acceleration.Value = value
@property
def AccelerationMax(self):
return self._acceleration_max.Value
@AccelerationMax.setter
def AccelerationMax(self, value):
self._acceleration_max.Value = value
def OnReseted(self):
super(is_connected_strategy, self).OnReseted()
self._prev_sar = 0.0
self._prev_close = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(is_connected_strategy, self).OnStarted2(time)
self._prev_sar = 0.0
self._prev_close = 0.0
self._has_prev = False
sar = ParabolicSar()
sar.Acceleration = self.Acceleration
sar.AccelerationMax = self.AccelerationMax
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(sar, self._process_candle).Start()
def _process_candle(self, candle, sar_value):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
sar_val = float(sar_value)
if self._has_prev:
if self._prev_close <= self._prev_sar and close > sar_val and self.Position <= 0:
self.BuyMarket()
elif self._prev_close >= self._prev_sar and close < sar_val and self.Position >= 0:
self.SellMarket()
self._prev_close = close
self._prev_sar = sar_val
self._has_prev = True
def CreateClone(self):
return is_connected_strategy()