在 GitHub 上查看

BTCUSD Adjustable SLTP Strategy

该策略在 BTCUSD 上使用 SMA(10) 与 SMA(25) 的交叉,并配合 EMA(150) 过滤。多头在均线金叉后等待回撤:记录最高价并计算回撤水平,当价格再次上破该水平时入场。空头在均线死叉且价格低于 EMA(150) 时立即入场。

退出使用可调的止盈、止损和保本距离。当价格低于 EMA(150) 时,若 SMA(10) 下穿 SMA(25),多头仓位也将被关闭。

细节

  • 入场条件
    • 多头:SMA(10) 上穿 SMA(25),价格回撤指定百分比后再次上破回撤水平。
    • 空头:SMA(10) 下穿 SMA(25),且价格低于 EMA(150)。
  • 多空:做多和做空。
  • 出场条件
    • 可调止盈、止损与保本距离。
    • 当 SMA(10) 下穿 SMA(25) 且价格低于 EMA(150) 时平多。
  • 止损:是,按点数设置。
  • 默认值
    • FastSmaLength = 10
    • SlowSmaLength = 25
    • EmaFilterLength = 150
    • TakeProfitDistance = 1000
    • StopLossDistance = 250
    • BreakEvenTrigger = 500
    • RetracementPercentage = 0.01
  • 筛选
    • 类别:趋势跟随
    • 方向:多空
    • 指标:SMA, EMA
    • 止损:是
    • 复杂度:中
    • 时间框架:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中
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>
/// BTCUSD strategy with adjustable SL/TP using SMA crossover.
/// Enters long on golden cross above EMA filter, short on death cross below EMA filter.
/// </summary>
public class BtcusdAdjustableSltpStrategy : Strategy
{
	private readonly StrategyParam<int> _fastSmaLength;
	private readonly StrategyParam<int> _slowSmaLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;

	public int FastSmaLength { get => _fastSmaLength.Value; set => _fastSmaLength.Value = value; }
	public int SlowSmaLength { get => _slowSmaLength.Value; set => _slowSmaLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BtcusdAdjustableSltpStrategy()
	{
		_fastSmaLength = Param(nameof(FastSmaLength), 120)
			.SetGreaterThanZero()
			.SetDisplay("Fast SMA", "Length of fast SMA", "Indicators");

		_slowSmaLength = Param(nameof(SlowSmaLength), 450)
			.SetGreaterThanZero()
			.SetDisplay("Slow SMA", "Length of slow SMA", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0m;
		_prevSlow = 0m;
	}

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

		var fastSma = new SimpleMovingAverage { Length = FastSmaLength };
		var slowSma = new SimpleMovingAverage { Length = SlowSmaLength };

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

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

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

		if (_prevFast == 0m || _prevSlow == 0m)
		{
			_prevFast = fast;
			_prevSlow = slow;
			return;
		}

		if (_prevFast <= _prevSlow && fast > slow && Position <= 0)
		{
			BuyMarket();
		}
		else if (_prevFast >= _prevSlow && fast < slow && Position >= 0)
		{
			SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}