SAR Trailing System Strategy
该策略在固定时间间隔随机开仓多头或空头,并使用抛物线 SAR 指标管理退出。 SAR 值作为跟踪止损,当价格穿越 SAR 水平时平仓。
细节
- 入场条件:
- 每到
TimerInterval且没有持仓并且UseRandomEntry启用时,随机开多或开空。
- 每到
- 多空方向:双向
- 出场条件:价格穿越 Parabolic SAR。
- 止损:初始止损以 tick 为单位,结合 Parabolic SAR 跟踪退出。
- 默认值:
TimerInterval= 300 秒StopLossTicks= 10AccelerationStep= 0.02AccelerationMax= 0.2UseRandomEntry= trueCandleType= TimeSpan.FromMinutes(1).TimeFrame()
- 筛选:
- 类别:趋势跟踪
- 方向:双向
- 指标:Parabolic SAR
- 止损:是
- 复杂度:初级
- 时间框架:短期
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中等
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 that uses Parabolic SAR for trend-following entries and exits.
/// Buy when price crosses above SAR, sell when below.
/// </summary>
public class SarTrailingSystemStrategy : Strategy
{
private readonly StrategyParam<decimal> _accelerationStep;
private readonly StrategyParam<decimal> _accelerationMax;
private readonly StrategyParam<DataType> _candleType;
public decimal AccelerationStep { get => _accelerationStep.Value; set => _accelerationStep.Value = value; }
public decimal AccelerationMax { get => _accelerationMax.Value; set => _accelerationMax.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SarTrailingSystemStrategy()
{
_accelerationStep = Param(nameof(AccelerationStep), 0.02m)
.SetGreaterThanZero()
.SetDisplay("SAR Step", "Parabolic SAR acceleration step", "Indicators");
_accelerationMax = Param(nameof(AccelerationMax), 0.2m)
.SetGreaterThanZero()
.SetDisplay("SAR Max", "Parabolic SAR maximum acceleration", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sar = new ParabolicSar
{
Acceleration = AccelerationStep,
AccelerationStep = AccelerationStep,
AccelerationMax = AccelerationMax
};
var subscription = SubscribeCandles(CandleType);
subscription.Bind(sar, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sar);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal sarValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
// Price above SAR = uptrend, buy
if (candle.ClosePrice > sarValue && Position <= 0)
BuyMarket();
// Price below SAR = downtrend, sell
else if (candle.ClosePrice < sarValue && 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 ParabolicSar
from StockSharp.Algo.Strategies import Strategy
class sar_trailing_system_strategy(Strategy):
def __init__(self):
super(sar_trailing_system_strategy, self).__init__()
self._acceleration_step = self.Param("AccelerationStep", 0.02) \
.SetDisplay("SAR Step", "Parabolic SAR acceleration step", "Indicators")
self._acceleration_max = self.Param("AccelerationMax", 0.2) \
.SetDisplay("SAR Max", "Parabolic SAR maximum acceleration", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def acceleration_step(self):
return self._acceleration_step.Value
@property
def acceleration_max(self):
return self._acceleration_max.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(sar_trailing_system_strategy, self).OnStarted2(time)
sar = ParabolicSar()
sar.Acceleration = self.acceleration_step
sar.AccelerationStep = self.acceleration_step
sar.AccelerationMax = self.acceleration_max
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sar, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sar)
self.DrawOwnTrades(area)
def process_candle(self, candle, sar_value):
if candle.State != CandleStates.Finished:
return
close_price = float(candle.ClosePrice)
sar_value = float(sar_value)
if close_price > sar_value and self.Position <= 0:
self.BuyMarket()
elif close_price < sar_value and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return sar_trailing_system_strategy()