在 GitHub 上查看
Signal Count With Array 策略
该策略复刻 MetaTrader 4 指标机器人 Signal-COunt-with array.mq4 的诊断思路。
它使用唐奇安通道在多个价格偏移上监测极值,并统计指标值变化、回到空值
以及离开零值的次数。策略本身不会下单,而是在检测到新的高点/低点或打开
逐K日志选项时输出详细统计信息。
核心思想
- 用唐奇安通道代替原始脚本中的
super_signals_v2_alert 自定义指标,
获取 ChannelPeriod 根K线的最高价和最低价。
- 遍历由
GapStart、GapStep、GapCount 定义的一组价格偏移,模拟 MQL
程序中的多参数循环。
- 对每个偏移维护六个计数器,对应原脚本中的数组,包含对空值 sentinel
(
2147483647 和 -2147483646) 的进出统计。
- 通过文本表格展示累积结果,便于比较不同虚拟配置下信号的出现频率。
参数
| 参数 |
默认值 |
说明 |
CandleType |
5 分钟 |
用于计算唐奇安通道的K线类型。 |
ChannelPeriod |
24 |
计算极值的K线数量。 |
GapStart |
0 |
第一个价格偏移(按最小变动价位计算)。 |
GapStep |
1 |
邻近偏移之间的步长。 |
GapCount |
8 |
需要评估的偏移数量(对应原脚本的 0..7)。 |
LogOnEachCandle |
false |
若为 true,每根完成的K线都会输出统计。 |
计数器说明
每个偏移包含两行:索引 0 代表上轨(看多信号),索引 1 代表下轨(看空信号)。
统计字段如下:
- Changed:当前值与前一次不同。
- Empty:返回正向 sentinel
2147483647 的次数。
- NegEmpty:返回负向 sentinel
-2147483646 的次数(主要针对下轨)。
- Zero:从默认的 0 变为任意非零值的次数。
- NewFromEmpty:真实价格信号替换 sentinel 的次数。
- BackToEmpty:在出现非 sentinel 值后又回到 sentinel 的次数。
这些统计与原策略中的数组 (GetInd_iCustom_changed 等) 完全对应。
日志输出
策略在以下场景通过 AddInfoLog 打印信息:
- 唐奇安上轨上移或下轨下移,意味着出现新的极值;
LogOnEachCandle 设为 true 时,每根完成的K线。
日志首行包含时间戳,随后按偏移列出各计数器,方便对比不同偏移的行为差异。
使用建议
- 策略仅分析K线数据,不会发送订单,可用于任何标的。
- 根据标的波动性调整
ChannelPeriod,周期越长越接近原 MT4 指标的节奏。
- 若需要观察更多偏移,可提升
GapCount,在启动时会自动重新分配数组。
- 建议与图表显示(K线 + 唐奇安通道)配合使用,便于直观验证统计结果。
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class SignalCountWithArrayStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cciPeriod;
private decimal? _prevCci;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public SignalCountWithArrayStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame()).SetDisplay("Candle Type", "Timeframe", "General");
_cciPeriod = Param(nameof(CciPeriod), 14).SetGreaterThanZero().SetDisplay("CCI Period", "CCI lookback", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevCci = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevCci = null;
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(cci, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, subscription); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal cciVal)
{
if (candle.State != CandleStates.Finished) return;
if (!IsFormedAndOnlineAndAllowTrading()) { _prevCci = cciVal; return; }
if (_prevCci == null) { _prevCci = cciVal; return; }
if (_prevCci.Value < 0m && cciVal >= 0m && Position <= 0) { if (Position < 0) BuyMarket(); BuyMarket(); }
else if (_prevCci.Value > 0m && cciVal <= 0m && Position >= 0) { if (Position > 0) SellMarket(); SellMarket(); }
_prevCci = cciVal;
}
}
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 CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
from datatype_extensions import *
from indicator_extensions import *
class signal_count_with_array_strategy(Strategy):
def __init__(self):
super(signal_count_with_array_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 14).SetGreaterThanZero().SetDisplay("CCI Period", "CCI lookback", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))).SetDisplay("Candle Type", "Timeframe", "General")
@property
def CandleType(self): return self._candle_type.Value
@CandleType.setter
def CandleType(self, value): self._candle_type.Value = value
def OnReseted(self):
super(signal_count_with_array_strategy, self).OnReseted()
self._prev_cci = None
def OnStarted2(self, time):
super(signal_count_with_array_strategy, self).OnStarted2(time)
self._prev_cci = None
cci = CommodityChannelIndex()
cci.Length = self._cci_period.Value
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(cci, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, sub)
self.DrawOwnTrades(area)
def OnProcess(self, candle, cci_val):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_cci = cci_val
return
if self._prev_cci is None:
self._prev_cci = cci_val
return
if self._prev_cci < 0 and cci_val >= 0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif self._prev_cci > 0 and cci_val <= 0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_cci = cci_val
def CreateClone(self):
return signal_count_with_array_strategy()