代码之家  ›  专栏  ›  技术社区  ›  mandroid

如果指示器在某个时间段内或该时间段结束时触发,则返回结果

  •  0
  • mandroid  · 技术社区  · 4 年前

    现在,我使用一个自定义指示符生成一行,它的值是每4小时1,其他时间都是0。我使用该指标生成一个购买警报,我的机器人每次线交叉超过0.5。

    study("Buy every 4 hours")
    is_newbar(res) =>
        t = time(res)
        not na(t) and (na(t[1]) or t > t[1])
    plot(is_newbar("240") ? 1 : 0)
    

    我基本上想要一个DCA策略,如果另一个指标显示有机会的话,它将略微加速买入。

    更新

    多亏了PineCoders,我才能够将第二个指示符合并到信号中,但是我无法弄清楚如果zscoreSignal在上一个周期中有一个1,如何使新的\u周期等于0。

    //@version=4
    study("DCA buys")
    
    buys_per_day = input(6) //number of buys to make per day
    minutes_between_buys = 24 * 60 / buys_per_day
    
    z_score_period = input(30) //period to calculate zscore from
    xStdDev = stdev(close, z_score_period)
    xMA = sma(close, z_score_period)
    zscore = (close - xMA) / xStdDev
    
    is_newbar(res) =>
        t = time(res)
        not na(t) and (na(t[1]) or t > t[1])
    new_period = is_newbar(tostring(minutes_between_buys))
    
    x = crossunder(zscore, -3.1)
    var zscoreXdn = false
    if new_period
        zscoreXdn := false
    else if not zscoreXdn and x
        zscoreXdn := true
    zscoreSignal = zscoreXdn and not zscoreXdn[1]
    
    signal = new_period or zscoreSignal ? 1 : 0
    plot(signal, "signal")
    

    红色的X表示由于之前的Z-Score信号而不应该发生的信号。 enter image description here

    0 回复  |  直到 4 年前
        1
  •  1
  •   AnyDozer    4 年前

    你需要找到并比较与上一个事件时刻的距离 new_period zscoreSignal

    //@version=4
    study("Help (DCA buys)")
    
    buys_per_day = input(6) //number of buys to make per day
    minutes_between_buys = 24 * 60 / buys_per_day
    
    z_score_period = input(30) //period to calculate zscore from
    xStdDev = stdev(close, z_score_period)
    xMA = sma(close, z_score_period)
    zscore = (close - xMA) / xStdDev
    
    is_newbar(res) =>
        t = time(res)
        not na(t) and (na(t[1]) or t > t[1])
    new_period = is_newbar(tostring(minutes_between_buys))
    
    x = crossunder(zscore, 0) // for testing, I had to change the value for the script to work
    var zscoreXdn = false
    if new_period
        zscoreXdn := false
    else if not zscoreXdn and x
        zscoreXdn := true
    zscoreSignal = zscoreXdn and not zscoreXdn[1]
    
    signal = (new_period and barssince(new_period[1]) < barssince(zscoreSignal[1])) or zscoreSignal  ? 1 : 0
    plot(signal, "signal")
    
        2
  •  1
  •   PineCoders-LucF    4 年前

    版本1

    只需将RSI条件添加到您的要求中。请注意,使用此代码,当4小时事件之间发生多个交叉时,您可以在4小时内获得大于1个信号:

    //@version=4
    study("Buy every 4 hours")
    is_newbar(res) =>
        t = time(res)
        not na(t) and (na(t[1]) or t > t[1])
    r = rsi(close, 14)
    signal = is_newbar("240") or crossunder(r, 30) ? 1 : 0
    plot(signal, "signal")
    
    // For validation.
    plot(r, "RSI")
    hline(30)
    plotchar(is_newbar("240"), "is_newbar(240)", "4", location.top, size = size.tiny)
    plotchar(crossunder(r, 30), "crossunder(r, 30)", "X", location.top, size = size.tiny)
    

    enter image description here

    此版本仅允许RSI信号每4小时出现一次:

    //@version=4
    study("Buy every 4 hours")
    is_newbar(res) =>
        t = time(res)
        not na(t) and (na(t[1]) or t > t[1])
    new4HourPeriod = is_newbar("240")
    
    // Trigger an RSI signal only once per period.
    r = rsi(close, 14)
    x = crossunder(r, 30)
    var rsiXdn = false
    if new4HourPeriod
        rsiXdn := false
    else if not rsiXdn and x
        rsiXdn := true
    rsiSignal = rsiXdn and not rsiXdn[1]
    
    signal = new4HourPeriod or rsiSignal ? 1 : 0
    plot(signal, "signal")
    
    // For validation.
    plot(r, "RSI")
    hline(30)
    plotchar(new4HourPeriod, "new4HourPeriod", "4", location.top, size = size.tiny)
    plotchar(rsiXdn, "rsiXdn", "X", location.bottom, size = size.tiny)
    plotchar(rsiSignal, "rsiSignal", "▼", location.bottom, size = size.tiny)
    bgcolor(signal == 1 ? color.green : na)