PZ リバーサル・トレンドフォロー
この戦略は長期的な高値と安値のブレイクアウトに従います。ルックバック期間の最高値を終値が超えたときに買い、最安値を終値が下回ったときに空売りします。逆のシグナルでは常にポジションが反転するため、戦略は常に市場に参加し続けます。
このアプローチは大きなブレイクアウト後にエントリーすることで持続的なトレンドを捉えようとします。システムは主要な極値でのみ取引するため、軽微なノイズを回避できる場合がありますが、もみ合い相場では大きなドローダウンが生じる可能性があります。
詳細
- エントリー条件: 前
Period本のバーの高値/安値のブレイクアウト。 - ロング/ショート: 両方向、常に市場にいる。
- エグジット条件: 逆のブレイクアウトシグナル。
- ストップ: いいえ
- デフォルト値:
Period= 100Volume= 1mCandleType= TimeSpan.FromDays(1)
- フィルター:
- カテゴリ: トレンド
- 方向: 両方
- インジケーター: Highest, Lowest
- ストップ: いいえ
- 複雑さ: 基本
- 時間軸: 日足
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// Breakout strategy using Highest/Lowest channels.
/// </summary>
public class PzReversalTrendFollowingStrategy : Strategy
{
private readonly StrategyParam<int> _period;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevHighest;
private decimal _prevLowest;
private bool _hasPrev;
public int Period { get => _period.Value; set => _period.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public PzReversalTrendFollowingStrategy()
{
_period = Param(nameof(Period), 30)
.SetGreaterThanZero()
.SetDisplay("Period", "Lookback period for breakout", "General");
_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();
_prevHighest = 0;
_prevLowest = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = Period };
var lowest = new Lowest { Length = Period };
SubscribeCandles(CandleType)
.Bind(highest, lowest, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal highestValue, decimal lowestValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevHighest = highestValue;
_prevLowest = lowestValue;
_hasPrev = true;
return;
}
if (candle.ClosePrice > _prevHighest && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (candle.ClosePrice < _prevLowest && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevHighest = highestValue;
_prevLowest = lowestValue;
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class pz_reversal_trend_following_strategy(Strategy):
def __init__(self):
super(pz_reversal_trend_following_strategy, self).__init__()
self._period = self.Param("Period", 30) .SetDisplay("Period", "Channel lookback period", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) .SetDisplay("Candle Type", "Candle type", "General")
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
@property
def period(self):
return self._period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(pz_reversal_trend_following_strategy, self).OnReseted()
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(pz_reversal_trend_following_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.period
lowest = Lowest()
lowest.Length = self.period
self.SubscribeCandles(self.candle_type).Bind(highest, lowest, self.process_candle).Start()
def process_candle(self, candle, high, low):
if candle.State != CandleStates.Finished:
return
hv = float(high)
lv = float(low)
if not self._has_prev:
self._prev_high = hv
self._prev_low = lv
self._has_prev = True
return
close = float(candle.ClosePrice)
if close > self._prev_high and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif close < self._prev_low and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_high = hv
self._prev_low = lv
def CreateClone(self):
return pz_reversal_trend_following_strategy()