在 GitHub 上查看

最佳定投策略

该策略通过在用户设定的起止日期之间以固定金额定期买入来累积持仓。 每个周期在所选时间框架的收盘价执行买入,不考虑价格波动,实现 经典的美元成本平均法。

详情

  • 入场条件
    • 在起止日期之间的每个周期(每日、每周或每月)按收盘价以设定金额买入。
  • 多空方向:仅做多。
  • 出场条件
    • 持仓不平仓,无自动退出逻辑。
  • 止损:无。
  • 默认值
    • 每周期金额 = 100。
    • 周期 = 每周。
    • 起始日期 = 2018-01-01,结束日期 = 2020-01-28。
  • 过滤器
    • 分类:定投累积。
    • 方向:多头。
    • 指标:无。
    • 止损:无。
    • 复杂度:低。
    • 时间框架:任意。
    • 季节性:无。
    • 神经网络:无。
    • 背离:无。
    • 风险等级:低。
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>
/// Dollar Cost Average strategy — accumulates position at regular intervals,
/// sells on RSI overbought with forced sell after max accumulation period.
/// </summary>
public class BestDollarCostAverageStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _buyIntervalBars;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _rsiSellLevel;
	private readonly StrategyParam<int> _maxAccumulationBars;

	private RelativeStrengthIndex _rsi;
	private int _barsSinceLastBuy;
	private int _totalBarsInPosition;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int BuyIntervalBars { get => _buyIntervalBars.Value; set => _buyIntervalBars.Value = value; }
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal RsiSellLevel { get => _rsiSellLevel.Value; set => _rsiSellLevel.Value = value; }
	public int MaxAccumulationBars { get => _maxAccumulationBars.Value; set => _maxAccumulationBars.Value = value; }

	public BestDollarCostAverageStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_buyIntervalBars = Param(nameof(BuyIntervalBars), 350)
			.SetGreaterThanZero()
			.SetDisplay("Buy Interval", "Bars between DCA buys", "DCA");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI period for sell signal", "Indicators");

		_rsiSellLevel = Param(nameof(RsiSellLevel), 60m)
			.SetDisplay("RSI Sell Level", "RSI level to trigger sell", "Indicators");

		_maxAccumulationBars = Param(nameof(MaxAccumulationBars), 1200)
			.SetGreaterThanZero()
			.SetDisplay("Max Accumulation", "Max bars before forced sell", "DCA");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_barsSinceLastBuy = 0;
		_totalBarsInPosition = 0;
	}

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

		_rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_rsi, ProcessCandle)
			.Start();

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

		var rsiArea = CreateChartArea();
		if (rsiArea != null)
		{
			DrawIndicator(rsiArea, _rsi);
		}
	}

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

		if (!_rsi.IsFormed)
			return;

		_barsSinceLastBuy++;

		if (Position > 0)
			_totalBarsInPosition++;

		// Forced sell after max accumulation period
		if (Position > 0 && _totalBarsInPosition >= MaxAccumulationBars)
		{
			SellMarket();
			_totalBarsInPosition = 0;
			_barsSinceLastBuy = 0;
			return;
		}

		// Sell accumulated position when RSI is overbought
		if (Position > 0 && rsiValue >= RsiSellLevel && _totalBarsInPosition >= BuyIntervalBars)
		{
			SellMarket();
			_totalBarsInPosition = 0;
			_barsSinceLastBuy = 0;
			return;
		}

		// DCA buy at regular intervals
		if (_barsSinceLastBuy >= BuyIntervalBars)
		{
			BuyMarket();
			_barsSinceLastBuy = 0;
			if (_totalBarsInPosition == 0)
				_totalBarsInPosition = 1;
		}
	}
}