Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is free to use, copy, and alter in any way you choose.
- // ...but credit is always nice :)
- //@version=5
- //@author=JayRogers
- indicator('Example - HTF Step Interpolation', 'Interpolation Example', overlay=false)
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // ====== ABOUT THIS INDICATOR //
- // //
- // - This example aims to highlight an alternative way to view HTF data //
- // by providing basic interpolation functionality. This script does //
- // not aim to provide use in itself as an indicator or strategy. //
- // //
- // ====== REASON FOR STUDY //
- // //
- // - Educational Purposes Only //
- // //
- // ====== DISCLAIMER //
- // //
- // Any trade decisions you make are entirely your own responsibility. //
- // I've made an effort to squash all the bugs, but you never know! //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // ====== INPUTS ====== //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- i_resolution = input.timeframe('D', 'Resolution')
- i_interpolate = input(true, 'Interpolate')
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // ====== FUNCTIONS ====== //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // -- helper function for checking if a value is inside a min-max range
- f_isInsideRange(_val, _a, _b) =>
- _val >= math.min(_a, _b) and _val <= math.max(_a, _b)
- // f_interpolateStep() @JayRogers
- //
- f_interpolateStep(_source) => //{
- // @function:
- // f_interpolateStep( float )
- // - interpolation for stepped HTF float values from `security()` and
- // other similar sources.
- // @parameters:
- // [ float _source ] stepped float series
- // @dependencies:
- // na
- // @returns:
- // float, interpolated result of stepped input.
- // @usage:
- // f_interpolateStep( series[float] ) -> series[float]
- // set up some variables to keep track of things
- var float _division = 0.0
- var float _interpolation = 0.0
- var float _prior = 0.0
- // detect change in the source series
- bool _change = _source != _source[1]
- // track bar count between changes
- int _since = ta.barssince(_change)
- // if we have a change in the higher timeframe series
- if _change
- // keep a record of the previous level for later
- _prior := _source[1]
- // subtract the last step level from the current, and divide by
- // value of previous barssince + 1, this should give total bars
- // from last step.
- _division := (_source - nz(_source[1])) / (nz(_since[1]) + 1)
- // apply the first addition of the division
- _interpolation := nz(_source[1]) + _division
- _interpolation
- else
- // if we have no change in the source series
- // then we apply subsequent additions of the division to create
- // the interpolation slope
- _interpolation += _division
- _interpolation
- // ** due to simply utilising the bar count from the previous period to
- // calculate the interpolation slope, if the reference timeframe does not
- // have the same candle count - month for example, or week with holiday
- // gaps - you will get 'kinks' in output. This could be avoided by
- // constructing some form of table of data for days in months and holidays
- // but since this is just a tech example, we use the following to smoosh
- // out those kinks in the return..
- _return = f_isInsideRange(_interpolation, _prior, _source) ? _interpolation : _source
- _return
- //}
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // ====== SERIES, LINES and LABELS ====== //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // so lets grab some previous period OHLC values in all their stepped glory
- [priorOpen, priorHigh, priorLow, priorClose] = request.security(syminfo.tickerid, i_resolution, [open[1], high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // ====== DRAWING and PLOTTING ====== //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // and plot the user requested results
- plot(i_interpolate ? f_interpolateStep(priorOpen) : priorOpen, 'Previous Open', color.new(color.blue, 0), 2)
- plot(i_interpolate ? f_interpolateStep(priorHigh) : priorHigh, 'Previous High', color.new(color.red, 0), 2)
- plot(i_interpolate ? f_interpolateStep(priorLow) : priorLow, 'Previous Low', color.new(color.green, 0), 2)
- plot(i_interpolate ? f_interpolateStep(priorClose) : priorClose, 'Previous Close', color.new(color.yellow, 0), 2)
- // custom candle plot to help with visualisation
- color colCandles = close > open ? color.silver : color.gray
- plotcandle(open, high, low, close, 'Candles', color=colCandles, bordercolor=colCandles)
- // -- PEANUT
Advertisement
Add Comment
Please, Sign In to add comment