Пересечение VWMA
Объёмно-взвешенная скользящая средняя (VWMA) придаёт больший вес ценовым уровням с большим объёмом. Стратегия торгует при пересечении цены и VWMA.
Тестирование показывает среднегодичную доходность около 184%. Стратегию лучше запускать на крипторынке.
Закрытие выше VWMA после нахождения ниже него даёт сигнал на покупку, а пробой вниз порождает короткую сделку. Позиции закрываются при обратном пересечении.
Использование средневзвешенной по объёму помогает убрать шум низкообъёмных периодов.
Подробности
- Критерий входа: цена пересекает VWMA снизу или сверху.
- Длинные/короткие: обе стороны.
- Критерий выхода: обратное пересечение или стоп.
- Стопы: да.
- Значения по умолчанию:
VWMAPeriod= 14CandleType= TimeSpan.FromMinutes(5)
- Фильтры:
- Категория: Тренд
- Направление: Обе стороны
- Индикаторы: VWMA
- Стопы: Да
- Сложность: Базовая
- Таймфрейм: Внутридневной
- Сезонность: Нет
- Нейросети: Нет
- Дивергенция: Нет
- Уровень риска: Средний
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>
/// Trades actual Close crossings of native rolling Close/volume VWMA.
/// The opposite crossing reverses the position; actual-fill percent protection flattens it.
/// </summary>
public class VWMAStrategy : Strategy
{
private readonly StrategyParam<int> _vwmaPeriod;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<decimal> _stopLossPercent;
private decimal? _previousClose;
private decimal _previousVwma;
private Order _pendingOrder;
public int VWMAPeriod { get => _vwmaPeriod.Value; set => _vwmaPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
public VWMAStrategy()
{
_vwmaPeriod = Param(nameof(VWMAPeriod), 14).SetGreaterThanZero()
.SetDisplay("VWMA Period", "Current-inclusive rolling Close/volume mean length", "Indicators")
.SetOptimize(5, 30, 5);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_stopLossPercent = Param(nameof(StopLossPercent), 2m).SetNotNegative()
.SetDisplay("Stop Loss (%)", "Actual-fill percent stop; zero disables it.", "Protection");
OrderRegistering += order => _pendingOrder = order;
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType), (Security, DataType.Level1)];
protected override void OnReseted()
{
base.OnReseted();
ClearSignalState();
}
private void ClearSignalState()
{
_previousClose = null;
_previousVwma = 0m;
_pendingOrder = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
ClearSignalState();
StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
}
var vwma = new VolumeWeightedMovingAverage { Length = VWMAPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.BindEx(vwma, ProcessCandle, false).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, vwma);
DrawOwnTrades(area);
}
}
private void ObserveProtectionQuote(Level1ChangeMessage quote)
{
// Native protection runs before this callback, including between finished candles.
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue value)
{
if (candle.State != CandleStates.Finished)
return;
if (value.IsEmpty || !value.Indicator.IsFormed)
{
// A zero-volume window has no mean and cannot connect crossing history across it.
_previousClose = null;
return;
}
var vwma = value.GetValue<decimal>();
var upwardCross = _previousClose is decimal up && up <= _previousVwma && candle.ClosePrice > vwma;
var downwardCross = _previousClose is decimal down && down >= _previousVwma && candle.ClosePrice < vwma;
// The first formed value only seeds; history continues while an order is pending.
_previousClose = candle.ClosePrice;
_previousVwma = vwma;
if (!IsFormedAndOnlineAndAllowTrading() ||
_pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed))
return;
if (upwardCross && Position <= 0m) BuyMarket(this.ReversalVolume());
else if (downwardCross && Position >= 0m) SellMarket(this.ReversalVolume());
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes, OrderStates, Level1Fields
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Indicators import VolumeWeightedMovingAverage
from StockSharp.Algo.Strategies import Strategy, StrategyHelper
class vwma_strategy(Strategy):
"""
Trades actual Close crossings of native rolling Close/volume VWMA.
The opposite crossing reverses the position; actual-fill percent protection flattens it.
"""
def __init__(self):
super(vwma_strategy, self).__init__()
self._vwma_period = self.Param("VWMAPeriod", 14).SetGreaterThanZero().SetDisplay("VWMA Period", "Current-inclusive rolling Close/volume mean length", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss (%)", "Actual-fill percent stop; zero disables it.", "Protection")
self._previous_close = None
self._previous_vwma = Decimal.Zero
self._pending_order = None
self.OrderRegistering += self._track_pending
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type), (self.Security, DataType.Level1)]
def _track_pending(self, order):
self._pending_order = order
def _clear_signal_state(self):
self._previous_close = None
self._previous_vwma = Decimal.Zero
self._pending_order = None
def OnReseted(self):
super(vwma_strategy, self).OnReseted()
self._clear_signal_state()
def OnStarted2(self, time):
super(vwma_strategy, self).OnStarted2(time)
self._clear_signal_state()
self.StartProtection(Unit(), Unit(Decimal(self._stop_loss_percent.Value), UnitTypes.Percent), useMarketOrders=True, isLocalStop=True)
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_protection_quote).Start()
vwma = VolumeWeightedMovingAverage()
vwma.Length = self._vwma_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(vwma, self._process_candle, False).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, vwma)
self.DrawOwnTrades(area)
def _observe_protection_quote(self, quote):
# Native protection runs before this callback, including between finished candles.
pass
def _process_candle(self, candle, value):
if candle.State != CandleStates.Finished:
return
if value.IsEmpty or not value.Indicator.IsFormed:
# An undefined zero-volume window cannot connect crossing history across it.
self._previous_close = None
return
vwma = value.GetValue[Decimal](None)
upward_cross = self._previous_close is not None and self._previous_close <= self._previous_vwma and candle.ClosePrice > vwma
downward_cross = self._previous_close is not None and self._previous_close >= self._previous_vwma and candle.ClosePrice < vwma
# The first formed value seeds; history continues during pending orders.
self._previous_close = candle.ClosePrice
self._previous_vwma = vwma
if not self.IsFormedAndOnlineAndAllowTrading() or (self._pending_order is not None and self._pending_order.State not in (OrderStates.Done, OrderStates.Failed)):
return
if upward_cross and self.Position <= 0:
self.BuyMarket(StrategyHelper.ReversalVolume(self))
elif downward_cross and self.Position >= 0:
self.SellMarket(StrategyHelper.ReversalVolume(self))
def CreateClone(self):
return vwma_strategy()