EIA Crude Oil Stock Statistics
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FX365_Thailand

//Revision history
    //v22.0 First release

//@version=5
indicator('EIA Crude Oil Stock Statistics', shorttitle='EIA Crude Oil Stock', overlay=false)

//Constants
usoil_stock_code = 'EIA/PET_WCESTUS1_W'
usoil_SPR_stock_code = 'EIA/WCSSTUS1'

//Logic
//Declare function to get data
get_data_0(datacode) =>
    request.quandl(datacode, barmerge.gaps_off, 0)

//Get data
//Ending stock excluding SPR
data_l_0 = get_data_0(usoil_stock_code)
//Ending stock of SPR
data_l_1 = get_data_0(usoil_SPR_stock_code)


//Calculate stock changes from previous week
diff = data_l_0 - data_l_0[1]

//Calculate % change from previous week
diff_pct = diff / data_l_0[1] * 100

//Plot using plot
plot(data_l_0, color=color.new(color.orange, 0), title='Crude Oil Ending Stock', style=plot.style_columns)
plot(data_l_1, color=color.new(color.blue, 0), title='Crude Oil SPR Ending Stock', style=plot.style_stepline, linewidth=2)
plot(diff, color=color.new(color.red, 0), title='Stock changes', style=plot.style_area)
plot(diff_pct, color=color.new(color.blue, 0), title='% changes')

