Diese Strategie verwendet den Aroon-Oszillator, um Handelssignale zu erzeugen, wenn der Oszillator vordefinierte Niveaus kreuzt. Eine Long-Position wird eröffnet, wenn der Oszillator das untere Niveau (Standard -50) nach oben kreuzt. Eine Short-Position wird eröffnet, wenn er das obere Niveau (Standard +50) nach unten kreuzt. Umgekehrte Signale schließen oder kehren die Position um.
Details
Einstiegskriterien:
Long: Der Aroon-Oszillator kreuzt das untere Niveau nach oben.
Short: Der Aroon-Oszillator kreuzt das obere Niveau nach unten.
Long/Short: Beide.
Ausstiegskriterien:
Das umgekehrte Signal schließt oder kehrt automatisch die aktuelle Position um.
Stops: Keine.
Filter: Keine.
Zeitrahmen: Standard 4-Stunden-Kerzen (konfigurierbar).
Parameter
AroonPeriod – Lookback-Periode für den Aroon-Oszillator (Standard 9).
UpLevel – oberer Schwellenwert für Verkaufssignale (Standard +50).
DownLevel – unterer Schwellenwert für Kaufsignale (Standard -50).
CandleType – Kerzen-Zeitrahmen für Berechnungen (Standard 4 Stunden).
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 based on Aroon Oscillator crossing predefined levels.
/// Opens long positions when the oscillator rises above the down level.
/// Opens short positions when the oscillator falls below the up level.
/// </summary>
public class AroonOscillatorSignAlertStrategy : Strategy
{
private readonly StrategyParam<int> _aroonPeriod;
private readonly StrategyParam<int> _upLevel;
private readonly StrategyParam<int> _downLevel;
private readonly StrategyParam<DataType> _candleType;
private decimal? _previousValue;
public int AroonPeriod
{
get => _aroonPeriod.Value;
set => _aroonPeriod.Value = value;
}
public int UpLevel
{
get => _upLevel.Value;
set => _upLevel.Value = value;
}
public int DownLevel
{
get => _downLevel.Value;
set => _downLevel.Value = value;
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public AroonOscillatorSignAlertStrategy()
{
_aroonPeriod = Param(nameof(AroonPeriod), 9)
.SetGreaterThanZero()
.SetDisplay("Aroon Period", "Lookback for Aroon oscillator", "Indicator");
_upLevel = Param(nameof(UpLevel), 50)
.SetDisplay("Up Level", "Upper threshold for sell signal", "Indicator");
_downLevel = Param(nameof(DownLevel), -50)
.SetDisplay("Down Level", "Lower threshold for buy signal", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Time frame for processing", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_previousValue = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_previousValue = null;
var aroon = new AroonOscillator { Length = AroonPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(aroon, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, aroon);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal aroonValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (_previousValue is null)
{
_previousValue = aroonValue;
return;
}
if (_previousValue <= DownLevel && aroonValue > DownLevel && Position <= 0)
BuyMarket();
else if (_previousValue >= UpLevel && aroonValue < UpLevel && Position >= 0)
SellMarket();
_previousValue = aroonValue;
}
}
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 AroonOscillator
from StockSharp.Algo.Strategies import Strategy
class aroon_oscillator_sign_alert_strategy(Strategy):
def __init__(self):
super(aroon_oscillator_sign_alert_strategy, self).__init__()
self._aroon_period = self.Param("AroonPeriod", 9) \
.SetDisplay("Aroon Period", "Lookback for Aroon oscillator", "Indicator")
self._up_level = self.Param("UpLevel", 50) \
.SetDisplay("Up Level", "Upper threshold for sell signal", "Indicator")
self._down_level = self.Param("DownLevel", -50) \
.SetDisplay("Down Level", "Lower threshold for buy signal", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Time frame for processing", "General")
self._previous_value = None
@property
def aroon_period(self):
return self._aroon_period.Value
@property
def up_level(self):
return self._up_level.Value
@property
def down_level(self):
return self._down_level.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(aroon_oscillator_sign_alert_strategy, self).OnReseted()
self._previous_value = None
def OnStarted2(self, time):
super(aroon_oscillator_sign_alert_strategy, self).OnStarted2(time)
self._previous_value = None
aroon = AroonOscillator()
aroon.Length = self.aroon_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(aroon, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, aroon)
self.DrawOwnTrades(area)
def process_candle(self, candle, aroon_value):
if candle.State != CandleStates.Finished:
return
aroon_value = float(aroon_value)
if self._previous_value is None:
self._previous_value = aroon_value
return
down_level = float(self.down_level)
up_level = float(self.up_level)
if self._previous_value <= down_level and aroon_value > down_level and self.Position <= 0:
self.BuyMarket()
elif self._previous_value >= up_level and aroon_value < up_level and self.Position >= 0:
self.SellMarket()
self._previous_value = aroon_value
def CreateClone(self):
return aroon_oscillator_sign_alert_strategy()