新手小白,写了一个日内量化策略,三均线策略,采用分级止盈,居然能跑起来,很是激动。但一看移动止盈部分始终移动不起来,总是在同一根K线上连续止盈。试了很多办法都不行,问了deepseek说把第第一次触发止盈的当前bar设置一下,不能再次平仓,但这明显也有问题,如果当根bar在第一次止盈后又大幅跳水就完蛋了,这明显也不行。这个是常规的移动止盈策略啊,自己写起来怎么就这么难呢。求助大神帮忙看看问题出在哪里。感谢
//------------------------------------------------------------------------
// 简称: my_test_function
// 名称: 日内短线策略
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
//开仓参数
Numeric FastMAPeriod(5); // 快线周期
Numeric MidMAPeriod(20); // 中线周期
Numeric SlowMAPeriod(60); // 慢线周期
Numeric atrLength(14); // ATR周期
// 止损参数
Numeric InitStopATR_L(2.0); // 多单止损倍数
Numeric ProfitATR_L(2.0); // 多单止盈倍数
Numeric TrailStopATR_L(0.5); // 多单跟踪止盈倍数
Vars
// 指标变量
Series<Numeric> emaFast;
Series<Numeric> emaMid;
Series<Numeric> emaSlow;
// 交易变量
Series<Numeric> stopLossPrice;
Series<Numeric> takeProfitPrice;
Series<Numeric> trailStopPrice;
Bool mytradingtime(False);
Bool startcondition(False);
Numeric ATRValue;
Numeric TrailPrice; // 跟踪最高价(用于回撤计算)
Bool IsPartialProfit(False); // 是否已部分平仓
Numeric myEntryPrice;
Events
OnBar(ArrayRef<Integer> indexs)
{
mytradingtime = (Time >= 0.0900 && Time < 0.1445) || (Time >= 0.2100 && Time < 0.2245);
ATRValue = Average(TrueRange,atrLength); //计算ATR值
emaFast = XAverage(Close, FastMAPeriod);
emaMid = XAverage(Close, MidMAPeriod);
emaSlow = XAverage(Close, SlowMAPeriod);
//画线
PlotNumeric("emaFast",emaFast);
PlotNumeric("emaMid",emaMid);
PlotNumeric("emaSlow",emaSlow);
If(mytradingtime && MarketPosition <> 1)
{
startcondition = emaFast[2] < emaMid[2] && emaFast[1] > emaMid[1] && emaMid[1] > emaSlow[1];
If(startcondition)
{
Buy(2,Open);
myEntryPrice = Open;
stopLossPrice = myEntryPrice - InitStopATR_L * ATRValue;
takeProfitPrice = myEntryPrice + ProfitATR_L * ATRValue;
TrailPrice = High;
IsPartialProfit = False;
Print(" ");
Print("---------------多单---------------");
Print("开仓点位:"+Text(myEntryPrice));
Print("止损点位:"+Text(stopLossPrice));
Print("止盈点位:"+Text(takeProfitPrice));
Print("ATR:"+Text(ATRValue));
Print("开仓时间:"+DateTimeToString(Date+time));
}
}
If(MarketPosition == 1)
{
TrailPrice = Max(TrailPrice,High); //更新持仓期间最高价
If(Low <= stopLossPrice && not IsPartialProfit)
{
If(Open<stopLossPrice) stopLossPrice = Open;
Sell(0,stopLossPrice);
Print("止损时间:"+DateTimeToString(Date+time));
}
If(High >= takeProfitPrice && not IsPartialProfit)
{
If(Open>takeProfitPrice) takeProfitPrice = Open;
Sell(1,takeProfitPrice);
trailStopPrice = TrailPrice - TrailStopATR_L * ATRValue;
IsPartialProfit = True; //标记已经部分平仓
Print("第一次止盈时间:"+DateTimeToString(Date+time));
}
If(IsPartialProfit)
{
If(Open < trailStopPrice) trailStopPrice = Open;
If(Low <= trailStopPrice)
{
Sell(1,trailStopPrice);
Print("第二次止盈时间:"+DateTimeToString(Date+time));
}
}
}
}
//------------------------------------------------------------------------
// 编译版本 2025/03/21 211601
// 版权所有 tb276958
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
// 每一版本的TradeBlazer公式修改和重写的权利
//------------------------------------------------------------------------
分析代码看置顶帖投稿
把你止盈价格打印出来
价格是不一样的,所以不是系统机制的问题
是你计算价格的逻辑不合理
这个只能自己修改策略逻辑咯
我没有说系统机制问题,这个代码哪里逻辑问题能帮忙看看吗
价格设置不合理。
“总是在同一根K线上连续止盈”问题的答案是价格在同一根k线上达到了你设定的两个平仓目标价格。
修改目标价格就行。(或者你本身只能接这样的价格区间,系统当然只能按照你设定的价格执行。)
你是否忽略了建仓后当前bar还没有走完就触发止盈止损的逻辑?
If(Open<stopLossPrice) stopLossPrice = Open;
长阳或者长阴都会触发止盈止损。
不知道我的看法是否正确?
要规避这种情况建议修改止盈止损的判断逻辑。