Octopus Nest 策略
该策略利用布林带和肯特纳通道的“挤压”突破。方向由 EMA 和 Parabolic SAR 确认。止损设置在最近极值,收益按风险比例计算。
详情
- 入场条件:
- 多头:价格高于 EMA 和 PSAR,并且不在挤压中。
- 空头:价格低于 EMA 和 PSAR,并且不在挤压中。
- 多空方向:双向。
- 退出条件:根据最近的高/低点设置止损,并按风险回报比计算止盈。
- 止损:是,根据最近极值。
- 过滤器:布林带/肯特纳通道挤压、EMA 趋势、PSAR 方向。
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class OctopusNestStrategy : Strategy
{
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<int> _bbLength;
private readonly StrategyParam<DataType> _candleType;
public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
public int BbLength { get => _bbLength.Value; set => _bbLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public OctopusNestStrategy()
{
_emaLength = Param(nameof(EmaLength), 100).SetGreaterThanZero();
_bbLength = Param(nameof(BbLength), 20).SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var ema = new ExponentialMovingAverage { Length = EmaLength };
var bb = new BollingerBands { Length = BbLength, Width = 2m };
var lastSignal = DateTimeOffset.MinValue;
var cooldown = TimeSpan.FromMinutes(360);
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(ema, bb, (candle, emaValue, bbValue) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!emaValue.IsFormed || !bbValue.IsFormed)
return;
var emaVal = emaValue.ToDecimal();
var bbTyped = (BollingerBandsValue)bbValue;
if (bbTyped.UpBand is not decimal bbUpper || bbTyped.LowBand is not decimal bbLower)
return;
var bbWidth = bbUpper - bbLower;
var squeeze = bbWidth < candle.ClosePrice * 0.01m;
if (squeeze || candle.OpenTime - lastSignal < cooldown)
return;
if (candle.ClosePrice > emaVal && candle.ClosePrice > bbUpper && Position <= 0)
{
BuyMarket();
lastSignal = candle.OpenTime;
}
else if (candle.ClosePrice < emaVal && candle.ClosePrice < bbLower && Position >= 0)
{
SellMarket();
lastSignal = candle.OpenTime;
}
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawIndicator(area, bb);
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, BollingerBands
from StockSharp.Algo.Strategies import Strategy
class octopus_nest_strategy(Strategy):
def __init__(self):
super(octopus_nest_strategy, self).__init__()
self._ema_length = self.Param("EmaLength", 100) \
.SetGreaterThanZero()
self._bb_length = self.Param("BbLength", 20) \
.SetGreaterThanZero()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._last_signal_ticks = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(octopus_nest_strategy, self).OnReseted()
self._last_signal_ticks = 0
def OnStarted2(self, time):
super(octopus_nest_strategy, self).OnStarted2(time)
self._last_signal_ticks = 0
self._ema = ExponentialMovingAverage()
self._ema.Length = self._ema_length.Value
self._bb = BollingerBands()
self._bb.Length = self._bb_length.Value
self._bb.Width = 2
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._ema, self._bb, self.OnProcess).Start()
def OnProcess(self, candle, ema_value, bb_value):
if candle.State != CandleStates.Finished:
return
if not ema_value.IsFormed or not bb_value.IsFormed:
return
ema_val = float(ema_value)
bb_upper = bb_value.UpBand
bb_lower = bb_value.LowBand
if bb_upper is None or bb_lower is None:
return
bb_upper = float(bb_upper)
bb_lower = float(bb_lower)
close = float(candle.ClosePrice)
bb_width = bb_upper - bb_lower
squeeze = bb_width < close * 0.01
cooldown_ticks = TimeSpan.FromMinutes(360).Ticks
current_ticks = candle.OpenTime.Ticks
if squeeze or current_ticks - self._last_signal_ticks < cooldown_ticks:
return
if close > ema_val and close > bb_upper and self.Position <= 0:
self.BuyMarket()
self._last_signal_ticks = current_ticks
elif close < ema_val and close < bb_lower and self.Position >= 0:
self.SellMarket()
self._last_signal_ticks = current_ticks
def CreateClone(self):
return octopus_nest_strategy()