xmd79

TPO Project

Sep 24th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 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. // © alphatrader09
  3.  
  4. //@version=4
  5. study("TPO Project", overlay = true, max_lines_count = 500)
  6. tpoSize = input(20)
  7. timeFrame = input("D")
  8. // pltLine = input(title = "TPO Length", type = input.integer, defval = 2)
  9. int i_sessions = input( title='Sessions to display:', defval=50, minval=1, maxval=50, tooltip='Session number to display.')
  10. _uppercol = input(title = "Colors for Value Area High" , type = input.color, defval = #f50000)
  11. _downcol = input(title = "Colors for Value Area Low" , type = input.color, defval = #00c700)
  12. _poccol = input(title = "Color of PoC" , type = input.color, defval = #00bfff)
  13. _pdhl = input(title = "Color of PDH & PDL" , type = input.color, defval = color.gray)
  14.  
  15.  
  16.  
  17. inrange(_tpo,_high,_low)=>
  18. cond = _high >= _tpo and _low <= _tpo
  19.  
  20.  
  21. get_index(_array, up, dn)=>
  22. upval = array.get(_array,up)
  23. dnval = array.get(_array,dn)
  24. index = upval > dnval ? up : dn
  25. val = max(upval, dnval)
  26. [index, val]
  27.  
  28. MarketProfile(res) =>
  29. bool newbar = change(time(res)) != 0
  30. float tfHigh = na
  31. float tfLow = na
  32. int barNum = na
  33. var tpoIndex = array.new_int(tpoSize + 2 , 0)
  34. var tpoValue = array.new_float(tpoSize + 2 , 0)
  35.  
  36.  
  37. tfHigh := newbar ? high : max(tfHigh[1], high)
  38. tfLow := newbar ? low : min(tfLow[1], low)
  39. barNum := newbar ? 0 : barNum[1] + 1
  40. ranGe = tfHigh - tfLow
  41. tpoDiff = ranGe / tpoSize
  42.  
  43. int tpoSum = 0
  44. for x = 0 to tpoSize
  45. _tpovalue = tfLow + (x)* tpoDiff
  46. array.set(tpoValue, x, _tpovalue)
  47. int tpoind = 0
  48. for i = 0 to barNum
  49. if inrange(_tpovalue, high[i], low[i])
  50. tpoind:= tpoind + 1
  51. _tpoIndex = tpoind
  52. array.set(tpoIndex, x, _tpoIndex)
  53. tpoSum := tpoSum + _tpoIndex
  54.  
  55.  
  56. pocLen = array.max(tpoIndex)
  57. pocIndex = array.indexof(tpoIndex,pocLen)
  58. pocValue = tfLow + pocIndex* tpoDiff
  59.  
  60. int vaHighIndex = pocIndex
  61. int vaLowIndex = pocIndex
  62. float valueArea = pocLen
  63. float targetArea = tpoSum * 0.70
  64.  
  65. // for i = 1 to tpoSize
  66. // if valueArea < targetArea
  67. // vaHighIndex := min(tpoSize - 1, vaHighIndex + 1)
  68. // valueArea := valueArea + array.get(tpoIndex,vaHighIndex)
  69. // if valueArea < targetArea
  70. // vaLowIndex := max(0, vaLowIndex - 1)
  71. // valueArea := valueArea + array.get(tpoIndex,vaLowIndex)
  72.  
  73. for x = 0 to tpoSize
  74. if valueArea <= tpoSum * 0.70
  75. tup = min(tpoSize,vaHighIndex + 1)
  76. tdn = max(0,vaLowIndex - 1)
  77. [index,val] = get_index(tpoIndex,tup,tdn)
  78. if index == tdn
  79. vaLowIndex := tdn
  80. valueArea := valueArea + val
  81. else
  82. vaHighIndex := tup
  83. valueArea := valueArea + val
  84.  
  85.  
  86.  
  87. vaHigh = (tfLow + (vaHighIndex ) * tpoDiff)
  88. vaLow = (tfLow + (vaLowIndex) * tpoDiff)
  89.  
  90.  
  91. pocV = valuewhen(newbar, pocValue[1],0)
  92. vaH = valuewhen(newbar, vaHigh[1],0)
  93. vaL = valuewhen(newbar, vaLow[1],0)
  94. maxH = tfHigh - pocV
  95. [pocValue,pocV, vaH, vaL, vaHigh, vaLow]
  96.  
  97.  
  98. [pocValue,pocV, vaH, vaL, vaHigh, vaLow] = MarketProfile(timeFrame)
  99.  
  100. newSession = change(time(timeFrame)) != 0
  101. insession = change(time(timeFrame)) == 0
  102. plot(insession ? pocValue: na, color = _poccol, linewidth = 1, style = plot.style_linebr)
  103. // plot(insession ? vaHigh: na, color = _uppercol, linewidth = 1, style = plot.style_linebr)
  104. // plot(insession ? vaLow: na, color = _downcol, linewidth = 1, style = plot.style_linebr)
  105.  
  106.  
  107. tfHigh = 0.0
  108. tfHigh := newSession ? high : max(tfHigh[1], high)
  109.  
  110. var int previous_session_start = na
  111. var int previous_session_end = na
  112.  
  113. if newSession
  114. previous_session_start := previous_session_end
  115. previous_session_end := time
  116.  
  117. f_Lines_show(_array,max_lines, new_line)=>
  118. int _size = array.size(_array)
  119. if _size >= max_lines
  120. line.delete(array.pop(_array))
  121. array.unshift(_array, new_line)
  122.  
  123. f_Lines_draw(_x1,_y1,_x2,_y2,_col) => line.new(_x1,_y1,_x2, _y2, extend =extend.none, xloc = xloc.bar_time, color =_col, style = line.style_solid, width =2)
  124.  
  125. var line[] valueH_Lines = array.new_line(0)
  126. var line[] valueL_Lines = array.new_line(0)
  127. var line[] poC_Lines = array.new_line(0)
  128.  
  129. if newSession
  130. // _valHline = line.new(previous_session_end, vah_value[1], time_close(i_session_timeframe), vah_value[1], extend = extend.none, xloc = xloc.bar_time, color = color.new(_uppercol,0), width = 2)
  131. // _valLline = line.new(previous_session_end, val_value[1], time_close(i_session_timeframe), val_value[1], extend = extend.none, xloc = xloc.bar_time, color = color.new(_downcol,0), width = 2)
  132. // _pocLine = line.new(previous_session_end, poc[1], time_close(i_session_timeframe), poc[1], extend = extend.none, xloc = xloc.bar_time, color = color.new(_poccol,0), width = 2)
  133. f_Lines_show(valueH_Lines, i_sessions,f_Lines_draw(previous_session_end, vaH, time_close(timeFrame), vaH, _uppercol))
  134. f_Lines_show(valueL_Lines, i_sessions,f_Lines_draw(previous_session_end, vaL, time_close(timeFrame), vaL, _downcol))
  135. f_Lines_show(poC_Lines, i_sessions,f_Lines_draw(previous_session_end, pocV, time_close(timeFrame), pocV, _poccol))
  136.  
Advertisement
Add Comment
Please, Sign In to add comment