diff --git a/faq_and_code/README.md b/faq_and_code/README.md index 75cdead..eeec4d6 100644 --- a/faq_and_code/README.md +++ b/faq_and_code/README.md @@ -420,37 +420,29 @@ resFromMinutes = f_resFromMinutes(resInMinutes) f_print("f_resInMinutes() = " + tostring(resInMinutes) +"\nf_resFromMinutes(_minutes) = " + resFromMinutes) ``` -### How can I get the resolution in milliseconds from a string in the `input.resolution` or `timeframe.period` format? -This code presents two ways to go about it. The second method should be preferred, as it is more reliable than the first, which breaks when intervals between bars are irregular, which can happen for many reasons. +### How can I get the resolution in minutes from a string in the `input.resolution` and `timeframe.period` format? ``` //@version=4 -study("f_resolution()", "", true, scale = scale.none) -minutes = 60 * 1000 +study("Resolution in minutes", "") higherRes = input("1D", "Interval used for security() calls", type = input.resolution) -f_print(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(time + (time-time[1])*3, high, _txt, xloc.bar_time, yloc.price, size = size.large) - -// ————— Method 1 (subject to irregularities). -// Use "security()" to fetch delta in ms between bars. -f_resolution(_resolution) => security(syminfo.tickerid, _resolution, change(time)) -higherResInMinutes = f_resolution(higherRes) / minutes -currentResInMinutes = f_resolution(timeframe.period) / minutes -plot(higherResInMinutes, "higherResInMinutes", color.blue, linewidth = 10) -plot(currentResInMinutes, "currentResInMinutes", color.aqua, linewidth = 10) - -// ————— Method 2 (more secure). -// Get higher interval in ms, using only the smallest values retrieved, -// since occasional situations generate higher than normal intervals. -higherResInMs = 10e20 -higherResInMs := min(security(syminfo.tickerid, higherRes, change(time)), nz(higherResInMs[1], 10e20)) -// Get current interval in ms using same technique. -currentResInMs = 10e20 -currentResInMs := min(change(time), nz(currentResInMs[1], 10e20)) -plot(higherResInMs / minutes, "higherTfInt / minutes", color.orange, linewidth = 3) -plot(currentResInMs / minutes, "currentTfInt / minutes", color.yellow, linewidth = 3) -// ————— Plot label. -f_print("METHOD 1\nHigher Resolution = " + tostring(higherResInMinutes) + "\nCurrent resolution = " + tostring(currentResInMinutes) + - "\n⭐METHOD 2⭐\nHigher Resolution = " + tostring(higherResInMs / minutes) + "\nCurrent resolution = " + tostring(currentResInMs / minutes)) +f_tfResInMinutes(_resolution) => + // Returns resolution of _resolution period in minutes. + // _resolution: resolution of other timeframe (in timeframe.period string format). + _mult = security(syminfo.tickerid, _resolution, timeframe.multiplier) + _res = security(syminfo.tickerid, _resolution, timeframe.isseconds ? 1 : timeframe.isintraday ? 2 : timeframe.isdaily ? 3 : timeframe.isweekly ? 4 : timeframe.ismonthly ? 5 : na) + _return = + _res == 1 ? _mult / 60 : + _res == 2 ? _mult : + _res == 3 ? _mult * 1440 : + _res == 4 ? _mult * 10080 : + _res == 5 ? _mult * 43800 : na + +higherResInMinutes = f_tfResInMinutes(higherRes) + +plot(higherResInMinutes, "higherResInMinutes", color.navy, linewidth = 10) +f_print(_txt) => var _lbl = label(na), label.delete(_lbl), _lbl := label.new(time + (time-time[1])*3, higherResInMinutes, _txt, xloc.bar_time, yloc.price, size = size.large) +f_print("Higher Resolution = " + tostring(higherResInMinutes)) ``` ### Is it possible to use `security()` on lower intervals than the chart's current interval?