Ticker Summary
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © apxsfo

//@version=5
indicator('Ticker Summary', overlay=true)

mil_or_bil_or_unknown(val) =>
    val >= 1000000000 ? str.tostring(math.round(val / 1000000000, 1)) + 'B' : val > 0 ? str.tostring(math.round(val / 1000000, 1)) + 'M' : '???'

reticule = input(title='ATR reticule', defval=true)
ma = input.int(title='Moving Average Bars', defval=10, options=[10, 20, 30, 50, 100, 200])
short_ratio_ma_length = input(defval=14, title='Short ratio days')
atrpDays = input(defval=14, title='ATRP days')
textColor = input.color(defval=color.orange, title="Text color")
maExtensionColor = input.color(defval=color.orange, title="Reticule MA extension color")
openExtensionColor = input.color(defval=color.yellow, title="Reticule Close extension color")
balance = input(title='Balance', defval=25000)
maxRisk = input.float(title='Max Risk %', defval=1)

atrValue = request.security(syminfo.tickerid, 'D', ta.atr(atrpDays))
atrp = atrValue / close * 100
atrpExtension = (close - ta.sma(close, ma)) / atrValue

valuePerSecond = ta.sma(volume * ((high + low + close) / 3) / 23400, 14)
rValue = balance * (maxRisk / 100) * close / ta.atr(14)
rFrequency = rValue / valuePerSecond

premarketUpperBand = open + atrValue
premarketDoubleUpperBand = open + atrValue * 2
premarketLowerBand = open - atrValue
premarketDoubleLowerBand = open - atrValue * 2

aveVolume = ta.sma(volume, short_ratio_ma_length)
finra_fnyx = 'QUANDL:FINRA/FNYX_' + syminfo.ticker
finra_fnsq = 'QUANDL:FINRA/FNSQ_' + syminfo.ticker
totalShortInterest = request.security(finra_fnyx, 'D', close, barmerge.gaps_on) + request.security(finra_fnsq, 'D', close, barmerge.gaps_on)
shortRatio = totalShortInterest / aveVolume

floatShares = request.financial(syminfo.tickerid, 'FLOAT_SHARES_OUTSTANDING', 'FY')
totalShares = request.financial(syminfo.tickerid, 'TOTAL_SHARES_OUTSTANDING', 'FY')

summaryText = 'Float: ' + mil_or_bil_or_unknown(floatShares) + '/' + mil_or_bil_or_unknown(totalShares) + '\nMkt cap: ' + mil_or_bil_or_unknown(totalShares * close) + '\nShort ratio: ' + str.tostring(math.round(shortRatio, 2)) + '\nATRP: ' + str.tostring(math.round(atrp, 2)) + '\nR freq: ' + str.tostring(math.round(rFrequency, 2)) + ' sec'
summaryLabel = label.new(bar_index + 4, (low + high)/2, summaryText, textcolor=textColor, color=color.new(color.black, 100), style=label.style_label_upper_left, textalign=text.align_right)
label.delete(summaryLabel[1])

doubleUpperLabel = label.new(bar_index, premarketDoubleUpperBand, textcolor=openExtensionColor, color=color.new(color.black, 100), text=reticule ? '+2' : '', style=label.style_label_right)
upperLabel = label.new(bar_index, premarketUpperBand, textcolor=openExtensionColor, color=color.new(color.black, 100), text=reticule ? '+1' : '', style=label.style_label_right)
openLabel = label.new(bar_index, open, textcolor=openExtensionColor, color=color.new(color.black, 100), text=reticule ? '+0' : '', style=label.style_label_right)
lowerLabel = label.new(bar_index, premarketLowerBand, textcolor=openExtensionColor, color=color.new(color.black, 100), text=reticule ? '-1' : '', style=label.style_label_right)
doubleLowerLabel = label.new(bar_index, premarketDoubleLowerBand, textcolor=openExtensionColor, color=color.new(color.black, 100), text=reticule ? '-2' : '', style=label.style_label_right)
noExtensionLabel = label.new(bar_index, ta.sma(close, ma), textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+0' : '', style=label.style_label_left)
oneExtensionLabel = label.new(bar_index, ta.sma(close, ma) + atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+1' : '', style=label.style_label_left)
twoExtensionLabel = label.new(bar_index, ta.sma(close, ma) + 2 * atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+2' : '', style=label.style_label_left)
threeExtensionLabel = label.new(bar_index, ta.sma(close, ma) + 3 * atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+3' : '', style=label.style_label_left)
oneMinusExtensionLabel = label.new(bar_index, ta.sma(close, ma) - atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+1' : '', style=label.style_label_left)
twoMinusExtensionLabel = label.new(bar_index, ta.sma(close, ma) - 2 * atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+2' : '', style=label.style_label_left)
threeMinusExtensionLabel = label.new(bar_index, ta.sma(close, ma) - 3 * atrValue, textcolor=maExtensionColor, color=color.new(color.black, 100), text=reticule ? '+3' : '', style=label.style_label_left)

label.delete(doubleUpperLabel[1])
label.delete(upperLabel[1])
label.delete(openLabel[1])
label.delete(lowerLabel[1])
label.delete(doubleLowerLabel[1])
label.delete(noExtensionLabel[1])
label.delete(oneExtensionLabel[1])
label.delete(twoExtensionLabel[1])
label.delete(threeExtensionLabel[1])
label.delete(oneMinusExtensionLabel[1])
label.delete(twoMinusExtensionLabel[1])
label.delete(threeMinusExtensionLabel[1])
