在 GitHub 上查看

Zahorchak Measure 策略

该策略使用多种移动平均线计算加权得分。得分转为正值时买入,转为负值时卖出。

细节

  • 入场条件: 得分上穿零
  • 多空: 双向
  • 出场条件: 相反信号
  • 止损: 无
  • 默认值:
    • Points = 1
    • EmaLength = 10
  • 筛选:
    • 类别: 市场宽度
    • 方向: 双向
    • 指标: SMA, EMA
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中等
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Breadth score based on moving averages.
/// Buys when score turns positive and sells when negative.
/// Uses short, medium, and long SMAs to compute a composite score.
/// </summary>
public class ZahorchakMeasureStrategy : Strategy
{
	private readonly StrategyParam<decimal> _points;
	private readonly StrategyParam<int> _emaSmoothing;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevScore;
	private bool _hasPrev;
	private decimal _emaMeasure;

	public decimal Points { get => _points.Value; set => _points.Value = value; }
	public int EmaSmoothing { get => _emaSmoothing.Value; set => _emaSmoothing.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ZahorchakMeasureStrategy()
	{
		_points = Param(nameof(Points), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Point Value", "Score per condition", "Scoring");

		_emaSmoothing = Param(nameof(EmaSmoothing), 10)
			.SetGreaterThanZero()
			.SetDisplay("EMA Smoothing", "Smoothing length", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevScore = 0;
		_hasPrev = false;
		_emaMeasure = 0;
	}

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

		var smaShort = new SimpleMovingAverage { Length = 25 };
		var smaMedium = new SimpleMovingAverage { Length = 75 };
		var smaLong = new SimpleMovingAverage { Length = 200 };

		_prevScore = 0;
		_hasPrev = false;
		_emaMeasure = 0;

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(smaShort, smaMedium, smaLong, ProcessCandle).Start();

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

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

		// Compute breadth score
		var score = 0m;
		score += candle.ClosePrice > shortVal ? Points : -Points;
		score += candle.ClosePrice > mediumVal ? Points : -Points;
		score += candle.ClosePrice > longVal ? Points : -Points;
		score += shortVal > mediumVal ? Points : -Points;
		score += mediumVal > longVal ? Points : -Points;
		score += shortVal > longVal ? Points : -Points;

		var maxScore = Points * 6m;
		var normalized = maxScore != 0 ? 10m * score / maxScore : 0;

		// EMA smoothing
		if (!_hasPrev)
		{
			_emaMeasure = normalized;
			_prevScore = normalized;
			_hasPrev = true;
			return;
		}

		var k = 2m / (EmaSmoothing + 1);
		_emaMeasure = normalized * k + _emaMeasure * (1 - k);

		var measure = _emaMeasure;

		// Trade on zero-line cross
		if (_prevScore <= 0 && measure > 0 && Position <= 0)
		{
			BuyMarket();
		}
		else if (_prevScore >= 0 && measure < 0 && Position >= 0)
		{
			SellMarket();
		}

		_prevScore = measure;
	}
}