TradingView Supertrend Flip 戦略
出来高確認を伴うSupertrendインジケーターのフリップに基づく戦略
テストでは年平均リターンが約79%であることが示されています。株式市場で最もよく機能します。
TradingView Supertrend Flipは人気インジケーターの色変化を模倣します。赤から緑への変化はロングエントリーを、緑から赤への変化はショートエントリーを示します。戦略は次のフリップで退場します。
出来高確認は薄商いの期間における偽シグナルを避けるために使用できます。支持する出来高を伴うフリップにのみ対応することで、より信頼性の高い反転を捉えることを目指します。
詳細
- エントリー条件: ATR、Supertrendに基づくシグナル。
- ロング/ショート: 両方向。
- エグジット条件: 逆シグナル。
- ストップ: いいえ。
- デフォルト値:
SupertrendPeriod= 10SupertrendMultiplier= 3.0mVolumeAvgPeriod= 20CandleType= TimeSpan.FromMinutes(5)
- フィルター:
- カテゴリ: トレンド
- 方向: 両方
- インジケーター: ATR、Supertrend
- ストップ: いいえ
- 複雑さ: 基本
- 時間軸: イントラデイ (5m)
- 季節性: いいえ
- Neural Networks: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// Strategy based on Supertrend indicator flips.
/// Detects when Supertrend direction changes and trades accordingly.
/// </summary>
public class TradingViewSupertrendFlipStrategy : Strategy
{
private readonly StrategyParam<int> _supertrendPeriod;
private readonly StrategyParam<decimal> _supertrendMultiplier;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevSupertrendValue;
private bool _prevIsUpTrend;
private bool _hasPrevValues;
/// <summary>
/// Period for Supertrend calculation.
/// </summary>
public int SupertrendPeriod
{
get => _supertrendPeriod.Value;
set => _supertrendPeriod.Value = value;
}
/// <summary>
/// Multiplier for Supertrend calculation.
/// </summary>
public decimal SupertrendMultiplier
{
get => _supertrendMultiplier.Value;
set => _supertrendMultiplier.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initialize the TradingView Supertrend Flip strategy.
/// </summary>
public TradingViewSupertrendFlipStrategy()
{
_supertrendPeriod = Param(nameof(SupertrendPeriod), 10)
.SetDisplay("Supertrend Period", "Period for Supertrend calculation", "Indicators")
.SetOptimize(7, 14, 1);
_supertrendMultiplier = Param(nameof(SupertrendMultiplier), 4.0m)
.SetDisplay("Supertrend Multiplier", "Multiplier for Supertrend", "Indicators")
.SetOptimize(3.0m, 5.0m, 0.5m);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();
_prevSupertrendValue = default;
_prevIsUpTrend = default;
_hasPrevValues = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var supertrend = new SuperTrend
{
Length = SupertrendPeriod,
Multiplier = SupertrendMultiplier
};
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(supertrend, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, supertrend);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal supertrendValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (supertrendValue == 0)
return;
// Determine trend direction
var isUpTrend = candle.ClosePrice > supertrendValue;
if (!_hasPrevValues)
{
_hasPrevValues = true;
_prevIsUpTrend = isUpTrend;
_prevSupertrendValue = supertrendValue;
return;
}
// Detect flip
var isFlippedBullish = isUpTrend && !_prevIsUpTrend;
var isFlippedBearish = !isUpTrend && _prevIsUpTrend;
_prevIsUpTrend = isUpTrend;
_prevSupertrendValue = supertrendValue;
if (isFlippedBullish && Position <= 0)
{
var volume = Volume + Math.Abs(Position);
BuyMarket(volume);
}
else if (isFlippedBearish && Position >= 0)
{
var volume = Volume + Math.Abs(Position);
SellMarket(volume);
}
}
}
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 SuperTrend
from StockSharp.Algo.Strategies import Strategy
class tradingview_supertrend_flip_strategy(Strategy):
"""
Strategy based on Supertrend indicator flips.
Detects when Supertrend direction changes and trades accordingly.
"""
def __init__(self):
super(tradingview_supertrend_flip_strategy, self).__init__()
self._supertrend_period = self.Param("SupertrendPeriod", 10).SetDisplay("Supertrend Period", "Period for Supertrend calculation", "Indicators")
self._supertrend_multiplier = self.Param("SupertrendMultiplier", 4.0).SetDisplay("Supertrend Multiplier", "Multiplier for Supertrend", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_supertrend_value = 0.0
self._prev_is_up_trend = False
self._has_prev_values = False
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(tradingview_supertrend_flip_strategy, self).OnReseted()
self._prev_supertrend_value = 0.0
self._prev_is_up_trend = False
self._has_prev_values = False
def OnStarted2(self, time):
super(tradingview_supertrend_flip_strategy, self).OnStarted2(time)
supertrend = SuperTrend()
supertrend.Length = self._supertrend_period.Value
supertrend.Multiplier = self._supertrend_multiplier.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(supertrend, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, supertrend)
self.DrawOwnTrades(area)
def _process_candle(self, candle, st_val):
if candle.State != CandleStates.Finished:
return
sv = float(st_val)
if sv == 0:
return
is_up_trend = float(candle.ClosePrice) > sv
if not self._has_prev_values:
self._has_prev_values = True
self._prev_is_up_trend = is_up_trend
self._prev_supertrend_value = sv
return
flipped_bullish = is_up_trend and not self._prev_is_up_trend
flipped_bearish = not is_up_trend and self._prev_is_up_trend
self._prev_is_up_trend = is_up_trend
self._prev_supertrend_value = sv
if flipped_bullish and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif flipped_bearish and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def CreateClone(self):
return tradingview_supertrend_flip_strategy()