【问题标题】:Pine Editor not executing strategy.close order even when it shouldPine Editor 不执行 strategy.close 命令,即使它应该执行
【发布时间】:2020-11-11 05:49:31
【问题描述】:

我正在使用 Pine Editor 在 TradingView 上测试随机策略。当 K 线越过 D 线以及 K 80时应做空。当K线下穿D线时应退出做空。

问题:有时它应该退出交易,但事实并非如此。例如,当K线下穿D线且K>80时将进入空头交易,但几根柱线后K线将回穿D线,但空头交易不会平仓。我在这里看到另一个帖子,他们建议使用:close_entries_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)

【问题讨论】:

    标签: pine-script trading algorithmic-trading tradingview-api


    【解决方案1】:

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

    if not (session_open)
        strategy.close("Long")
        strategy.close("Short")
    
    1. 为了结果的一致性,应在全局范围内调用函数(crossovercrossunder
    2. isLongEntryisShortEntry 转换为变量而不是函数,并添加strategy.closeisLongExitisShortExit 参数。
    3. 添加了背景颜色以发现活动会话。

    .

    //@version=4
    strategy("Stochastic Strategy", overlay=false, default_qty_value=100, initial_capital=5000)//, close_entries_rule="ANY")
    
    exit_all_trades = false
    
    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")
    
    crossOver = crossover(k, d)
    crossUnder = crossunder(k, d)
    
    isLongEntry = crossOver and k < 20
    isLongExit = crossUnder
    
    isShortEntry = crossUnder and k > 80
    isShortExit = crossOver
    
    t = time(timeframe.period, "0830-1500")
    session_open = na(t) ? false : true
    
    if (session_open)
        strategy.entry("Long", strategy.long,100, when = isLongEntry)
        strategy.entry("Short", strategy.short,100, when = isShortEntry)
        
        strategy.close("Long", when = isLongExit)
        strategy.close("Short", when = isShortExit)
    else
        strategy.close_all()
    
    bgcolor(session_open ? color.green : na)
    
    plotshape(isLongEntry, style=shape.arrowup, color=color.green, location=location.bottom)
    plotshape(isShortEntry, style=shape.arrowdown, color=color.red, location=location.top)
    

    【讨论】:

    • 这很好用!感谢您还指出脚本中还有改进空间的其他部分。几天前刚开始使用该语言,并且是一个略高于新手级别的编码器。所以,谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多