在 GitHub 上查看

Go Candle Body Reversal 策略

该策略基于 Go 指标,对蜡烛实体大小进行平滑。当实体 SMA 从正值转为负值时做多,反向交叉时做空。当前持仓在出现反向信号时平仓。

细节

  • 入场条件: 实体 SMA 符号变化(正→负做多,负→正做空)
  • 多空方向: 双向
  • 出场条件: 实体 SMA 的相反符号变化
  • 止损: 无
  • 默认值:
    • Period = 174
    • CandleType = 1 小时
  • 过滤器:
    • 分类: Reversal
    • 方向: 多空
    • 指标: SMA
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Counter-trend reversal on the sign of the simple moving average of candle bodies.
/// A positive-to-negative sign change buys; a negative-to-positive change sells.
/// </summary>
public class GoCandleBodyReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;
	private readonly Queue<decimal> _bodies = new();
	private decimal _bodySum;
	private int _prevNonzeroSign;

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

	public GoCandleBodyReversalStrategy()
	{
		_period = Param(nameof(Period), 174)
			.SetGreaterThanZero()
			.SetDisplay("Period", "SMA period for candle body", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "Parameters");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_bodies.Clear();
		_bodySum = 0m;
		_prevNonzeroSign = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_bodies.Clear();
		_bodySum = 0m;
		_prevNonzeroSign = 0;
		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 body = candle.ClosePrice - candle.OpenPrice;
		_bodies.Enqueue(body);
		_bodySum += body;
		if (_bodies.Count > Period)
			_bodySum -= _bodies.Dequeue();
		if (_bodies.Count < Period)
			return;

		var sign = Math.Sign(_bodySum); // Dividing by positive Period does not change the SMA sign.
		if (sign == 0)
			return; // Retain the last nonzero sign across exact-zero SMA readings.

		var previous = _prevNonzeroSign;
		_prevNonzeroSign = sign;
		if (previous == 0 || previous == sign || !IsFormedAndOnlineAndAllowTrading())
			return;

		var position = Position;
		if (previous > 0 && sign < 0 && position <= 0m)
		{
			if (position < 0m)
				BuyMarket(-position);
			BuyMarket(Volume);
		}
		else if (previous < 0 && sign > 0 && position >= 0m)
		{
			if (position > 0m)
				SellMarket(position);
			SellMarket(Volume);
		}
	}
}