GitHub で見る

Arrows & Curves 戦略

概要

この戦略はMQL5エキスパートアドバイザー Exp_Arrows_Curves を変換したものです。 最近の高値と安値を使用して動的な価格チャンネルを構築し、 ブレイクアウトに反応します。ユーザーの権限とトレンドの方向に応じて ポジションを開いたり閉じたりすることができます。

戦略ロジック

  • 設定された期間の最高値と最安値を計算する。
  • 外部チャンネルラインを形成するために、範囲をパーセンテージで拡張する。
  • 追加のパーセンテージを使用して内部ストップラインを作成する。
  • 価格が上部チャンネルを上抜けした場合はロング、 下部チャンネルを下抜けした場合はショートに入る。
  • 内部ストップラインはチャンネルの反対側が交差したときにポジションの決済を促す。

パラメーター

  • SspPeriod – 高値・安値のルックバック期間。
  • Channel – メインチャンネルラインの拡張パーセンテージ。
  • StopChannel – 内部ストップライン用の追加パーセンテージ。
  • CandleType – ローソク足の時間軸。
  • BuyPosOpen / SellPosOpen – ロング/ショートポジションの開設を許可する。
  • BuyPosClose / SellPosClose – ロング/ショートポジションの決済を許可する。

インジケーター

  • Highest
  • Lowest

注意事項

この戦略は完成したローソク足でのみ動作します。ストップロスとテイクプロフィットの 管理は含まれておらず、決済はチャンネルのクロスによって行われます。

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>
/// Channel breakout strategy using Highest/Lowest midpoint crossing.
/// </summary>
public class ArrowsCurvesStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _hasPrev;

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

	public ArrowsCurvesStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Channel period", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;
	}

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

		var highest = new Highest { Length = Period };
		var lowest = new Lowest { Length = Period };

		SubscribeCandles(CandleType)
			.Bind(highest, lowest, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevHigh = high;
			_prevLow = low;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		if (close > _prevHigh && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (close < _prevLow && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevHigh = high;
		_prevLow = low;
	}
}