xmd79

Example - HTF Step Interpolation

Jan 3rd, 2023
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. // This source code is free to use, copy, and alter in any way you choose.
  2. // ...but credit is always nice :)
  3.  
  4. //@version=5
  5. //@author=JayRogers
  6.  
  7. indicator('Example - HTF Step Interpolation', 'Interpolation Example', overlay=false)
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // //
  11. // ====== ABOUT THIS INDICATOR //
  12. // //
  13. // - This example aims to highlight an alternative way to view HTF data //
  14. // by providing basic interpolation functionality. This script does //
  15. // not aim to provide use in itself as an indicator or strategy. //
  16. // //
  17. // ====== REASON FOR STUDY //
  18. // //
  19. // - Educational Purposes Only //
  20. // //
  21. // ====== DISCLAIMER //
  22. // //
  23. // Any trade decisions you make are entirely your own responsibility. //
  24. // I've made an effort to squash all the bugs, but you never know! //
  25. // //
  26. ////////////////////////////////////////////////////////////////////////////////
  27.  
  28. ////////////////////////////////////////////////////////////////////////////////
  29. // //
  30. // ====== INPUTS ====== //
  31. // //
  32. ////////////////////////////////////////////////////////////////////////////////
  33.  
  34. i_resolution = input.timeframe('D', 'Resolution')
  35. i_interpolate = input(true, 'Interpolate')
  36.  
  37. ////////////////////////////////////////////////////////////////////////////////
  38. // //
  39. // ====== FUNCTIONS ====== //
  40. // //
  41. ////////////////////////////////////////////////////////////////////////////////
  42.  
  43. // -- helper function for checking if a value is inside a min-max range
  44. f_isInsideRange(_val, _a, _b) =>
  45. _val >= math.min(_a, _b) and _val <= math.max(_a, _b)
  46.  
  47.  
  48. // f_interpolateStep() @JayRogers
  49. //
  50. f_interpolateStep(_source) => //{
  51. // @function:
  52. // f_interpolateStep( float )
  53. // - interpolation for stepped HTF float values from `security()` and
  54. // other similar sources.
  55. // @parameters:
  56. // [ float _source ] stepped float series
  57. // @dependencies:
  58. // na
  59. // @returns:
  60. // float, interpolated result of stepped input.
  61. // @usage:
  62. // f_interpolateStep( series[float] ) -> series[float]
  63.  
  64. // set up some variables to keep track of things
  65. var float _division = 0.0
  66. var float _interpolation = 0.0
  67. var float _prior = 0.0
  68.  
  69. // detect change in the source series
  70. bool _change = _source != _source[1]
  71.  
  72. // track bar count between changes
  73. int _since = ta.barssince(_change)
  74.  
  75. // if we have a change in the higher timeframe series
  76. if _change
  77.  
  78. // keep a record of the previous level for later
  79. _prior := _source[1]
  80.  
  81. // subtract the last step level from the current, and divide by
  82. // value of previous barssince + 1, this should give total bars
  83. // from last step.
  84. _division := (_source - nz(_source[1])) / (nz(_since[1]) + 1)
  85.  
  86. // apply the first addition of the division
  87. _interpolation := nz(_source[1]) + _division
  88. _interpolation
  89. else
  90.  
  91. // if we have no change in the source series
  92. // then we apply subsequent additions of the division to create
  93. // the interpolation slope
  94. _interpolation += _division
  95. _interpolation
  96.  
  97. // ** due to simply utilising the bar count from the previous period to
  98. // calculate the interpolation slope, if the reference timeframe does not
  99. // have the same candle count - month for example, or week with holiday
  100. // gaps - you will get 'kinks' in output. This could be avoided by
  101. // constructing some form of table of data for days in months and holidays
  102. // but since this is just a tech example, we use the following to smoosh
  103. // out those kinks in the return..
  104.  
  105. _return = f_isInsideRange(_interpolation, _prior, _source) ? _interpolation : _source
  106. _return
  107. //}
  108.  
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // //
  111. // ====== SERIES, LINES and LABELS ====== //
  112. // //
  113. ////////////////////////////////////////////////////////////////////////////////
  114.  
  115. // so lets grab some previous period OHLC values in all their stepped glory
  116. [priorOpen, priorHigh, priorLow, priorClose] = request.security(syminfo.tickerid, i_resolution, [open[1], high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
  117.  
  118. ////////////////////////////////////////////////////////////////////////////////
  119. // //
  120. // ====== DRAWING and PLOTTING ====== //
  121. // //
  122. ////////////////////////////////////////////////////////////////////////////////
  123.  
  124. // and plot the user requested results
  125. plot(i_interpolate ? f_interpolateStep(priorOpen) : priorOpen, 'Previous Open', color.new(color.blue, 0), 2)
  126. plot(i_interpolate ? f_interpolateStep(priorHigh) : priorHigh, 'Previous High', color.new(color.red, 0), 2)
  127. plot(i_interpolate ? f_interpolateStep(priorLow) : priorLow, 'Previous Low', color.new(color.green, 0), 2)
  128. plot(i_interpolate ? f_interpolateStep(priorClose) : priorClose, 'Previous Close', color.new(color.yellow, 0), 2)
  129.  
  130. // custom candle plot to help with visualisation
  131. color colCandles = close > open ? color.silver : color.gray
  132. plotcandle(open, high, low, close, 'Candles', color=colCandles, bordercolor=colCandles)
  133.  
  134. // -- PEANUT
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment