Diese Strategie eröffnet Positionen, wenn der Schlusskurs die SuperTrend-Linie kreuzt. Ein Long-Trade wird platziert, wenn der Preis über die Linie steigt, und ein Short-Trade wird eröffnet, wenn der Preis darunter fällt. Entgegengesetzte Signale schließen und kehren bestehende Positionen um.
Der SuperTrend-Indikator verwendet die Average True Range (ATR), um dem Preis zu folgen und den vorherrschenden Trend zu definieren. Parameter ermöglichen die Konfiguration der ATR-Periode, des Multiplikators und des Kerzen-Zeitrahmens.
Details
Einstiegskriterien:
Long: Schlusskurs kreuzt SuperTrend von unten
Short: Schlusskurs kreuzt SuperTrend von oben
Long/Short: Long und Short
Ausstiegskriterien:
Entgegengesetzter SuperTrend-Kreuzung
Stops: Keine
Standardwerte:
AtrPeriod = 5
Multiplier = 3
CandleType = TimeSpan.FromMinutes(15).TimeFrame()
Filter:
Kategorie: Trendfolge
Richtung: Beide
Indikatoren: SuperTrend (ATR-basiert)
Stops: Nein
Komplexität: Anfänger
Zeitrahmen: Mittelfristig
Saisonalität: Keine
Neuronale Netze: Nein
Divergenz: Nein
Risikolevel: Mittel
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>
/// Simple SuperTrend crossover strategy.
/// </summary>
public class SupertrendSignalStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private bool? _prevIsUpTrend;
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SupertrendSignalStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 5)
.SetDisplay("ATR Period", "ATR period for SuperTrend", "Parameters");
_multiplier = Param(nameof(Multiplier), 3m)
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for candles", "Parameters");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevIsUpTrend = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var st = new SuperTrend { Length = AtrPeriod, Multiplier = Multiplier };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(st, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, st);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue stValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!stValue.IsFormed)
return;
var stv = stValue as SuperTrendIndicatorValue;
if (stv == null)
return;
var isUpTrend = stv.IsUpTrend;
if (IsFormedAndOnlineAndAllowTrading() && _prevIsUpTrend.HasValue)
{
if (isUpTrend && !_prevIsUpTrend.Value && Position <= 0)
BuyMarket();
else if (!isUpTrend && _prevIsUpTrend.Value && Position >= 0)
SellMarket();
}
_prevIsUpTrend = isUpTrend;
}
}
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 supertrend_signal_strategy(Strategy):
def __init__(self):
super(supertrend_signal_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 5) \
.SetDisplay("ATR Period", "ATR period for SuperTrend", "Parameters")
self._multiplier = self.Param("Multiplier", 3.0) \
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for candles", "Parameters")
self._prev_is_up_trend = None
@property
def atr_period(self):
return self._atr_period.Value
@property
def multiplier(self):
return self._multiplier.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(supertrend_signal_strategy, self).OnReseted()
self._prev_is_up_trend = None
def OnStarted2(self, time):
super(supertrend_signal_strategy, self).OnStarted2(time)
st = SuperTrend()
st.Length = self.atr_period
st.Multiplier = self.multiplier
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(st, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, st)
self.DrawOwnTrades(area)
def process_candle(self, candle, st_value):
if candle.State != CandleStates.Finished:
return
if not st_value.IsFormed:
return
is_up_trend = st_value.IsUpTrend
if self._prev_is_up_trend is not None:
if is_up_trend and not self._prev_is_up_trend and self.Position <= 0:
self.BuyMarket()
elif not is_up_trend and self._prev_is_up_trend and self.Position >= 0:
self.SellMarket()
self._prev_is_up_trend = is_up_trend
def CreateClone(self):
return supertrend_signal_strategy()