GitHub で見る

Go ローソク足実体リバーサル戦略

ローソク足実体サイズを平均化するGoインジケーターに基づいた戦略です。平滑化されたローソク足実体がポジティブから0を下回るとロングポジションをオープンし、逆のクロスでショートポジションをオープンします。既存のポジションは逆シグナルでクローズされます。

詳細

  • エントリー条件: 実体SMAの符号変化(正から負へはロング、負から正へはショート)
  • ロング/ショート: 両方
  • エグジット条件: 実体SMAの逆の符号変化
  • ストップ: なし
  • デフォルト値:
    • Period = 174
    • CandleType = 1時間
  • フィルター:
    • カテゴリ: リバーサル
    • 方向: ロング&ショート
    • インジケーター: SMA
    • ストップ: いいえ
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Strategy based on smoothed candle body direction.
/// Smooths (close-open) with SMA, trades on sign changes.
/// </summary>
public class GoCandleBodyReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _bodySma;
	private int _prevSign;

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

	public GoCandleBodyReversalStrategy()
	{
		_period = Param(nameof(Period), 30)
			.SetGreaterThanZero()
			.SetDisplay("Period", "SMA period for candle body", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_bodySma = null;
		_prevSign = 0;
	}

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

		_bodySma = new ExponentialMovingAverage { Length = Period };
		Indicators.Add(_bodySma);

		// Use a warmup EMA bound to close price
		var warmup = new ExponentialMovingAverage { Length = Period };

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

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

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

		var body = candle.ClosePrice - candle.OpenPrice;
		var maResult = _bodySma.Process(new DecimalIndicatorValue(_bodySma, body, candle.OpenTime) { IsFinal = true });

		if (!maResult.IsFormed)
			return;

		var value = maResult.GetValue<decimal>();
		var sign = value > 0 ? 1 : value < 0 ? -1 : 0;

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevSign = sign;
			return;
		}

		if (_prevSign == 0)
		{
			_prevSign = sign;
			return;
		}

		// Body direction turns negative (bearish reversal) -> sell
		if (sign < 0 && _prevSign > 0 && Position >= 0)
			SellMarket();
		// Body direction turns positive (bullish reversal) -> buy
		else if (sign > 0 && _prevSign < 0 && Position <= 0)
			BuyMarket();

		_prevSign = sign;
	}
}