在 GitHub 上查看

Renko Scalper 策略

该策略通过比较当前蜡烛的收盘价与前一根蜡烛的收盘价来捕捉短期动量。 如果当前蜡烛收于上一根之上,则开多仓; 如果收于上一根之下,则开空仓。

止损、止盈和可选的跟踪止损由内置保护模块管理。策略可在多空两端运作,仅依赖价格行为。

细节

  • 入场条件
    • 多头Close(t) > Close(t-1)
    • 空头Close(t) < Close(t-1)
  • 多空方向:双向。
  • 出场条件:反向信号或保护性止损。
  • 止损:通过 StartProtection 实现的可选止损、止盈和跟踪止损。
  • 默认值
    • CandleType = 1分钟。
    • StopLossPercent = 1。
    • TakeProfitPercent = 2。
    • IsTrailingStop = true。
  • 过滤器
    • 分类:剥头皮。
    • 方向:双向。
    • 指标:无。
    • 止损:有。
    • 复杂度:简单。
    • 时间框架:短期。
    • 季节性:无。
    • 神经网络:无。
    • 背离:无。
    • 风险水平:高。
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>
/// Renko scalper strategy.
/// Opens long when close moves significantly above previous close, short when below.
/// </summary>
public class RenkoScalperStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _previousClose;
	private bool _hasPrev;

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

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

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

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

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

		var stdev = new StandardDeviation { Length = 20 };
		SubscribeCandles(CandleType).Bind(stdev, ProcessCandle).Start();
	}

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

		var close = candle.ClosePrice;

		if (!_hasPrev)
		{
			_previousClose = close;
			_hasPrev = true;
			return;
		}

		if (stdevVal <= 0) { _previousClose = close; return; }

		var diff = close - _previousClose;

		if (diff > stdevVal && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (diff < -stdevVal && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_previousClose = close;
	}
}