Reputation: 1
How to fetch OHLC data based on expiry periods for multiple time frames (Weekly, Monthly, Quarterly, Semi-Annually, and Annually) in Pine Script?
Define Expiry Rules:
Weekly: Expiry is the last trading day of the week (e.g., Thursday).
Monthly: Expiry is the last Thursday of the month.
Quarterly: Expiry is the last Thursday of the last month of the quarter (March, June, September, December).
Semi-Annually: Expiry is the last Thursday of June and December.
Annually: Expiry is the last Thursday of December.
how can this approach to get the ohlc data of the expiry based ohlc
Thanks in advance
//@version=6
indicator('MTF OHLC plot', overlay = true)
// Function to find the last Thursday of a given month
last_thursday_of_month(input_year, input_month) =>
days_in_month = array.new_int()
array.push(days_in_month, 31) // January
array.push(days_in_month, 28) // February
array.push(days_in_month, 31) // March
array.push(days_in_month, 30) // April
array.push(days_in_month, 31) // May
array.push(days_in_month, 30) // June
array.push(days_in_month, 31) // July
array.push(days_in_month, 31) // August
array.push(days_in_month, 30) // September
array.push(days_in_month, 31) // October
array.push(days_in_month, 30) // November
array.push(days_in_month, 31) // December
days = array.get(days_in_month, input_month - 1)
if input_month == 2 and input_year % 4 == 0 and (input_year % 100 != 0 or input_year % 400 == 0)
days := 29 // Adjust for leap year
days
var int last_thursday = na
for day = 1 to days by 1
if dayofweek(timestamp(input_year, input_month, day)) == dayofweek.thursday
last_thursday := timestamp(input_year, input_month, day)
last_thursday
last_thursday
// Expiry Calculation Function
calc_expiry(input_year, input_month, timeframe) =>
if timeframe == 'Weekly'
timestamp(input_year, input_month, dayofmonth(time) + 7 - dayofweek(time)) // Last trading day of the week
else if timeframe == 'Monthly'
last_thursday_of_month(input_year, input_month) // Last Thursday of the month
else if timeframe == 'Quarterly'
q_month = input_month % 3 == 0 ? input_month : input_month + 3 - input_month % 3
last_thursday_of_month(input_year, q_month) // Last Thursday of the quarter
else if timeframe == 'Semi-Annually'
semi_month = input_month <= 6 ? 6 : 12
last_thursday_of_month(input_year, semi_month) // Last Thursday of June or December
else if timeframe == 'Annually'
last_thursday_of_month(input_year, 12) // Last Thursday of December
else
na
// Expiry Timeframes
weekly_expiry = calc_expiry(year(time), month(time), 'Weekly')
monthly_expiry = calc_expiry(year(time), month(time), 'Monthly')
quarterly_expiry = calc_expiry(year(time), month(time), 'Quarterly')
semiannual_expiry = calc_expiry(year(time), month(time), 'Semi-Annually')
annual_expiry = calc_expiry(year(time), month(time), 'Annually')
// Variables to store OHLC for each timeframe
var float weekly_open = na
var float weekly_high = na
var float weekly_low = na
var float weekly_close = na
var float monthly_open = na
var float monthly_high = na
var float monthly_low = na
var float monthly_close = na
var float quarterly_open = na
var float quarterly_high = na
var float quarterly_low = na
var float quarterly_close = na
var float semiannual_open = na
var float semiannual_high = na
var float semiannual_low = na
var float semiannual_close = na
var float annual_open = na
var float annual_high = na
var float annual_low = na
var float annual_close = na
// Capture OHLC for each timeframe
if time == weekly_expiry
weekly_open := open
weekly_high := high
weekly_low := low
weekly_close := close
label.new(bar_index, high, 'Weekly Expiry!', style = label.style_label_down, color = color.green)
if time == monthly_expiry
monthly_open := open
monthly_high := high
monthly_low := low
monthly_close := close
label.new(bar_index, high, 'Monthly Expiry!', style = label.style_label_down, color = color.blue)
if time == quarterly_expiry
quarterly_open := open
quarterly_high := high
quarterly_low := low
quarterly_close := close
label.new(bar_index, high, 'Quarterly Expiry!', style = label.style_label_down, color = color.red)
if time == semiannual_expiry
semiannual_open := open
semiannual_high := high
semiannual_low := low
semiannual_close := close
label.new(bar_index, high, 'Semi-Annual Expiry!', style = label.style_label_down, color = color.orange)
if time == annual_expiry
annual_open := open
annual_high := high
annual_low := low
annual_close := close
label.new(bar_index, high, 'Annual Expiry!', style = label.style_label_down, color = color.purple)
// Visualization
plot(weekly_open, title = 'Weekly Open', color = color.green, linewidth = 1, style = plot.style_circles)
plot(weekly_high, title = 'Weekly High', color = color.red, linewidth = 1, style = plot.style_circles)
plot(weekly_low, title = 'Weekly Low', color = color.blue, linewidth = 1, style = plot.style_circles)
plot(weekly_close, title = 'Weekly Close', color = color.yellow, linewidth = 1, style = plot.style_circles)
plot(monthly_open, title = 'Monthly Open', color = color.new(color.green, 20), linewidth = 1, style = plot.style_circles)
plot(monthly_high, title = 'Monthly High', color = color.new(color.red, 20), linewidth = 1, style = plot.style_circles)
plot(monthly_low, title = 'Monthly Low', color = color.new(color.blue, 20), linewidth = 1, style = plot.style_circles)
plot(monthly_close, title = 'Monthly Close', color = color.new(color.yellow, 20), linewidth = 1, style = plot.style_circles)
plot(quarterly_open, title = 'Quarterly Open', color = color.new(color.green, 40), linewidth = 1, style = plot.style_circles)
plot(quarterly_high, title = 'Quarterly High', color = color.new(color.red, 40), linewidth = 1, style = plot.style_circles)
plot(quarterly_low, title = 'Quarterly Low', color = color.new(color.blue, 40), linewidth = 1, style = plot.style_circles)
plot(quarterly_close, title = 'Quarterly Close', color = color.new(color.yellow, 40), linewidth = 1, style = plot.style_circles)
plot(semiannual_open, title = 'Semi-Annual Open', color = color.new(color.green, 60), linewidth = 1, style = plot.style_circles)
plot(semiannual_high, title = 'Semi-Annual High', color = color.new(color.red, 60), linewidth = 1, style = plot.style_circles)
plot(semiannual_low, title = 'Semi-Annual Low', color = color.new(color.blue, 60), linewidth = 1, style = plot.style_circles)
plot(semiannual_close, title = 'Semi-Annual Close', color = color.new(color.yellow, 60), linewidth = 1, style = plot.style_circles)
plot(annual_open, title = 'Annual Open', color = color.new(color.green, 80), linewidth = 1, style = plot.style_circles)
plot(annual_high, title = 'Annual High', color = color.new(color.red, 80), linewidth = 1, style = plot.style_circles)
plot(annual_low, title = 'Annual Low', color = color.new(color.blue, 80), linewidth = 1, style = plot.style_circles)
plot(annual_close, title = 'Annual Close', color = color.new(color.yellow, 80), linewidth = 1, style = plot.style_circles)
Upvotes: 0
Views: 29