GitHub で見る
MultiTrader Currency Strength戦略 (3253)
概要
この戦略は、公開MQLパネル「MultiTrader」(コードベース#24786)のStockSharp高レベルポートです。オリジナルのエキスパートアドバイザーは、8つの主要通貨の相対的な強さを表示し、通貨が極端に強くまたは弱くなったときに視覚的/音声アラートをトリガーし、取引するFXペアを提案する裁量ダッシュボードでした。StockSharpバージョンは同じ分析ワークフローを自動化し、オプションで最強対最弱のペアでトレードを実行します。
ロジックは各シンボルのクローズが現在のキャンドル範囲内での百分率位置を計算します。関連するクロスの平均がAUD、CAD、CHF、EUR、GBP、JPY、NZD、USDの強さスコアを生成します。ある通貨が設定可能な買いレベルを超えて上昇し、別の通貨が売りレベルを下回ると、戦略はそれらの通貨から構築されたペアを推奨します。ペアが設定されたユニバースに存在する場合、戦略はその方向に自動的に成行注文を出すことができます。
通貨強度モデル
シンボルのパーセントスコアは次のように計算されます:
percent = 100 * (Close - Low) / (High - Low)
各通貨の強さはMQL実装を反映した7つのクロスから導かれます。通貨がペアの決済通貨として現れる場合、100 - percentの反転が適用されます:
| 通貨 |
コンポーネント |
| AUD |
AUDJPY, AUDNZD, AUDUSD, 100-EURAUD, 100-GBPAUD, AUDCHF, AUDCAD |
| CAD |
CADJPY, 100-NZDCAD, 100-USDCAD, 100-EURCAD, 100-GBPCAD, 100-AUDCAD, CADCHF |
| CHF |
CHFJPY, 100-NZDCHF, 100-USDCHF, 100-EURCHF, 100-GBPCHF, 100-AUDCHF, 100-CADCHF |
| EUR |
EURJPY, EURNZD, EURUSD, EURCAD, EURGBP, EURAUD, EURCHF |
| GBP |
GBPJPY, GBPNZD, GBPUSD, GBPCAD, 100-EURGBP, GBPAUD, GBPCHF |
| JPY |
100-AUDJPY, 100-CHFJPY, 100-CADJPY, 100-EURJPY, 100-GBPJPY, 100-NZDJPY, 100-USDJPY |
| NZD |
NZDJPY, 100-GBPNZD, NZDUSD, NZDCAD, 100-EURNZD, 100-AUDNZD, NZDCHF |
| USD |
100-AUDUSD, USDCHF, USDCAD, 100-EURUSD, 100-GBPUSD, USDJPY, 100-NZDUSD |
戦略はペアごとの最後の完了キャンドルを格納し、最新のパーセントを維持し、各更新後に通貨強度を更新します。
トレードとアラート
- 8つすべての通貨に有効なデータがある場合、戦略はスナップショットをログに記録します(最強から最弱へ)。
- 最強値が**≥ BuyLevelで最弱値が≤ SellLevel**の場合、取引提案が生成されます。
- 戦略は直接ペア(強通貨をベース、弱通貨を決済)を見つけようとします。存在しない場合は逆方向を確認し、最終的にUSDを含むペアにフォールバックします。
- 検出されたペアと方向がログに記録されます。
EnableAutoTradingがtrueでOrderVolumeが正の場合、戦略は提案された方向に成行注文を発行します。反対のポジションは注文サイズを増やすことで自動的にフラット化されます。
シグナルは最後に提案されたペアとサイドを記憶することでスロットルされ、市場が閾値ゾーンを離れるまで重複アラートを防ぎます。
パラメーター
| 名称 |
説明 |
デフォルト |
Universe |
FXペアを表すSecurityオブジェクトのリスト(28主要ペア推奨)。 |
必須 |
CandleType |
計算に使用されるキャンドル仕様(日足、週足、月足など)。 |
日足キャンドル |
BuyLevel |
通貨が買われすぎと見なされる閾値。 |
90 |
SellLevel |
通貨が売られすぎと見なされる閾値。 |
10 |
EnableAutoTrading |
自動注文配置を有効/無効にします。 |
false |
OrderVolume |
オートトレードが有効な場合の成行注文のボリューム。 |
1 |
SymbolPrefix |
ブローカー/取引所が使用するオプションのプレフィックス(例:m.)。 |
"" |
SymbolSuffix |
ブローカー/取引所が使用するオプションのサフィックス(例:.FX)。 |
"" |
設定手順
- ユニバース設定。 戦略ユニバースに28の主要FXクロスを追加します。コードは標準のペア名(例:
EURUSD)と一致する必要があります。ブローカーが装飾を追加する場合はSymbolPrefix/SymbolSuffixを使用します。
- 時間軸の選択。 希望する
CandleTypeを選択します。日足、週足、月足キャンドルはオリジナルパネルモードを再現します。
- 閾値調整。
BuyLevel/SellLevelを調整して、シグナルが生成される前にどれだけ極端な強さが必要かを制御します。
- オートトレード(オプション)。
EnableAutoTradingをtrueに設定し、OrderVolumeを定義します。情報ログのみを受け取るにはフラグをfalseのままにします。
移行ノート
- オリジナルMQLパネルのGUIレイヤー全体は意図的に省略されています。すべての出力は戦略ログを通じて利用可能です。
- アラートは
LogInfoエントリーとして発行されます;プッシュ/メール/デスクトップ通知はポートされていません。
- MQLバージョンの自動ストップロス/ターゲット計算はサポートされていません;トレーダーはStockSharp保護モジュールまたは外部リスク管理を使用してリスクを管理する必要があります。
- MQLスクリプトに組み込まれたDESベースのライセンスヘルパーは削除されました。
推奨される使用方法
- すべての関連ペアのリアルタイムおよび履歴キャンドルを提供するコネクターセッション内に戦略をデプロイします。
- チャートウィジェットと組み合わせて、提案されたペアを視覚化し、基礎となるキャンドルシリーズを監視します。
- StockSharpの
StartProtectionパラメーターまたは個別のリスク戦略を使用してグローバルストップ/ターゲットを実施します。
テストに関する考慮事項
- データソースが選択された時間軸の完了したキャンドルを配信することを確認してください;戦略は未完成のバーを無視します。
- ユニバースからいくつかのペアが欠けている場合、対応する通貨を計算できず、シグナルは生成されません。
- 過去のパフォーマンスを評価する際は、強度のギャップを避けるためにバックテスト全体でユニバースが静的であることを確認してください。
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 MultiTraderStrategy : 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 MultiTraderStrategy()
{
_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;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class multi_trader_strategy(Strategy):
def __init__(self):
super(multi_trader_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast Period", "Fast MA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow MA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def stop_loss_points(self):
return self._stop_loss_points.Value
@property
def take_profit_points(self):
return self._take_profit_points.Value
def OnReseted(self):
super(multi_trader_strategy, self).OnReseted()
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(multi_trader_strategy, self).OnStarted2(time)
self._fast = ExponentialMovingAverage()
self._fast.Length = self.fast_period
self._slow = ExponentialMovingAverage()
self._slow.Length = self.slow_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._fast, self._slow, self._process_candle)
subscription.Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_value)
slow_val = float(slow_value)
if not self._fast.IsFormed or not self._slow.IsFormed:
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = float(candle.ClosePrice)
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if self.Position > 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close <= self._entry_price - self.stop_loss_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close >= self._entry_price + self.take_profit_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
elif self.Position < 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close >= self._entry_price + self.stop_loss_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close <= self._entry_price - self.take_profit_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._prev_fast <= self._prev_slow and fast_val > slow_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast_val < slow_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return multi_trader_strategy()