GitHub で見る
ZigZag Climber戦略
概要
fxDreemaで生成されたZigZag Climberエキスパートアドバイザーには、No tradeフィルターに続くBuy nowとSell nowの3ブロックしかありません。端末がオープンポジションなしを検出すると、事前定義のストップロスとテイクプロフィットを持つ成行買い注文を即座に発注し、追加確認なしで対称的な成行売り注文を置きます。両方の取引は同じリスクパラメーターを継承し、ヘッジペアとして共存することを意図しています。
このC#移植版は、選択した時間軸の最初の確定ローソク足を待ち、その後同じ保護距離で買い脚と売り脚を連続送信することで、StockSharp上でその動作を再現します。追加シグナル生成、トレーリングロジック、ポジション管理はありません。元のMQLプロジェクトとまったく同じです。
取引ロジック
- 設定した時間軸の最初のローソク足が完全に形成されるまで待ちます。
- 戦略が取引可能で、まだ注文が置かれていない場合、固定数量で成行買いを送信します。
- MetaTrader風のpip距離(銘柄の
PriceStepで変換)を使って、ロング取引にストップロスとテイクプロフィット注文を付与します。
- 同じ数量で成行売りを即座に送信し、反転した保護水準を付与します。
- 実行の残り期間で追加注文は開きません。
重要: MetaTrader 4はヘッジモードで動作するため、両側を同時に開いたままにできます。StockSharpはブローカーの実行モデルを使用します。ネッティング口座では2つ目の注文が1つ目を相殺し、戦略はフラットで終了します。両脚を維持したい場合は、ヘッジ対応コネクター(例: ヘッジ口座用に設定したMetaTraderゲートウェイ)を使用してください。
パラメーター
| 名前 |
既定値 |
説明 |
Candle Type |
1分 |
一度だけのエントリーシーケンスを起動する時間軸。 |
Trade Volume |
0.01 |
両方の成行注文に適用される固定数量。 |
Stop-Loss (pips) |
99.9 |
MetaTrader pipsでの保護ストップ距離(4/5桁シンボルを自動処理)。 |
Take-Profit (pips) |
100 |
MetaTrader pipsでの目標距離。 |
すべての距離は、SetStopLoss/SetTakeProfitへ渡される前に、銘柄のPriceStepと小数精度を使って価格ポイントへ変換されます。
リスク管理
戦略は組み込みのStartProtection()サービスとヘルパーメソッドSetStopLoss/SetTakeProfitに依存し、各成行注文の直後に保護注文を置きます。トレーリングやブレイクイーブンのロジックはありません。
使用上の注意
- 戦略開始前に、希望する銘柄とポートフォリオを割り当ててください。pip変換が正しく動作するよう、シンボルが
PriceStepとDecimalsを公開していることを確認してください。
- エントリーロジックは一度だけ実行されるため、新しいヘッジサイクルを作る唯一の方法は戦略の再起動です。
- ネッティングシミュレーターでテストすると、実際の動作はMetaTraderと異なります。売り注文が買い注文をほぼ即座に中和します。
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// ZigZag Climber strategy: Highest/Lowest channel breakout.
/// Buys when close >= highest, sells when close <= lowest.
/// </summary>
public class ZigZagClimberStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _channelPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int ChannelPeriod
{
get => _channelPeriod.Value;
set => _channelPeriod.Value = value;
}
public ZigZagClimberStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_channelPeriod = Param(nameof(ChannelPeriod), 12)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Highest/Lowest lookback", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var high = new Highest { Length = ChannelPeriod };
var low = new Lowest { Length = ChannelPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(high, low, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (candle.ClosePrice >= high && Position <= 0)
{
BuyMarket();
}
else if (candle.ClosePrice <= low && Position >= 0)
{
SellMarket();
}
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class zig_zag_climber_strategy(Strategy):
def __init__(self):
super(zig_zag_climber_strategy, self).__init__()
self._channel_period = self.Param("ChannelPeriod", 12) \
.SetDisplay("Channel Period", "Highest/Lowest lookback", "Indicators")
self._highest = None
self._lowest = None
@property
def channel_period(self):
return self._channel_period.Value
def OnReseted(self):
super(zig_zag_climber_strategy, self).OnReseted()
self._highest = None
self._lowest = None
def OnStarted2(self, time):
super(zig_zag_climber_strategy, self).OnStarted2(time)
self._highest = Highest()
self._highest.Length = self.channel_period
self._lowest = Lowest()
self._lowest.Length = self.channel_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._highest, self._lowest, self._process_candle)
subscription.Start()
def _process_candle(self, candle, high_val, low_val):
if candle.State != CandleStates.Finished:
return
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
close = float(candle.ClosePrice)
h = float(high_val)
l = float(low_val)
if close >= h and self.Position <= 0:
self.BuyMarket()
elif close <= l and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return zig_zag_climber_strategy()