ReInitChart 策略
该策略把 MetaTrader 的 ReInitChart 工具移植到 StockSharp。原始脚本会在每个图表上放置一个按钮,并通过临时切换时间框架来迫使平台重新计算指标。StockSharp 版本通过 ManualRefreshRequest 参数与可选的 AutoRefreshEnabled 计时器保留这种思想:它们重置内部的 SMA 指标并在日志中记录刷新事件,同时示例性地使用简单的均线趋势策略来展示刷新后的交易行为。
工作流程
- 主数据源 —— 根据
CandleType订阅蜡烛,并计算长度为SmaLength的简单移动平均。 - 手动刷新 —— 当
ManualRefreshRequest被设为true时,移动平均被重置,标志自动恢复为false,并在日志中写入原始按钮的元数据(RefreshCommandName、RefreshCommandText、TextColorName、BackgroundColorName)。 - 自动刷新 —— 启用
AutoRefreshEnabled后,策略会按照AutoRefreshInterval周期性地再次重置指标,复现 MetaTrader 中基于定时器的重新初始化。 - 交易逻辑 —— 在 SMA 成形之后,策略最多只持有一个方向的仓位:收盘价高于均线时做多,跌破均线时先平掉多头再做空。
这样就用 StockSharp 的原生机制(指标重置与日志)达到了重新初始化所有图表的效果,无需在时间框架之间来回切换。
参数
| 参数 | 说明 |
|---|---|
CandleType |
用于订阅市场数据的时间框架。 |
SmaLength |
每次刷新后重新计算的移动平均长度。 |
AutoRefreshEnabled |
是否启用周期性刷新。 |
AutoRefreshInterval |
自动刷新的时间间隔。 |
ManualRefreshRequest |
手动设置为 true 以立即刷新,策略处理完毕后会自动清零。 |
RefreshCommandName |
保留自 MetaTrader 的按钮名称,刷新时写入日志。 |
RefreshCommandText |
保留自 MetaTrader 的按钮标题,刷新时写入日志。 |
TextColorName |
按钮文字颜色的描述,方便追溯与记录。 |
BackgroundColorName |
按钮背景颜色的描述,方便追溯与记录。 |
使用方法
- 根据需要设置
CandleType和SmaLength。 - 如果需要定期重新初始化,启用
AutoRefreshEnabled并调整AutoRefreshInterval;若只需手动控制,则保持关闭。 - 当需要刷新计算结果时,把
ManualRefreshRequest改为true。策略会自动将其恢复为false,并从下一根蜡烛开始重新积累数据。 - 启动策略。它会订阅市场数据、绘制蜡烛和 SMA 曲线以及成交记录,并在指标准备就绪后执行简单的趋势跟随交易。
与原始 MQL 脚本的差异
- StockSharp 没有图表按钮这一界面元素,因此刷新触发通过策略参数实现。
- 不再通过在 M1 与 M5 之间切换时间框架,而是直接重置指标,这更符合 StockSharp 的工作方式。
- 按钮的名称和颜色仅作为日志元数据被保留,策略不会在图表上创建额外的控件。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy that mimics the MetaTrader ReInitChart utility by resetting indicators on demand.
/// It resets a moving average either manually or on a timer and uses the SMA for simple trend-following entries.
/// </summary>
public class ReInitChartStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _smaLength;
private readonly StrategyParam<bool> _autoRefreshEnabled;
private readonly StrategyParam<TimeSpan> _autoRefreshInterval;
private readonly StrategyParam<bool> _manualRefreshRequest;
private readonly StrategyParam<string> _refreshCommandName;
private readonly StrategyParam<string> _refreshCommandText;
private readonly StrategyParam<string> _textColorName;
private readonly StrategyParam<string> _backgroundColorName;
private SimpleMovingAverage _sma = null!;
private bool _manualRefreshArmed;
private DateTimeOffset? _nextAutoRefreshTime;
private int _previousRelation;
/// <summary>
/// Primary candle type used to drive the strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Length of the moving average that is recalculated after each refresh.
/// </summary>
public int SmaLength
{
get => _smaLength.Value;
set => _smaLength.Value = value;
}
/// <summary>
/// Enables or disables automatic refresh events.
/// </summary>
public bool AutoRefreshEnabled
{
get => _autoRefreshEnabled.Value;
set => _autoRefreshEnabled.Value = value;
}
/// <summary>
/// Interval between automatic refresh operations.
/// </summary>
public TimeSpan AutoRefreshInterval
{
get => _autoRefreshInterval.Value;
set => _autoRefreshInterval.Value = value;
}
/// <summary>
/// Manual refresh flag that emulates the original MetaTrader button press.
/// </summary>
public bool ManualRefreshRequest
{
get => _manualRefreshRequest.Value;
set => _manualRefreshRequest.Value = value;
}
/// <summary>
/// Identifier of the refresh command, matching the MetaTrader button name.
/// </summary>
public string RefreshCommandName
{
get => _refreshCommandName.Value;
set => _refreshCommandName.Value = value;
}
/// <summary>
/// Text displayed for the refresh command.
/// </summary>
public string RefreshCommandText
{
get => _refreshCommandText.Value;
set => _refreshCommandText.Value = value;
}
/// <summary>
/// Text color descriptor preserved from the original script.
/// </summary>
public string TextColorName
{
get => _textColorName.Value;
set => _textColorName.Value = value;
}
/// <summary>
/// Background color descriptor preserved from the original script.
/// </summary>
public string BackgroundColorName
{
get => _backgroundColorName.Value;
set => _backgroundColorName.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public ReInitChartStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Time frame used for the primary chart subscription.", "Data");
_smaLength = Param(nameof(SmaLength), 20)
.SetGreaterThanZero()
.SetDisplay("SMA Length", "Number of candles for the recalculated moving average.", "Reinitialization");
_autoRefreshEnabled = Param(nameof(AutoRefreshEnabled), false)
.SetDisplay("Auto Refresh", "Enable periodic indicator reinitialization.", "Reinitialization");
_autoRefreshInterval = Param(nameof(AutoRefreshInterval), TimeSpan.FromMinutes(5))
.SetDisplay("Refresh Interval", "Time interval between automatic refresh operations.", "Reinitialization");
_manualRefreshRequest = Param(nameof(ManualRefreshRequest), false)
.SetDisplay("Manual Refresh", "Set to true to trigger a one-time indicator reset.", "Reinitialization");
_refreshCommandName = Param(nameof(RefreshCommandName), "ButtonReDraw")
.SetDisplay("Command Name", "Identifier that matches the MetaTrader button name.", "Appearance");
_refreshCommandText = Param(nameof(RefreshCommandText), "Recalculate")
.SetDisplay("Command Text", "Text shown on logs to mirror the MetaTrader button caption.", "Appearance");
_textColorName = Param(nameof(TextColorName), "NavajoWhite")
.SetDisplay("Text Color", "Original MetaTrader button text color.", "Appearance");
_backgroundColorName = Param(nameof(BackgroundColorName), "SlateGray")
.SetDisplay("Background Color", "Original MetaTrader button background color.", "Appearance");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_manualRefreshArmed = false;
_nextAutoRefreshTime = null;
_previousRelation = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_sma = new SMA
{
Length = SmaLength,
};
// Subscribe to the primary candle stream and bind the moving average.
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_sma, ProcessCandle)
.Start();
// Draw candles, indicator and executed trades when charting is available.
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _sma);
DrawOwnTrades(area);
}
if (AutoRefreshEnabled)
{
_nextAutoRefreshTime = time + AutoRefreshInterval;
}
}
private void ProcessCandle(ICandleMessage candle, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
// Handle manual or automatic refresh triggers using the candle close time.
CheckManualRefresh(candle.CloseTime);
CheckAutoRefresh(candle.CloseTime);
// Synchronize the moving average length if the parameter changed on the fly.
if (_sma.Length != SmaLength)
{
_sma.Length = SmaLength;
_sma.Reset();
this.LogInfo($"[{RefreshCommandName}] SMA length changed to {SmaLength}. Indicator state reset.");
return;
}
if (!_sma.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Volume <= 0)
return;
var relation = candle.ClosePrice > smaValue ? 1 : candle.ClosePrice < smaValue ? -1 : 0;
if (relation == 0 || relation == _previousRelation)
{
_previousRelation = relation;
return;
}
if (relation > 0)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
if (Position <= 0)
BuyMarket(Volume);
}
else if (relation < 0)
{
if (Position > 0)
SellMarket(Position);
if (Position >= 0)
SellMarket(Volume);
}
_previousRelation = relation;
}
private void CheckManualRefresh(DateTimeOffset currentTime)
{
if (ManualRefreshRequest)
{
if (!_manualRefreshArmed)
{
PerformRefresh(currentTime, "manual request");
ManualRefreshRequest = false;
_manualRefreshArmed = true;
}
}
else
{
_manualRefreshArmed = false;
}
}
private void CheckAutoRefresh(DateTimeOffset currentTime)
{
if (!AutoRefreshEnabled)
{
_nextAutoRefreshTime = null;
return;
}
if (_nextAutoRefreshTime is null)
_nextAutoRefreshTime = currentTime + AutoRefreshInterval;
if (currentTime < _nextAutoRefreshTime.Value)
return;
PerformRefresh(currentTime, "automatic schedule");
_nextAutoRefreshTime = currentTime + AutoRefreshInterval;
}
private void PerformRefresh(DateTimeOffset time, string reason)
{
// Reset the moving average so that future candles rebuild the indicator state.
_sma.Reset();
_previousRelation = 0;
this.LogInfo($"[{RefreshCommandName}] {RefreshCommandText} triggered by {reason} at {time:O}. TextColor={TextColorName}, BackgroundColor={BackgroundColorName}.");
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class re_init_chart_strategy(Strategy):
def __init__(self):
super(re_init_chart_strategy, self).__init__()
self._sma_length = self.Param("SmaLength", 20).SetGreaterThanZero().SetDisplay("SMA Length", "Moving average length", "Reinitialization")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))).SetDisplay("Candle Type", "Timeframe", "Data")
@property
def CandleType(self): return self._candle_type.Value
@CandleType.setter
def CandleType(self, value): self._candle_type.Value = value
def OnReseted(self):
super(re_init_chart_strategy, self).OnReseted()
self._prev_relation = 0
def OnStarted2(self, time):
super(re_init_chart_strategy, self).OnStarted2(time)
self._prev_relation = 0
sma = SimpleMovingAverage()
sma.Length = self._sma_length.Value
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(sma, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, sub)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
def OnProcess(self, candle, sma_val):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
if close > sma_val:
relation = 1
elif close < sma_val:
relation = -1
else:
relation = 0
if relation == 0 or relation == self._prev_relation:
self._prev_relation = relation
return
if relation > 0:
if self.Position < 0:
self.BuyMarket()
if self.Position <= 0:
self.BuyMarket()
elif relation < 0:
if self.Position > 0:
self.SellMarket()
if self.Position >= 0:
self.SellMarket()
self._prev_relation = relation
def CreateClone(self):
return re_init_chart_strategy()