在 GitHub 上查看

Ma通道策略

Ma通道策略利用由最高价和最低价的移动平均构建的通道来交易。当价格 向上突破通道上轨时开多仓,向下跌破下轨时开空仓;趋势反转时头寸也 会随之翻转。通道边界基于指数移动平均并加上固定偏移量。

系统同时支持做多和做空,并且只在完成的K线收盘后作出反应。 其目标是在趋势初期捕捉变化,同时避免通道内的噪音。

详情

  • 入场条件
    • 多头:价格突破通道上轨。
    • 空头:价格跌破通道下轨。
  • 离场条件
    • 反向突破会反转持仓。
  • 指标:最高价和最低价的指数移动平均,具有可调节的周期和偏移。
  • 止损:默认不使用,只在反向信号出现时平仓。
  • 默认参数
    • Length = 8
    • Offset = 10
    • CandleType = 1小时时间框
  • 过滤器
    • 类型:趋势跟随
    • 方向:双向
    • 指标数量:1
    • 止损:否
    • 复杂度:简单
    • 时间框架:中期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险级别:中等
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>
/// Moving average channel breakout strategy.
/// Buys when price crosses above the upper channel (MA of highs + offset).
/// Sells when price crosses below the lower channel (MA of lows - offset).
/// </summary>
public class MaChannelStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _offset;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _maHigh;
	private ExponentialMovingAverage _maLow;
	private int _trend;

	public int Length { get => _length.Value; set => _length.Value = value; }
	public decimal Offset { get => _offset.Value; set => _offset.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MaChannelStrategy()
	{
		_length = Param(nameof(Length), 8)
			.SetDisplay("Length", "Moving average period", "Parameters")
			.SetOptimize(5, 20, 1);

		_offset = Param(nameof(Offset), 100m)
			.SetDisplay("Offset", "Price offset from the average", "Parameters")
			.SetOptimize(50m, 500m, 50m);

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_maHigh = null;
		_maLow = null;
		_trend = 0;
	}

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

		_trend = 0;
		_maHigh = new ExponentialMovingAverage { Length = Length };
		_maLow = new ExponentialMovingAverage { Length = Length };

		Indicators.Add(_maHigh);
		Indicators.Add(_maLow);

		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 t = candle.ServerTime;

		var highResult = _maHigh.Process(new DecimalIndicatorValue(_maHigh, candle.HighPrice, t) { IsFinal = true });
		var lowResult = _maLow.Process(new DecimalIndicatorValue(_maLow, candle.LowPrice, t) { IsFinal = true });

		if (!_maHigh.IsFormed || !_maLow.IsFormed)
			return;

		var upper = highResult.GetValue<decimal>() + Offset;
		var lower = lowResult.GetValue<decimal>() - Offset;

		var prevTrend = _trend;

		if (candle.HighPrice > upper)
			_trend = 1;
		else if (candle.LowPrice < lower)
			_trend = -1;

		if (prevTrend <= 0 && _trend > 0 && Position <= 0)
			BuyMarket();
		else if (prevTrend >= 0 && _trend < 0 && Position >= 0)
			SellMarket();
	}
}