Buy On 5 Day Low 策略
Buy On 5 Day Low 策略在收盘价跌破前一根K线的5日最低价时做多,收盘价突破前一根K线的最高价时平仓。交易只在指定的时间窗口内进行。
细节
- 入场条件:收盘价低于过去N根K线的最低值。
- 多空方向:仅做多。
- 出场条件:收盘价高于前一根K线的最高价。
- 止损:无。
- 默认值:
LowestPeriod = 5CandleType = TimeSpan.FromMinutes(5).TimeFrame()StartTime = new DateTimeOffset(2014, 1, 1, 0, 0, 0, TimeSpan.Zero)EndTime = new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero)
- 过滤器:
- 分类:均值回归
- 方向:多头
- 指标:Lowest, High
- 止损:无
- 复杂度:基础
- 时间框架:日线
- 季节性:否
- 神经网络:否
- 背离:否
- 风险等级:中等
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using System.Linq;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
/// <summary>
/// Buys below the previous N-bar low and closes the long above the previous bar's high.
/// </summary>
public class BuyOn5DayLowStrategy : Strategy
{
private readonly StrategyParam<int> _lowestPeriod;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<DateTimeOffset> _startTime;
private readonly StrategyParam<DateTimeOffset> _endTime;
private readonly Queue<decimal> _lows = new();
private decimal? _previousHigh;
public int LowestPeriod { get => _lowestPeriod.Value; set => _lowestPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public DateTimeOffset StartTime { get => _startTime.Value; set => _startTime.Value = value; }
public DateTimeOffset EndTime { get => _endTime.Value; set => _endTime.Value = value; }
public BuyOn5DayLowStrategy()
{
_lowestPeriod = Param(nameof(LowestPeriod), 5)
.SetGreaterThanZero()
.SetDisplay("Lowest Period", "Number of previous candles in the low window", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_startTime = Param(nameof(StartTime), new DateTimeOffset(2014, 1, 1, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("Start Time", "Beginning of the trading window", "General");
_endTime = Param(nameof(EndTime), new DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("End Time", "End of the trading window", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_lows.Clear();
_previousHigh = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
// Evaluate before adding this candle: its own low cannot be the entry threshold.
if (_lows.Count == LowestPeriod && IsFormedAndOnlineAndAllowTrading()
&& candle.OpenTime >= StartTime.UtcDateTime && candle.OpenTime <= EndTime.UtcDateTime)
{
if (Position == 0 && candle.ClosePrice < _lows.Min())
BuyMarket();
else if (Position > 0 && candle.ClosePrice > _previousHigh)
SellMarket(Position);
}
_lows.Enqueue(candle.LowPrice);
while (_lows.Count > LowestPeriod)
_lows.Dequeue();
_previousHigh = candle.HighPrice;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import DateTimeOffset, TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class buy_on_5_day_low_strategy(Strategy):
"""Buys below the previous N-bar low and exits above the previous bar's high."""
def __init__(self):
super(buy_on_5_day_low_strategy, self).__init__()
self._lowest_period = self.Param("LowestPeriod", 5) \
.SetDisplay("Lowest Period", "Number of previous candles in the low window", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._start_time = self.Param("StartTime", DateTimeOffset(2014, 1, 1, 0, 0, 0, TimeSpan.Zero)) \
.SetDisplay("Start Time", "Beginning of the trading window", "General")
self._end_time = self.Param("EndTime", DateTimeOffset(2099, 1, 1, 0, 0, 0, TimeSpan.Zero)) \
.SetDisplay("End Time", "End of the trading window", "General")
self._lows = []
self._previous_high = None
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type)]
def OnReseted(self):
super(buy_on_5_day_low_strategy, self).OnReseted()
self._lows = []
self._previous_high = None
def OnStarted2(self, time):
super(buy_on_5_day_low_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
period = int(self._lowest_period.Value)
# Evaluate before adding this candle: its own low cannot be the entry threshold.
if (len(self._lows) == period and self.IsFormedAndOnlineAndAllowTrading()
and self._start_time.Value.UtcDateTime <= candle.OpenTime <= self._end_time.Value.UtcDateTime):
if self.Position == 0 and candle.ClosePrice < min(self._lows):
self.BuyMarket()
elif self.Position > 0 and candle.ClosePrice > self._previous_high:
self.SellMarket(self.Position)
self._lows.append(candle.LowPrice)
while len(self._lows) > period:
self._lows.pop(0)
self._previous_high = candle.HighPrice
def CreateClone(self):
return buy_on_5_day_low_strategy()