Strategie basierend auf dem Parabolic SAR-Indikator. PSAR Trader Ticks folgt den Punkten des Parabolic SAR-Indikators und reagiert, wenn der Preis von einer Seite zur anderen kreuzt. Es eröffnet eine Long-Position, wenn der Preis über den SAR steigt, und eine Short-Position, wenn der Preis unter ihn fällt. Der Handel kann auf einen bestimmten Zeitbereich beschränkt werden, und bestehende Positionen können optional geschlossen werden, wenn ein gegenteiliges Signal erscheint. Die Strategie wendet auch Take-Profit- und Stop-Loss-Niveaus an, die in Ticks gemessen werden.
Details
Einstiegskriterien: Preis, der den Parabolic SAR-Indikator kreuzt.
Long/Short: Beide Richtungen.
Ausstiegskriterien: Entgegengesetztes Signal (optional), Stop-Loss oder Take-Profit.
Stops: Take-Profit und Stop-Loss in Ticks.
Standardwerte:
Step = 0.001m
Maximum = 0.2m
TakeProfitTicks = 50
StopLossTicks = 50
StartHour = 0
EndHour = 23
CloseOnOpposite = true
CandleType = TimeSpan.FromMinutes(5)
Filter:
Kategorie: Trend
Richtung: Beide
Indikatoren: Parabolic SAR
Stops: Take-Profit, Stop-Loss
Komplexität: Grundlegend
Zeitrahmen: Intraday (5m)
Saisonalität: Nein
Neuronale Netze: Nein
Divergenz: Nein
Risikolevel: Mittel
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>
/// PSAR Trader strategy - opens long when price crosses above SAR
/// and short when price crosses below SAR.
/// </summary>
public class PsarTraderTicksStrategy : Strategy
{
private readonly StrategyParam<decimal> _step;
private readonly StrategyParam<decimal> _maximum;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevSar;
private decimal _prevPrice;
private bool _hasPrev;
public decimal Step { get => _step.Value; set => _step.Value = value; }
public decimal Maximum { get => _maximum.Value; set => _maximum.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public PsarTraderTicksStrategy()
{
_step = Param(nameof(Step), 0.001m)
.SetDisplay("SAR Step", "Acceleration factor step", "Indicators");
_maximum = Param(nameof(Maximum), 0.2m)
.SetDisplay("SAR Maximum", "Maximum acceleration factor", "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()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevSar = 0;
_prevPrice = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var psar = new ParabolicSar
{
AccelerationStep = Step,
AccelerationMax = Maximum
};
SubscribeCandles(CandleType).Bind(psar, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal sarValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevSar = sarValue;
_prevPrice = candle.ClosePrice;
_hasPrev = true;
return;
}
var prevAbove = _prevPrice > _prevSar;
var currAbove = candle.ClosePrice > sarValue;
if (currAbove && !prevAbove && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (!currAbove && prevAbove && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevSar = sarValue;
_prevPrice = candle.ClosePrice;
}
}
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_ticks_strategy(Strategy):
def __init__(self):
super(psar_trader_ticks_strategy, self).__init__()
self._step = self.Param("Step", 0.001) \
.SetDisplay("SAR Step", "Acceleration factor step", "Indicators")
self._maximum = self.Param("Maximum", 0.2) \
.SetDisplay("SAR Maximum", "Maximum acceleration factor", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_sar = 0.0
self._prev_price = 0.0
self._has_prev = False
@property
def step(self):
return self._step.Value
@property
def maximum(self):
return self._maximum.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(psar_trader_ticks_strategy, self).OnReseted()
self._prev_sar = 0.0
self._prev_price = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(psar_trader_ticks_strategy, self).OnStarted2(time)
psar = ParabolicSar()
psar.AccelerationStep = self.step
psar.AccelerationMax = self.maximum
self.SubscribeCandles(self.candle_type).Bind(psar, self.process_candle).Start()
def process_candle(self, candle, sar_value):
if candle.State != CandleStates.Finished:
return
sv = float(sar_value)
price = float(candle.ClosePrice)
if not self._has_prev:
self._prev_sar = sv
self._prev_price = price
self._has_prev = True
return
prev_above = self._prev_price > self._prev_sar
curr_above = price > sv
if curr_above and not prev_above and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif not curr_above and prev_above and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_sar = sv
self._prev_price = price
def CreateClone(self):
return psar_trader_ticks_strategy()