Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. //@version=3
  2. study(title="Smart Volume", shorttitle="SmVol2ex", precision=4) // Edit: Precision 0 > Precision 4
  3.  
  4. // the resolution to build up/down parts of the volume bar
  5. resol = input('15', type=resolution, title="detailed resolution")
  6.  
  7. stackTypeStacked = 'Stacked'
  8. stackTypePlusMinus = 'PlusMinus'
  9. stackType = input(stackTypeStacked, options=[stackTypeStacked, stackTypePlusMinus], title="Style")
  10.  
  11. fixErrorsTypeNone = 'None'
  12. fixErrorsTypeHighlight = 'Highlight'
  13. fixErrorsTypeNormalize = 'Normalize'
  14. fixErrorsType = input(fixErrorsTypeNormalize, options = [fixErrorsTypeNone, fixErrorsTypeHighlight, fixErrorsTypeNormalize], title="What to do with errors")
  15.  
  16.  
  17. len = input(200, type=integer, title="SMA Length") //Edit: Added customizable SMA length
  18. ex5 = "COINBASE:BTCUSD" //Edit: Add additional exchange
  19. ex5Vol = security(ex5, period, volume) //pull vol from other exchange. used in reference to any "volume" declarations
  20. //Need to call coinbase's open and close to edit lines 56 &57
  21. srcopen = security(ex5, period, open)
  22. srcclose = security(ex5, period, close)
  23.  
  24. // DEFINE FUNCTIONS
  25.  
  26. fastSearchN(xs, x) => // xs - sorted, ascending
  27. len = 2048
  28. size = floor(len / 2)
  29. k = size
  30. for i = 0 to 15
  31. size := floor(size / 2)
  32. if (x <= xs[k]?1:0) * (x > xs[k+1]?1:0) > 0
  33. break
  34. if ((xs[k] < x)?1:0) + (na(xs[k])?1:0) > 0
  35. k := k - size
  36. else
  37. k := k + size
  38. if size == 0
  39. if na(xs[k])?1:0 + (1 - (x <= xs[k] ?1:0) * (x > xs[k+1]?1:0)) > 0
  40. k := na
  41. break
  42. k
  43.  
  44. timeToPreviousBar() =>
  45. timeDiff = security(ex5, period, (time-time[1]))
  46. timeDiffRt = security(ex5, period, timenow-time - 2000)
  47. min(timeDiff, timeDiffRt)
  48. // This is working correctly
  49.  
  50. sumLastN(series, back) =>
  51. cummulative = cum(series)
  52. cummulative - cummulative[nz(back, 1)]
  53.  
  54. lowerTfSumOf(v) => //12
  55. ttpb = timeToPreviousBar()
  56. ntpb = fastSearchN(time, time - ttpb)
  57. sumLastN(v, ntpb)
  58. // ttpb and ntpb are working correctly
  59. // So I have validated that timeToPreviousBar() and fastSearchN() are both working properly. That indicates that my problem is in sumLastN. But I don't quite understand ithat one.
  60.  
  61.  
  62. upVolume() => srcopen <= srcclose ? ex5Vol : 0 //edit: volume > ex5Vol, open > srcopen, close > srcclose
  63. downVolume() => srcopen > srcclose ? ex5Vol : 0 //edit: volume > ex5Vol, open > src open, close > srcclose
  64. //These are working correctly
  65.  
  66. // CALL FUNCTIONS THROUGH SECURITY
  67.  
  68. upVol = security(ex5, resol, lowerTfSumOf(upVolume()), lookahead=false)
  69. downVol = security(ex5, resol, lowerTfSumOf(downVolume()), lookahead=false)
  70. //PROBLEM: MY OBSTACLE TO SUCCEsS IS IN THE lowerTfSumOf FUNCTION. IT IS THE COMBINATION OF ALL THE FUNCTIONS
  71. // the issue is I don't understand exactly what all the fuctions do. Particularly the issue is with fastSearchN.
  72. // Now I know ntpb IS working correctly, so that must mean (since it uses fastSearchN) that fastSearchN does not require modification
  73. // the fix is probably to work forward from each function and figure out how to declare it properly to compare values between the referenced exchange and the alt exchange
  74.  
  75. // FIX ERRORS
  76.  
  77. totalVol = upVol + downVol
  78. isError = abs(totalVol / ex5Vol - 1) > 0.05 //edit: volume > ex5Vol
  79.  
  80. bgcolor(isError and fixErrorsType == fixErrorsTypeHighlight ? red : na)
  81.  
  82. upVolFixed = upVol
  83. downVolFixed = downVol
  84. if isError and fixErrorsType == fixErrorsTypeNormalize
  85. if totalVol == 0
  86. upVolFixed := upVolume()
  87. downVolFixed := downVolume()
  88. else
  89. coeff = ex5Vol / totalVol
  90. upVolFixed := upVol * coeff
  91. downVolFixed := downVol * coeff
  92. //edit: line 77 volume > ex5Vol
  93.  
  94. // APPLY STACKED TYPE
  95.  
  96. upPlotValue = upVolFixed
  97. downPlotValue = if stackType == stackTypeStacked
  98. downVolFixed
  99. else
  100. -downVolFixed
  101. //Edit: Line 80 upVolFixed + downVolfixed > downVolFixed
  102.  
  103. // PLOT VOLUME
  104.  
  105. upColor = #63b987
  106. downColor = #eb3d5c
  107. plot(downPlotValue, style=columns, color=downColor, transp=0, linewidth=2) //edit: histogram > columns
  108. plot(upPlotValue, style=columns, color=upColor, transp=0, linewidth=2) //edit: histogram > columns
  109.  
  110. // PLOT SMA
  111.  
  112. upSmaColor = #a3b987
  113. downSmaColor = #ff7050
  114. upSmaVolume = sma(upVolFixed, len) //edit: change length: 25 > "len" variable
  115. downSmaValue = if stackType == stackTypeStacked
  116. sma(downVolFixed, len) //edit: change length: 25 > "len" variable
  117. else
  118. sma(-downVolFixed, len) //edit: change length: 25 > "len" variable
  119. plot(downSmaValue, style=line, color=downSmaColor, title="Down Volume MA")
  120. plot(upSmaVolume, style=line, color=upSmaColor, title="Up Volume MA")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement