在 GitHub 上查看

VininI Trend 策略

描述

该策略将 MQL5 顾问 Exp_VininI_Trend 转换为 StockSharp 实现。使用商品通道指数 (CCI) 模拟 VininI Trend 振荡器。当振荡器突破上轨或向上转折时开多仓;当振荡器跌破下轨或向下转折时开空仓。策略仅在蜡烛收盘后处理信号。

参数

  • CCI Period – CCI 指标的周期。
  • Upper Level – 触发买入信号的上限。
  • Lower Level – 触发卖出信号的下限。
  • Entry ModesBreakdown 处理突破,Twist 处理方向变化。
  • Candle Type – 计算所用蜡烛的时间框架。

原始来源

基于 MQL/1365/exp_vinini_trend.mq5 的 MQL5 策略改写。

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>
/// Strategy based on the VininI Trend concept using the CCI indicator.
/// Buys when CCI breaks above upper level, sells when CCI breaks below lower level.
/// </summary>
public class VininITrendStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<int> _upLevel;
	private readonly StrategyParam<int> _downLevel;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prev1;
	private decimal? _prev2;

	public int Period { get => _period.Value; set => _period.Value = value; }
	public int UpLevel { get => _upLevel.Value; set => _upLevel.Value = value; }
	public int DownLevel { get => _downLevel.Value; set => _downLevel.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public VininITrendStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("CCI Period", "Period for the CCI indicator", "Parameters");

		_upLevel = Param(nameof(UpLevel), 10)
			.SetDisplay("Upper Level", "Upper threshold to trigger buy", "Parameters");

		_downLevel = Param(nameof(DownLevel), -10)
			.SetDisplay("Lower Level", "Lower threshold to trigger sell", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).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();
		_prev1 = default;
		_prev2 = default;
	}

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

		_prev1 = _prev2 = null;

		var cci = new CommodityChannelIndex { Length = Period };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(cci, ProcessCandle)
			.Start();

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

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

		if (!cciValue.IsFormed)
			return;

		var cci = cciValue.ToDecimal();

		var buySignal = false;
		var sellSignal = false;

		if (_prev1 is not null)
		{
			// Breakdown mode: CCI crosses level
			if (_prev1 <= UpLevel && cci > UpLevel)
				buySignal = true;

			if (_prev1 >= DownLevel && cci < DownLevel)
				sellSignal = true;
		}

		if (buySignal && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (sellSignal && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prev2 = _prev1;
		_prev1 = cci;
	}
}