PSAR Trader 策略
PSAR Trader 策略基于 Parabolic SAR 指标的翻转。当 SAR 点移动到价格下方时做多,移动到价格上方时做空。可选的 “Close On Opposite” 参数在出现反向信号时反手。策略只在设定的交易时段内运行,止损和止盈由保护模块自动设置。
详情
- 入场条件:价格与 Parabolic SAR 的交叉。
- 多空:双向。
- 出场条件:相反的 SAR 交叉或反手。
- 止损:有,固定值。
- 默认值:
SarStep= 0.001mSarMaxStep= 0.2mStartHour= 0EndHour= 23CloseOnOpposite= trueTakeValue= 50 (绝对值)StopValue= 50 (绝对值)CandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 分类: 趋势
- 方向: 双向
- 指标: Parabolic SAR
- 止损: 固定
- 复杂度: 基础
- 时间框架: 日内 (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 cross strategy.
/// Opens long when price moves above SAR and short when price moves below SAR.
/// </summary>
public class PsarTraderStrategy : Strategy
{
private readonly StrategyParam<decimal> _sarStep;
private readonly StrategyParam<decimal> _sarMaxStep;
private readonly StrategyParam<DataType> _candleType;
private bool _prevPriceAboveSar;
private bool _hasPrev;
public decimal SarStep { get => _sarStep.Value; set => _sarStep.Value = value; }
public decimal SarMaxStep { get => _sarMaxStep.Value; set => _sarMaxStep.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public PsarTraderStrategy()
{
_sarStep = Param(nameof(SarStep), 0.001m)
.SetGreaterThanZero()
.SetDisplay("SAR Step", "Acceleration factor for Parabolic SAR", "Parabolic SAR");
_sarMaxStep = Param(nameof(SarMaxStep), 0.2m)
.SetGreaterThanZero()
.SetDisplay("SAR Max Step", "Maximum acceleration factor", "Parabolic SAR");
_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()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevPriceAboveSar = false;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var parabolicSar = new ParabolicSar
{
AccelerationStep = SarStep,
AccelerationMax = SarMaxStep
};
SubscribeCandles(CandleType).Bind(parabolicSar, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal sarValue)
{
if (candle.State != CandleStates.Finished) return;
var isPriceAboveSar = candle.ClosePrice > sarValue;
if (!_hasPrev)
{
_prevPriceAboveSar = isPriceAboveSar;
_hasPrev = true;
return;
}
if (_prevPriceAboveSar != isPriceAboveSar)
{
if (isPriceAboveSar && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (!isPriceAboveSar && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
}
_prevPriceAboveSar = isPriceAboveSar;
}
}
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 psar_trader_strategy(Strategy):
def __init__(self):
super(psar_trader_strategy, self).__init__()
self._sar_step = self.Param("SarStep", 0.001) \
.SetDisplay("SAR Step", "Acceleration factor for Parabolic SAR", "Parabolic SAR")
self._sar_max_step = self.Param("SarMaxStep", 0.2) \
.SetDisplay("SAR Max Step", "Maximum acceleration factor", "Parabolic SAR")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_price_above_sar = False
self._has_prev = False
@property
def sar_step(self):
return self._sar_step.Value
@property
def sar_max_step(self):
return self._sar_max_step.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(psar_trader_strategy, self).OnReseted()
self._prev_price_above_sar = False
self._has_prev = False
def OnStarted2(self, time):
super(psar_trader_strategy, self).OnStarted2(time)
psar = ParabolicSar()
psar.AccelerationStep = self.sar_step
psar.AccelerationMax = self.sar_max_step
self.SubscribeCandles(self.candle_type).Bind(psar, self.process_candle).Start()
def process_candle(self, candle, sar_value):
if candle.State != CandleStates.Finished:
return
is_price_above_sar = float(candle.ClosePrice) > float(sar_value)
if not self._has_prev:
self._prev_price_above_sar = is_price_above_sar
self._has_prev = True
return
if self._prev_price_above_sar != is_price_above_sar:
if is_price_above_sar and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif not is_price_above_sar and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_price_above_sar = is_price_above_sar
def CreateClone(self):
return psar_trader_strategy()