GitHub で見る

Exp Fishing 戦略

この戦略は、完成したローソク足の終値が始値と少なくとも Price Step だけ異なる場合にポジションに入ります。差が正の場合は買い、負の場合は売りです。

ポジションを開いた後、取引に有利な方向への Price Step の追加移動ごとに、Max Orders に達するまで同じ方向への追加の成行注文がトリガーされます。すべてのポジションに絶対価格距離を使った防護的なストップロスとテイクプロフィットが適用されます。

パラメーター

  • Price Step – ポジションを開くまたは追加するために必要な最小価格移動(絶対単位)。
  • Max Orders – 一方向に許可される成行注文の最大数。
  • Stop Loss – 防護ストップが置かれるエントリー価格からの距離。
  • Take Profit – 利益目標が置かれるエントリー価格からの距離。
  • Candle Type – 計算に使用するローソク足の時間軸(デフォルト1分)。

取引ロジック

  1. 完成したローソク足を待つ。
  2. ポジションが開いていない場合:
    • Close - Open >= Price Step なら買い。
    • Open - Close >= Price Step なら売り。
  3. ポジションが存在する場合:
    • 最後のエントリーから価格が Price Step 進んだら、同じ方向に別の注文を追加。
    • 数が Max Orders に達したら注文の追加を停止。
  4. ストップロスとテイクプロフィットは各注文に対して自動的に管理されます。

この戦略は MQL5 エキスパート「Exp Fishing」から適応されており、シンプルなグリッドスタイルのトレンドフォローアプローチを示しています。

using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Trend-following strategy that adds to position every time price moves by configured step.
/// Based on MQL5 Exp_Fishing expert.
/// </summary>
public class ExpFishingStrategy : Strategy
{
	private readonly StrategyParam<decimal> _priceStep;
	private readonly StrategyParam<int> _maxOrders;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;
	private int _ordersCount;
	private bool _isLong;

	/// <summary>
	/// Initializes a new instance of <see cref="ExpFishingStrategy"/>.
	/// </summary>
	public ExpFishingStrategy()
	{
		_priceStep = Param(nameof(PriceStep), 300m)
			.SetGreaterThanZero()
			.SetDisplay("Price Step", "Minimum price move to enter or add", "Parameters")
			;

		_maxOrders = Param(nameof(MaxOrders), 10)
			.SetGreaterThanZero()
			.SetDisplay("Max Orders", "Maximum number of orders in one direction", "Parameters")
			;

		_stopLoss = Param(nameof(StopLoss), 1000m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss", "Stop loss distance in price units", "Parameters")
			;

		_takeProfit = Param(nameof(TakeProfit), 2000m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit", "Take profit distance in price units", "Parameters")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for analysis", "Parameters");
	}

	/// <summary>
	/// Price move required to open or add position.
	/// </summary>
	public decimal PriceStep
	{
		get => _priceStep.Value;
		set => _priceStep.Value = value;
	}

	/// <summary>
	/// Maximum number of orders in single direction.
	/// </summary>
	public int MaxOrders
	{
		get => _maxOrders.Value;
		set => _maxOrders.Value = value;
	}

	/// <summary>
	/// Stop loss distance in price units.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Take profit distance in price units.
	/// </summary>
	public decimal TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

	/// <summary>
	/// Candle type used for strategy calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0m;
		_ordersCount = 0;
		_isLong = false;
	}

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

		StartProtection(new Unit(TakeProfit, UnitTypes.Absolute), new Unit(StopLoss, UnitTypes.Absolute));

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		var move = candle.ClosePrice - candle.OpenPrice;

		if (Position == 0)
		{
			_ordersCount = 0;

			if (move >= PriceStep)
			{
				BuyMarket();
				_entryPrice = candle.ClosePrice;
				_ordersCount = 1;
				_isLong = true;
			}
			else if (move <= -PriceStep)
			{
				SellMarket();
				_entryPrice = candle.ClosePrice;
				_ordersCount = 1;
				_isLong = false;
			}

			return;
		}

		if (_ordersCount >= MaxOrders)
			return;

		if (_isLong)
		{
			if (candle.ClosePrice - _entryPrice >= PriceStep)
			{
				BuyMarket();
				_entryPrice = candle.ClosePrice;
				_ordersCount++;
			}
		}
		else
		{
			if (_entryPrice - candle.ClosePrice >= PriceStep)
			{
				SellMarket();
				_entryPrice = candle.ClosePrice;
				_ordersCount++;
			}
		}
	}
}