GitHub で見る

Awesome Osc Trader戦略

この戦略は、Bollinger Band の幅、stochastic フィルター、正規化された Awesome Oscillator momentum チェックを組み合わせ、MetaTrader エキスパート "Awesome Osc Trader" を再現します。ロング取引は、オシレーターが負の極値から上昇し、stochastic が売られ過ぎ領域を離れ、市場ボラティリティが設定可能なバンド幅内に留まると開かれます。ショートには反転条件が必要です。設定可能な取引窓は新規注文を特定の時間に限定し、オープンポジションは含み益が選択したフィルターに一致する場合にのみ反対シグナルで強制決済できます。

詳細

  • エントリー条件:
    • pips に変換された Bollinger Band スプレッドは、BollingerSpreadLowerLimitBollingerSpreadUpperLimit の間に留まる必要があります。
    • stochastic メインラインは、ロングでは StochLower より上、ショートでは StochUpper より下です。
    • 正規化された Awesome Oscillator は、ゼロの反対側で少なくとも 4 本連続バーを示し、AoStrengthLimit を超える強さでゼロへ戻り始めています。
    • 現在時刻は EntryHourOpenHours で定義される取引窓内です。
  • ロング/ショート: 両方向を取引します。
  • エグジット条件:
    • 反対シグナルが現れたとき、またはオシレーターがゼロをクロスしたときの任意の早期エグジット。CloseTradeProfitTypeClTrd で制御されます。
    • pips で指定される保護 stop-loss、take-profit、trailing stop 距離。
  • ストップ: StartProtection で管理される固定ストップ、take-profit、任意の trailing stop。
  • デフォルト値:
    • BollingerPeriod = 20, BollingerSigma = 2
    • BollingerSpreadLowerLimit = 55, BollingerSpreadUpperLimit = 380
    • PeriodFast = 3, PeriodSlow = 32
    • AoStrengthLimit = 0.13
    • StochK = 8, StochD = 3, StochSlow = 3
    • StochLower = 18, StochUpper = 76
    • EntryHour = 0, OpenHours = 16
    • Lots = 0.01, TakeProfit = 200, StopLoss = 80, TrailingStop = 40
    • CloseTrade = true, ProfitTypeClTrd = 1 (利益のあるポジションのみ閉じる)
  • フィルター:
    • カテゴリ: ボラティリティフィルター付きMomentum
    • 方向: ロングとショート
    • インジケーター: Bollinger Bands, Stochastic Oscillator, Awesome Oscillator
    • ストップ: はい (固定とtrailing)
    • 複雑さ: 中
    • 時間軸: H1 向けに設計されていますが、任意のローソク足系列で動作します
    • 季節性: 取引時間窓
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中程度
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;

public class AwesomeOscTraderStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public AwesomeOscTraderStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}