GitHub で見る

Exp Color PEMA Digit Tm Plus戦略

概要

Exp Color PEMA Digit Tm Plus戦略はMetaTrader 5のエキスパートアドバイザー「Exp_ColorPEMA_Digit_Tm_Plus」の直接ポートです。戦略はオリジナルの五重指数移動平均(PEMA)インジケーターを再構築し、EAに存在するすべての取引許可フラグを再現します。注文はインジケーターが色の変化を確認し、オプションの待機期間(Signal Bar)が経過した後のみ、選択されたローソク足シリーズで実行されます。

StockSharpバージョンはMQL実装に存在したのと同じマネーマネジメントオプション、ストップ/ターゲットコントロール、時間ベースのエグジットを維持します。各設定はUIの設定と最適化をサポートするために StrategyParam<T> を通じて公開されます。

インジケーターロジック

  • インジケーターは設定された PEMA LengthApplied Price を使用して8つの指数移動平均のカスケードに供給します。
  • 最終ラインは要求された Rounding Digits に丸められます。これはオリジナルインジケーターとまったく同じです。
  • 丸められたラインの傾きが3つの状態を生成します:
    • Up(マゼンタ) – 強気の圧力、ロングセットアップの可能性。
    • Flat(グレー) – 中立、アクションなし。
    • Down(ドジャーブルー) – 弱気の圧力、ショートセットアップの可能性。
  • 戦略は Signal Bar がゼロより大きい場合に古いバーを参照できるように、各完成したローソク足のインジケーター状態を保存します。

トレードルール

  1. シグナル検出 – 完成したローソク足で、Signal Bar 本古いインジケーター状態を評価し、前の状態と比較します。
  2. ロングセットアップ – 状態が他の何かから Up に変わると:
    • Allow Long Entries が有効な場合、ロングエントリーをキューに入れます;
    • Allow Short Exits が有効な場合、既存のショートのエグジットをキューに入れます。
  3. ショートセットアップ – 状態が他の何かから Down に変わると:
    • Allow Short Entries が有効な場合、ショートエントリーをキューに入れます;
    • Allow Long Exits が有効な場合、既存のロングのエグジットをキューに入れます。
  4. 実行レイヤー – キューに入れられたアクションは以下の場合のみ実行されます:
    • 戦略がオンラインで取引が許可されている;
    • ソースローソク足に紐付けられたアクティベーションタイムスタンプに達した;そして
    • ポジションサイジングルールが非ゼロのボリュームを許可している。
  5. リスク管理
    • オプションのストップロスとテイクプロフィットのレベルはMetaTraderと同じポイント距離を使用して執行価格から導出されます;
    • Use Time Exit は設定された Holding Minutes の生存期間を超えるポジションを閉じます;
    • それぞれのエグジット許可がアクティブな場合、反対シグナルはエクスポージャーを即座に平らにできます。

パラメーター

名前 説明
Money Management ポジションサイジングルールで使用される基本値。
Money Mode ロットベースのサイジングまたは残高/フリーマージンのパーセンテージモデルを選択。
Stop Loss (points) 価格ポイントでのストップロスへの距離。
Take Profit (points) 価格ポイントでのテイクプロフィットへの距離。
Allowed Deviation 完全性のためにEAから保持されたプレースホルダーパラメーター。
Allow Long Entries / Allow Short Entries 各方向での取引の開始を有効または無効にする。
Allow Long Exits / Allow Short Exits 反対シグナルが現れた時の取引のクローズを有効または無効にする。
Use Time Exit 時間ベースの平らにするロジックをアクティブにします。
Holding Minutes ポジションの最大保有時間(分単位)。
Candle Type 戦略によって処理されるローソク足シリーズ。デフォルトH4。
PEMA Length 五重EMAの8つのEMAステージすべてに使用される長さ。
Applied Price インジケーター計算で使用されるソース価格。
Rounding Digits インジケーター出力を丸めるために使用される小数点以下の桁数。
Signal Bar シグナルを評価する前に待機する完成したバーの数。

使用上のノート

  • 目的のインストゥルメントとローソク足シリーズへのアクセスを提供するStockSharpコネクター内に戦略を配置してください。
  • 複製したいMetaTraderのセットアップに合わせてパラメーターを設定してください。
  • 必要に応じてバックテストまたはライブトレードを実行してください;戦略は完全に閉じたローソク足のみに反応します。

変換ステータス

  • C#バージョン – 実装済み(CS/ExpColorPemaDigitTmPlusStrategy.cs)。
  • Pythonバージョン – 作成せず(指示に従い)。
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 ExpColorPemaDigitTmPlusStrategy : 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 ExpColorPemaDigitTmPlusStrategy()
	{
		_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;
	}
}