RRP Daily
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nyanderfull
//@version=4
study("RRP Flex", format = format.volume, max_labels_count=100)

//Input / Colors and Base lines
plot_lineWidth  = input(1, " Line Width", input.integer, minval=1, maxval=11 )
p1 = plot(0, title="Baseline", linewidth = 0, color=#00000000, display=display.none)
hline(0,color=#FFFFFF35,linestyle=hline.style_solid)

startofnewmonth = month != month[1]
startofnewday = dayofweek != dayofweek[1]

//////US//////
US_TreasuryMA =     security("TREASURY", "1D", close)       //in billions
US_TreasuryWed =    security("WDTGAL", "1D", close)             //in millions
US_TreasuryWA =     security("WTREGEN", "1D", close)         //in billions

US_EFFR =           security("FEDFUNDS", "1D", close[1])                //Rate: no change in %
US_SOFR =           security("SOFR", "1D", close[1])                    //Rate: no change in %
US_OBFR =           security("OBFR", "1D", close[1])                    //Rate: no change in %
US_EFFRVOL =        security("EFFRVOL", "1D", close)         //in billions
US_SOFRVOL =        security("SOFRVOL", "1D", close)        //in billions
US_OBFRVOL =        security("OBFRVOL", "1D", close)         //in billions
US_RCVOL =          (US_EFFRVOL  + US_OBFRVOL)

US_ORRP =           security("RRPONTTLD", "1D", close)       //in billions
US_ORRPR =          security("RRPONTSYAWARD", "1D", close[1])           //no change, in %
US_LRRP =           security("WLRRAL", "1D", close)            //in millions
US_IRRP =           security("WLRRAFOIAL", "1D", close)         //in millions
US_MMFSRP =         security("BOGZ1FL632051103Q", "1D", close)  //in millions
US_ELRRP =          US_ORRP + US_IRRP

    
    
/////Input / Colors
//US_Treasury_plot_color =        input(title="US Treasury Monthly",      type=input.color,   defval=#CCFFFF)         //CCFFFF
//US_MMFSRP_plot_color =          input(title="US MMF SRP",               type=input.color,   defval=#FF26FF)         //FF26FF
US_Treasury_plot_color1 =       input(title="US Treasury Wednsday",     type=input.color,   defval=#00FFFF)         //00FFFF
US_Treasury_plot_color2 =       input(title="US Treasury Weekly",       type=input.color,   defval=color.yellow)    //yellow
  
// US_EFFRVOL_plot_color =         input(title="US EFFR Volume",           type=input.color,   defval=color.green)     //green
// US_OBFRVOL_plot_color =         input(title="US OBFR Volume",           type=input.color,   defval=color.green)     //green
US_SOFRVOL_plot_color =         input(title="US SOFR Volume",           type=input.color,   defval=color.green)     //green
US_RCVOL_plot_color =           input(title="US RCVOL",                 type=input.color,   defval=color.lime)      //lime
        
US_ORRP_plot_color =            input(title="US Overnight RRP",         type=input.color,   defval=#4801ff)         //0026FF
US_IRRP_plot_color =            input(title="US Foreign RRP",           type=input.color,   defval=#CD26FF)         //CD26FF
US_LRRP_plot_color =            input(title="US Liabilities RRP",       type=input.color,   defval=#ff02ff)         //5D26FF
US_ELRRP_plot_color =           input(title="US Expected RRP",          type=input.color,   defval=#ff0143)         //color.red
   


/////Plots
//US_MMFSRP_plot =        plot(US_MMFSRP,         title="US - Money Market Fund SRP (3 Months)",                      color=US_MMFSRP_plot_color,     linewidth = plot_lineWidth, display=display.none),   
//US_Treasury_plot =      plot(US_TreasuryMA,     title="US - Treasury Deposits (Montly Average)",                    color=US_Treasury_plot_color,   linewidth = plot_lineWidth, display=display.all),    //fill(US_Treasury_plot, p1, color=color.blue)
US_TreasuryFR_plot =    plot(US_TreasuryWed,    title="US - Treasury Deposits (Wednesday)",                         color=US_Treasury_plot_color1,  linewidth = plot_lineWidth, display=display.all),    
US_Treasuryhh_plot =    plot(US_TreasuryWA,     title="US - Treasury Deposits (Weekly Average)",                    color=US_Treasury_plot_color2,  linewidth = plot_lineWidth, display=display.all),    

US_RCVOL_plot =         plot(US_RCVOL,          title="US - EFFR + OBFR Volume (Daily)",                            color=US_RCVOL_plot_color,      linewidth = plot_lineWidth)
US_SOFRVOL_plot =       plot(US_SOFRVOL,        title="US - SOFR Volume (Daily)",                                   color=US_SOFRVOL_plot_color,    linewidth = plot_lineWidth)

US_ORRP_plot =          plot(US_ORRP,           title="US - Total Overnight RRP (Daily)",                           color=US_ORRP_plot_color,       linewidth = plot_lineWidth, display=display.all),
US_IRRP_plot =          plot(US_IRRP,           title="US - Foreign / International RRP (Wednesday)",               color=US_IRRP_plot_color,       linewidth = plot_lineWidth, display=display.all),    
US_LRRP_plot =          plot(US_LRRP,           title="US - Liabilities RRP (Wednesday)",                           color=US_LRRP_plot_color,       linewidth = plot_lineWidth, display=display.all), 
US_ELRRP_plot =         plot(US_ELRRP,          title="US - Expected Liabilities RRP (International + Overnight)",  color=US_ELRRP_plot_color,      linewidth = plot_lineWidth, display=display.all),   
US_LRRPtoELRRP_plot =   plot((US_LRRP==US_ELRRP) and startofnewday ? US_ELRRP : na, title="Liabilities overlap",  color=US_ELRRP_plot_color,   style=plot.style_circles,   linewidth=plot_lineWidth+2,    display=display.none)  



/////labels
//EFFR / SOFR / OBFR rates monthly
if ((US_RCVOL != 0) and startofnewmonth)
    string US_RCVOL_Label_Text = "MONTHLY RATES\n"+   tostring(US_EFFR)+"% EFFR\n"+   tostring(US_SOFR)+"% SOFR\n"+  tostring(US_OBFR)+"% OBFR"
    label.new(x=bar_index,      y=0,     text=US_RCVOL_Label_Text,   yloc=yloc.price,    color=US_RCVOL_plot_color,  style=label.style_label_up,   textcolor=color.white)

//RRP Award Rate
if ((US_ORRPR != US_ORRPR[1]) and startofnewday)
    string US_ORRPR_Text = "ORRP Award\n"+tostring(US_ORRPR)+"%"
    label.new(x=bar_index,      y=US_ELRRP,      text=US_ORRPR_Text,         yloc=yloc.price,    color=US_ELRRP_plot_color,       textcolor=color.white)
