You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用input.bool排除新闻交易日功能失效,求排查代码问题

代码错误分析与修正方案

核心错误点

  • 逻辑运算符优先级错误:所有no_trade_day_*变量的定义中,and与or的优先级处理不当,导致日期判断逻辑完全失效。比如原代码中no_trade_day_dec22会判定任意年份任意月份的13/14/2号都属于禁止交易日,而非仅2022年12月的这几天。
  • NewsExcluded逻辑完全颠倒:当前代码中NewsExcluded的判断逻辑与需求完全不符,开启FilterNewsTrading时,实际是在判断当前日期是否同时满足三个时间段的禁止日(这几乎不可能发生),而非排除任何一个禁止交易日。

修正后的代码

//@version=5
strategy("News Filter Strategy", overlay=true)

FilterNewsTrading = input.bool(defval=false, title='Filter Newstrading')

// 修正逻辑:用括号包裹同一年月的所有禁止日期,确保优先级正确
no_trade_day_dec22 = (year == 2022) and (month == 12) and (dayofmonth == 13 or dayofmonth == 14 or dayofmonth == 2) // 13.12.2022 CPI // 14.12.2022 Interest Rate // 02.12.2022 NFP
no_trade_day_jan23 = (year == 2023) and (month == 1) and (dayofmonth == 12 or dayofmonth == 6) // 12.01.23 CPI // 06.01.2023 NFP
no_trade_day_feb23 = (year == 2023) and (month == 2) and (dayofmonth == 1 or dayofmonth == 3) // 01.02.23 Interest Rate // 03.02.2023 NFP

// 修正逻辑:开启过滤时,当前日期不属于任何禁止交易日则允许交易
isNewsDay = no_trade_day_dec22 or no_trade_day_jan23 or no_trade_day_feb23
NewsExcluded = FilterNewsTrading ? not isNewsDay : true

// 假设inDateRange和LongEntry是已定义的变量,替换为你的实际逻辑
inDateRange = true 
LongEntry = true 

if (inDateRange and LongEntry and NewsExcluded)
    strategy.entry("long", strategy.long, comment="entry long") 

修正说明

  1. 修复日期判断逻辑:给每个时间段的dayofmonth判断加上括号,确保year和month的条件能覆盖所有对应日期,避免跨年月的错误判定。
  2. 重构NewsExcluded逻辑:
    • 先定义isNewsDay变量,判断当前日期是否属于任何一个禁止交易日
    • 当FilterNewsTrading开启时,NewsExcluded取not isNewsDay(即非新闻交易日才允许交易);关闭过滤时直接返回true(不限制交易)
  3. 优化可读性:拆分逻辑变量,让每一步的判断逻辑更清晰直观。

内容的提问来源于stack exchange,提问作者honeybadger21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 01:50:28