WellMartin 戦略
概要
WellMartin 戦略は、ボリンジャーバンドと平均方向指数(ADX)を組み合わせた平均回帰システムです。トレンド強度が低い状態で価格がボリンジャーバンドの下限を割り込んだときにロングポジションを取り、同条件で上限を超えたときにショートポジションを取ります。価格が反対のバンドに達するか、設定されたテイクプロフィットまたはストップロスレベルに達するとポジションがクローズされます。
パラメーター
- CandleType – 計算に使用するローソク足シリーズ。
- BollingerPeriod – ボリンジャーバンドの期間。
- BollingerWidth – ボリンジャーバンドの標準偏差乗数。
- AdxPeriod – ADXインジケーターの期間。
- AdxLevel – ADXの閾値。ADX値がこのレベルを下回るときのみ取引が行われます。
- Volume – 各エントリーの取引量。
- TakeProfit – 価格単位での利益目標。
- StopLoss – 価格単位での損失限界。
ロジック
- ローソク足データを購読し、ボリンジャーバンドとADXを計算します。
- オープンポジションがない場合:
- 終値が下限バンドを下回り、ADXが閾値を下回っていれば買い。
- 終値が上限バンドを上回り、ADXが閾値を下回っていれば売り。
- 最後に執行された取引サイドを追跡し、同じ方向か取引がない場合のみエントリーを許可します。
- ロングポジション時:
- 価格が上限バンドに触れるか、テイクプロフィットに達するか、ストップロスに達したら退出します。
- ショートポジション時:
- 価格が下限バンドに触れるか、テイクプロフィットに達するか、ストップロスに達したら退出します。
注意事項
この実装では固定の取引量を使用します。元のMQLバージョンでは損失取引後に量を増加させていました。必要に応じてこの動作を後で追加できます。
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>
/// Well Martin mean reversion strategy using Bollinger Bands.
/// Buys at lower band, sells at upper band.
/// </summary>
public class WellMartinStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _bollingerPeriod;
private readonly StrategyParam<decimal> _bollingerWidth;
private readonly StrategyParam<decimal> _takeProfit;
private readonly StrategyParam<decimal> _stopLoss;
private decimal _entryPrice;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int BollingerPeriod { get => _bollingerPeriod.Value; set => _bollingerPeriod.Value = value; }
public decimal BollingerWidth { get => _bollingerWidth.Value; set => _bollingerWidth.Value = value; }
public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
public WellMartinStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_bollingerPeriod = Param(nameof(BollingerPeriod), 84)
.SetDisplay("Bollinger Period", "Bollinger Bands period", "Indicators");
_bollingerWidth = Param(nameof(BollingerWidth), 1.8m)
.SetDisplay("Bollinger Width", "Bollinger Bands width", "Indicators");
_takeProfit = Param(nameof(TakeProfit), 1200m)
.SetDisplay("Take Profit", "Take profit in price units", "Risk");
_stopLoss = Param(nameof(StopLoss), 1400m)
.SetDisplay("Stop Loss", "Stop loss in price units", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bb = new BollingerBands
{
Length = BollingerPeriod,
Width = BollingerWidth
};
SubscribeCandles(CandleType)
.BindEx(bb, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue bbValue)
{
if (candle.State != CandleStates.Finished)
return;
var bb = (IBollingerBandsValue)bbValue;
if (bb.UpBand is not decimal upper || bb.LowBand is not decimal lower)
return;
var close = candle.ClosePrice;
// Exit management
if (Position > 0)
{
var profit = close - _entryPrice;
if (close >= upper || (TakeProfit > 0 && profit >= TakeProfit) || (StopLoss > 0 && -profit >= StopLoss))
{
SellMarket();
return;
}
}
else if (Position < 0)
{
var profit = _entryPrice - close;
if (close <= lower || (TakeProfit > 0 && profit >= TakeProfit) || (StopLoss > 0 && -profit >= StopLoss))
{
BuyMarket();
return;
}
}
if (Position != 0)
return;
// Mean reversion: buy at lower band, sell at upper band
if (close < lower)
{
BuyMarket();
_entryPrice = close;
}
else if (close > upper)
{
SellMarket();
_entryPrice = close;
}
}
}
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 BollingerBands
from StockSharp.Algo.Strategies import Strategy
class well_martin_strategy(Strategy):
def __init__(self):
super(well_martin_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._bollinger_period = self.Param("BollingerPeriod", 84) \
.SetDisplay("Bollinger Period", "Bollinger Bands period", "Indicators")
self._bollinger_width = self.Param("BollingerWidth", 1.8) \
.SetDisplay("Bollinger Width", "Bollinger Bands width", "Indicators")
self._take_profit = self.Param("TakeProfit", 1200.0) \
.SetDisplay("Take Profit", "Take profit in price units", "Risk")
self._stop_loss = self.Param("StopLoss", 1400.0) \
.SetDisplay("Stop Loss", "Stop loss in price units", "Risk")
self._entry_price = 0.0
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def BollingerPeriod(self):
return self._bollinger_period.Value
@BollingerPeriod.setter
def BollingerPeriod(self, value):
self._bollinger_period.Value = value
@property
def BollingerWidth(self):
return self._bollinger_width.Value
@BollingerWidth.setter
def BollingerWidth(self, value):
self._bollinger_width.Value = value
@property
def TakeProfit(self):
return self._take_profit.Value
@TakeProfit.setter
def TakeProfit(self, value):
self._take_profit.Value = value
@property
def StopLoss(self):
return self._stop_loss.Value
@StopLoss.setter
def StopLoss(self, value):
self._stop_loss.Value = value
def OnStarted2(self, time):
super(well_martin_strategy, self).OnStarted2(time)
bb = BollingerBands()
bb.Length = self.BollingerPeriod
bb.Width = self.BollingerWidth
self.SubscribeCandles(self.CandleType) \
.BindEx(bb, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, bb_value):
if candle.State != CandleStates.Finished:
return
upper_raw = bb_value.UpBand
lower_raw = bb_value.LowBand
if upper_raw is None or lower_raw is None:
return
upper = float(upper_raw)
lower = float(lower_raw)
close = float(candle.ClosePrice)
tp = float(self.TakeProfit)
sl = float(self.StopLoss)
if self.Position > 0:
profit = close - self._entry_price
if close >= upper or (tp > 0 and profit >= tp) or (sl > 0 and -profit >= sl):
self.SellMarket()
return
elif self.Position < 0:
profit = self._entry_price - close
if close <= lower or (tp > 0 and profit >= tp) or (sl > 0 and -profit >= sl):
self.BuyMarket()
return
if self.Position != 0:
return
if close < lower:
self.BuyMarket()
self._entry_price = close
elif close > upper:
self.SellMarket()
self._entry_price = close
def OnReseted(self):
super(well_martin_strategy, self).OnReseted()
self._entry_price = 0.0
def CreateClone(self):
return well_martin_strategy()