Simple Daily Weekly Monthly Yearly
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Toastimane

//@version=4
//Keeps track of the previous close and open of the current security.
study("Daily Weekly Monthly Yearly", shorttitle="DWMY", overlay = true)

//---------------------------- Inputs ------------------------------
var i_dailyOpenPeriod = input(defval = 1, title = "Daily Open Period",  group = "Settings", minval = 0)
var i_dailyClosePeriod = input(defval = 1, title = "Daily Close Period",  group = "Settings", minval = 0)

var i_weeklyOpenPeriod = input(defval = 1, title = "Weekly Open Period",  group = "Settings", minval = 0)
var i_weeklyClosePeriod = input(defval = 1, title = "Weekly Close Period",  group = "Settings", minval = 0)

var i_monthlyOpenPeriod = input(defval = 1, title = "Monthly Open Period",  group = "Settings", minval = 0)
var i_monthlyClosePeriod = input(defval = 1, title = "Monthly Close Period",  group = "Settings", minval = 0)

var i_yearlyOpenPeriod = input(defval = 1, title = "Yearly Open Period",  group = "Settings", minval = 0)
var i_yearlyClosePeriod = input(defval = 1, title = "Yearly Close Period",  group = "Settings", minval = 0)

//---------------------------- Colors ------------------------------
color color1 = color.blue
color color2 = color.green
color color3 = color.red
color color4 = color.yellow

color color5 = color.navy
color color6 = color.olive
color color7 = color.maroon
color color8 = color.orange


[dailyOpen, dailyClose]      = security(syminfo.tickerid, 'D', [open[i_dailyOpenPeriod], close[i_dailyClosePeriod]], lookahead = barmerge.lookahead_on)
[weeklyOpen, weeklyClose]    = security(syminfo.tickerid, 'W', [open[i_weeklyOpenPeriod], close[i_weeklyClosePeriod]], lookahead = barmerge.lookahead_on)
[monthlyOpen, monthlyClose]  = security(syminfo.tickerid, 'M', [open[i_monthlyOpenPeriod], close[i_monthlyClosePeriod]], lookahead = barmerge.lookahead_on)
[yearlyOpen, yearlyClose]    = security(syminfo.tickerid, '12M', [open[i_yearlyOpenPeriod], close[i_yearlyClosePeriod]], lookahead = barmerge.lookahead_on)

//---------------------------- Previous Opens ------------------------------
plot(dailyOpen, "PDO", color1, 3, show_last = 1, trackprice = true)
plot(weeklyOpen, "PWO", color2, 3, show_last = 1, trackprice = true)
plot(monthlyOpen, "PMO", color3, 3, show_last = 1, trackprice = true)
plot(yearlyOpen, "PYO", color4, 3, show_last = 1, trackprice = true)

//---------------------------- Previous Closes ------------------------------
plot(dailyClose, "PDC", color5, 3, show_last = 1, trackprice = true)
plot(weeklyClose, "PWC", color6, 3, show_last = 1, trackprice = true)
plot(monthlyClose, "PMC", color7, 3, show_last = 1, trackprice = true)
plot(yearlyClose, "PYC", color8, 3, show_last = 1, trackprice = true)
