Economic Calendar Events Nick
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jdehorty
// @version=5

indicator('Economic Calendar Events', overlay=true, scale=scale.none, max_lines_count=500, max_labels_count = 500)

import jdehorty/EconomicCalendar/1 as calendar

// ==================
// ==== Settings ====
// ==================

use = input(true, "only Abbreviation")

show_fomc_meetings = input.bool(defval = true, title = "📅 FOMC", inline = "FOMC", group="⚙️ Settings", tooltip="The FOMC meets eight times a year to determine the course of monetary policy. The FOMC's decisions are announced in a press release at 2:15 p.m. ET on the day of the meeting. The press release is followed by a press conference at 2:30 p.m. ET. The FOMC's decisions are based on a review of economic and financial developments and its assessment of the likely effects of these developments on the economic outlook.")
c_fomcMeeting = input.color(color.new(color.red, 50), title = "Color", group="⚙️ Settings", inline = "FOMC")
t_fomcMeeting = "FOMC Meeting"
show_fomc_minutes = input.bool(defval = true, title = "📅 FOMC Minutes", inline = "FOMCMinutes", group="⚙️ Settings", tooltip="The FOMC minutes are released three weeks after each FOMC meeting. The minutes provide a detailed account of the FOMC's discussion of economic and financial developments and its assessment of the likely effects of these developments on the economic outlook.")
c_fomcMinutes = input.color(color.new(color.orange, 50), title = "Color", group="⚙️ Settings", inline = "FOMCMinutes")
t_fomcMinutes = "FOMC Minutes"
show_ppi = input.bool(defval = true, title = "📅 Producer Price Index (PPI)", inline = "PPI", group="⚙️ Settings", tooltip="The Producer Price Index (PPI) measures changes in the price level of goods and services sold by domestic producers. The PPI is a weighted average of prices of a basket of goods and services, such as transportation, food, and medical care. The PPI is a leading indicator of CPI.")
c_ppi = input.color(color.new(color.yellow, 50), title = "Color", group="⚙️ Settings", inline = "PPI")
t_ppi = use? 'PPI' : 'Producer Price Index'
show_cpi = input.bool(defval = true, title = "📅 Consumer Price Index (CPI)", inline = "CPI", group="⚙️ Settings", tooltip="The Consumer Price Index (CPI) measures changes in the price level of goods and services purchased by households. The CPI is a weighted average of prices of a basket of consumer goods and services, such as transportation, food, and medical care. The CPI-U is the most widely used measure of inflation. The CPI-U is based on a sample of about 87,000 households and measures the change in the cost of a fixed market basket of goods and services purchased by urban consumers.")
c_cpi = input.color(color.new(color.lime, 50), title = "Color", group="⚙️ Settings", inline = "CPI")
t_cpi = use ? 'CPI' : 'Consumer Price Index'
show_csi = input.bool(defval = true, title = "📅 Consumer Sentiment Index (CSI)", inline = "CSI", group="⚙️ Settings", tooltip="The University of Michigan's Consumer Sentiment Index (CSI) is a measure of consumer attitudes about the economy. The CSI is based on a monthly survey of 500 U.S. households. The index is based on consumers' assessment of present and future economic conditions. The CSI is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_csi = input.color(color.new(color.aqua, 50), title = "Color", group="⚙️ Settings", inline = "CSI")
t_csi = use ? 'CSI' : 'Consumer Sentiment Index'
show_cci = input.bool(defval = true, title = "📅 Consumer Confidence Index (CCI)", inline = "CCI", group="⚙️ Settings", tooltip="The Conference Board's Consumer Confidence Index (CCI) is a measure of consumer attitudes about the economy. The CCI is based on a monthly survey of 5,000 U.S. households. The index is based on consumers' assessment of present and future economic conditions. The CCI is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_cci = input.color(color.new(color.fuchsia, 50), title = "Color", group="⚙️ Settings", inline = "CCI")
t_cci = use ? 'CCI' :  'Consumer Confidence Index' 
show_nfp = input.bool(defval = true, title = "📅 Non-Farm Payroll (NFP)", inline = "NFP", group="⚙️ Settings", tooltip="The Non-Farm Payroll (NFP) is a measure of the change in the number of employed persons, excluding farm workers and government employees. The NFP is a leading indicator of consumer spending, which accounts for about two-thirds of U.S. economic activity.")
c_nfp = input.color(color.new(color.silver, 50), title = "Color", group="⚙️ Settings", inline = "NFP")
t_nfp = use ? 'NFP' : 'Non-Farm Payroll'
// show_legend = input.bool(true, "Show Legend", group="⚙️ Settings", inline = "Legend", tooltip="Show the color legend for the economic calendar events.")

// =======================
// ==== Dates & Times ====
// =======================

getUnixTime(_eventArr, _index) => 
    switch 
        timeframe.isdaily and timeframe.multiplier > 1 => array.get(_eventArr, _index) - timeframe.multiplier*86400000 // -n days
        timeframe.isweekly => array.get(_eventArr, _index) - timeframe.multiplier*604800000 // -n week(s)
        timeframe.ismonthly => array.get(_eventArr, _index) - timeframe.multiplier*2592000000 // -n month(s)
        timeframe.isminutes and timeframe.multiplier > 60 => array.get(_eventArr, _index) - timeframe.multiplier*60000 // -n minutes
        => array.get(_eventArr, _index)

// Note: An offset of -n units is needed to realign events with the timeframe in which they occurred
if show_fomc_meetings
    fomcMeetingsArr = calendar.fomcMeetings()
    for i = 0 to array.size(fomcMeetingsArr) - 1
        unixTime = getUnixTime(fomcMeetingsArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_fomcMeeting, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_fomcMeeting, text=t_fomcMeeting, color=color.new(color.blue, 100))
if show_fomc_minutes
    fomcMinutesArr = calendar.fomcMinutes()
    for i = 0 to array.size(fomcMinutesArr) - 1
        unixTime = getUnixTime(fomcMinutesArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_fomcMinutes, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_fomcMinutes, text=t_fomcMinutes, color=color.new(color.blue, 100))
if show_ppi
    ppiArr = calendar.ppiReleases()
    for i = 0 to array.size(ppiArr) - 1
        unixTime = getUnixTime(ppiArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_ppi, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_ppi, text=t_ppi, color=color.new(color.blue, 100))
if show_cpi
    cpiArr = calendar.cpiReleases()
    for i = 0 to array.size(cpiArr) - 1
        unixTime = getUnixTime(cpiArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_cpi, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_cpi, text=t_cpi, color=color.new(color.blue, 100))
if show_csi
    csiArr = calendar.csiReleases()
    for i = 0 to array.size(csiArr) - 1
        unixTime = getUnixTime(csiArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_csi, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_csi, text=t_csi, color=color.new(color.blue, 100))
if show_cci
    cciArr = calendar.cciReleases()
    for i = 0 to array.size(cciArr) - 1
        unixTime = getUnixTime(cciArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_cci, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_cci, text=t_cci, color=color.new(color.blue, 100))
if show_nfp
    nfpArr = calendar.nfpReleases()
    for i = 0 to array.size(nfpArr) - 1
        unixTime = getUnixTime(nfpArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_nfp, width=2, xloc=xloc.bar_time)
        label.new(unixTime,y = 0, yloc=yloc.price, xloc=xloc.bar_time, textcolor=c_nfp, text=t_nfp, color=color.new(color.blue, 100))
// // ================
// // ==== Legend ====
// // ================
// if show_legend
//     var tbl = table.new(position.top_right, columns=1, rows=8, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.black, 100))
//     units = timeframe.isminutes ? "m" : ""
//     if barstate.islast
//         table.cell(tbl, 0, 0, syminfo.ticker + ' | ' + str.tostring(timeframe.period) + units, text_halign=text.align_center, text_color=color.gray, text_size=size.normal)
//         table.cell(tbl, 0, 1, 'FOMC Meeting', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_fomcMeeting, 10), text_size=size.small)
//         table.cell(tbl, 0, 2, 'FOMC Minutes', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_fomcMinutes, 10), text_size=size.small)
//         table.cell(tbl, 0, 3, 'Producer Price Index (PPI)', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_ppi, 10), text_size=size.small)
//         table.cell(tbl, 0, 4, 'Consumer Price Index (CPI)', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_cpi, 10), text_size=size.small)
//         table.cell(tbl, 0, 5, 'Consumer Sentiment Index (CSI)', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_csi, 10), text_size=size.small)
//         table.cell(tbl, 0, 6, 'Consumer Confidence Index (CCI)', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_cci, 10), text_size=size.small)
//         table.cell(tbl, 0, 7, 'Non-Farm Payrolls (NFP)', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_nfp, 10), text_size=size.small)