GitHub で見る
Exp Super Trend 戦略
MQL スクリプト Exp_Super_Trend.mq5(ID 14269)から変換された戦略。SuperTrend インジケーターの方向に従い、トレンドが反転するたびにポジションを逆転させます。実装はStockSharpの高レベルAPIと組み込みのSuperTrendインジケーターを使用しています。
インジケーターはATRに基づいて動的なサポートまたはレジスタンスラインを計算します。価格がこのラインの上にある場合、トレンドは強気と見なされ、それ以外は弱気と見なされます。戦略は強気の期間にロングポジションを開き、弱気の期間にショートポジションに切り替えます。インジケーターが反転するたびに、即座にポジション反転が行われます。
このアプローチは、ブレイクアウト後に大きな動きが続くトレンド市場で最も効果的です。また、BindEx を使ってインジケーターを接続し、完了したローソク足で成行注文を執行する方法を示す教育用テンプレートとしても有用です。
詳細
- エントリー条件:
- ロング: SuperTrendが上昇トレンドを示す。
- ショート: SuperTrendが下降トレンドを示す。
- ロング/ショート: 両方。
- エグジット条件: SuperTrendからの反対シグナル(ポジションが反転する)。
- ストップ: 明示的なストップロスなし;インジケーターラインがトレーリングストップとして機能する。
- デフォルト値:
AtrPeriod = 10
Multiplier = 3m
CandleType = TimeSpan.FromHours(1).TimeFrame()
- フィルター:
- カテゴリ: トレンドフォロー
- 方向: 両方
- インジケーター: SuperTrend
- ストップ: インジケーターベース
- 複雑さ: 基本
- 時間軸: 中期(デフォルト1時間)
- 季節性: なし
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// SuperTrend expert strategy.
/// Opens positions following the SuperTrend direction.
/// </summary>
public class ExpSuperTrendStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private SuperTrend _superTrend;
/// <summary>
/// ATR period for SuperTrend.
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// ATR multiplier for SuperTrend.
/// </summary>
public decimal Multiplier
{
get => _multiplier.Value;
set => _multiplier.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="ExpSuperTrendStrategy"/>.
/// </summary>
public ExpSuperTrendStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 10)
.SetDisplay("ATR Period", "ATR period for SuperTrend", "SuperTrend")
.SetGreaterThanZero()
.SetOptimize(5, 20, 1);
_multiplier = Param(nameof(Multiplier), 3m)
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "SuperTrend")
.SetGreaterThanZero()
.SetOptimize(1m, 5m, 0.5m);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for indicator calculation", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_superTrend = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_superTrend = new SuperTrend
{
Length = AtrPeriod,
Multiplier = Multiplier
};
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(_superTrend, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue stValue)
{
if (candle.State != CandleStates.Finished)
return;
if (stValue is not SuperTrendIndicatorValue st)
return;
var isUpTrend = st.IsUpTrend;
if (isUpTrend && Position <= 0)
{
BuyMarket();
}
else if (!isUpTrend && Position >= 0)
{
SellMarket();
}
}
}
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 exp_super_trend_strategy(Strategy):
def __init__(self):
super(exp_super_trend_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 10) \
.SetDisplay("ATR Period", "ATR period for SuperTrend", "SuperTrend")
self._multiplier = self.Param("Multiplier", 3.0) \
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "SuperTrend")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for indicator calculation", "General")
self._super_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(exp_super_trend_strategy, self).OnReseted()
self._super_trend = None
def OnStarted2(self, time):
super(exp_super_trend_strategy, self).OnStarted2(time)
self._super_trend = SuperTrend()
self._super_trend.Length = self.atr_period
self._super_trend.Multiplier = self.multiplier
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._super_trend, self.process_candle).Start()
def process_candle(self, candle, st_value):
if candle.State != CandleStates.Finished:
return
is_up_trend = st_value.IsUpTrend
if is_up_trend and self.Position <= 0:
self.BuyMarket()
elif not is_up_trend and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return exp_super_trend_strategy()