Coensio Swing Trader V06 Strategy
该策略复现了原始 Coensio Swing Trader 的突破逻辑。通过 Donchian 通道确定动态支撑和阻力。当价格突破上轨或下轨超过设定阈值时开仓。
细节
- 入场:
- 多头:收盘价突破 Donchian 上轨并超出
Entry Threshold点。 - 空头:收盘价跌破 Donchian 下轨并超过
Entry Threshold点。
- 多头:收盘价突破 Donchian 上轨并超出
- 出场:
- 固定的
Stop Loss和Take Profit,以入场价为基准的点数。 - 在获得
Break Even点利润后可移动止损至保本价。 - 可选的追踪止损,在保本后按
Trailing Step点跟随价格。
- 固定的
- 止损:止损、止盈、保本、追踪止损。
- 默认值:
Channel Period= 20Entry Threshold= 15 点Stop Loss= 50 点Take Profit= 80 点Break Even= 25 点Trailing Step= 5 点Enable Trailing= falseCandle Type= 15 分钟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>
/// Coensio Swing Trader based on Donchian channel breakouts with optional trailing stop and break-even.
/// Manually tracks highest high and lowest low over a rolling window.
/// </summary>
public class CoensioSwingTraderV06Strategy : Strategy
{
private readonly StrategyParam<int> _channelPeriod;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<decimal> _takeProfitPercent;
private readonly StrategyParam<DataType> _candleType;
private readonly List<decimal> _highs = new();
private readonly List<decimal> _lows = new();
private decimal _entryPrice;
public int ChannelPeriod { get => _channelPeriod.Value; set => _channelPeriod.Value = value; }
public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
public decimal TakeProfitPercent { get => _takeProfitPercent.Value; set => _takeProfitPercent.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public CoensioSwingTraderV06Strategy()
{
_channelPeriod = Param(nameof(ChannelPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Period for Donchian Channel", "Indicators");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk");
_takeProfitPercent = Param(nameof(TakeProfitPercent), 4m)
.SetDisplay("Take Profit %", "Take profit percent", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_highs.Clear();
_lows.Clear();
_entryPrice = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
StartProtection(
stopLoss: new Unit(StopLossPercent, UnitTypes.Percent),
takeProfit: new Unit(TakeProfitPercent, UnitTypes.Percent)
);
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;
_highs.Add(candle.HighPrice);
_lows.Add(candle.LowPrice);
if (_highs.Count > ChannelPeriod)
_highs.RemoveAt(0);
if (_lows.Count > ChannelPeriod)
_lows.RemoveAt(0);
if (_highs.Count < ChannelPeriod)
return;
var upper = decimal.MinValue;
var lower = decimal.MaxValue;
for (var i = 0; i < _highs.Count - 1; i++)
{
if (_highs[i] > upper) upper = _highs[i];
if (_lows[i] < lower) lower = _lows[i];
}
var price = candle.ClosePrice;
if (price > upper && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
_entryPrice = price;
}
else if (price < lower && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
_entryPrice = price;
}
}
}
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.Strategies import Strategy
class coensio_swing_trader_v06_strategy(Strategy):
def __init__(self):
super(coensio_swing_trader_v06_strategy, self).__init__()
self._channel_period = self.Param("ChannelPeriod", 20) \
.SetGreaterThanZero() \
.SetDisplay("Channel Period", "Period for Donchian Channel", "Indicators")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percent", "Risk")
self._take_profit_percent = self.Param("TakeProfitPercent", 4.0) \
.SetDisplay("Take Profit %", "Take profit percent", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._highs = []
self._lows = []
self._entry_price = 0.0
@property
def channel_period(self):
return self._channel_period.Value
@property
def stop_loss_percent(self):
return self._stop_loss_percent.Value
@property
def take_profit_percent(self):
return self._take_profit_percent.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(coensio_swing_trader_v06_strategy, self).OnReseted()
self._highs = []
self._lows = []
self._entry_price = 0.0
def OnStarted2(self, time):
super(coensio_swing_trader_v06_strategy, self).OnStarted2(time)
self.StartProtection(
Unit(float(self.stop_loss_percent), UnitTypes.Percent),
Unit(float(self.take_profit_percent), UnitTypes.Percent))
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
self._highs.append(float(candle.HighPrice))
self._lows.append(float(candle.LowPrice))
cp = self.channel_period
if len(self._highs) > cp:
self._highs.pop(0)
if len(self._lows) > cp:
self._lows.pop(0)
if len(self._highs) < cp:
return
upper = max(self._highs[:-1])
lower = min(self._lows[:-1])
price = float(candle.ClosePrice)
if price > upper and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = price
elif price < lower and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = price
def CreateClone(self):
return coensio_swing_trader_v06_strategy()