Стратегия простого трейлинг-стопа
Пример демонстрирует управление открытой позицией с трейлинг-стопом на основе высокоуровневого API StockSharp.
Обзор
- Открывает одну длинную позицию после получения первой завершённой свечи.
- Включает защиту позиции с трейлинг-стопом.
- Уровень стопа следует за ценой на фиксированном расстоянии.
Параметры
TrailPoints– расстояние в пунктах, на которое стоп следует за ценой.CandleType– тип свечей, используемых стратегией.
Логика
- При старте стратегия подписывается на свечи и включает
StartProtectionс трейлинг-стопом. - После первой завершённой свечи стратегия покупает по рынку.
- При движении цены в сторону позиции стоп переносится, сохраняя расстояние
TrailPoints. - Когда цена разворачивается и достигает трейлинг-стопа, позиция закрывается автоматически.
Стратегия упрощена и предназначена для демонстрации базового использования трейлинг-стопа.
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>
/// Simple trailing stop strategy.
/// Uses EMA crossover for entries with a trailing stop for protection.
/// </summary>
public class SimpleTrailingStopStrategy : Strategy
{
private readonly StrategyParam<decimal> _trailPercent;
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<DataType> _candleType;
public decimal TrailPercent { get => _trailPercent.Value; set => _trailPercent.Value = value; }
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SimpleTrailingStopStrategy()
{
_trailPercent = Param(nameof(TrailPercent), 2m)
.SetDisplay("Trail %", "Trailing stop distance as percentage", "Protection");
_fastLength = Param(nameof(FastLength), 10)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA period", "Indicator");
_slowLength = Param(nameof(SlowLength), 30)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA period", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
StartProtection(
takeProfit: null,
stopLoss: new Unit(TrailPercent, UnitTypes.Percent),
isStopTrailing: true,
useMarketOrders: true);
var fast = new ExponentialMovingAverage { Length = FastLength };
var slow = new ExponentialMovingAverage { Length = SlowLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fast, slow, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fast);
DrawIndicator(area, slow);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
{
if (candle.State != CandleStates.Finished)
return;
if (fastVal > slowVal && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (fastVal < slowVal && Position >= 0)
{
if (Position > 0) SellMarket();
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, Unit, UnitTypes
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class simple_trailing_stop_strategy(Strategy):
def __init__(self):
super(simple_trailing_stop_strategy, self).__init__()
self._trail_percent = self.Param("TrailPercent", 2.0) \
.SetDisplay("Trail %", "Trailing stop distance as percentage", "Protection")
self._fast_length = self.Param("FastLength", 10) \
.SetDisplay("Fast EMA", "Fast EMA period", "Indicator")
self._slow_length = self.Param("SlowLength", 30) \
.SetDisplay("Slow EMA", "Slow EMA period", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(8))) \
.SetDisplay("Candle Type", "Type of candles", "General")
@property
def TrailPercent(self):
return self._trail_percent.Value
@TrailPercent.setter
def TrailPercent(self, value):
self._trail_percent.Value = value
@property
def FastLength(self):
return self._fast_length.Value
@FastLength.setter
def FastLength(self, value):
self._fast_length.Value = value
@property
def SlowLength(self):
return self._slow_length.Value
@SlowLength.setter
def SlowLength(self, value):
self._slow_length.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(simple_trailing_stop_strategy, self).OnStarted2(time)
self.StartProtection(
takeProfit=None,
stopLoss=Unit(float(self.TrailPercent), UnitTypes.Percent),
isStopTrailing=True
)
fast = ExponentialMovingAverage()
fast.Length = self.FastLength
slow = ExponentialMovingAverage()
slow.Length = self.SlowLength
self.SubscribeCandles(self.CandleType) \
.Bind(fast, slow, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast_f = float(fast_val)
slow_f = float(slow_val)
if fast_f > slow_f and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif fast_f < slow_f and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
def OnReseted(self):
super(simple_trailing_stop_strategy, self).OnReseted()
def CreateClone(self):
return simple_trailing_stop_strategy()