VLT Trader 策略
概述
VLT Trader 是从原始 MQL 版本移植到 StockSharp 的波动率收缩突破策略。策略监控最近蜡烛的波动范围,当最新 完成的蜡烛成为指定窗口内的最小范围时,在区间两侧放置突破性止损单,以捕捉盘整后的强势行情。
交易逻辑
- 新蜡烛过滤:每根新蜡烛仅检查一次条件。如果当前蜡烛的开盘价高于上一根蜡烛的最高价,则忽略信号, 以避免跳空直接触发挂单。
- 波动率筛选:计算上一根已完成蜡烛的振幅,并与最近
CandleCount根蜡烛(且振幅小于MaxCandleSizePips) 中的最小振幅比较。如果最新振幅更小,则认为出现有效的收缩。 - 下单逻辑:当条件满足时,策略放置两个挂单:
- 当净头寸非多头时,在上一根高点上方
10个点设置 买入止损单。 - 当净头寸非空头时,在上一根低点下方
10个点设置 卖出止损单。 在放置新的挂单之前,会取消同方向的旧挂单以避免重复。
- 当净头寸非多头时,在上一根高点上方
- 风险控制:挂单成交后,自动附加止盈和止损单,距离分别为
TakeProfitPips与StopLossPips。当仓位归零时, 这些保护性订单会自动撤销。
参数
| 参数 | 说明 |
|---|---|
Volume |
每次挂单使用的下单量。 |
TakeProfitPips |
成交后止盈单与入场价之间的点数距离。 |
StopLossPips |
成交后止损单与入场价之间的点数距离。 |
MaxCandleSizePips |
参与比较的历史蜡烛最大允许振幅。 |
CandleCount |
用于寻找最小振幅的历史蜡烛数量。 |
CandleType |
进行分析的蜡烛时间框架。 |
实现细节
- 点值依据合约的最小价格步长计算。当价格步长小于或等于
0.001时,会乘以10,以符合 MetaTrader 对 3 或 5 位小数品种的点值定义。 - 蜡烛振幅存储在长度为
CandleCount的先进先出队列中,完整复现原专家顾问的历史扫描流程。 - 所有订单均通过 StockSharp 的高级 API 创建,并在条件失效或仓位平仓时自动取消。
- 代码中的注释为英文,而三种语言的 README 提供了完整的文字说明。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Volatility contraction breakout strategy converted from the VLT_TRADER MQL version.
/// Enters when the latest candle range is the smallest within recent history and
/// price breaks above/below the previous candle high/low.
/// </summary>
public class VltTraderFilterStrategy : Strategy
{
private readonly StrategyParam<int> _candleCount;
private readonly StrategyParam<decimal> _takeProfitMultiplier;
private readonly StrategyParam<decimal> _stopLossMultiplier;
private readonly StrategyParam<DataType> _candleType;
private readonly List<decimal> _rangeHistory = new();
private decimal? _prevHigh;
private decimal? _prevLow;
private decimal? _prevRange;
private decimal _entryPrice;
private bool _isLong;
/// <summary>
/// Number of historical candles used for the volatility filter.
/// </summary>
public int CandleCount
{
get => _candleCount.Value;
set => _candleCount.Value = value;
}
/// <summary>
/// Take profit as a multiplier of the narrow range candle.
/// </summary>
public decimal TakeProfitMultiplier
{
get => _takeProfitMultiplier.Value;
set => _takeProfitMultiplier.Value = value;
}
/// <summary>
/// Stop loss as a multiplier of the narrow range candle.
/// </summary>
public decimal StopLossMultiplier
{
get => _stopLossMultiplier.Value;
set => _stopLossMultiplier.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="VltTraderFilterStrategy"/> class.
/// </summary>
public VltTraderFilterStrategy()
{
_candleCount = Param(nameof(CandleCount), 6)
.SetGreaterThanZero()
.SetDisplay("Candle Count", "Number of historical candles used for the volatility filter", "Signals")
.SetOptimize(3, 15, 1);
_takeProfitMultiplier = Param(nameof(TakeProfitMultiplier), 3m)
.SetGreaterThanZero()
.SetDisplay("TP Multiplier", "Take profit as multiplier of narrow range", "Risk")
.SetOptimize(1m, 5m, 0.5m);
_stopLossMultiplier = Param(nameof(StopLossMultiplier), 1.5m)
.SetGreaterThanZero()
.SetDisplay("SL Multiplier", "Stop loss as multiplier of narrow range", "Risk")
.SetOptimize(0.5m, 3m, 0.5m);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Time frame used to build signal candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_rangeHistory.Clear();
_prevHigh = null;
_prevLow = null;
_prevRange = null;
_entryPrice = 0m;
_isLong = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var high = candle.HighPrice;
var low = candle.LowPrice;
var close = candle.ClosePrice;
var range = high - low;
// no indicators bound via .Bind()
// Check exit conditions for existing position
if (Position != 0 && _entryPrice != 0 && _prevRange is decimal narrowRange && narrowRange > 0)
{
var tp = narrowRange * TakeProfitMultiplier;
var sl = narrowRange * StopLossMultiplier;
if (_isLong && Position > 0)
{
if (close >= _entryPrice + tp || close <= _entryPrice - sl)
{
SellMarket();
UpdateHistory(range, high, low);
return;
}
}
else if (!_isLong && Position < 0)
{
if (close <= _entryPrice - tp || close >= _entryPrice + sl)
{
BuyMarket();
UpdateHistory(range, high, low);
return;
}
}
}
// Check entry conditions only when flat
if (Position == 0 && _prevHigh.HasValue && _prevLow.HasValue && _prevRange.HasValue)
{
var prevH = _prevHigh.Value;
var prevL = _prevLow.Value;
var prevR = _prevRange.Value;
if (prevR > 0 && _rangeHistory.Count >= CandleCount)
{
// Check if previous candle range was the narrowest
var isNarrowest = true;
foreach (var histRange in _rangeHistory)
{
if (histRange > 0 && histRange <= prevR)
{
isNarrowest = false;
break;
}
}
if (isNarrowest)
{
// Breakout detection on current candle
if (close > prevH)
{
var volume = Volume;
if (volume > 0)
{
BuyMarket();
_entryPrice = close;
_isLong = true;
}
}
else if (close < prevL)
{
var volume = Volume;
if (volume > 0)
{
SellMarket();
_entryPrice = close;
_isLong = false;
}
}
}
}
}
UpdateHistory(range, high, low);
}
private void UpdateHistory(decimal range, decimal high, decimal low)
{
if (_prevRange.HasValue)
{
_rangeHistory.Add(_prevRange.Value);
while (_rangeHistory.Count > CandleCount)
try { _rangeHistory.RemoveAt(0); } catch { break; }
}
_prevRange = range;
_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.Strategies import Strategy
class vlt_trader_filter_strategy(Strategy):
"""Volatility contraction breakout strategy: enters when narrowest range breaks previous candle high/low."""
def __init__(self):
super(vlt_trader_filter_strategy, self).__init__()
self._candle_count = self.Param("CandleCount", 6) \
.SetGreaterThanZero() \
.SetDisplay("Candle Count", "Number of historical candles for volatility filter", "Signals")
self._tp_mult = self.Param("TakeProfitMultiplier", 3.0) \
.SetGreaterThanZero() \
.SetDisplay("TP Multiplier", "Take profit as multiplier of narrow range", "Risk")
self._sl_mult = self.Param("StopLossMultiplier", 1.5) \
.SetGreaterThanZero() \
.SetDisplay("SL Multiplier", "Stop loss as multiplier of narrow range", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Time frame used for signal candles", "General")
self._range_history = []
self._prev_high = None
self._prev_low = None
self._prev_range = None
self._entry_price = 0.0
self._is_long = False
@property
def CandleCount(self):
return self._candle_count.Value
@property
def TakeProfitMultiplier(self):
return self._tp_mult.Value
@property
def StopLossMultiplier(self):
return self._sl_mult.Value
@property
def CandleType(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(vlt_trader_filter_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
high = float(candle.HighPrice)
low = float(candle.LowPrice)
close = float(candle.ClosePrice)
rng = high - low
# Check exit conditions
if self.Position != 0 and self._entry_price != 0 and self._prev_range is not None and self._prev_range > 0:
tp = self._prev_range * float(self.TakeProfitMultiplier)
sl = self._prev_range * float(self.StopLossMultiplier)
if self._is_long and self.Position > 0:
if close >= self._entry_price + tp or close <= self._entry_price - sl:
self.SellMarket()
self._update_history(rng, high, low)
return
elif not self._is_long and self.Position < 0:
if close <= self._entry_price - tp or close >= self._entry_price + sl:
self.BuyMarket()
self._update_history(rng, high, low)
return
# Check entry conditions when flat
if self.Position == 0 and self._prev_high is not None and self._prev_low is not None and self._prev_range is not None:
prev_h = self._prev_high
prev_l = self._prev_low
prev_r = self._prev_range
if prev_r > 0 and len(self._range_history) >= self.CandleCount:
is_narrowest = True
for hist_range in self._range_history:
if hist_range > 0 and hist_range <= prev_r:
is_narrowest = False
break
if is_narrowest:
if close > prev_h:
self.BuyMarket()
self._entry_price = close
self._is_long = True
elif close < prev_l:
self.SellMarket()
self._entry_price = close
self._is_long = False
self._update_history(rng, high, low)
def _update_history(self, rng, high, low):
if self._prev_range is not None:
self._range_history.append(self._prev_range)
while len(self._range_history) > self.CandleCount:
self._range_history.pop(0)
self._prev_range = rng
self._prev_high = high
self._prev_low = low
def OnReseted(self):
super(vlt_trader_filter_strategy, self).OnReseted()
self._range_history = []
self._prev_high = None
self._prev_low = None
self._prev_range = None
self._entry_price = 0.0
self._is_long = False
def CreateClone(self):
return vlt_trader_filter_strategy()