Labouchere EA戦略
この戦略はストキャスティクスオシレーターのクロスオーバーとLabouchere資金管理シーケンスを組み合わせています。ストキャスティクスインジケーターは%Kが%Dをクロスしたときにシグナルを生成します。Laboucheresystemは各ポジション決済後に取引量を調整します:損失の場合はシーケンスの最初と最後の数字の合計に等しい新しい要素を追加し、利益の場合はそれらの要素を削除します。
取引は確定済みローソク足のみで行われます。すべての数字が削除されたときにシーケンスをオプションで再起動できます。時間フィルターにより特定のイントラデイ時間帯内での取引が可能で、反対シグナルによって既存のポジションを決済できます。固定ストップロスとテイクプロフィットレベル(価格ステップ単位)がサポートされています。
詳細
- エントリー条件:
- ロング: %Kが%Dを上抜け。
- ショート: %Kが%Dを下抜け。
- ロング/ショート: 両方向。
- エグジット条件:
- オプションの反対シグナルによる決済。
- 固定ストップロスとテイクプロフィット(設定した場合)。
- ストップ: あり。
- 資金管理: Laboucheresシーケンス。
- デフォルト値:
LotSequence= "0.01,0.02,0.01,0.02,0.01,0.01,0.01,0.01"NewRecycle= trueStopLoss= 40TakeProfit= 50IsReversed= falseUseOppositeExit= falseUseWorkTime= falseStartTime= 00:00StopTime= 24:00KPeriod= 10DPeriod= 190
- フィルター:
- カテゴリ: 混合
- 方向: 両方
- インジケーター: Stochastic Oscillator
- ストップ: あり
- 複雑さ: 中級
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Labouchere betting system strategy using Stochastic oscillator for signals.
/// Adjusts position sizing based on the Labouchere sequence.
/// </summary>
public class LabouchereEaStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _kPeriod;
private readonly StrategyParam<int> _dPeriod;
private readonly StrategyParam<decimal> _stopLossPct;
private readonly StrategyParam<decimal> _takeProfitPct;
private decimal? _prevK;
private decimal? _prevD;
private decimal _entryPrice;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int KPeriod { get => _kPeriod.Value; set => _kPeriod.Value = value; }
public int DPeriod { get => _dPeriod.Value; set => _dPeriod.Value = value; }
public decimal StopLossPct { get => _stopLossPct.Value; set => _stopLossPct.Value = value; }
public decimal TakeProfitPct { get => _takeProfitPct.Value; set => _takeProfitPct.Value = value; }
public LabouchereEaStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles used", "General");
_kPeriod = Param(nameof(KPeriod), 10)
.SetDisplay("K Period", "Stochastic %K period", "Indicator")
.SetGreaterThanZero();
_dPeriod = Param(nameof(DPeriod), 3)
.SetDisplay("D Period", "Stochastic %D period", "Indicator")
.SetGreaterThanZero();
_stopLossPct = Param(nameof(StopLossPct), 1m)
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk")
.SetGreaterThanZero();
_takeProfitPct = Param(nameof(TakeProfitPct), 1.5m)
.SetDisplay("Take Profit %", "Take profit percent", "Risk")
.SetGreaterThanZero();
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var stoch = new StochasticOscillator
{
K = { Length = KPeriod },
D = { Length = DPeriod }
};
StartProtection(
takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
stopLoss: new Unit(StopLossPct, UnitTypes.Percent));
var subscription = SubscribeCandles(CandleType);
subscription.BindEx(stoch, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue stochValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!stochValue.IsFinal || !stochValue.IsFormed)
return;
var stoch = (IStochasticOscillatorValue)stochValue;
if (stoch.K is not decimal k || stoch.D is not decimal d)
return;
var signal = 0;
if (_prevK.HasValue && _prevD.HasValue)
{
if (_prevK <= _prevD && k > d)
signal = 1;
else if (_prevK >= _prevD && k < d)
signal = -1;
}
_prevK = k;
_prevD = d;
if (signal == 0)
return;
if (signal > 0 && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
_entryPrice = candle.ClosePrice;
}
else if (signal < 0 && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
_entryPrice = candle.ClosePrice;
}
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevK = null;
_prevD = null;
_entryPrice = 0m;
}
}
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, Unit, UnitTypes
from StockSharp.Algo.Indicators import StochasticOscillator
from StockSharp.Algo.Strategies import Strategy
class labouchere_ea_strategy(Strategy):
"""
Labouchere strategy using Stochastic K/D crossover for entry signals.
Uses StartProtection for percentage-based SL/TP.
"""
def __init__(self):
super(labouchere_ea_strategy, self).__init__()
self._k_period = self.Param("KPeriod", 10) \
.SetDisplay("K Period", "Stochastic %K period", "Indicator")
self._d_period = self.Param("DPeriod", 3) \
.SetDisplay("D Period", "Stochastic %D period", "Indicator")
self._stop_loss_pct = self.Param("StopLossPct", 1.0) \
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk")
self._take_profit_pct = self.Param("TakeProfitPct", 1.5) \
.SetDisplay("Take Profit %", "Take profit percent", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles used", "General")
self._prev_k = None
self._prev_d = None
self._entry_price = 0.0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(labouchere_ea_strategy, self).OnReseted()
self._prev_k = None
self._prev_d = None
self._entry_price = 0.0
def OnStarted2(self, time):
super(labouchere_ea_strategy, self).OnStarted2(time)
stoch = StochasticOscillator()
stoch.K.Length = self._k_period.Value
stoch.D.Length = self._d_period.Value
self.StartProtection(
Unit(self._take_profit_pct.Value, UnitTypes.Percent),
Unit(self._stop_loss_pct.Value, UnitTypes.Percent))
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(stoch, self._process_candle).Start()
def _process_candle(self, candle, stoch_value):
if candle.State != CandleStates.Finished:
return
if not stoch_value.IsFormed:
return
k = stoch_value.K
d = stoch_value.D
if k is None or d is None:
return
k = float(k)
d = float(d)
signal = 0
if self._prev_k is not None and self._prev_d is not None:
if self._prev_k <= self._prev_d and k > d:
signal = 1
elif self._prev_k >= self._prev_d and k < d:
signal = -1
self._prev_k = k
self._prev_d = d
if signal == 0:
return
if signal > 0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = float(candle.ClosePrice)
elif signal < 0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = float(candle.ClosePrice)
def CreateClone(self):
return labouchere_ea_strategy()