在 GitHub 上查看

VWMA 斜率

VWMA 斜率 策略通过观察成交量加权移动平均线(VWMA)的方向来交易。当 VWMA 连续两个柱向上时做多,连续两个柱向下时做空;当斜率反转时,平掉现有仓位。

该方法利用考虑成交量的平均价格来识别趋势,避免在低量波动中产生信号。

细节

  • 入场条件:VWMA 连续两根柱上升(做多)或下降(做空)。
  • 多空方向:双向。
  • 出场条件:VWMA 斜率反转。
  • 止损:支持(默认止损 1%,止盈 2%)。
  • 默认值
    • VwmaPeriod = 12
    • CandleType = TimeSpan.FromHours(4)
  • 筛选
    • 类别:Trend
    • 方向:Both
    • 指标:VWMA
    • 止损:Yes
    • 复杂度:Basic
    • 时间框架:Swing
    • 季节性:No
    • 神经网络:No
    • 背离:No
    • 风险等级:Medium
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>
/// Volume Weighted Moving Average slope strategy.
/// Goes long when VWMA turns up (valley), short when VWMA turns down (peak).
/// </summary>
public class VolumeWeightedMaSlopeStrategy : Strategy
{
	private readonly StrategyParam<int> _vwmaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevVwma1;
	private decimal? _prevVwma2;

	public int VwmaPeriod { get => _vwmaPeriod.Value; set => _vwmaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public VolumeWeightedMaSlopeStrategy()
	{
		_vwmaPeriod = Param(nameof(VwmaPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("VWMA Period", "Period of the Volume Weighted Moving Average", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevVwma1 = null;
		_prevVwma2 = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevVwma1 = null;
		_prevVwma2 = null;

		var vwma = new VolumeWeightedMovingAverage { Length = VwmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(vwma, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, vwma);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal currentVwma)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prevVwma1 is null)
		{
			_prevVwma1 = currentVwma;
			return;
		}

		if (_prevVwma2 is null)
		{
			_prevVwma2 = _prevVwma1;
			_prevVwma1 = currentVwma;
			return;
		}

		// Valley: was falling, now rising -> buy
		if (_prevVwma2 > _prevVwma1 && currentVwma > _prevVwma1 && Position <= 0)
			BuyMarket();
		// Peak: was rising, now falling -> sell
		else if (_prevVwma2 < _prevVwma1 && currentVwma < _prevVwma1 && Position >= 0)
			SellMarket();

		_prevVwma2 = _prevVwma1;
		_prevVwma1 = currentVwma;
	}
}