GitHub で見る

Limits Martin戦略

この戦略は現在の市場価格の上下に対となる指値注文を配置します。各取引では設定可能なステップ距離を使用し、オプションのマーチンゲールポジションサイジングで前の損失を回収します。

パラメーター

  • Step – 市場価格と未決の指値注文の間のpipsでの距離。
  • Stop Loss – 建玉に対するpipsでの保護ストップサイズ。
  • Take Profit – 建玉に対するpipsでの目標利益サイズ。
  • Use Martingale – 損失取引後のボリューム増加を有効にする。
  • Loss Limit – ボリュームをリセットする前の連続損失取引の最大数。
  • Volume – 初期注文ボリューム。
  • Use MegaLot – マーチンゲールが有効な場合、ベースボリュームを追加する代わりにボリュームを倍増する。
  • Candle Type – 処理に使用するローソク足データタイプ。

取引ロジック

  1. 建玉や有効な注文がない場合、戦略は最後のクローズの下にBuy Limit注文を、上にSell Limit注文を指定したStep距離で配置します。
  2. 注文が約定した後、反対の未決注文は残り、一度に1つのアクティブポジションのみ許可されます。
  3. ストップロスまたはテイクプロフィットレベルに達したときにポジションがクローズされます。
  4. 損失取引後、マーチンゲールの設定に従ってポジションボリュームを増やすことができます。

注意事項

  • この戦略はローソク足データ処理にBindアプローチを使用したStockSharpの高レベルAPIを使用しています。
  • コード内のすべてのコメントはリポジトリの規約に従い英語で記述されています。
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>
/// Simple martingale strategy with RSI-based entries.
/// Buys when RSI is oversold, sells when overbought.
/// </summary>
public class LimitsMartinStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _oversold;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<DataType> _candleType;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal Oversold { get => _oversold.Value; set => _oversold.Value = value; }
	public decimal Overbought { get => _overbought.Value; set => _overbought.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LimitsMartinStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Period for RSI", "Indicators");

		_oversold = Param(nameof(Oversold), 30m)
			.SetDisplay("Oversold", "RSI oversold level", "Indicators");

		_overbought = Param(nameof(Overbought), 70m)
			.SetDisplay("Overbought", "RSI overbought level", "Indicators");

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

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

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (rsiValue < Oversold && Position <= 0)
			BuyMarket();
		else if (rsiValue > Overbought && Position >= 0)
			SellMarket();
	}
}