I am trying to write a code that will find a price at a certain time yesterday…or the day before…etc , does anyone know how this is done?
TIA
Robert
You’ll have to use FOR…NEXT (or WHILE…WEND). It’s very time consuming!
If you are on a hourly chart, to retrieve the closing price of yesterday at 090000 you may write (not tested):
MyClose = 0
n = 47 //47 hours back
i = 1
WHILE i < n DO
IF OpenDay[i] <> OpenDay THEN //when it's yesterday....
IF TIME[i] = 090000 THEN //... and it's my desired time...
MyClose = close[i] //... save CLOSE and...
BREAK //... exit loop...
ENDIF
ENDIF
i = i + 1 //... else keep scanning next bar
WEND
now MyClose will retain yesterday’s CLOSE price at 090000.
Of course if you’re on a 1-minute chart the lookback period will greatly increase making it difficult to run!
Roberto, thank you for your speedy reply, and help.
Robert
The only other way is to calculate how many bars between the time now and the time yesterday or the day before but this becomes slightly complicated when you go from day 1 to day 7 and when you want to go back more than 24 hours. Knowing the number of bars back you could then simply use close[n] to get the closing price.
Here is an example of what I mean;
defparam calculateonlastbars = 200
mytime = 120000
n = (24 - (mytime / 10000)) + (opentime / 10000)
a = close[n]
return a as " close at mytime yesterday"
This will tell you what the closing price was at ‘mytime’ yesterday on an hourly chart. Now all you have to do is adapt it for whatever time frame you want to use it on and add in a calculation to know how many bars to add on for the number of extra days back you might want to go. Good luck! 🙂
Obviously it does not work on Monday or Sunday due to the quantity of bars on Sundays.