在 GitHub 上查看

Unseasonalised ATR and Forecast 策略

概述

该策略分析最近蜡烛的平均交易区间,并通过线性趋势回归预测下一个区间。策略不自动交易,仅输出统计信息供人工决策。

参数

  • SampleSize – 用于计算的最近蜡烛数量。
  • DesiredRange – 用于估计置信区间的目标区间。
  • CandleType – 要分析的蜡烛类型。

指标

  • SimpleMovingAverage – 用于计算平均区间。
  • StandardDeviation – 用于测量区间波动性。
  • 线性回归(自定义) – 预测下一个区间并计算 MAPE。

行为

对于每根完成的蜡烛,策略会:

  1. 计算区间(最高价减最低价)并更新平均值和标准差。
  2. 根据目标区间估计置信区间。
  3. 构建区间的线性趋势并预测下一根蜡烛的区间。
  4. 评估预测的平均绝对百分误差(MAPE)。

结果记录在策略日志中,可在图表上可视化。

注意

  • 该策略仅用于信息展示,不执行任何订单。
  • 区间以价格单位表示,请根据标的调整 DesiredRange 参数。
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>
/// EMA crossover strategy with volatility awareness.
/// </summary>
public class UnseasonalisedAtrForecastStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public UnseasonalisedAtrForecastStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Parameters");
		_slowPeriod = Param(nameof(SlowPeriod), 36)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}