勢力均衡戦略
勢力均衡戦略は、終値と取引レンジを比較することで各ローソク足内の強気と弱気の力を評価します。この値が正の閾値を上抜けたとき、強い買い圧力を示します。
Balance of Power が定義された Threshold を上回ったときにロングポジションを建て、負の閾値を下回ったときに決済します。
詳細
- エントリー条件:
- Balance of Power が
Thresholdを上抜け。
- Balance of Power が
- ロング/ショート: ロングのみ。
- エグジット条件:
- Balance of Power が
-Thresholdを下抜け。
- Balance of Power が
- ストップ: なし。
- デフォルト値:
Threshold= 0.8
- フィルター:
- カテゴリ: モメンタム
- 方向: ロング
- インジケーター: Balance of Power
- ストップ: なし
- 複雑さ: 低
- 時間軸: 任意
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// Balance of Power strategy.
/// Long only: buys when Balance of Power crosses above Threshold and closes the long when it crosses below -Threshold.
/// </summary>
public class BalanceOfPowerStrategy : Strategy
{
private readonly StrategyParam<decimal> _threshold;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevBop;
/// <summary>
/// Balance of Power level whose upward cross opens a long.
/// </summary>
public decimal Threshold
{
get => _threshold.Value;
set => _threshold.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public BalanceOfPowerStrategy()
{
_threshold = Param(nameof(Threshold), 0.8m)
.SetNotNegative()
.SetDisplay("Threshold", "Balance of Power level whose upward cross opens a long", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevBop = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevBop = null;
var bop = new BalanceOfPower();
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(bop, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
DrawIndicator(oscillators, bop);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue bopValue)
{
if (candle.State != CandleStates.Finished)
return;
// A candle without range has no Balance of Power value.
if (!bopValue.IsFormed || bopValue.IsEmpty)
return;
var bop = bopValue.GetValue<decimal>();
var prev = _prevBop;
_prevBop = bop;
if (prev is not decimal prevBop)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (Position == 0 && prevBop <= Threshold && bop > Threshold)
BuyMarket(Volume);
else if (Position > 0 && prevBop >= -Threshold && bop < -Threshold)
SellMarket(Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import BalanceOfPower
from StockSharp.Algo.Strategies import Strategy
class balance_of_power_strategy(Strategy):
"""
Balance of Power strategy.
Long only: buys when Balance of Power crosses above Threshold and closes the long when it crosses below -Threshold.
"""
def __init__(self):
super(balance_of_power_strategy, self).__init__()
self._threshold = self.Param("Threshold", 0.8).SetNotNegative().SetDisplay("Threshold", "Balance of Power level whose upward cross opens a long", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_bop = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(balance_of_power_strategy, self).OnReseted()
self._prev_bop = None
def OnStarted2(self, time):
super(balance_of_power_strategy, self).OnStarted2(time)
self._prev_bop = None
bop = BalanceOfPower()
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(bop, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, bop)
def _process_candle(self, candle, bop_value):
if candle.State != CandleStates.Finished:
return
# A candle without range has no Balance of Power value.
if not bop_value.IsFormed or bop_value.IsEmpty:
return
bop = bop_value.GetValue[Decimal](None)
prev = self._prev_bop
self._prev_bop = bop
if prev is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
threshold = Decimal(self._threshold.Value)
if self.Position == 0 and prev <= threshold and bop > threshold:
self.BuyMarket(self.Volume)
elif self.Position > 0 and prev >= -threshold and bop < -threshold:
self.SellMarket(self.Position)
def CreateClone(self):
return balance_of_power_strategy()