finance - 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 内的数据。例如,我想要
- 1430的计算基于1121-1130(与1130的计算相同)
- 1431 的计算基于 1122-1130 和 1430
- 1432 的计算基于 1123-1130 和 1430-1431
- 1433 的计算基于 1124-1130 和 1430-1432
- ...
- 1439 的计算基于 1130-1130 和 1430-1439
- 1440 的计算基于 1431-1440
解决方案
您需要数组来手动维护 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)
推荐阅读
- r - 如何求和对应于组元素的列?
- xmlhttprequest - cypress 和 react-apollo 的兼容性
- python - 如何使用 Python 将 SQL 查询拆分为多个部分、清理它们并获取子句之间的功能?
- json - 是否可以使用新的 clickatell API 添加 FROM 到 SMS?
- c - Node.js 接受请求,形成 2xx 响应,但响应状态码为 400
- c# - 根据分隔符从现有列表创建较小的列表
- android - Recyclerview 字母滚动条
- sql - Oracle SQL 重用用户输入
- python-3.x - 手动添加成本会导致 MissingDataError
- python - 如何在 Python 的列表推导下使用 if 和 else 语句从数字列表中列出奇数和偶数?