OCO注文執行戦略
この戦略は、MetaTrader向けに書かれた「One Cancels the Other」注文チケットを再現したものです。トレーダーは最大4つの独立した価格トリガーを定義できます。
- Buy Limit Price
- Sell Limit Price
- Buy Stop Price
- Sell Stop Price
戦略はLevel1データをサブスクライブし、最良の買値と売値を継続的に監視します。トリガー価格に達すると、対応する方向の成行注文を送信します。注文の執行後、pipsで測定した距離を使ってストップロスとテイクプロフィットの保護が適用されます。これらの距離は、インストゥルメントのPriceStepに基づいて自動的に絶対価格に変換されます。
OCOモードが有効な場合、いずれかのトリガーが発動すると、他の全てのトリガーが自動的に無効化され、古典的な「一方が成立したらもう一方はキャンセル」の動作を実装します。OCOモードが無効の場合、他のトリガーはアクティブのまま残り、価格が動き続けると追加ポジションを建てる可能性があります。
詳細
- エントリー条件:
Ask <= BuyLimitPriceのときロング(Buy Limitトリガー)。Ask >= BuyStopPriceのときロング(Buy Stopトリガー)。Bid >= SellLimitPriceのときショート(Sell Limitトリガー)。Bid <= SellStopPriceのときショート(Sell Stopトリガー)。
- ロング/ショート: 両方。
- エグジット条件:
- ポジションは事前定義されたストップロスまたはテイクプロフィットレベルによって自動的に決済される。
- ストップ: はい、pipsでのストップロスとテイクプロフィット。
- デフォルト値:
StopLossPips= 300。TakeProfitPips= 300。OCO Mode= 有効。
- フィルター:
- カテゴリ: 注文執行。
- 方向: 両方。
- インジケーター: なし。
- ストップ: はい。
- 複雑さ: シンプル。
- 時間軸: ティックベース。
- 季節性: いいえ。
- ニューラルネットワーク: いいえ。
- ダイバージェンス: いいえ。
- リスクレベル: 中。
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;
/// <summary>
/// OCO-style breakout strategy. Calculates dynamic buy-stop and sell-stop levels
/// from recent high/low and enters on breakout, with StdDev-based stop-loss and take-profit.
/// </summary>
public class OcoOrderStrategy : Strategy
{
private readonly StrategyParam<int> _lookbackPeriod;
private readonly StrategyParam<decimal> _stdMultiplier;
private readonly StrategyParam<DataType> _candleType;
private decimal _recentHigh;
private decimal _recentLow;
private decimal _entryPrice;
private int _barCount;
public int LookbackPeriod { get => _lookbackPeriod.Value; set => _lookbackPeriod.Value = value; }
public decimal StdMultiplier { get => _stdMultiplier.Value; set => _stdMultiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public OcoOrderStrategy()
{
_lookbackPeriod = Param(nameof(LookbackPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("Lookback", "Bars for high/low calculation", "General");
_stdMultiplier = Param(nameof(StdMultiplier), 1.5m)
.SetDisplay("StdDev Multiplier", "Multiplier for SL/TP distance", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_recentHigh = 0;
_recentLow = decimal.MaxValue;
_entryPrice = 0;
_barCount = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var stdDev = new StandardDeviation { Length = LookbackPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(stdDev, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal stdValue)
{
if (candle.State != CandleStates.Finished)
return;
_barCount++;
// Track rolling high/low
if (_barCount <= LookbackPeriod)
{
if (candle.HighPrice > _recentHigh)
_recentHigh = candle.HighPrice;
if (candle.LowPrice < _recentLow)
_recentLow = candle.LowPrice;
return;
}
if (stdValue <= 0)
return;
var close = candle.ClosePrice;
var slDistance = stdValue * StdMultiplier;
// Exit logic
if (Position > 0)
{
if (close <= _entryPrice - slDistance || close >= _entryPrice + slDistance * 2)
{
SellMarket();
_entryPrice = 0;
}
}
else if (Position < 0)
{
if (close >= _entryPrice + slDistance || close <= _entryPrice - slDistance * 2)
{
BuyMarket();
_entryPrice = 0;
}
}
// Entry: breakout above recent high or below recent low
if (Position == 0)
{
if (close > _recentHigh)
{
BuyMarket();
_entryPrice = close;
}
else if (close < _recentLow)
{
SellMarket();
_entryPrice = close;
}
}
// Update high/low
if (candle.HighPrice > _recentHigh)
_recentHigh = candle.HighPrice;
if (candle.LowPrice < _recentLow)
_recentLow = candle.LowPrice;
}
}
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 StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class oco_order_strategy(Strategy):
def __init__(self):
super(oco_order_strategy, self).__init__()
self._lookback_period = self.Param("LookbackPeriod", 20) \
.SetDisplay("Lookback", "Bars for high/low calculation", "General")
self._std_multiplier = self.Param("StdMultiplier", 1.5) \
.SetDisplay("StdDev Multiplier", "Multiplier for SL/TP distance", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._recent_high = 0.0
self._recent_low = 1e18
self._entry_price = 0.0
self._bar_count = 0
@property
def lookback_period(self):
return self._lookback_period.Value
@property
def std_multiplier(self):
return self._std_multiplier.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(oco_order_strategy, self).OnReseted()
self._recent_high = 0.0
self._recent_low = 1e18
self._entry_price = 0.0
self._bar_count = 0
def OnStarted2(self, time):
super(oco_order_strategy, self).OnStarted2(time)
std_dev = StandardDeviation()
std_dev.Length = self.lookback_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(std_dev, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, std_value):
if candle.State != CandleStates.Finished:
return
self._bar_count += 1
# Track rolling high/low
if self._bar_count <= self.lookback_period:
if candle.HighPrice > self._recent_high:
self._recent_high = candle.HighPrice
if candle.LowPrice < self._recent_low:
self._recent_low = candle.LowPrice
return
if std_value <= 0:
return
close = candle.ClosePrice
sl_distance = std_value * self.std_multiplier
# Exit logic
if self.Position > 0:
if close <= self._entry_price - sl_distance or close >= self._entry_price + sl_distance * 2:
self.SellMarket()
self._entry_price = 0
elif self.Position < 0:
if close >= self._entry_price + sl_distance or close <= self._entry_price - sl_distance * 2:
self.BuyMarket()
self._entry_price = 0
# Entry: breakout above recent high or below recent low
if self.Position == 0:
if close > self._recent_high:
self.BuyMarket()
self._entry_price = close
elif close < self._recent_low:
self.SellMarket()
self._entry_price = close
# Update high/low
if candle.HighPrice > self._recent_high:
self._recent_high = candle.HighPrice
if candle.LowPrice < self._recent_low:
self._recent_low = candle.LowPrice
def CreateClone(self):
return oco_order_strategy()