Sophia 1_1 策略
Sophia 1_1 是一种基于网格的马丁格尔交易策略。 当连续出现四根同方向的蜡烛时开仓:
- 四根上升蜡烛后开空。
- 四根下降蜡烛后开多。
进入市场后,价格每逆当前持仓方向移动一定的价格步长(Pip Step)时,策略会加仓。
每次加仓的手数按 Lot Exponent 成倍增加,形成经典的马丁格尔网格。
风险管理通过 Take Profit、Stop Loss 以及可选的移动止损完成。
当盈利达到 Trail Start 时启用移动止损,并以 Trail Stop 的价格步长跟随。
参数
- Volume – 首笔交易的基本手数。
- Pip Step – 触发加仓的价格步长。
- Lot Exponent – 每次加仓的手数倍率。
- Max Trades – 网格中的最大持仓数量。
- Take Profit – 相对于平均入场价的盈利目标(价格步长)。
- Stop Loss – 相对于平均入场价的亏损阈值(价格步长)。
- Use Trailing – 是否启用移动止损。
- Trail Start – 启用移动止损所需的最低盈利。
- Trail Stop – 移动止损的跟随距离(价格步长)。
- Candle Type – 计算时使用的蜡烛周期。
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>
/// Grid mean-reversion strategy. Enters on 3-bar momentum, exits at SMA or ATR stop.
/// </summary>
public class Sophia11Strategy : Strategy
{
private readonly StrategyParam<int> _smaPeriod;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prev1, _prev2, _prev3;
public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public Sophia11Strategy()
{
_smaPeriod = Param(nameof(SmaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("SMA Period", "SMA for exit target", "Indicators");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Period", "ATR for stops", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prev1 = _prev2 = _prev3 = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = SmaPeriod };
var atr = new StandardDeviation { Length = AtrPeriod };
SubscribeCandles(CandleType).Bind(sma, atr, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal sma, decimal atr)
{
if (candle.State != CandleStates.Finished) return;
var close = candle.ClosePrice;
if (_prev3 > 0)
{
// 3-bar declining => counter-trend buy
if (_prev1 < _prev2 && _prev2 < _prev3 && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
// 3-bar rising => counter-trend sell
else if (_prev1 > _prev2 && _prev2 > _prev3 && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
// Exit long at SMA or ATR stop
else if (Position > 0 && (close >= sma || (atr > 0 && close < sma - atr * 3)))
{
SellMarket();
}
// Exit short at SMA or ATR stop
else if (Position < 0 && (close <= sma || (atr > 0 && close > sma + atr * 3)))
{
BuyMarket();
}
}
_prev3 = _prev2;
_prev2 = _prev1;
_prev1 = close;
}
}
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, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class sophia11_strategy(Strategy):
def __init__(self):
super(sophia11_strategy, self).__init__()
self._sma_period = self.Param("SmaPeriod", 20) \
.SetDisplay("SMA Period", "SMA for exit target", "Indicators")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetDisplay("ATR Period", "ATR for stops", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._prev1 = 0.0
self._prev2 = 0.0
self._prev3 = 0.0
@property
def sma_period(self):
return self._sma_period.Value
@property
def atr_period(self):
return self._atr_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(sophia11_strategy, self).OnReseted()
self._prev1 = 0.0
self._prev2 = 0.0
self._prev3 = 0.0
def OnStarted2(self, time):
super(sophia11_strategy, self).OnStarted2(time)
sma = SimpleMovingAverage()
sma.Length = self.sma_period
atr = StandardDeviation()
atr.Length = self.atr_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, atr, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, sma, atr):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
if self._prev3 > 0:
# 3-bar declining => counter-trend buy
if self._prev1 < self._prev2 and self._prev2 < self._prev3 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
# 3-bar rising => counter-trend sell
elif self._prev1 > self._prev2 and self._prev2 > self._prev3 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
# Exit long at SMA or ATR stop
elif self.Position > 0 and (close >= sma or (atr > 0 and close < sma - atr * 3)):
self.SellMarket()
# Exit short at SMA or ATR stop
elif self.Position < 0 and (close <= sma or (atr > 0 and close > sma + atr * 3)):
self.BuyMarket()
self._prev3 = self._prev2
self._prev2 = self._prev1
self._prev1 = close
def CreateClone(self):
return sophia11_strategy()