首页
/
策略示例
在 GitHub 上查看
Avalanche AV 策略
Avalanche AV 是一套随机化的马丁格尔策略。在等待设定数量的收盘 K 线之后,系统以 50/50 的概率开多或开空。每笔新订单都会根据点数设置固定的止损和止盈。当交易以亏损结束时,下一笔订单的手数会按照马丁系数放大;当账户余额创出新的历史高点时,手数会被重置为初始值。此外,策略还监控浮动回撤,并在亏损超过允许的百分比阈值时强制平仓。
原版 MQL 脚本基于逐笔行情运行。本移植版本保持相同的概率逻辑,但改为在蜡烛图更新时执行,因此更适合在 StockSharp 中进行回测和实时交易。
交易规则
决策间隔: 等待指定数量的已完成 K 线后才评估新的入场信号。如果当前仍有持仓,计数继续,但不会触发新的开仓。
入场方向: 生成一个随机数;大于 16384 时做多,否则做空。只有在没有持仓时才会开新单。
仓位大小: 初始手数由 InitialVolume 参数给出。每次亏损后,下一单的手数变为 上一单手数 * MartingaleMultiplier,并会按照交易品种的成交量步长进行规范化。若交易盈利且账户余额创出新高,则手数重置为初始值;否则继续沿用放大的手数。
止损止盈: 以点数为单位,从入场价计算止损和止盈。一个点等于品种的最小报价步长。
浮动回撤: 持仓期间持续计算未实现盈亏。当亏损超过 MaxDrawdownPercent(按账户余额的百分比)时立即平仓。
参数
参数
默认值
说明
InitialVolume
0.1
初始下单手数。
StopLossPips
15
止损距离(点),0 表示不设置。
TakeProfitPips
30
止盈距离(点),0 表示不设置。
MaxDrawdownPercent
75
允许的最大浮亏占余额百分比。
MartingaleMultiplier
1.6
亏损后放大的手数倍数。
DecisionInterval
9
两次评估之间需要完成的 K 线数量。
CandleType
1 分钟
驱动策略的蜡烛类型。
说明
手数会自动根据 VolumeStep、MinVolume 和 MaxVolume 进行规范化;若规范化失败,则回退到初始手数。
止损和止盈通过 PriceStep 来计算点值,使用前请确认交易品种的最小报价步长。
浮动回撤保护依赖 PriceStep 和 StepPrice,若这两个值缺失,则保护逻辑不会触发。
策略含有随机性,如需可重复结果需要在外部固定随机数种子。
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Avalanche strategy using channel breakout with Highest/Lowest.
/// </summary>
public class AvalancheAvStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _period;
private decimal? _prevHigh;
private decimal? _prevLow;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int Period
{
get => _period.Value;
set => _period.Value = value;
}
public AvalancheAvStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
_period = Param(nameof(Period), 14)
.SetGreaterThanZero()
.SetDisplay("Period", "Channel period", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevHigh = null;
_prevLow = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevHigh = null;
_prevLow = null;
var highest = new Highest { Length = Period };
var lowest = new Lowest { Length = Period };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, highest);
DrawIndicator(area, lowest);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevHigh = high;
_prevLow = low;
return;
}
var close = candle.ClosePrice;
if (_prevHigh == null || _prevLow == null)
{
_prevHigh = high;
_prevLow = low;
return;
}
if (close > _prevHigh.Value && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
else if (close < _prevLow.Value && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
_prevHigh = high;
_prevLow = low;
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class avalanche_av_strategy(Strategy):
def __init__(self):
super(avalanche_av_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._period = self.Param("Period", 14) \
.SetDisplay("Period", "Channel period", "Indicators")
self._prev_high = None
self._prev_low = None
@property
def CandleType(self):
return self._candle_type.Value
@property
def Period(self):
return self._period.Value
def OnReseted(self):
super(avalanche_av_strategy, self).OnReseted()
self._prev_high = None
self._prev_low = None
def OnStarted2(self, time):
super(avalanche_av_strategy, self).OnStarted2(time)
self._prev_high = None
self._prev_low = None
highest = Highest()
highest.Length = self.Period
lowest = Lowest()
lowest.Length = self.Period
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(highest, lowest, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, highest)
self.DrawIndicator(area, lowest)
self.DrawOwnTrades(area)
def _on_process(self, candle, high_value, low_value):
if candle.State != CandleStates.Finished:
return
hv = float(high_value)
lv = float(low_value)
close = float(candle.ClosePrice)
if self._prev_high is None or self._prev_low is None:
self._prev_high = hv
self._prev_low = lv
return
if close > self._prev_high and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif close < self._prev_low and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_high = hv
self._prev_low = lv
def CreateClone(self):
return avalanche_av_strategy()