GitHub で見る

即時執行戦略

この戦略は最初の完成したローソク足で即座に単一のポジションに入り、シンプルな利益とリスクのルールで管理します。ポジションの方向はパラメーターで選択できます。取引がオープンされると、アルゴリズムは利益と損失を追跡し、利益を保護するために価格をトレーリングすることができます。

このロジックは、オプションのテイクプロフィット、ストップロス、トレーリングストップ値を使用してマーケット注文の即時執行を可能にした元のMQLスクリプトの動作を再現します。

詳細

  • エントリー条件: 開始後の最初の完成したローソク足でマーケットポジションをオープン。方向はDirectionパラメーターで定義される。
  • ロング/ショート: 両方のサイドに対応。
  • エグジット条件:
    • テイクプロフィットに到達。
    • ストップロスに到達。
    • トレーリングストップが作動し価格がトレーリングレベルに達する。
  • ストップ: テイクプロフィット、ストップロス、トレーリングストップが利用可能。
  • デフォルト値:
    • TakeProfit = 70 価格単位。
    • StopLoss = 0(無効)。
    • TrailingStart = 5 価格単位。
    • TrailingSize = 5 価格単位。
  • フィルター:
    • カテゴリ: ユーティリティ
    • 方向: 両方
    • インジケーター: なし
    • ストップ: はい
    • 複雑さ: シンプル
    • 時間軸: 任意
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Strategy that opens a position based on EMA direction and manages it with
/// take profit, stop loss and trailing stop rules.
/// </summary>
public class InstantExecutionStrategy : Strategy
{
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevEma;

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

	public int EmaPeriod
	{
		get => _emaPeriod.Value;
		set => _emaPeriod.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public InstantExecutionStrategy()
	{
		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "EMA trend filter period", "Indicator");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevEma = null;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ema, (candle, emaValue) =>
		{
			if (candle.State != CandleStates.Finished)
				return;

			if (_prevEma.HasValue)
			{
				var rising = emaValue > _prevEma.Value;
				var falling = emaValue < _prevEma.Value;

				if (rising && candle.ClosePrice > emaValue && Position <= 0)
				{
					if (Position < 0) BuyMarket();
					BuyMarket();
				}
				else if (falling && candle.ClosePrice < emaValue && Position >= 0)
				{
					if (Position > 0) SellMarket();
					SellMarket();
				}
			}

			_prevEma = emaValue;
		}).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			isStopTrailing: true,
			useMarketOrders: true);

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