Ver no GitHub
IsConnected Strategy
Summary
- Source: Converted from the MetaTrader 5 script
IsConnected.mq5 (folder MQL/35056).
- Purpose: Continuously monitors the connector status and reports online/offline transitions with timestamps and uptime/downtime durations.
- Type: Utility strategy focused on infrastructure monitoring rather than order execution.
Behaviour
- When the strategy starts it immediately logs that the monitoring module has been initialised and captures the current connector state.
- A background timer checks the
Connector.IsConnected flag every CheckIntervalSeconds (default: 1 second).
- When the state changes, the strategy:
- Stores the moment of transition using the strategy
CurrentTime.
- Logs the new state (
Online or Offline).
- Reports how long the previous state lasted (time online before a disconnect, or time offline before reconnection).
- When the strategy stops, it cancels the timer and logs the last known state so the operator knows whether the connection was up or down at shutdown.
Parameters
| Name |
Type |
Default |
Description |
CheckIntervalSeconds |
int |
1 |
Interval (in seconds) between successive connection checks. Must be greater than zero. |
Logging details
- All messages are written with
LogInfo in English to match the MetaTrader implementation that relied on Print statements.
- Time intervals are formatted using invariant culture and include both start timestamps and the time spent in the previous state.
Differences vs original script
- The busy waiting loop from MQL5 is replaced with a managed timer that does not block the strategy thread.
- Instead of printing duplicate status lines, the StockSharp version reports structured status changes along with uptime/downtime metrics.
- The conversion handles graceful disposal by stopping the timer in both
OnStopped and 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()