Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

finance - Pinescript: How to get highest, lowest, sma, excluding specific timeframe?

I am trying to calculate KDJ of index, excluding specific timeframe. e.g. 1130-1430. Below is the code I used to calculate KDJ and apply to 1/5/15 mins bars.

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)

However, I dont know how to exclude data within 1130-1430 during the calculation of highest/lowest/sma as part of calculation. For example, I want

  • calculations of 1430 are based on 1121-1130 ( same as calculations of 1130 )
  • calculations of 1431 are based on 1122-1130 and 1430
  • calculations of 1432 are based on 1123-1130 and 1430-1431
  • calculations of 1433 are based on 1124-1130 and 1430-1432
  • ...
  • calculations of 1439 are based on 1130-1130 and 1430-1439
  • calculations of 1440 are based on 1431-1440

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll need arrays to maintain the his/los manually so you can skip the "1130-1429" bars:

//@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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...