アラート システム 戦略は、MetaTrader 4 エキスパート アドバイザー AlertingSystem.mq4 を忠実に StockSharp に変換したものです。オリジナルのスクリプトは 2 本の水平線を描き、市場がそれらに触れるたびにサウンドを再生します。 The StockSharp version accomplishes the same goal by subscribing to Level1 (best bid/ask) quotes and printing journal messages when either configurable alert level is crossed.
Emit a single log notification when the price crosses an active level and wait until the market returns to the safe zone before arming the alert again.これにより、元のサウンドトリガーの意図を維持しながら、騒々しい重複アラートが防止されます。
Alert detection: Helper methods (CheckUpperAlert and CheckLowerAlert) store internal flags to guarantee that each breach produces exactly one notification until the market moves back beyond the threshold.
The logic intentionally stays indicator-free: only raw quotes are required, so the strategy works on any timeframe or instrument that provides Level1 data.
分類
カテゴリ: ユーティリティ / アラート
取引方向: なし
実行スタイル: イベント駆動型の監視
データ要件: レベル 1 の買値/売値
複雑さ: 基本
推奨される期間: 任意 (引用ベース)
リスク管理: 該当なし (オープンなポジションはありません)
This documentation summarizes the StockSharp implementation and highlights the practical steps needed to reproduce the MetaTrader alerting workflow inside the platform.
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Alerting System strategy: Bollinger Band breakout.
/// Buys when price crosses above upper band, sells when below lower band.
/// </summary>
public class AlertingSystemStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _bbPeriod;
private readonly StrategyParam<decimal> _bbWidth;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int BbPeriod { get => _bbPeriod.Value; set => _bbPeriod.Value = value; }
public decimal BbWidth { get => _bbWidth.Value; set => _bbWidth.Value = value; }
public AlertingSystemStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_bbPeriod = Param(nameof(BbPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("BB Period", "Bollinger Bands period", "Indicators");
_bbWidth = Param(nameof(BbWidth), 2m)
.SetDisplay("BB Width", "Bollinger Bands width multiplier", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bb = new BollingerBands
{
Length = BbPeriod,
Width = BbWidth
};
var subscription = SubscribeCandles(CandleType);
subscription.BindEx(bb, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue bbValue)
{
if (candle.State != CandleStates.Finished) return;
if (!bbValue.IsFinal) return;
var typed = (BollingerBandsValue)bbValue;
if (typed.UpBand is not decimal upper || typed.LowBand is not decimal lower) return;
var close = candle.ClosePrice;
// Mean reversion: buy at lower band, sell at upper band
if (close < lower && Position <= 0)
BuyMarket();
else if (close > upper && Position >= 0)
SellMarket();
}
}
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 BollingerBands
from StockSharp.Algo.Strategies import Strategy
class alerting_system_strategy(Strategy):
def __init__(self):
super(alerting_system_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._bb_period = self.Param("BbPeriod", 20) \
.SetGreaterThanZero() \
.SetDisplay("BB Period", "Bollinger Bands period", "Indicators")
self._bb_width = self.Param("BbWidth", 2.0) \
.SetDisplay("BB Width", "Bollinger Bands width multiplier", "Indicators")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(alerting_system_strategy, self).OnStarted2(time)
self._bb = BollingerBands()
self._bb.Length = self._bb_period.Value
self._bb.Width = self._bb_width.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._bb, self._process_candle).Start()
def _process_candle(self, candle, bb_value):
if candle.State != CandleStates.Finished:
return
if not bb_value.IsFinal:
return
upper = bb_value.UpBand
lower = bb_value.LowBand
if upper is None or lower is None:
return
close = float(candle.ClosePrice)
upper_val = float(upper)
lower_val = float(lower)
if close < lower_val and self.Position <= 0:
self.BuyMarket()
elif close > upper_val and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return alerting_system_strategy()