通道剥头皮策略
基于ATR的通道突破剥头皮系统。每根K线的高低价取平均得到中点,然后加减ATR乘以系数形成上下通道。当收盘价突破上一根K线的上通道时开多;跌破下通道时开空。通道随趋势移动,既作为动态止损,也用于产生反向信号。
细节
- 入场条件:
- 买入:收盘价上穿上一根K线的上通道。
- 卖出:收盘价下破上一根K线的下通道。
- 多空方向:双向。
- 出场条件:
- 价格穿越相反通道时反向。
- 止损:通道带作为移动止损。
- 过滤器:无。
参数
- ATR Period – ATR计算周期。
- ATR Multiplier – ATR通道宽度系数。
- Candle Type – 使用的K线周期。
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>
/// StdDev based channel breakout scalping strategy.
/// </summary>
public class ChannelScalperStrategy : Strategy
{
private readonly StrategyParam<int> _stdevPeriod;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private decimal _up;
private decimal _down;
private int _direction;
private bool _isInitialized;
public int StdevPeriod { get => _stdevPeriod.Value; set => _stdevPeriod.Value = value; }
public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public ChannelScalperStrategy()
{
_stdevPeriod = Param(nameof(StdevPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("StdDev Period", "Standard deviation period", "General");
_multiplier = Param(nameof(Multiplier), 1.5m)
.SetDisplay("Multiplier", "Channel width multiplier", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle Type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_up = 0;
_down = 0;
_direction = 0;
_isInitialized = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var stdev = new StandardDeviation { Length = StdevPeriod };
SubscribeCandles(CandleType).Bind(stdev, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal stdevValue)
{
if (candle.State != CandleStates.Finished || stdevValue <= 0) return;
var middle = (candle.HighPrice + candle.LowPrice) / 2;
var currentUp = middle + Multiplier * stdevValue;
var currentDown = middle - Multiplier * stdevValue;
if (!_isInitialized)
{
_up = currentUp;
_down = currentDown;
_isInitialized = true;
return;
}
if (_direction <= 0 && candle.ClosePrice > _up)
{
if (Position < 0) BuyMarket();
BuyMarket();
_direction = 1;
}
else if (_direction >= 0 && candle.ClosePrice < _down)
{
if (Position > 0) SellMarket();
SellMarket();
_direction = -1;
}
if (_direction > 0)
currentDown = Math.Max(currentDown, _down);
else if (_direction < 0)
currentUp = Math.Min(currentUp, _up);
_up = currentUp;
_down = currentDown;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class channel_scalper_strategy(Strategy):
def __init__(self):
super(channel_scalper_strategy, self).__init__()
self._stdev_period = self.Param("StdevPeriod", 14) \
.SetDisplay("StdDev Period", "Standard deviation period", "General")
self._multiplier = self.Param("Multiplier", 1.5) \
.SetDisplay("Multiplier", "Channel width multiplier", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle Type", "General")
self._up = 0.0
self._down = 0.0
self._direction = 0
self._is_initialized = False
@property
def stdev_period(self):
return self._stdev_period.Value
@property
def multiplier(self):
return self._multiplier.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(channel_scalper_strategy, self).OnReseted()
self._up = 0.0
self._down = 0.0
self._direction = 0
self._is_initialized = False
def OnStarted2(self, time):
super(channel_scalper_strategy, self).OnStarted2(time)
stdev = StandardDeviation()
stdev.Length = self.stdev_period
self.SubscribeCandles(self.candle_type).Bind(stdev, self.process_candle).Start()
def process_candle(self, candle, stdev_value):
if candle.State != CandleStates.Finished or float(stdev_value) <= 0:
return
middle = (float(candle.HighPrice) + float(candle.LowPrice)) / 2.0
current_up = middle + float(self.multiplier) * float(stdev_value)
current_down = middle - float(self.multiplier) * float(stdev_value)
if not self._is_initialized:
self._up = current_up
self._down = current_down
self._is_initialized = True
return
close = float(candle.ClosePrice)
if self._direction <= 0 and close > self._up:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._direction = 1
elif self._direction >= 0 and close < self._down:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._direction = -1
if self._direction > 0:
current_down = max(current_down, self._down)
elif self._direction < 0:
current_up = min(current_up, self._up)
self._up = current_up
self._down = current_down
def CreateClone(self):
return channel_scalper_strategy()