Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="Smart Volume", shorttitle="SmVol2ex", precision=4) // Edit: Precision 0 > Precision 4
- // the resolution to build up/down parts of the volume bar
- resol = input('15', type=resolution, title="detailed resolution")
- stackTypeStacked = 'Stacked'
- stackTypePlusMinus = 'PlusMinus'
- stackType = input(stackTypeStacked, options=[stackTypeStacked, stackTypePlusMinus], title="Style")
- fixErrorsTypeNone = 'None'
- fixErrorsTypeHighlight = 'Highlight'
- fixErrorsTypeNormalize = 'Normalize'
- fixErrorsType = input(fixErrorsTypeNormalize, options = [fixErrorsTypeNone, fixErrorsTypeHighlight, fixErrorsTypeNormalize], title="What to do with errors")
- len = input(200, type=integer, title="SMA Length") //Edit: Added customizable SMA length
- ex5 = "COINBASE:BTCUSD" //Edit: Add additional exchange
- ex5Vol = security(ex5, period, volume) //pull vol from other exchange. used in reference to any "volume" declarations
- //Need to call coinbase's open and close to edit lines 56 &57
- srcopen = security(ex5, period, open)
- srcclose = security(ex5, period, close)
- // DEFINE FUNCTIONS
- fastSearchN(xs, x) => // xs - sorted, ascending
- len = 2048
- size = floor(len / 2)
- k = size
- for i = 0 to 15
- size := floor(size / 2)
- if (x <= xs[k]?1:0) * (x > xs[k+1]?1:0) > 0
- break
- if ((xs[k] < x)?1:0) + (na(xs[k])?1:0) > 0
- k := k - size
- else
- k := k + size
- if size == 0
- if na(xs[k])?1:0 + (1 - (x <= xs[k] ?1:0) * (x > xs[k+1]?1:0)) > 0
- k := na
- break
- k
- timeToPreviousBar() =>
- timeDiff = security(ex5, period, (time-time[1]))
- timeDiffRt = security(ex5, period, timenow-time - 2000)
- min(timeDiff, timeDiffRt)
- // This is working correctly
- sumLastN(series, back) =>
- cummulative = cum(series)
- cummulative - cummulative[nz(back, 1)]
- lowerTfSumOf(v) => //12
- ttpb = timeToPreviousBar()
- ntpb = fastSearchN(time, time - ttpb)
- sumLastN(v, ntpb)
- // ttpb and ntpb are working correctly
- // 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.
- upVolume() => srcopen <= srcclose ? ex5Vol : 0 //edit: volume > ex5Vol, open > srcopen, close > srcclose
- downVolume() => srcopen > srcclose ? ex5Vol : 0 //edit: volume > ex5Vol, open > src open, close > srcclose
- //These are working correctly
- // CALL FUNCTIONS THROUGH SECURITY
- upVol = security(ex5, resol, lowerTfSumOf(upVolume()), lookahead=false)
- downVol = security(ex5, resol, lowerTfSumOf(downVolume()), lookahead=false)
- //PROBLEM: MY OBSTACLE TO SUCCEsS IS IN THE lowerTfSumOf FUNCTION. IT IS THE COMBINATION OF ALL THE FUNCTIONS
- // the issue is I don't understand exactly what all the fuctions do. Particularly the issue is with fastSearchN.
- // Now I know ntpb IS working correctly, so that must mean (since it uses fastSearchN) that fastSearchN does not require modification
- // 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
- // FIX ERRORS
- totalVol = upVol + downVol
- isError = abs(totalVol / ex5Vol - 1) > 0.05 //edit: volume > ex5Vol
- bgcolor(isError and fixErrorsType == fixErrorsTypeHighlight ? red : na)
- upVolFixed = upVol
- downVolFixed = downVol
- if isError and fixErrorsType == fixErrorsTypeNormalize
- if totalVol == 0
- upVolFixed := upVolume()
- downVolFixed := downVolume()
- else
- coeff = ex5Vol / totalVol
- upVolFixed := upVol * coeff
- downVolFixed := downVol * coeff
- //edit: line 77 volume > ex5Vol
- // APPLY STACKED TYPE
- upPlotValue = upVolFixed
- downPlotValue = if stackType == stackTypeStacked
- downVolFixed
- else
- -downVolFixed
- //Edit: Line 80 upVolFixed + downVolfixed > downVolFixed
- // PLOT VOLUME
- upColor = #63b987
- downColor = #eb3d5c
- plot(downPlotValue, style=columns, color=downColor, transp=0, linewidth=2) //edit: histogram > columns
- plot(upPlotValue, style=columns, color=upColor, transp=0, linewidth=2) //edit: histogram > columns
- // PLOT SMA
- upSmaColor = #a3b987
- downSmaColor = #ff7050
- upSmaVolume = sma(upVolFixed, len) //edit: change length: 25 > "len" variable
- downSmaValue = if stackType == stackTypeStacked
- sma(downVolFixed, len) //edit: change length: 25 > "len" variable
- else
- sma(-downVolFixed, len) //edit: change length: 25 > "len" variable
- plot(downSmaValue, style=line, color=downSmaColor, title="Down Volume MA")
- plot(upSmaVolume, style=line, color=upSmaColor, title="Up Volume MA")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement