Sichtbares-Chart-Strategie
Dieses Beispiel zeigt, wie mit einem sichtbaren Bereich von Kerzen gearbeitet wird. Die Strategie verfolgt das höchste Hoch und das niedrigste Tief über eine konfigurierbare Anzahl von jüngsten Bars. Ein Ausbruch über das sichtbare Hoch löst einen Long-Einstieg aus, während ein Bruch unter das sichtbare Tief einen Short-Einstieg auslöst.
Details
- Einstieg: Ausbruch des sichtbaren Hochs/Tiefs.
- Ausstieg: Entgegengesetzter Ausbruch.
- Indikatoren: Highest, Lowest.
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy that trades breakouts of the visible chart range defined by a number of recent bars.
/// </summary>
public class VisibleChartStrategy : Strategy
{
private readonly StrategyParam<int> _visibleBars;
private readonly StrategyParam<DataType> _candleType;
private decimal _highVal;
private decimal _lowVal;
private int _cooldown;
public int VisibleBars { get => _visibleBars.Value; set => _visibleBars.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VisibleChartStrategy()
{
_visibleBars = Param(nameof(VisibleBars), 40)
.SetDisplay("Visible Bars", "Number of bars considered visible", "General")
.SetOptimize(20, 200, 20);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to analyze", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_highVal = 0;
_lowVal = 0;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = VisibleBars };
var lowest = new Lowest { Length = VisibleBars };
_highVal = 0;
_lowVal = 0;
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, highest);
DrawIndicator(area, lowest);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldown > 0)
{
_cooldown--;
_highVal = high;
_lowVal = low;
return;
}
if (_highVal == 0)
{
_highVal = high;
_lowVal = low;
return;
}
var breakoutUp = candle.ClosePrice >= _highVal && Position <= 0;
var breakoutDown = candle.ClosePrice <= _lowVal && Position >= 0;
if (breakoutUp)
{
BuyMarket();
_cooldown = 30;
}
else if (breakoutDown)
{
SellMarket();
_cooldown = 30;
}
_highVal = high;
_lowVal = low;
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class visible_chart_strategy(Strategy):
def __init__(self):
super(visible_chart_strategy, self).__init__()
self._visible_bars = self.Param("VisibleBars", 40) \
.SetDisplay("Visible Bars", "Number of bars considered visible", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to analyze", "General")
self._high_val = 0.0
self._low_val = 0.0
self._cooldown = 0
@property
def visible_bars(self):
return self._visible_bars.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(visible_chart_strategy, self).OnReseted()
self._high_val = 0.0
self._low_val = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(visible_chart_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.visible_bars
lowest = Lowest()
lowest.Length = self.visible_bars
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, highest)
self.DrawIndicator(area, lowest)
self.DrawOwnTrades(area)
def on_process(self, candle, high, low):
if candle.State != CandleStates.Finished:
return
if self._cooldown > 0:
self._cooldown -= 1
self._high_val = high
self._low_val = low
return
if self._high_val == 0:
self._high_val = high
self._low_val = low
return
breakout_up = candle.ClosePrice >= self._high_val and self.Position <= 0
breakout_down = candle.ClosePrice <= self._low_val and self.Position >= 0
if breakout_up:
self.BuyMarket()
self._cooldown = 30
elif breakout_down:
self.SellMarket()
self._cooldown = 30
self._high_val = high
self._low_val = low
def CreateClone(self):
return visible_chart_strategy()