首页 > 解决方案 > Pinescript:如何获得最高,最低,sma,不包括特定时间范围?

问题描述

我正在尝试计算指数的 KDJ,不包括特定的时间范围。例如 1130-1430。下面是我用来计算 KDJ 并应用于 1/5/15 分钟柱的代码。

study("My Script")
ilong = 10 // period
isigK = 3
isigD = 3

c = close
h = highest(high, ilong)
l = lowest(low,ilong)
RSV = 100*((c-l)/(h-l))
pK = sma(RSV, isigK)
pD = sma(pK, isigD)
pJ = 3 * pK - 2 * pD
 
plot(pK, color=color.red)
plot(pD, color=color.blue)
plot(pJ, color=color.green)

timeinrange(res, sess) => time(res, sess) != 0
bgcolor(timeinrange(timeframe.period, "1130-1430") ? color.silver : na, transp=0)

但是,作为计算的一部分,我不知道如何在计算最高/最低/sma 期间排除 1130-1430 内的数据。例如,我想要

标签: financepine-script

解决方案


您需要数组来手动维护 his/los,以便您可以跳过“1130-1429”条:

//@version=4
study("")
ilong = 10 // period
isigK = 3
isigD = 3

// Maintain list of last `ilong` his/los manually in arrays.
var his = array.new_float(ilong)
var los = array.new_float(ilong)
notInSession = na(time(timeframe.period, "1130-1429:1234567"))
if notInSession
    // We are outside session time; q and dq new high/low.
    array.shift(his)
    array.push(his, high)
    array.shift(los)
    array.push(los, low)

c = close
h = array.max(his)
l = array.min(los)
RSV = 100*((c-l)/(h-l))
pK = sma(RSV, isigK)
pD = sma(pK, isigD)
pJ = 3 * pK - 2 * pD
 
plot(pK, color=color.red)
plot(pD, color=color.blue)
plot(pJ, color=color.green)

// For session validation in pane.
plotchar(notInSession, "notInSession", "•", location.top, size = size.tiny)
// For validation in Data Window.
plotchar(h, "h", "", location.top, size = size.tiny)
plotchar(l, "l", "", location.top, size = size.tiny)

在此处输入图像描述


推荐阅读