为何我的Pine Script策略代码无法触发任何交易入场?
Pine Script策略入场不触发问题排查
问题描述
编写的Pine Script策略无语法错误,但始终无法触发多单入场。代码及预期逻辑如下:
原代码
//@version=5 strategy("calcolo ingresso 4", overlay=true) VAR1A = (close[1]<open[1] and close>open and close>high[1]) plotshape(VAR1A, title = "VAR1A", location=location.belowbar, color=color.rgb(23, 159, 180), style=shape.triangleup, text="VAR1A") var float my_close = na if VAR1A my_close := high[0] var float my_open = na if VAR1A my_open := low[1] var float entry_level = (my_close-((my_close - my_open)/2)) //VERIFICA CONDIZIONI INGRESSO LONG long_condition = false first_cross_index = ta.valuewhen(ta.crossover(close, entry_level), bar_index, 0) last_cross_index = ta.valuewhen(ta.crossover(close, entry_level), bar_index, 4) if last_cross_index > first_cross_index long_condition := true // Inserisci l'ordine di ingresso long if long_condition and VAR1A strategy.entry("buy",strategy.long,qty=5000) // Chiudi l'operazione long se il prezzo chiude sotto entry_level close_condition = false close_cross_index = ta.valuewhen(ta.crossunder(close, entry_level), bar_index, 0) if strategy.position_size > 0 and close_cross_index > 0 and close[close_cross_index] > entry_level close_condition := true if strategy.position_size > 0 and close_condition strategy.close("buy")
预期逻辑
当VAR1A条件满足后,在接下来4根K线内,价格触及触发VAR1A的当前K线高点与前一根K线低点的中间价位(entry_level)时,触发多单入场;持仓状态下,价格收于entry_level下方时平仓。
核心问题及修正方案
1. 入场条件逻辑矛盾
原代码入场条件if long_condition and VAR1A永远无法成立:
VAR1A仅在触发的单根K线为truelong_condition是后续K线才可能满足的条件
两者不可能同时为true,直接导致无法入场。
2. entry_level未动态更新
用var定义entry_level会使其仅初始化一次,后续VAR1A触发时不会更新该值,导致使用的是历史旧值或na。
3. long_condition逻辑完全偏离需求
原代码用ta.valuewhen取第4次交叉的bar索引,和“VAR1A触发后4根K线内触及entry_level”的需求无关,需改为跟踪VAR1A触发后的时间窗口。
4. 平仓条件逻辑错误
原代码中close[close_cross_index] > entry_level与“收于entry_level下方”的逻辑矛盾,且无需用ta.valuewhen复杂化判断。
修正后的代码
//@version=5 strategy("calcolo ingresso 4", overlay=true) // VAR1A 条件:前阴后阳且收盘价突破前高 VAR1A = (close[1]<open[1] and close>open and close>high[1]) plotshape(VAR1A, title = "VAR1A", location=location.belowbar, color=color.rgb(23, 159, 180), style=shape.triangleup, text="VAR1A") // 跟踪VAR1A触发的bar索引及对应的entry_level var int var1a_bar = na var float entry_level = na // 当VAR1A触发时,更新触发bar索引和entry_level if VAR1A var1a_bar := bar_index current_high = high[0] prev_low = low[1] entry_level := current_high - (current_high - prev_low) / 2 // 计算中间价 plot(entry_level, title="Entry Level", color=color.red, style=plot.style_linebr) // 检查是否在VAR1A触发后的4根K线内,且价格上穿entry_level in_time_window = bar_index <= var1a_bar + 4 and not na(var1a_bar) cross_entry = ta.crossover(close, entry_level) long_condition = in_time_window and cross_entry // 多单入场:满足条件且无持仓时入场 if long_condition and strategy.position_size == 0 strategy.entry("buy", strategy.long, qty=5000) // 平仓条件:持仓且收盘价低于entry_level close_condition = strategy.position_size > 0 and close < entry_level if close_condition strategy.close("buy")
修改说明
- 新增
var1a_bar跟踪VAR1A触发的bar索引,判断当前是否在触发后的4根K线窗口内 - 移除
entry_level的var修饰,每次VAR1A触发时重新计算并更新 - 简化入场条件:时间窗口内价格上穿entry_level且无持仓时入场
- 简化平仓条件:持仓且收盘价低于entry_level时直接平仓
内容的提问来源于stack exchange,提问作者Sa La
相关产品推荐
相关产品推荐

