Advertisement
xmd79

TBC

Jan 5th, 2023
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Β© doth4581 (DotH)
  3. // Version 1.0 - 2nd January 2023
  4. // This is an implementation for displaying a tick based chart in Trading View.
  5. //
  6. // Tick based charts are charts with candles that are rendered in the same way as traditional candles.
  7. // However, instead of rendering a new candle at a specific time period,tick based candles are rendered after
  8. // a set number of ticks have occured.
  9. // There are benefits to using ticks based candles, as each candle represents the same number of "price moves"
  10. // For more info see this page (not affiliated with me):
  11. // https://www.centralcharts.com/en/gm/1-learn/7-technical-analysis/29-chart/569-understanding-tick-charts
  12. //
  13. // Limitations/known issues:
  14. // - Currently the indicator has been restricted to 100 candles. This is for limiting the line and box usage to a max of 300 objects.
  15. // - On timeframes above 1 minute, the seconds values will always be 0. In order to be able to see seconds values in the chart scale
  16. // you need to be on a second level chart, which requires a premium TradingView subscription.
  17. // - Changing the parameters in the settings will cause the chart to empty and start redrawing from its first candle again. This is
  18. // because the tick chart is being drawn from realtime data, unlike the standard TradingView charts.
  19. //
  20. // TODOs & Bugs:
  21. // - Add some moving average indicators (SMA, EMA as a minmum)
  22. // - Add a corresponding tick based volume chart
  23. // - Create RSI, MACD, BB variations of this indicator
  24. // if you have any ideas/suggestions or bug reports, please feel free to let me know, however keep in mind
  25. // that I do not have too much spare time to add things, so updates are going to be sporadic.
  26. //
  27. // CODE REUSE:
  28. // If you plan to use this code to make a derivative indicator or strategy, it would be nice to know,
  29. // so let me know if you like!
  30. //
  31.  
  32.  
  33.  
  34. //@version=5
  35. indicator(shorttitle = "TBC", title="Tick based chart [DotH]", overlay=false,max_boxes_count = 100,max_lines_count = 200,max_bars_back = 2)
  36. //Input parameters
  37. int ticks_per_candle=input.int(20,"Ticks per candle",1,1000,1)
  38. int max_candles_in_chart=input.int(50,"Max number of candles to render",5,100,1)
  39. int scale_labels_pos_ticksize=input.int(1,"Time scale labels position below ticksize multiplier",0,100,1)
  40.  
  41. //Initialize our globals
  42. varip openp=open
  43. varip highp=high
  44. varip lowp=low
  45. varip closep=close
  46. varip timep=time
  47. varip tick_count = ticks_per_candle
  48. varip open_prices = array.new_float()
  49. varip high_prices = array.new_float()
  50. varip low_prices = array.new_float()
  51. varip close_prices = array.new_float()
  52. varip close_times = array.new_int()
  53. varip tick_counter = array.new_int(1,ticks_per_candle+1)
  54. varip new_candle = array.new_bool(1,true)
  55. varip openID=0
  56. varip highID=1
  57. varip lowID=2
  58. varip closeID=3
  59. varip timeID=4
  60. varip prices = array.new_float(5,close)
  61.  
  62. //Reset a candle for first use
  63. reset_candle_prices() =>
  64. array.set(prices,openID,close)
  65. array.set(prices,highID,close)
  66. array.set(prices,lowID,close)
  67. array.set(prices,closeID,close)
  68. array.set(prices,timeID,time)
  69.  
  70. //Calculate OHLC of candle
  71. update_candle_prices(tick_price) =>
  72. array.set(prices,timeID,time)
  73. if array.get(new_candle,0)
  74. array.set(prices,openID,tick_price)
  75. array.set(prices,highID,tick_price)
  76. array.set(prices,lowID,tick_price)
  77. array.set(prices,closeID,tick_price)
  78. array.set(new_candle,0,false)
  79. else
  80. array.set(prices,highID,math.max(array.get(prices,highID), tick_price))
  81. array.set(prices,lowID,math.min(array.get(prices,lowID), tick_price))
  82. array.set(prices,closeID,tick_price)
  83.  
  84. //Update and close the last candle and add a new candle
  85. save_candle() =>
  86. array.unshift(open_prices,array.get(prices,openID))
  87. array.unshift(high_prices,array.get(prices,highID))
  88. array.unshift(low_prices,array.get(prices,lowID))
  89. array.unshift(close_prices,array.get(prices,closeID))
  90. array.unshift(close_times,int(array.get(prices,timeID)))
  91. reset_candle_prices()
  92. if (array.size(open_prices)>max_candles_in_chart)
  93. array.pop(open_prices)
  94. array.pop(high_prices)
  95. array.pop(low_prices)
  96. array.pop(close_prices)
  97. array.pop(close_times)
  98.  
  99. //Update values of last candle
  100. update_last_candle() =>
  101. array.set(high_prices,0,array.get(prices,highID))
  102. array.set(low_prices,0,array.get(prices,lowID))
  103. array.set(close_prices,0,array.get(prices,closeID))
  104. array.set(close_times,0,int(array.get(prices,timeID)))
  105.  
  106. //Calculate candles
  107. updatecandles(tick_price) =>
  108. update_candle_prices(tick_price)
  109.  
  110. tick_counter_newval = array.get(tick_counter,0)+1
  111. array.set(tick_counter,0,tick_counter_newval)
  112. if array.get(tick_counter,0) >= tick_count
  113. save_candle()
  114. array.set(tick_counter,0,0)
  115. array.set(new_candle,0,false)
  116.  
  117. //If this is the last bar, calculate our tick based candles
  118. if last_bar_index==bar_index
  119. updatecandles(close)
  120.  
  121. //Clear all previous boxes
  122. a_allBoxes = box.all
  123. if array.size(a_allBoxes) > 0
  124. for i = 0 to array.size(a_allBoxes) - 1
  125. box.delete(array.get(a_allBoxes, i))
  126.  
  127. //Clear all previous labels
  128. a_allLabels = label.all
  129. if array.size(a_allLabels) > 0
  130. for i = 0 to array.size(a_allLabels) - 1
  131. label.delete(array.get(a_allLabels, i))
  132.  
  133. //Clear all previous lines
  134. a_allLines = line.all
  135. if array.size(a_allLines) > 0
  136. for i = 0 to array.size(a_allLines) - 1
  137. line.delete(array.get(a_allLines, i))
  138.  
  139.  
  140. // Calculate scale and time labeling minimum and maximum positions
  141. lowPosT=math.min(array.min(low_prices),array.get(prices,lowID))-syminfo.mintick*scale_labels_pos_ticksize
  142. highPosT=math.max(array.max(high_prices),array.get(prices,highID))+syminfo.mintick*scale_labels_pos_ticksize
  143. //Render past candles
  144. if array.size(open_prices)>0
  145. for i = 0 to array.size(open_prices)-1 by 1
  146. pos=i*3
  147. openp:=array.get(open_prices,i)
  148. highp:=array.get(high_prices,i)
  149. lowp:=array.get(low_prices,i)
  150. closep:=array.get(close_prices,i)
  151. timep:=array.get(close_times,i)
  152. past_candles_color=openp>closep?color.red:color.rgb(37, 190, 150)
  153. box.new(bar_index-pos,math.max(openp,closep),bar_index-pos+2,math.min(openp,closep),past_candles_color,1,line.style_solid,extend.none,xloc.bar_index,past_candles_color)
  154. line.new(bar_index-pos+1,math.max(openp,closep),bar_index-pos+1,highp,color = past_candles_color,width = 2)
  155. line.new(bar_index-pos+1,math.min(openp,closep),bar_index-pos+1,lowp,color = past_candles_color,width = 2)
  156. if pos%30==0 and pos>15
  157. label.new(bar_index-pos+1,lowPosT,str.format_time(timep,'MM/DD-HH:mm:ss'),size = size.tiny,textcolor = color.white,style = label.style_none,yloc = yloc.price,textalign = text.align_center,text_font_family=font.family_monospace)
  158. line.new(bar_index-pos+1,lowPosT,bar_index-pos+1,highPosT,color = color.gray,width = 1,style = line.style_dotted)
  159. //Render current candle
  160. lpos=-3
  161. current_candle_color=array.get(prices,openID)>array.get(prices,closeID)?color.red:color.rgb(37, 190, 150)
  162. box.new(bar_index-lpos,math.max(array.get(prices,openID),array.get(prices,closeID)),bar_index-lpos+2,math.min(array.get(prices,openID),array.get(prices,closeID)),current_candle_color,1,line.style_solid,extend.none,xloc.bar_index,current_candle_color)
  163. line.new(bar_index-lpos+1,math.max(array.get(prices,openID),array.get(prices,closeID)),bar_index-lpos+1,array.get(prices,highID),color = current_candle_color,width = 2)
  164. line.new(bar_index-lpos+1,math.min(array.get(prices,openID),array.get(prices,closeID)),bar_index-lpos+1,array.get(prices,lowID),color = current_candle_color,width = 2)
  165. label.new(bar_index-lpos+1,lowPosT,str.format_time(int(array.get(prices,timeID)),'MM/DD-HH:mm:ss'),size = size.tiny,textcolor = color.white,style = label.style_none,yloc = yloc.price,textalign = text.align_center,text_font_family=font.family_monospace)
  166. line.new(bar_index-lpos+1,lowPosT,bar_index-lpos+1,highPosT,color = color.gray,width = 1,style = line.style_dotted)
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement