Aver4 Stoch Post ZigZag
该策略结合了四个不同周期的 Stochastic 振荡器和一个简单的 ZigZag 枢轴检测器。平均 Stochastic 用于判断超买和超卖水平,而 ZigZag 用于确认新的高点和低点。当平均 Stochastic 低于超卖阈值并出现新的 ZigZag 低点时买入;当平均 Stochastic 高于超买阈值并出现新的 ZigZag 高点时卖出。反向信号出现时会平掉相反的持仓。
细节
- 入场条件:平均 Stochastic 穿越超买/超卖水平并出现匹配的 ZigZag 枢轴。
- 多空方向:双向。
- 出场条件:相反信号。
- 止损:StartProtection 2%/2%(默认)。
- 默认值:
ShortLength= 26MidLength1= 72MidLength2= 144LongLength= 288ZigZagDepth= 14Oversold= 5Overbought= 95CandleType= TimeSpan.FromMinutes(5)
- 筛选条件:
- 分类:Oscillator
- 方向:双向
- 指标:Stochastic, ZigZag
- 止损:是
- 复杂度:高级
- 时间框架:日内
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中等
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>
/// Averaged Stochastic with ZigZag-style pivot confirmation.
/// Uses RSI as simplified oscillator and highest/lowest for pivots.
/// </summary>
public class Aver4StochPostZigZagStrategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _pivotLength;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevRsi;
private bool _hasPrev;
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public int PivotLength { get => _pivotLength.Value; set => _pivotLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public Aver4StochPostZigZagStrategy()
{
_rsiLength = Param(nameof(RsiLength), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_pivotLength = Param(nameof(PivotLength), 20)
.SetGreaterThanZero()
.SetDisplay("Pivot Length", "Highest/Lowest period", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var highest = new Highest { Length = PivotLength };
var lowest = new Lowest { Length = PivotLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, highest, lowest, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsi, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!_hasPrev)
{
_prevRsi = rsi;
_hasPrev = true;
return;
}
var close = candle.ClosePrice;
// Near pivot low + RSI oversold -> buy
if (close <= low * 1.001m && rsi < 30 && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
// Near pivot high + RSI overbought -> sell
else if (close >= high * 0.999m && rsi > 70 && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
// Exit long on RSI overbought
else if (Position > 0 && rsi > 70)
{
SellMarket();
}
// Exit short on RSI oversold
else if (Position < 0 && rsi < 30)
{
BuyMarket();
}
_prevRsi = rsi;
}
}
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 RelativeStrengthIndex, Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class aver4_stoch_post_zig_zag_strategy(Strategy):
def __init__(self):
super(aver4_stoch_post_zig_zag_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._pivot_length = self.Param("PivotLength", 20) \
.SetDisplay("Pivot Length", "Highest/Lowest period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_rsi = 0.0
self._has_prev = False
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def pivot_length(self):
return self._pivot_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(aver4_stoch_post_zig_zag_strategy, self).OnReseted()
self._prev_rsi = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(aver4_stoch_post_zig_zag_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
highest = Highest()
highest.Length = self.pivot_length
lowest = Lowest()
lowest.Length = self.pivot_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, highest, lowest, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, rsi, high, low):
if candle.State != CandleStates.Finished:
return
if not self._has_prev:
self._prev_rsi = rsi
self._has_prev = True
return
close = candle.ClosePrice
# Near pivot low + RSI oversold -> buy
if close <= low * 1.001 and rsi < 30 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
# Near pivot high + RSI overbought -> sell
elif close >= high * 0.999 and rsi > 70 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
# Exit long on RSI overbought
elif self.Position > 0 and rsi > 70:
self.SellMarket()
# Exit short on RSI oversold
elif self.Position < 0 and rsi < 30:
self.BuyMarket()
self._prev_rsi = rsi
def CreateClone(self):
return aver4_stoch_post_zig_zag_strategy()