GitHub で見る

Shuriken Lite戦略

この戦略はオリジナルのShuriken Lite MQLツールの機能を再現します。アカウント上の約定済みトレードを追跡し、magic numbersと呼ばれる数値識別子によってグループ化します。各グループについて戦略は以下を計算します。

  • トレード数
  • 勝ちトレードと負けトレード
  • pips単位の総利益または損失
  • プロフィットファクター

スコア表示が有効な場合、新しいトレードごとに統計がログに記録されます。

パラメーター

  • 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();
		}
	}
}