GitHub で見る

Geedo戦略

特定の時間帯に2本の過去のバーの始値を比較する時間ベースの戦略です。古いバーが最近のバーより閾値分高い場合、ショート取引が開始されます。最近のバーが古いバーより高い場合、ロング取引が開始されます。各ポジションは固定のストップロスとテイクプロフィットを使用し、最大保有時間後にクローズされます。

詳細

  • エントリー条件: TradeTimeT1T2 本前のバーの始値を比較する。Open[T1] - Open[T2]DeltaShort を超えた場合は売り;Open[T2] - Open[T1]DeltaLong を超えた場合は買い。
  • ロング/ショート: 両方向。
  • エグジット条件: ストップロス、テイクプロフィット、またはエントリーから MaxOpenTime 時間後。
  • ストップ: ポイント単位の固定ストップロスとテイクプロフィット。
  • デフォルト値:
    • TakeProfitLong = 39
    • StopLossLong = 147
    • TakeProfitShort = 15
    • StopLossShort = 6000
    • TradeTime = 18
    • T1 = 6
    • T2 = 2
    • DeltaLong = 6
    • DeltaShort = 21
    • Volume = 0.01
    • MaxOpenTime = 504
  • フィルター:
    • カテゴリ: 時間ベース
    • 方向: 両方
    • インジケーター: なし
    • ストップ: 固定
    • 複雑さ: 初心者
    • 時間軸: イントラデイ (1h)
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Time-based strategy comparing open prices with ATR-based stop/take.
/// </summary>
public class GeedoStrategy : Strategy
{
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private readonly List<decimal> _openHistory = new();
	private decimal _entryPrice;

	public int Lookback { get => _lookback.Value; set => _lookback.Value = value; }
	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public GeedoStrategy()
	{
		_lookback = Param(nameof(Lookback), 6)
			.SetGreaterThanZero()
			.SetDisplay("Lookback", "Open price lookback bars", "Indicators");
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "ATR period for stops", "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 OnReseted()
	{
		base.OnReseted();
		_openHistory.Clear();
		_entryPrice = 0;
	}

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

		var atr = new StandardDeviation { Length = AtrPeriod };
		SubscribeCandles(CandleType).Bind(atr, ProcessCandle).Start();
	}

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

		_openHistory.Add(candle.OpenPrice);
		if (_openHistory.Count > Lookback + 1)
			_openHistory.RemoveAt(0);

		if (_openHistory.Count <= Lookback) return;
		if (atrVal <= 0) return;

		var close = candle.ClosePrice;

		// Exit check
		if (Position > 0 && _entryPrice > 0)
		{
			if (close <= _entryPrice - atrVal * 2 || close >= _entryPrice + atrVal * 1.5m)
			{
				SellMarket();
				_entryPrice = 0;
				return;
			}
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (close >= _entryPrice + atrVal * 2 || close <= _entryPrice - atrVal * 1.5m)
			{
				BuyMarket();
				_entryPrice = 0;
				return;
			}
		}

		var pastOpen = _openHistory[0];
		var currentOpen = _openHistory[^1];
		var diff = currentOpen - pastOpen;

		// Price rising => long
		if (diff > atrVal * 0.5m && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
			_entryPrice = close;
		}
		// Price falling => short
		else if (diff < -atrVal * 0.5m && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
			_entryPrice = close;
		}
	}
}