Supertrend Signal 策略
当收盘价穿越 SuperTrend 线时,本策略开仓。价格上破该线时做多,跌破该线时做空。相反的信号会平掉并反向已有仓位。
SuperTrend 指标基于平均真实波幅 (ATR),跟随价格以确定主导趋势。参数可设置 ATR 周期、倍数以及K线时间框架。
细节
- 入场条件:
- 多头:收盘价向上突破 SuperTrend 线
- 空头:收盘价向下跌破 SuperTrend 线
- 多/空:双向
- 出场条件:
- 相反的 SuperTrend 突破
- 止损:无
- 默认值:
AtrPeriod= 5Multiplier= 3CandleType= TimeSpan.FromMinutes(15).TimeFrame()
- 筛选:
- 类别:趋势跟随
- 方向:双向
- 指标:SuperTrend(基于 ATR)
- 止损:否
- 复杂度:入门
- 时间框架:中期
- 季节性:无
- 神经网络:无
- 背离:无
- 风险级别:中
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()