该策略复刻 Caudate X Period Candle TM Plus 智能顾问的逻辑。它先对每根蜡烛的开、高、低、收价格进行可配置的平滑处理,再构建平滑 Donchian 区间,并依据蜡烛实体在区间中的位置将已完成蜡烛划分为六种颜色代码。颜色 0/1(看涨下影)触发做多,颜色 5/6(看跌上影)触发做空,对立的颜色组用于平仓。
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>
/// Caudate X Period Candle TM Plus strategy (simplified). Detects candle body/tail
/// patterns using ATR-based filtering and EMA trend direction.
/// </summary>
public class CaudateXPeriodCandleTmPlusStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<int> _emaLength;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int AtrLength
{
get => _atrLength.Value;
set => _atrLength.Value = value;
}
public int EmaLength
{
get => _emaLength.Value;
set => _emaLength.Value = value;
}
public CaudateXPeriodCandleTmPlusStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_atrLength = Param(nameof(AtrLength), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length", "ATR period", "Indicators");
_emaLength = Param(nameof(EmaLength), 20)
.SetGreaterThanZero()
.SetDisplay("EMA Length", "Trend EMA period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var atr = new AverageTrueRange { Length = AtrLength };
var ema = new ExponentialMovingAverage { Length = EmaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(atr, ema, (ICandleMessage candle, decimal atrValue, decimal emaValue) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (atrValue <= 0)
return;
var close = candle.ClosePrice;
var body = Math.Abs(close - candle.OpenPrice);
// Strong body candle in the trend direction.
if (body > atrValue * 0.75m)
{
if (close > candle.OpenPrice && close > emaValue && Position <= 0)
BuyMarket();
else if (close < candle.OpenPrice && close < emaValue && Position >= 0)
SellMarket();
}
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
}
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 AverageTrueRange, ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class caudate_x_period_candle_tm_plus_strategy(Strategy):
def __init__(self):
super(caudate_x_period_candle_tm_plus_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Candles", "General")
self._atr_length = self.Param("AtrLength", 14) \
.SetDisplay("ATR Length", "ATR period", "Indicators")
self._ema_length = self.Param("EmaLength", 20) \
.SetDisplay("EMA Length", "Trend EMA period", "Indicators")
@property
def CandleType(self):
return self._candle_type.Value
@property
def AtrLength(self):
return self._atr_length.Value
@property
def EmaLength(self):
return self._ema_length.Value
def OnStarted2(self, time):
super(caudate_x_period_candle_tm_plus_strategy, self).OnStarted2(time)
atr = AverageTrueRange()
atr.Length = self.AtrLength
ema = ExponentialMovingAverage()
ema.Length = self.EmaLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(atr, ema, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ema)
self.DrawOwnTrades(area)
def _on_process(self, candle, atr_value, ema_value):
if candle.State != CandleStates.Finished:
return
av = float(atr_value)
if av <= 0:
return
close = float(candle.ClosePrice)
open_p = float(candle.OpenPrice)
ev = float(ema_value)
body = abs(close - open_p)
if body > av * 0.75:
if close > open_p and close > ev and self.Position <= 0:
self.BuyMarket()
elif close < open_p and close < ev and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return caudate_x_period_candle_tm_plus_strategy()