Pine script Pine编辑器未执行策略。即使在应该关闭订单时也关闭订单

Pine script Pine编辑器未执行策略。即使在应该关闭订单时也关闭订单,pine-script,trading,algorithmic-trading,tradingview-api,Pine Script,Trading,Algorithmic Trading,Tradingview Api,我正在使用Pine Editor在TradingView上测试随机策略。每当K线穿过D线并且K80时,应进入短线交易。当K线越过D线时,应退出空头交易 问题是:有时它应该退出交易,但事实并非如此。例如,当K线穿过D线下方且K>80时,它将进入短线交易,但几小节后K线将穿过D线,但短线交易不会结束。我在这里看到另一篇帖子,他们建议使用:close\u entries\u rule=“ANY”。然而,这导致了一个问题,许多订单立即打开和关闭 这是完整的代码,如果您有任何帮助,我们将不胜感激 //@v

我正在使用Pine Editor在TradingView上测试随机策略。每当K线穿过D线并且K<20时,我让代码执行一个长条目。当K线穿过D线下方时,应退出多头交易。当K线穿过D线下方且K>80时,应进入短线交易。当K线越过D线时,应退出空头交易

问题是:有时它应该退出交易,但事实并非如此。例如,当K线穿过D线下方且K>80时,它将进入短线交易,但几小节后K线将穿过D线,但短线交易不会结束。我在这里看到另一篇帖子,他们建议使用:close\u entries\u rule=“ANY”。然而,这导致了一个问题,许多订单立即打开和关闭

这是完整的代码,如果您有任何帮助,我们将不胜感激

//@version=4
strategy("Stochastic Strategy", overlay=false, default_qty_value=100, initial_capital=5000)//, close_entries_rule="ANY")

//strategy.risk.max_drawdown(value=10, type=strategy.percent_of_equity)

exit_all_trades = false
//current_hour = hour
//current_minute = minute
//plot(current_hour)
//plot(current_minute)
//if (current_hour = 15 and current_minute = 58.00)
//    exit_all_trades = true


periodK = input(14, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(3, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.red)
h0 = hline(80, "Upper Band", color=#606060)
h1 = hline(20, "Lower Band", color=#606060)
fill(h0, h1, color=#9915FF, transp=80, title="Background")



isLongEntry() => crossover(k, d) and k < 20
//isLongExit() => k < d
isLongExit() => crossunder(k, d) 

isShortEntry() => crossunder(k, d) and k > 80
//isShortExit() => k > d
isShortExit() => crossover(k, d)




t = time(timeframe.period, "0830-1500")
session_open = na(t) ? false : true

//if (session_open)
strategy.entry("Long", strategy.long,100, when = isLongEntry())

//if (session_open)
strategy.entry("Short", strategy.short,100, when = isShortEntry())

//Close out position before the end of the session.    
if not (session_open)
    strategy.close("Long")
    strategy.close("Short")





//intraday_sess = "0830-1500"
//t = time(timeframe.period, intraday_sess)
//sess_over = not na(t[1]) and na(t)
//strategy.close_all(when = sess_over)
//strategy.close_all(when=(hour==14 and minute==30),comment="force exit")


plotshape(isLongEntry(), style=shape.arrowup, color=color.green, location=location.bottom)
plotshape(isShortEntry(), style=shape.arrowdown, color=color.red, location=location.top)
/@version=4
策略(“随机策略”,叠加=false,默认数量=100,初始资本=5000)/,关闭条目\u rule=“ANY”)
//策略.风险.最大提取(价值=10,类型=strategy.权益百分比)
退出所有交易=错误
//当前小时=小时
//当前分钟=分钟
//绘图(当前小时)
//绘图(当前分钟)
//如果(当前小时=15,当前分钟=58.00)
//退出所有交易=真
周期K=输入(14,title=“K”,minval=1)
periodD=输入(3,title=“D”,minval=1)
smoothK=输入(3,title=“Smooth”,minval=1)
k=sma(stoch(闭合、高、低、周期k)、平滑k)
d=sma(k,周期d)
绘图(k,title=“%k”,color=color.blue)
绘图(d,title=“%d”,color=color.red)
h0=hline(80,“上波段”,颜色=#606060)
h1=hline(20,“下波段”,颜色=#606060)
填充(h0,h1,颜色=#9915FF,传输=80,title=“背景”)
isLongEntry()=>交叉(k,d)和k<20
//isLongExit()=>kcrossunder(k,d)
isShortEntry()=>crossunder(k,d)和k>80
//isShortExit()=>k>d
isShortExit()=>交叉(k,d)
t=时间(时间段,“0830-1500”)
会话开放=na(t)?假:真
//如果(会话打开)
strategy.entry(“Long”,strategy.Long,100,当=isLongEntry()时)
//如果(会话打开)
strategy.entry(“Short”,strategy.Short,100,当=isShortEntry()时)
//会议结束前的收尾位置。
如果没有(会话\u打开)
策略。收盘(“多头”)
策略。结束(“短”)
//日内搜索=“0830-1500”
//t=时间(时间段、日内)
//sess_over=非na(t[1])和na(t)
//策略。全部关闭(当=sess\u结束时)
//策略。全部关闭(时间=(小时==14分钟==30),注释=“强制退出”)
plotshape(isLongEntry(),style=shape.arrowup,color=color.green,location=location.bottom)
plotshape(isShortEntry(),style=shape.arrowdown,color=color.red,location=location.top)

忽略注释行-您编码为仅在会话结束时关闭交易-

if not (session_open)
    strategy.close("Long")
    strategy.close("Short")
  • 为了结果的一致性,应在全局范围内调用函数(
    crossover
    crossunder
  • isLongEntry
    isShortEntry
    转换为变量,而不是函数,并添加了
    策略。使用
    isLongExit
    isShortExit
    参数关闭
  • 添加了背景色以显示活动会话
  • /@version=4
    策略(“随机策略”,叠加=false,默认数量=100,初始资本=5000)/,关闭条目\u rule=“ANY”)
    退出所有交易=错误
    周期K=输入(14,title=“K”,minval=1)
    periodD=输入(3,title=“D”,minval=1)
    smoothK=输入(3,title=“Smooth”,minval=1)
    k=sma(stoch(闭合、高、低、周期k)、平滑k)
    d=sma(k,周期d)
    绘图(k,title=“%k”,color=color.blue)
    绘图(d,title=“%d”,color=color.red)
    h0=hline(80,“上波段”,颜色=#606060)
    h1=hline(20,“下波段”,颜色=#606060)
    填充(h0,h1,颜色=#9915FF,传输=80,title=“背景”)
    交叉=交叉(k,d)
    交叉下=交叉下(k,d)
    isLongEntry=交叉和k<20
    isLongExit=crossUnder
    isShortEntry=交叉,k>80
    isShortExit=交叉
    t=时间(时间段,“0830-1500”)
    会话开放=na(t)?假:真
    如果(会话打开)
    strategy.entry(“Long”,strategy.Long,100,when=isLongEntry)
    strategy.entry(“Short”,strategy.Short,100,when=isShortEntry)
    策略关闭(“长”,当=isLongExit)
    策略关闭(“短”,当=isShortExit时)
    其他的
    策略。全部关闭()
    bgcolor(会话打开?颜色。绿色:na)
    plotshape(isLongEntry,style=shape.arrowup,color=color.green,location=location.bottom)
    plotshape(isShortEntry,style=shape.arrowdown,color=color.red,location=location.top)
    
    忽略注释行-您编码为仅在会话结束时关闭交易-

    if not (session_open)
        strategy.close("Long")
        strategy.close("Short")
    
  • 为了结果的一致性,应在全局范围内调用函数(
    crossover
    crossunder
  • isLongEntry
    isShortEntry
    转换为变量,而不是函数,并添加了
    策略。使用
    isLongExit
    isShortExit
    参数关闭
  • 添加了背景色以显示活动会话
  • /@version=4
    策略(“随机策略”,叠加=false,默认数量=100,初始资本=5000)/,关闭条目\u rule=“ANY”)
    退出所有交易=错误
    周期K=输入(14,title=“K”,minval=1)
    periodD=输入(3,title=“D”,minval=1)
    smoothK=输入(3,title=“Smooth”,minval=1)
    k=sma(stoch(闭合、高、低、周期k)、平滑k)
    d=sma(k,周期d)
    绘图(k,title=“%k”,color=color.blue)
    绘图(d,title=“%d”,color=color.red)
    h0=hline(80,“上波段”,颜色=#606060)
    h1=hline(20,“下波段”,颜色=#606060)
    填充(h0,h1,颜色=#9915FF,传输=80,title=“背景”)
    交叉=交叉(k,d)
    交叉下=交叉下(k,d)
    isLongEntry=交叉和k<20
    isLongExit=crossUnder
    isShortEntry=交叉,k>80
    isShortExit=交叉
    t=时间(时间表)