日志输出格式与原脚本一致:New tick on the symbol <id> index in the list=<index> bid=<bid> spread=<spread>。若卖价缺失,则点差显示为 n/a。
当 SecurityProvider 无法找到某个符号时,记录警告并跳过该符号。
使用步骤
通过界面或代码设置策略主标的 (Strategy.Security)。
如需监控额外标的,将其以逗号分隔写入 SymbolsList 参数,顺序决定日志中显示的索引。
确保连接器能够为这些标的提供 Level1 数据。
启动策略后会立即发起 Level1 订阅并开始打印 Tick 日志。
在策略日志中检查实际的报价与计算出的点差信息。
与 MQL 版本的差异
C# 版本完全事件驱动,无需 Sleep 或手工循环。
SymbolsTotal(true) 的效果通过保持添加顺序实现,索引从 0 开始。
MetaTrader 中点差以点值表示,而此处使用十进制价格差。
自定义图表事件被日志替代,利用了 StockSharp 自带的日志系统。
若当前更新缺少卖价,会明确输出 n/a,提醒 Level1 数据不完整。
策略仅用于监控,不会提交任何委托。
示例日志
New tick on the symbol AAPL@NASDAQ index in the list=0 bid=171.25 spread=0.02
New tick on the symbol MSFT@NASDAQ index in the list=1 bid=324.10 spread=n/a
上述日志展示了策略如何针对每个被监控的品种输出买价和点差信息。
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// On Tick Market Watch strategy (simplified).
/// Uses simple price momentum for entries.
/// </summary>
public class OnTickMarketWatchStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _emaLength;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int EmaLength
{
get => _emaLength.Value;
set => _emaLength.Value = value;
}
public OnTickMarketWatchStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_emaLength = Param(nameof(EmaLength), 20)
.SetGreaterThanZero()
.SetDisplay("EMA Length", "EMA period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = EmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, (ICandleMessage candle, decimal emaValue) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var close = candle.ClosePrice;
if (close > emaValue && Position <= 0)
BuyMarket();
else if (close < emaValue && Position >= 0)
SellMarket();
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class on_tick_market_watch_strategy(Strategy):
def __init__(self):
super(on_tick_market_watch_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candles", "General")
self._ema_length = self.Param("EmaLength", 20) \
.SetDisplay("EMA Length", "EMA period", "Indicators")
@property
def CandleType(self):
return self._candle_type.Value
@property
def EmaLength(self):
return self._ema_length.Value
def OnStarted2(self, time):
super(on_tick_market_watch_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self.EmaLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(ema, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _on_process(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
ev = float(ema_value)
if close > ev and self.Position <= 0:
self.BuyMarket()
elif close < ev and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return on_tick_market_watch_strategy()