Breakout Nifty BN Strategy
该策略在开盘后首个15分钟区间突破时交易,并结合成交量过滤和ATR跟踪止损。
using System;
using System.Linq;
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>
/// Breakout strategy using highest/lowest channel with ATR trailing stop.
/// </summary>
public class BreakoutNiftyBnStrategy : Strategy
{
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<int> _channelLength;
private readonly StrategyParam<DataType> _candleType;
public int AtrLength { get => _atrLength.Value; set => _atrLength.Value = value; }
public decimal AtrMultiplier { get => _atrMultiplier.Value; set => _atrMultiplier.Value = value; }
public int ChannelLength { get => _channelLength.Value; set => _channelLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BreakoutNiftyBnStrategy()
{
_atrLength = Param(nameof(AtrLength), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length", "ATR period", "General");
_atrMultiplier = Param(nameof(AtrMultiplier), 2m)
.SetGreaterThanZero()
.SetDisplay("ATR Multiplier", "ATR stop multiplier", "General");
_channelLength = Param(nameof(ChannelLength), 20)
.SetGreaterThanZero()
.SetDisplay("Channel Length", "Donchian channel period", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = ChannelLength };
var lowest = new Lowest { Length = ChannelLength };
var atr = new AverageTrueRange { Length = AtrLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, atr, ProcessCandle)
.Start();
StartProtection(
takeProfit: new Unit(2, UnitTypes.Percent),
stopLoss: new Unit(1, UnitTypes.Percent)
);
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low, decimal atr)
{
if (candle.State != CandleStates.Finished)
return;
if (Position == 0)
{
if (candle.ClosePrice >= high)
{
BuyMarket();
}
else if (candle.ClosePrice <= low)
{
SellMarket();
}
}
}
}
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, Unit, UnitTypes
from StockSharp.Algo.Indicators import AverageTrueRange, Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class breakout_nifty_bn_strategy(Strategy):
def __init__(self):
super(breakout_nifty_bn_strategy, self).__init__()
self._atr_length = self.Param("AtrLength", 14) \
.SetDisplay("ATR Length", "ATR period", "General")
self._atr_multiplier = self.Param("AtrMultiplier", 2) \
.SetDisplay("ATR Multiplier", "ATR stop multiplier", "General")
self._channel_length = self.Param("ChannelLength", 20) \
.SetDisplay("Channel Length", "Donchian channel period", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._trail_sl = 0.0
self._entry_price = 0.0
@property
def atr_length(self):
return self._atr_length.Value
@property
def atr_multiplier(self):
return self._atr_multiplier.Value
@property
def channel_length(self):
return self._channel_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(breakout_nifty_bn_strategy, self).OnReseted()
self._trail_sl = 0.0
self._entry_price = 0.0
def OnStarted2(self, time):
super(breakout_nifty_bn_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.channel_length
lowest = Lowest()
lowest.Length = self.channel_length
atr = AverageTrueRange()
atr.Length = self.atr_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(highest, lowest, atr, self.on_process).Start()
self.StartProtection(
takeProfit=Unit(2, UnitTypes.Percent),
stopLoss=Unit(1, UnitTypes.Percent)
)
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, high, low, atr):
if candle.State != CandleStates.Finished:
return
if self.Position == 0:
if candle.ClosePrice >= high:
self.BuyMarket()
elif candle.ClosePrice <= low:
self.SellMarket()
def CreateClone(self):
return breakout_nifty_bn_strategy()