Sail System EA 是一款对冲型剥头皮策略,会同时保持多头和空头敞口,并实时监控
经纪商的要求(最大点差、最小止损距离以及交易时段限制)。StockSharp 版本完全使
用高级 Strategy API:策略订阅一级报价,自动重新布置双向头寸,并在策略内部管理
虚拟止损/止盈,无需调用底层连接器接口。
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Sail System EA strategy: Momentum + SMA crossover trend.
/// Buys when close crosses above SMA and momentum confirms.
/// Sells when close crosses below SMA and momentum confirms.
/// </summary>
public class SailSystemEaStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _smaPeriod;
private readonly StrategyParam<int> _momPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int SmaPeriod
{
get => _smaPeriod.Value;
set => _smaPeriod.Value = value;
}
public int MomPeriod
{
get => _momPeriod.Value;
set => _momPeriod.Value = value;
}
public SailSystemEaStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_smaPeriod = Param(nameof(SmaPeriod), 30)
.SetGreaterThanZero()
.SetDisplay("SMA Period", "SMA period", "Indicators");
_momPeriod = Param(nameof(MomPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("Momentum", "Momentum period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = SmaPeriod };
var mom = new Momentum { Length = MomPeriod };
decimal? prevClose = null;
decimal? prevSma = null;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(sma, mom, (candle, smaVal, momVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var close = candle.ClosePrice;
if (prevClose.HasValue && prevSma.HasValue)
{
var crossUp = prevClose.Value <= prevSma.Value && close > smaVal;
var crossDown = prevClose.Value >= prevSma.Value && close < smaVal;
if (crossUp && momVal > 100m && Position <= 0)
BuyMarket();
else if (crossDown && momVal < 100m && Position >= 0)
SellMarket();
}
prevClose = close;
prevSma = smaVal;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
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 SimpleMovingAverage, Momentum
from StockSharp.Algo.Strategies import Strategy
class sail_system_ea_strategy(Strategy):
def __init__(self):
super(sail_system_ea_strategy, self).__init__()
self._sma_period = self.Param("SmaPeriod", 30) \
.SetDisplay("SMA Period", "SMA period", "Indicators")
self._mom_period = self.Param("MomPeriod", 20) \
.SetDisplay("Momentum", "Momentum period", "Indicators")
self._sma = None
self._mom = None
self._prev_close = None
self._prev_sma = None
@property
def sma_period(self):
return self._sma_period.Value
@property
def mom_period(self):
return self._mom_period.Value
def OnReseted(self):
super(sail_system_ea_strategy, self).OnReseted()
self._sma = None
self._mom = None
self._prev_close = None
self._prev_sma = None
def OnStarted2(self, time):
super(sail_system_ea_strategy, self).OnStarted2(time)
self._sma = SimpleMovingAverage()
self._sma.Length = self.sma_period
self._mom = Momentum()
self._mom.Length = self.mom_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromHours(1)))
subscription.Bind(self._sma, self._mom, self._process_candle)
subscription.Start()
def _process_candle(self, candle, sma_value, mom_value):
if candle.State != CandleStates.Finished:
return
if not self._sma.IsFormed or not self._mom.IsFormed:
return
close = float(candle.ClosePrice)
sma_val = float(sma_value)
mom_val = float(mom_value)
if self._prev_close is not None and self._prev_sma is not None:
cross_up = self._prev_close <= self._prev_sma and close > sma_val
cross_down = self._prev_close >= self._prev_sma and close < sma_val
if cross_up and mom_val > 100.0 and self.Position <= 0:
self.BuyMarket()
elif cross_down and mom_val < 100.0 and self.Position >= 0:
self.SellMarket()
self._prev_close = close
self._prev_sma = sma_val
def CreateClone(self):
return sail_system_ea_strategy()