绘制自定义数据 - 每天 = 正常,每周 = 不正常 [英] Plotting custom data - daily = ok, weekly = not ok
问题描述
这是我的示例脚本
//@version=4研究(",覆盖=真)//颜色C_FORECAST_HIGH = color.redC_FORECAST_LOW = color.redC_FORECAST_CLOSE = color.blueC_ERROR_HIGH = color.yellowC_ERROR_LOW = color.yellowC_ERROR_CLOSE = color.lime//绘图样式P_STYLE_FORECAST = plot.style_line//}//————— 我们绘制水平线时隐藏连接线//当用plot.style_line 或plot.style_linebr 绘图时,线通常是相连的.//我们在日内将水平线绘制为水平线,我们不想看到从今天的水平线到明天的水平线的连接线.f_c_hide_connectline(_series, _color) =>改变(_系列)?#FFFFFF00 : _color//当级别改变时,使绘图线具有00"不透明度.这使得跳跃"不可见.//————— 当指定日期为当前柱的日期时返回真f_is_date(_y,_m,_d) =>_d==dayofmonth 和 _m==month 和 _y==year ?真假//————— 当指定日期在当前柱的同一周时返回真f_is_week(_y,_m,_d) =>ts = 时间戳(_y,_m,_d,0,0,0)如果时间 >= ts 和 weekofyear(ts) == weekofyear真的别的错误的// - - - - - 数据输入//{//————— 每日数据//{f_prediction_daily() =>float _h = na//高float _l = na//低float _c = na//关闭int _y = 0//年份int _m = 0//月份bool _s = true//继续搜索?到达柱线日期时设置为 false,因此我们不会不必要地处理后续 f_is_date() 函数调用_y:=2020,_m:=04//2020 年 4 月的每日数据如果 _s 和 f_is_date(_y,_m,01)_h:=2516,_l:=2418,_c:=2489,_s:=false如果 _s 和 f_is_date(_y,_m,02)_h:=2500,_l:=2411,_c:=2475,_s:=false如果 _s 和 f_is_date(_y,_m,03)_h:=2553,_l:=2484,_c:=2492,_s:=false如果 _s 和 f_is_date(_y,_m,06)_h:=2637,_l:=2579,_c:=2606,_s:=false如果 _s 和 f_is_date(_y,_m,07)_h:=2801,_l:=2749,_c:=2790,_s:=false如果 _s 和 f_is_date(_y,_m,08)_h:=2742,_l:=2660,_c:=2666,_s:=false如果 _s 和 f_is_date(_y,_m,09)_h:=2825,_l:=2758,_c:=2796,_s:=false如果 _s 和 f_is_date(_y,_m,13)_h:=2816,_l:=2757,_c:=2811,_s:=false如果 _s 和 f_is_date(_y,_m,14)_h:=2857,_l:=2798,_c:=2825,_s:=false如果 _s 和 f_is_date(_y,_m,15)_h:=2804,_l:=2759,_c:=2761,_s:=false如果 _s 和 f_is_date(_y,_m,16)_h:=2827,_l:=2790,_c:=2798,_s:=false如果 _s 和 f_is_date(_y,_m,17)_h:=2875,_l:=2850,_c:=2857,_s:=false如果 _s 和 f_is_date(_y,_m,20)_h:=2861,_l:=2818,_c:=2828,_s:=false如果 _s 和 f_is_date(_y,_m,21)_h:=2801,_l:=2746,_c:=2763,_s:=false如果 _s 和 f_is_date(_y,_m,22)_h:=2838,_l:=2790,_c:=2832,_s:=false如果 _s 和 f_is_date(_y,_m,23)_h:=2837,_l:=2808,_c:=2828,_s:=false如果 _s 和 f_is_date(_y,_m,24)_h:=2843,_l:=2800,_c:=2840,_s:=false如果 _s 和 f_is_date(_y,_m,27)_h:=2888,_l:=2847,_c:=2877,_s:=false如果 _s 和 f_is_date(_y,_m,28)_h:=2948,_l:=2914,_c:=2920,_s:=false如果 _s 和 f_is_date(_y,_m,29)_h:=2967,_l:=2918,_c:=2956,_s:=false如果 _s 和 f_is_date(_y,_m,30)_h:=2950,_l:=2910,_c:=2946,_s:=false_y:=2020,_m:=05//2020 年 5 月的每日数据如果 _s 和 f_is_date(_y,_m,01)_h:=2890,_l:=2795,_c:=2800,_s:=false如果 _s 和 f_is_date(_y,_m,04)_h:=2844,_l:=2759,_c:=2798,_s:=false如果 _s 和 f_is_date(_y,_m,05)_h:=2910,_l:=2843,_c:=2870,_s:=false如果 _s 和 f_is_date(_y,_m,06)_h:=2953,_l:=2865,_c:=2881,_s:=false[_h,_l,_c]//返回值//}//————— 每周数据//{f_prediction_weekly() =>var float _h = na//高var float _l = na//低var float _c = na//关闭var int _y = 0//年份var int _m = 0//月var bool _s = true//继续搜索?当达到当前柱的日期时设置为 false,因此我们不会不必要地处理后续 f_is_date() 函数调用_y:=2020,_m:=04//2020 年 4 月的每周数据如果 _s 和 f_is_week(_y,_m,06)_h:=2700,_l:=2400,_c:=2500,_s:=false如果 _s 和 f_is_week(_y,_m,13)_h:=2800,_l:=2700,_c:=2750,_s:=false如果 _s 和 f_is_week(_y,_m,20)_h:=2900,_l:=2750,_c:=2800,_s:=false如果 _s 和 f_is_week(_y,_m,27)_h:=2950,_l:=2775,_c:=2825,_s:=false_y:=2020,_m:=05//2020 年 5 月的周数据如果 _s 和 f_is_week(_y,_m,04)_h:=2900,_l:=2650,_c:=2700,_s:=false[_h,_l,_c]//返回值//}//}[daily_high, daily_low, daily_close ] = f_prediction_daily()[weekly_high,weekly_low,weekly_close] = f_prediction_weekly()canplot_daily = 输入(真,每日")//绘制预测 - 每日情节(canplot_daily?daily_high:na,颜色=f_c_hide_connectline(daily_high,C_FORECAST_HIGH),风格=P_STYLE_FORECAST,标题=每日高")情节(canplot_daily?daily_low:na,颜色=f_c_hide_connectline(daily_low,C_FORECAST_LOW),风格=P_STYLE_FORECAST,标题=每日低")情节(canplot_daily?daily_close:na,颜色=f_c_hide_connectline(daily_close,C_FORECAST_CLOSE),风格=P_STYLE_FORECAST,标题=每日收盘")canplot_weekly = 输入(真,每周")//绘制预测 - 每周情节(canplot_weekly ?weekly_high : na, color=f_c_hide_connectline(weekly_high, C_ERROR_HIGH), style=P_STYLE_FORECAST , title="Weekly High")情节(canplot_weekly ?weekly_low : na, color=f_c_hide_connectline(weekly_low, C_ERROR_LOW), style=P_STYLE_FORECAST , title="Weekly Low")情节(canplot_weekly?weekly_close:na,颜色=f_c_hide_connectline(weekly_close,C_ERROR_CLOSE),风格=P_STYLE_FORECAST,标题=每周关闭")
产生这个图
为了完整起见,这个脚本是我过去问的一系列问题的结果.
请参阅
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
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?
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.
这篇关于绘制自定义数据 - 每天 = 正常,每周 = 不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!