PZ Parabolic SAR EA
该策略复刻了 PZ Parabolic SAR 专家顾问。它使用两个 Parabolic SAR 指标,分别具有不同的步长和最大加速度参数。"交易" SAR 用于判断趋势方向并产生入场信号,而 "止损" SAR 紧跟价格,当趋势反转时触发退出。
风险控制通过平均真实波幅 (ATR) 实现。开仓时根据 ATR 设置初始止损,可选的 ATR 跟踪止损会在行情向有利方向发展时收紧止损。当利润超过初始止损距离时,策略会部分平仓(默认 50%)并将止损移至保本位置。
策略可双向交易,仅处理已完成的K线,并使用市价单执行,无真实止损委托。
细节
- 入场条件:价格与交易SAR同向且止损SAR位于同侧。
- 交易方向:多空双向。
- 离场条件:价格穿越止损SAR或触发 ATR 跟踪止损。
- 止损方式:基于ATR的止损,可选跟踪止损与保本。
- 默认参数:
TradeStep= 0.002TradeMax= 0.2StopStep= 0.004StopMax= 0.4AtrPeriod= 30AtrMultiplier= 2.5UseTrailing= falseTrailingAtrPeriod= 30TrailingAtrMultiplier= 1.75PartialClosing= truePercentageToClose= 0.5BreakEven= trueLotSize= 0.1CandleType= TimeFrame(5m)
- 筛选:
- 类别:Trend
- 方向:Both
- 指标:Parabolic SAR, ATR
- 止损:ATR, Trailing
- 复杂度:中等
- 时间框架:日内 (5m)
- 季节性:否
- 神经网络:否
- 背离:否
- 风险级别:中等
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>
/// Parabolic SAR crossover strategy.
/// </summary>
public class PzParabolicSarEaStrategy : Strategy
{
private readonly StrategyParam<decimal> _sarStep;
private readonly StrategyParam<decimal> _sarMax;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevClose;
private decimal _prevSar;
private bool _hasPrev;
public decimal SarStep { get => _sarStep.Value; set => _sarStep.Value = value; }
public decimal SarMax { get => _sarMax.Value; set => _sarMax.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public PzParabolicSarEaStrategy()
{
_sarStep = Param(nameof(SarStep), 0.02m)
.SetDisplay("SAR Step", "Acceleration step", "Indicators");
_sarMax = Param(nameof(SarMax), 0.2m)
.SetDisplay("SAR Max", "Maximum acceleration", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevClose = 0;
_prevSar = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sar = new ParabolicSar { AccelerationStep = SarStep, AccelerationMax = SarMax };
SubscribeCandles(CandleType)
.Bind(sar, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal sarVal)
{
if (candle.State != CandleStates.Finished) return;
var close = candle.ClosePrice;
if (!_hasPrev)
{
_prevClose = close;
_prevSar = sarVal;
_hasPrev = true;
return;
}
var crossUp = _prevClose <= _prevSar && close > sarVal;
var crossDown = _prevClose >= _prevSar && close < sarVal;
if (crossUp && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (crossDown && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevClose = close;
_prevSar = sarVal;
}
}
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 pz_parabolic_sar_ea_strategy(Strategy):
def __init__(self):
super(pz_parabolic_sar_ea_strategy, self).__init__()
self._sar_step = self.Param("SarStep", 0.02) .SetDisplay("SAR Step", "Acceleration step", "Indicators")
self._sar_max = self.Param("SarMax", 0.2) .SetDisplay("SAR Max", "Maximum acceleration", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) .SetDisplay("Candle Type", "Candle type", "General")
self._prev_close = 0.0
self._prev_sar = 0.0
self._has_prev = False
@property
def sar_step(self):
return self._sar_step.Value
@property
def sar_max(self):
return self._sar_max.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(pz_parabolic_sar_ea_strategy, self).OnReseted()
self._prev_close = 0.0
self._prev_sar = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(pz_parabolic_sar_ea_strategy, self).OnStarted2(time)
sar = ParabolicSar()
sar.AccelerationStep = self.sar_step
sar.AccelerationMax = self.sar_max
self.SubscribeCandles(self.candle_type).Bind(sar, self.process_candle).Start()
def process_candle(self, candle, sar_val):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
sv = float(sar_val)
if not self._has_prev:
self._prev_close = close
self._prev_sar = sv
self._has_prev = True
return
cross_up = self._prev_close <= self._prev_sar and close > sv
cross_down = self._prev_close >= self._prev_sar and close < sv
if cross_up and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif cross_down and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_close = close
self._prev_sar = sv
def CreateClone(self):
return pz_parabolic_sar_ea_strategy()