Sambit
Sambit

Reputation: 43

How to get close price of a specific date

My code below plots a horizontal line at the close price of a date entered by me. However it works only with 'Daily' time frame on chart. I need it to work in any time frame(Mainly 15 minute time frame). For any other time frame, it doesn't plot the actual closing price. Instead it plots the closing price of the last candle.

I tried the security function, but I cannot use it inside if condition. But if condition is my requirement to validate the date entered by me.

//@version=4
study("How to get actual close price for a specific date", overlay=true)

text = input(false, type=input.bool, title="---------------------Select Date--------------------------")
startDate = input(title="Date", type=input.integer,
     defval=12, minval=1, maxval=31)
startMonth = input(title="Month", type=input.integer,
     defval=5, minval=1, maxval=12)
startYear = input(title="Year", type=input.integer,
     defval=2021, minval=1800, maxval=2100)
     
Time_start = timestamp(syminfo.timezone, startYear, startMonth, startDate, 00, 00)
Time_end = timestamp(syminfo.timezone, startYear, startMonth, startDate, 23, 59)

pinDateRange = time >= Time_start and time <= Time_end

var pclose = 0.00
if pinDateRange
    pclose := close
    // pclose := security(syminfo.tickerid, 'D', close)
        
plot(pclose, title="Close Price of Selected Date", color=#F94D00, linewidth=1, style=plot.style_stepline, offset=-9999, trackprice=true)

Upvotes: 1

Views: 3955

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1961

Try to use this example as a reference:

//@version=4
study("Plot value starting n months/years back", "", true)
monthsBack = input(3, minval = 0)
yearsBack  = input(0, minval = 0)
fromCurrentDateTime = input(false, "Calculate from current Date/Time instead of first of the month")

targetDate = time >= timestamp(
  year(timenow) - yearsBack, 
  month(timenow) - monthsBack,
  fromCurrentDateTime ? dayofmonth(timenow) : 1,
  fromCurrentDateTime ? hour(timenow)       : 0,
  fromCurrentDateTime ? minute(timenow)     : 0,
  fromCurrentDateTime ? second(timenow)     : 0)
beginMonth = not targetDate[1] and targetDate

var float valueToPlot = na
if beginMonth
    valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)

https://www.pinecoders.com/faq_and_code/#how-can-i-plot-a-value-starting-n-monthsyears-back

Upvotes: 1

Related Questions