Charles 1.3.7 策略
该策略在当前价格上下对称地放置止损挂单,并使用跟踪退出以捕获突破行情。
参数
- Anchor – 挂单与收盘价的距离(价格步长)。
- XFactor – 订单成交量的倍数。
- Trailing Stop – 跟踪止损距离(价格步长)。
- Trailing Profit – 达到该利润后退出仓位。
- Stop Loss – 固定止损距离(价格步长,0 表示禁用)。
- Volume – 基础成交量。
- Candle Type – 使用的蜡烛图时间框架。
交易逻辑
- 没有持仓时,取消现有挂单,并在最近一根蜡烛的收盘价上下
Anchor步长处分别挂出 Buy Stop 和 Sell Stop。 - 仓位建立后,取消另一侧的挂单,并记录入场价格以计算退出条件。
- 多头仓位在利润达到
Trailing Profit或价格回撤Stop Loss时平仓;空头仓位逻辑相同。
该策略演示了使用简单风控的突破交易方法。
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Charles 1.3.7 breakout strategy using symmetric price levels.
/// </summary>
public class Charles137Strategy : Strategy
{
private readonly StrategyParam<decimal> _anchor;
private readonly StrategyParam<decimal> _trailingProfit;
private readonly StrategyParam<decimal> _stopLoss;
private readonly StrategyParam<DataType> _candleType;
private decimal _entryPrice;
private decimal _buyLevel;
private decimal _sellLevel;
private bool _levelsSet;
public decimal Anchor { get => _anchor.Value; set => _anchor.Value = value; }
public decimal TrailingProfit { get => _trailingProfit.Value; set => _trailingProfit.Value = value; }
public decimal StopLossVal { get => _stopLoss.Value; set => _stopLoss.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public Charles137Strategy()
{
_anchor = Param(nameof(Anchor), 200m)
.SetGreaterThanZero()
.SetDisplay("Anchor", "Distance for breakout levels", "General");
_trailingProfit = Param(nameof(TrailingProfit), 500m)
.SetGreaterThanZero()
.SetDisplay("Trailing Profit", "Profit target distance", "General");
_stopLoss = Param(nameof(StopLossVal), 300m)
.SetGreaterThanZero()
.SetDisplay("Stop Loss", "Stop loss distance", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Working timeframe", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
_buyLevel = 0;
_sellLevel = 0;
_levelsSet = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
SubscribeCandles(CandleType)
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished) return;
var price = candle.ClosePrice;
if (Position == 0)
{
if (!_levelsSet)
{
_buyLevel = price + Anchor;
_sellLevel = price - Anchor;
_levelsSet = true;
return;
}
if (price >= _buyLevel)
{
BuyMarket();
_entryPrice = price;
_levelsSet = false;
}
else if (price <= _sellLevel)
{
SellMarket();
_entryPrice = price;
_levelsSet = false;
}
else
{
_buyLevel = price + Anchor;
_sellLevel = price - Anchor;
}
}
else if (Position > 0)
{
var profit = price - _entryPrice;
if (profit >= TrailingProfit || profit <= -StopLossVal)
{
SellMarket();
_levelsSet = false;
}
}
else if (Position < 0)
{
var profit = _entryPrice - price;
if (profit >= TrailingProfit || profit <= -StopLossVal)
{
BuyMarket();
_levelsSet = false;
}
}
}
}
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.Strategies import Strategy
class charles_137_strategy(Strategy):
"""
Charles 1.3.7 breakout strategy using symmetric price levels.
"""
def __init__(self):
super(charles_137_strategy, self).__init__()
self._anchor = self.Param("Anchor", 200.0) \
.SetDisplay("Anchor", "Distance for breakout levels", "General")
self._trailing_profit = self.Param("TrailingProfit", 500.0) \
.SetDisplay("Trailing Profit", "Profit target distance", "General")
self._stop_loss = self.Param("StopLossVal", 300.0) \
.SetDisplay("Stop Loss", "Stop loss distance", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Working timeframe", "General")
self._entry_price = 0.0
self._buy_level = 0.0
self._sell_level = 0.0
self._levels_set = False
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(charles_137_strategy, self).OnReseted()
self._entry_price = 0.0
self._buy_level = 0.0
self._sell_level = 0.0
self._levels_set = False
def OnStarted2(self, time):
super(charles_137_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle):
if candle.State != CandleStates.Finished:
return
price = float(candle.ClosePrice)
if self.Position == 0:
if not self._levels_set:
self._buy_level = price + self._anchor.Value
self._sell_level = price - self._anchor.Value
self._levels_set = True
return
if price >= self._buy_level:
self.BuyMarket()
self._entry_price = price
self._levels_set = False
elif price <= self._sell_level:
self.SellMarket()
self._entry_price = price
self._levels_set = False
else:
self._buy_level = price + self._anchor.Value
self._sell_level = price - self._anchor.Value
elif self.Position > 0:
profit = price - self._entry_price
if profit >= self._trailing_profit.Value or profit <= -self._stop_loss.Value:
self.SellMarket()
self._levels_set = False
elif self.Position < 0:
profit = self._entry_price - price
if profit >= self._trailing_profit.Value or profit <= -self._stop_loss.Value:
self.BuyMarket()
self._levels_set = False
def CreateClone(self):
return charles_137_strategy()