在 GitHub 上查看
TakeProfitTimeGuardStrategy
概述
TakeProfitTimeGuardStrategy 复刻了 MetaTrader 专家 Exp_GTakeProfit_Tm 的核心思路:监控账户整体利润,并在达到目标或超出允许的交易时间段时强制平仓。该策略本身不会开仓,而是作为风险控制层叠加在其他交易策略之上,确保在收益受限或交易时段结束后账户能够及时回到空仓状态。
工作原理
- 订阅可配置的蜡烛数据(默认 1 分钟),利用最近一根蜡烛的收盘价计算已实现与未实现盈亏之和。
- 未实现盈亏通过当前持仓量与收盘价和持仓均价 (
Strategy.PositionPrice) 的差值计算得到。
- 当交易时段仍然开启且总利润为负时,不触发止盈逻辑,与原始 MQL 策略保持一致。
- 一旦达到止盈目标,设置内部
_stop 标志,并持续发送市价单平掉所有剩余仓位;只有在仓位完全归零后才会重置该标志。
- 启用时间过滤后,只要当前时间不在允许区间内,也会执行同样的强制平仓流程。
参数
| 参数 |
类型 |
默认值 |
说明 |
CandleType |
DataType |
1 分钟蜡烛 |
用于评估逻辑的蜡烛序列。 |
TargetMode |
ProfitTargetModes(Percent / Currency) |
Percent |
控制 TakeProfitValue 解释为资本百分比还是绝对货币值。 |
TakeProfitValue |
decimal |
100 |
止盈阈值。必须大于零。 |
UseTradingWindow |
bool |
true |
是否启用时间窗口过滤。 |
StartTime |
TimeSpan |
00:00:00 |
允许交易的起始时间(包含)。 |
EndTime |
TimeSpan |
23:59:00 |
允许交易的结束时间。当起始时间大于结束时间时,时间窗口跨越午夜。 |
行为说明
- 启动时记录投资组合的初始价值(若初值为零,则在首次收到有效数值时记录),用于百分比模式的参考基数。
- 由于未实现盈亏基于最新的蜡烛收盘价,所选时间框架将直接影响策略的反应速度。
- 当利润达到目标时,策略会写入日志并持续发送平仓指令直至仓位清零。
- 启用时间窗口后,只要当前时间不在窗口内,即使未达到止盈目标也会执行同样的平仓流程。
_stop 标志仅在仓位归零后清除,防止在平仓过程中重复触发。
与 MQL 版本的差异
- 使用 StockSharp 的高阶 API
SubscribeCandles,而非逐笔处理行情。
- 通过
Strategy.PositionPrice 计算浮动盈亏,适配 StockSharp 的持仓模型。
- 在达到目标时输出详细日志,便于监控。
- 时间比较基于所订阅蜡烛的
CloseTime。
使用建议
- 将该策略附加在已运行的主交易策略之上,以提供额外的收益与时间控制。
- 根据需要的响应速度选择合适的蜡烛周期;周期越短,控制越及时。
- 确保投资组合提供
CurrentValue 数据,否则百分比模式无法正确计算参考资本。
- 可与
StartProtection() 等 StockSharp 内置风险控制工具配合使用,以构建完整的风控体系。
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>
/// Take Profit Time Guard strategy (simplified). Uses CCI momentum
/// with session time awareness for entries and profit management.
/// </summary>
public class TakeProfitTimeGuardStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cciLength;
private readonly StrategyParam<decimal> _upperLevel;
private readonly StrategyParam<decimal> _lowerLevel;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int CciLength
{
get => _cciLength.Value;
set => _cciLength.Value = value;
}
public decimal UpperLevel
{
get => _upperLevel.Value;
set => _upperLevel.Value = value;
}
public decimal LowerLevel
{
get => _lowerLevel.Value;
set => _lowerLevel.Value = value;
}
public TakeProfitTimeGuardStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_cciLength = Param(nameof(CciLength), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Length", "CCI period", "Indicators");
_upperLevel = Param(nameof(UpperLevel), 100m)
.SetDisplay("Upper Level", "CCI level for sell signal", "Logic");
_lowerLevel = Param(nameof(LowerLevel), -100m)
.SetDisplay("Lower Level", "CCI level for buy signal", "Logic");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var cci = new CommodityChannelIndex { Length = CciLength };
decimal prevCci = 0;
var hasPrev = false;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(cci, (ICandleMessage candle, decimal cciVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!hasPrev)
{
prevCci = cciVal;
hasPrev = true;
return;
}
if (!IsFormedAndOnlineAndAllowTrading())
{
prevCci = cciVal;
return;
}
// CCI crosses up from below lower level
if (prevCci < LowerLevel && cciVal >= LowerLevel && Position <= 0)
BuyMarket();
// CCI crosses down from above upper level
else if (prevCci > UpperLevel && cciVal <= UpperLevel && Position >= 0)
SellMarket();
prevCci = cciVal;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
var cciArea = CreateChartArea();
if (cciArea != null)
DrawIndicator(cciArea, cci);
}
}
}
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.Indicators import CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class take_profit_time_guard_strategy(Strategy):
def __init__(self):
super(take_profit_time_guard_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Candles", "General")
self._cci_length = self.Param("CciLength", 14) \
.SetDisplay("CCI Length", "CCI period", "Indicators")
self._upper_level = self.Param("UpperLevel", 100.0) \
.SetDisplay("Upper Level", "CCI level for sell signal", "Logic")
self._lower_level = self.Param("LowerLevel", -100.0) \
.SetDisplay("Lower Level", "CCI level for buy signal", "Logic")
self._prev_cci = 0.0
self._has_prev = False
@property
def CandleType(self):
return self._candle_type.Value
@property
def CciLength(self):
return self._cci_length.Value
@property
def UpperLevel(self):
return self._upper_level.Value
@property
def LowerLevel(self):
return self._lower_level.Value
def OnReseted(self):
super(take_profit_time_guard_strategy, self).OnReseted()
self._prev_cci = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(take_profit_time_guard_strategy, self).OnStarted2(time)
self._prev_cci = 0.0
self._has_prev = False
cci = CommodityChannelIndex()
cci.Length = self.CciLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(cci, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _on_process(self, candle, cci_value):
if candle.State != CandleStates.Finished:
return
cv = float(cci_value)
if not self._has_prev:
self._prev_cci = cv
self._has_prev = True
return
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_cci = cv
return
if self._prev_cci < self.LowerLevel and cv >= self.LowerLevel and self.Position <= 0:
self.BuyMarket()
elif self._prev_cci > self.UpperLevel and cv <= self.UpperLevel and self.Position >= 0:
self.SellMarket()
self._prev_cci = cv
def CreateClone(self):
return take_profit_time_guard_strategy()