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
618 views
in Technique[技术] by (71.8m points)

pine script - Plotting manual levels for daily high,low,close

As I've already asked a couple of questions here in an attempt to solve my problem, this new question ties them all together and provides some context.
Previous questions related to this:

I have a program (outside of TradingView) that tries to estimate the high/low/close levels of the ticker SPX for the next trading day.
I'm planning to plot this only on intraday timeframes (anything from 1 minute to 4 hours).
The only ticker I'm going to plot this on is SPX.

Now I'm trying to plot these levels in TradingView, to have a visual representation on how accurate the predicted levels are.
This means that I have a high/low/close value for every trading day.
The current dataset contains about 50 entries (so 50 days where I have a high/low/close level to plot) and will grow by 1 entry every trading day.
So in 1 year, the dataset will contain about 200 entries.

Because the dataset is larger than the number of drawing objects that can be displayed by TradingView, I've ruled out the use of drawing objects like line.new().
The garbage collecter would cause only the last 50 or so drawn objects to be shown, and I'd like the full dataset to be displayed.
Therefore, I think I will need to resort to the plot() function.

This means I will end up with 3 series to plot: high, low and close.
Each will have a different value per trading date.
So essentially, I'm trying to manually create these 3 series by assigning them a value for each date.
But so far, I've failed to accomplish that.
I'm also trying to have only 1 data entry line per trading date to avoid clutter and to keep the code clean and maintainable.
Therefore, I'm trying to set these 3 data points per trading date with only 1 function.

The below script is my latest attempt with a reduced dataset of only 5 trading days for clarity.
I know the script below is wrong, because a variable cannot be re-used.
That's why it says line 19: 'h' is already defined.
I don't have a workaround for this, and I'd really like to find one.

//@version=4
study("Study", overlay=true)

// Variables    
var float dh = na
var float dl = na
var float dc = na

// Functions
isDate(y,m,d) => y==year and m==month and d==dayofmonth ? true:false 
d(y,m,d,h,l,c) => // Daily Levels
    if isDate(y,m,d)
        [h,l,c]
    else
        [na,na,na] 

// Set data for Daily High,Low,Close
[h,l,c] = d(2020,04,13,2800,2700,2725), dh:=h,dl:=l,dc:=c
[h,l,c] = d(2020,04,14,2850,2810,2825), dh:=h,dl:=l,dc:=c
[h,l,c] = d(2020,04,15,2800,2750,2710), dh:=h,dl:=l,dc:=c
[h,l,c] = d(2020,04,16,2850,2700,2790), dh:=h,dl:=l,dc:=c
[h,l,c] = d(2020,04,17,2900,2800,2850), dh:=h,dl:=l,dc:=c

// Plot Daily High,Low,Close
plot(dh, color=color.red)
plot(dl, color=color.green)
plot(dc, color=color.blue)

Another attempt is this code below, which does compile ok.

//@version=4
study("Functions test", overlay=true)

var float h = na
var float l = na
var float c = na

isDate(y,m,d) => y==year and m==month and d==dayofmonth ? true:false
setData() => 
    if isDate(2020,04,13)
        [2800,2700,2725]
    if isDate(2020,04,14)
        [2850,2810,2825]
    if isDate(2020,04,15)
        [2800,2750,2710]
    if isDate(2020,04,16)
        [2850,2700,2790]
    if isDate(2020,04,17)
        [2900,2800,2850]

[h1,l1,c1] = setData()

h := h1
l := l1
c := c1

plot(h,color=color.red)
plot(l,color=color.green)
plot(c,color=color.blue)

The problem here is that it only plots the values for the last date.

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This should scale. Just POC it with lots of lines to be sure:

//@version=4
study("HLC Levels", "", true)

initOnDate(_y, _m, _d, _prev, _init) => 
    if _y == year and _m == month and _d == dayofmonth
        _init
    else
        _prev

float h = na
float l = na
float c = na
int   y = 0
int   m = 0
int   d = 0

y := 2020, m := 03, d := 31, h := initOnDate(y, m, d, h, 2600), l := initOnDate(y, m, d, l, 2500), c := initOnDate(y, m, d, c, 2525)
y := 2020, m := 04, d := 01, h := initOnDate(y, m, d, h, 2610), l := initOnDate(y, m, d, l, 2510), c := initOnDate(y, m, d, c, 2535)
y := 2020, m := 04, d := 02, h := initOnDate(y, m, d, h, 2620), l := initOnDate(y, m, d, l, 2520), c := initOnDate(y, m, d, c, 2545)

plot(h, "h", change(h) ? na : color.green,  1)
plot(l, "l", change(l) ? na : color.red,    1)
plot(c, "c", change(c) ? na : color.orange, 1)

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

...