GitHub で見る
Bulls Bears Eyes戦略
概要
この戦略はBulls PowerとBears Powerインジケーターを使用して、強気と弱気の圧力のバランスを評価します。2つのインジケーターは0から100にスケーリングされた単一のオシレーターに組み合わされます。高い値は買い方の優位性を示し、低い値は売り方の強さを示します。
取引の判断はオリジナルのBullsBearsEyesエキスパートと同様のしきい値レベルに基づいています。オシレーターが買われ過ぎレベルを下から上に抜けると、ロングポジションが開かれ、ショートポジションは閉じられます。逆に、売られ過ぎレベルを下に抜けるとショートエントリーがトリガーされ、既存のロングが閉じられます。しきい値の間の中立的な値は現在のポジションを維持しますが、反対の取引を閉じます。
パラメーター
- Period – Bulls/Bears Powerの平均化期間(デフォルト:13)。
- High Level – ロングシグナルを生成する買われ過ぎしきい値(デフォルト:75)。
- Middle Level – トレンド解釈に使用する参照中間レベル(デフォルト:50)。
- Low Level – ショートシグナルを生成する売られ過ぎしきい値(デフォルト:25)。
- Candle Type – 戦略が処理するローソク足の時間軸(デフォルト:4時間足)。
エントリーとエグジットのルール
- 各ローソク足のBulls PowerとBears Powerを計算し、0から100の間のオシレーター値を導き出す。
- ロングエントリー: オシレーターがHigh Levelを下から上に抜ける。ロングを開く前にショートポジションを閉じる。
- ショートエントリー: オシレーターがLow Levelを上から下に抜ける。ショートを開く前に既存のロングポジションを閉じる。
- ポジション決済: オシレーターが側面を切り替える(中間ゾーンの上/下)と、反対のポジションが閉じられる。
オシレーターは視覚的な分析のためにローソク足とともにプロットされます。
注意事項
- 戦略はインジケーター処理に高レベルの
SubscribeCandlesとBind APIを使用します。
- 保護メカニズムは起動時に
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;
/// <summary>
/// EMA crossover strategy.
/// </summary>
public class BullsBearsEyesStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFast;
private decimal _prevSlow;
private bool _hasPrev;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BullsBearsEyesStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 13)
.SetGreaterThanZero()
.SetDisplay("Fast Period", "Fast EMA period", "Parameters");
_slowPeriod = Param(nameof(SlowPeriod), 26)
.SetGreaterThanZero()
.SetDisplay("Slow Period", "Slow EMA period", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevFast = 0;
_prevSlow = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastPeriod };
var slow = new ExponentialMovingAverage { Length = SlowPeriod };
SubscribeCandles(CandleType)
.Bind(fast, slow, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevFast = fastVal;
_prevSlow = slowVal;
_hasPrev = true;
return;
}
var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevFast = fastVal;
_prevSlow = slowVal;
}
}
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 bulls_bears_eyes_strategy(Strategy):
def __init__(self):
super(bulls_bears_eyes_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 13) .SetDisplay("Fast Period", "Fast EMA period", "Parameters")
self._slow_period = self.Param("SlowPeriod", 26) .SetDisplay("Slow Period", "Slow EMA period", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) .SetDisplay("Candle Type", "Candle type", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(bulls_bears_eyes_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(bulls_bears_eyes_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_period
slow = ExponentialMovingAverage()
slow.Length = self.slow_period
self.SubscribeCandles(self.candle_type).Bind(fast, slow, self.process_candle).Start()
def process_candle(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fv = float(fast_val)
sv = float(slow_val)
if not self._has_prev:
self._prev_fast = fv
self._prev_slow = sv
self._has_prev = True
return
cross_up = self._prev_fast <= self._prev_slow and fv > sv
cross_down = self._prev_fast >= self._prev_slow and fv < sv
if cross_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cross_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_fast = fv
self._prev_slow = sv
def CreateClone(self):
return bulls_bears_eyes_strategy()