Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

Plotting custom data - daily = ok, weekly = not ok

This is my example script

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

// Colors
C_FORECAST_HIGH             = color.red
C_FORECAST_LOW              = color.red
C_FORECAST_CLOSE            = color.blue
C_ERROR_HIGH                = color.yellow
C_ERROR_LOW                 = color.yellow
C_ERROR_CLOSE               = color.lime
// Plot styles
P_STYLE_FORECAST            = plot.style_line
// }

// ————— Hide the connection line when we plot horizontal levels
// When plotting with plot.style_line or plot.style_linebr, lines are normally connected. 
// We are plotting the levels as horizontal lines intraday, and we don't want to see the connection line from one level today to the next level tomorrow.
f_c_hide_connectline(_series, _color) => 
    change(_series) ? #FFFFFF00 : _color     // Make the plot line have "00" opacity when the level changes. This makes the 'jump' invisible.

// ————— Returns true when the specified date is the date of the current bar
f_is_date(_y,_m,_d) => _d==dayofmonth and _m==month and _y==year ? true : false

// ————— Returns true when the specified date is in the same week of the current bar
f_is_week(_y,_m,_d) => 
    ts = timestamp(_y,_m,_d,0,0,0)
    if time >= ts and weekofyear(ts) == weekofyear
        true
    else
        false

// —————————— Data input
// {
// ————— Daily data
// {
f_prediction_daily() =>
    float   _h = na      // High
    float   _l = na      // Low
    float   _c = na      // Close
    int     _y = 0       // Year
    int     _m = 0       // Month
    bool    _s = true    // Keep searching? Set to false when date of bar is reached, so we don't unneccesarily process subsequent f_is_date() function calls

    _y:=2020,_m:=04 // Daily data for April 2020
    if _s and f_is_date(_y,_m,01)   
        _h:=2516,_l:=2418,_c:=2489,_s:=false
    if _s and f_is_date(_y,_m,02)   
        _h:=2500,_l:=2411,_c:=2475,_s:=false
    if _s and f_is_date(_y,_m,03)   
        _h:=2553,_l:=2484,_c:=2492,_s:=false
    if _s and f_is_date(_y,_m,06)   
        _h:=2637,_l:=2579,_c:=2606,_s:=false
    if _s and f_is_date(_y,_m,07)   
        _h:=2801,_l:=2749,_c:=2790,_s:=false
    if _s and f_is_date(_y,_m,08)   
        _h:=2742,_l:=2660,_c:=2666,_s:=false
    if _s and f_is_date(_y,_m,09)   
        _h:=2825,_l:=2758,_c:=2796,_s:=false
    if _s and f_is_date(_y,_m,13)   
        _h:=2816,_l:=2757,_c:=2811,_s:=false
    if _s and f_is_date(_y,_m,14)   
        _h:=2857,_l:=2798,_c:=2825,_s:=false
    if _s and f_is_date(_y,_m,15)   
        _h:=2804,_l:=2759,_c:=2761,_s:=false
    if _s and f_is_date(_y,_m,16)   
        _h:=2827,_l:=2790,_c:=2798,_s:=false
    if _s and f_is_date(_y,_m,17)   
        _h:=2875,_l:=2850,_c:=2857,_s:=false
    if _s and f_is_date(_y,_m,20)   
        _h:=2861,_l:=2818,_c:=2828,_s:=false
    if _s and f_is_date(_y,_m,21)   
        _h:=2801,_l:=2746,_c:=2763,_s:=false
    if _s and f_is_date(_y,_m,22)   
        _h:=2838,_l:=2790,_c:=2832,_s:=false
    if _s and f_is_date(_y,_m,23)   
        _h:=2837,_l:=2808,_c:=2828,_s:=false
    if _s and f_is_date(_y,_m,24)   
        _h:=2843,_l:=2800,_c:=2840,_s:=false
    if _s and f_is_date(_y,_m,27)   
        _h:=2888,_l:=2847,_c:=2877,_s:=false
    if _s and f_is_date(_y,_m,28)   
        _h:=2948,_l:=2914,_c:=2920,_s:=false
    if _s and f_is_date(_y,_m,29)   
        _h:=2967,_l:=2918,_c:=2956,_s:=false
    if _s and f_is_date(_y,_m,30)   
        _h:=2950,_l:=2910,_c:=2946,_s:=false

    _y:=2020,_m:=05 // Daily data for May 2020
    if _s and f_is_date(_y,_m,01)
        _h:=2890,_l:=2795,_c:=2800,_s:=false
    if _s and f_is_date(_y,_m,04)
        _h:=2844,_l:=2759,_c:=2798,_s:=false
    if _s and f_is_date(_y,_m,05)
        _h:=2910,_l:=2843,_c:=2870,_s:=false
    if _s and f_is_date(_y,_m,06)
        _h:=2953,_l:=2865,_c:=2881,_s:=false

    [_h,_l,_c] // Return the values
// }

// ————— Weekly data
// {
f_prediction_weekly() => 
    var float   _h = na      // High
    var float   _l = na      // Low
    var float   _c = na      // Close
    var int     _y = 0       // Year
    var int     _m = 0       // Month
    var bool    _s = true    // Keep searching? Set to false when date of current bar is reached, so we don't unneccesarily process subsequent f_is_date() function calls

    _y:=2020,_m:=04 // Weekly data for April 2020
    if _s and f_is_week(_y,_m,06)
        _h:=2700,_l:=2400,_c:=2500,_s:=false
    if _s and f_is_week(_y,_m,13)
        _h:=2800,_l:=2700,_c:=2750,_s:=false
    if _s and f_is_week(_y,_m,20)
        _h:=2900,_l:=2750,_c:=2800,_s:=false
    if _s and f_is_week(_y,_m,27)
        _h:=2950,_l:=2775,_c:=2825,_s:=false

    _y:=2020,_m:=05 // Weekly data for May 2020
    if _s and f_is_week(_y,_m,04)
        _h:=2900,_l:=2650,_c:=2700,_s:=false

    [_h,_l,_c] // Return the values
// }
// }

[daily_high,  daily_low,  daily_close ] = f_prediction_daily()   
[weekly_high, weekly_low, weekly_close] = f_prediction_weekly() 

canplot_daily  = input(true, "daily")
// Plot forecasts - daily
plot(canplot_daily ? daily_high  : na, color=f_c_hide_connectline(daily_high,  C_FORECAST_HIGH),  style=P_STYLE_FORECAST, title = "Daily High")
plot(canplot_daily ? daily_low   : na, color=f_c_hide_connectline(daily_low,   C_FORECAST_LOW),   style=P_STYLE_FORECAST, title = "Daily Low")
plot(canplot_daily ? daily_close : na, color=f_c_hide_connectline(daily_close, C_FORECAST_CLOSE), style=P_STYLE_FORECAST, title = "Daily Close")

canplot_weekly = input(true, "weekly")
// Plot forecasts - weekly
plot(canplot_weekly ? weekly_high  : na, color=f_c_hide_connectline(weekly_high,  C_ERROR_HIGH),  style=P_STYLE_FORECAST , title="Weekly High")
plot(canplot_weekly ? weekly_low   : na, color=f_c_hide_connectline(weekly_low,   C_ERROR_LOW),   style=P_STYLE_FORECAST , title="Weekly Low")
plot(canplot_weekly ? weekly_close : na, color=f_c_hide_connectline(weekly_close, C_ERROR_CLOSE), style=P_STYLE_FORECAST , title="Weekly Close")

Which yields this plot

Screenshot

For the sake of completeness, this script is the result of a series of questions I asked in the past.
See Plotting manual levels for daily high,low,close for historical info and context.

The above script is designed to be plotted on ticker SPX, and to be viewed on an intraday timeframe.

My question is this:
Why are the predicted levels for the weekly data plotted incorrectly (only one level plotted)?

I'm expecting a different level to be plotted for each week.
However, the weekly plot only seems to be plotting the first data point that it encounters, being

_y:=2020,_m:=04 // Weekly data for April 2020
if _s and f_is_week(_y,_m,06)
    _h:=2700,_l:=2400,_c:=2500,_s:=false

The data retrieval is being done in the same way for daily and weekly data

[daily_high,  daily_low,  daily_close ] = f_prediction_daily()   
[weekly_high, weekly_low, weekly_close] = f_prediction_weekly() 

Therefore, I don't understand the difference in the output.
Why would the daily plot be correct, but not the weekly plot?

I've already debugged by plotting the f_is_week(_y,_m,_d) function, but that function seems to return correct results.

I'm obviously missing something here, but I can't see it.
Does anyone have an idea what causes the weekly plot to only plot one level?

Upvotes: 0

Views: 175

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

Need to delete this var. Otherwise, once it's set to false its state is remembered through all successive function calls, like a static variable, so no further inits can take place.

enter image description here

Upvotes: 1

Related Questions