在 GitHub 上查看

Shuriken Lite 策略

该策略复现了原始 Shuriken Lite MQL 工具的功能。它监控账户中已成交的交易,并根据数字标识 magic numbers 进行分组。每个分组都会统计:

  • 交易次数
  • 盈亏笔数
  • 点数总收益或亏损
  • 利润因子

当启用显示时,每笔新成交后都会在日志中输出统计信息。

参数

  • Magic Numbers — 以逗号分隔的标识列表,需要与订单评论中的数字相匹配。
  • Show Scores — 是否记录统计信息到日志。

使用方法

  1. 在参数中设置需要的 magic numbers。
  2. 与其他在订单评论中写入数字的策略一起运行。
  3. 在日志中查看汇总绩效指标。
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>
/// Shuriken Lite - fast EMA/RSI scalping strategy.
/// </summary>
public class ShurikenLiteStrategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<DataType> _candleType;

	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ShurikenLiteStrategy()
	{
		_emaLength = Param(nameof(EmaLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("EMA", "EMA period", "Indicators");

		_rsiLength = Param(nameof(RsiLength), 7)
			.SetGreaterThanZero()
			.SetDisplay("RSI", "RSI period", "Indicators");

		_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 OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var rsi = new RelativeStrengthIndex { Length = RsiLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, rsi, ProcessCandle)
			.Start();
	}

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

		var close = candle.ClosePrice;

		// Buy when RSI oversold
		if (rsi < 30 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Sell when RSI overbought
		else if (rsi > 70 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}