Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Function to get Close price for given date and time
I am trying to get the close price from a chart for a given time and date. For example:
My current chart is NQH19
60 minute chart
Get Close from February 20th, 2019 at 1700 (or in EasyLanguage, date = 1190220 and time = 1700)
In one case, I wanted to know the close of a day based on a 60 minute chart (the daily close does not match the 60 minute daily close). I have tried using the function CloseSession(1,1), which would return the close price for one regular session back. However, that sometimes returns a value that is on the weekend, i.e. not a regular session day. I'm not sure if that is a bug or feature, but I dread taking this to TradeStation for support.
So this lead me to writing my own function. I have been trying for the past several days to code a function that will return the closing price for a given date and time. Try as I might, I cannot get it to work. I've tried a number of different ideas, all of which involved using a for loop to work backwards through each bar until I found a date/time match.
Here is my latest iterration, which doesn't work, it just runs through the entire loop, the conditions are never met, and the function returns 0. (I use print statement in an indicator to test it).
Can anyone assist? Any help is greatly appreciated.
Can you help answer these questions from other members on NexusFi?
the best approach depends a bit on how often you need to find a value and if you know the next time and date in advance. The latter would allow you to simply store the value at that date and time and you wouldn't have to loop through the previous bars.
You could also store the values in a dictionary and look them up using BarDateTime.ToString() as the key.
for your specific function code the conditions might not be able to ever become true as you check them outside the loop. Put them within the loop and reset v_counter to 0 in front of the loop.
Using a loop here might create problems with the Maximum Number of Bars a study references i.e. the number of bars your code requires here might become unnecessarily high (as TS could theoretically require 5000 bars on the chart before it can return a value): Maximum Number of Bars Study Will Reference Setting
That's why one of the approaches mentioned in my other post might be better suited here (although the loop will work).
Thanks for the tips and the condition outside of the loop makes sense (*duh*). I generally only need to go back 50-60 bars, give or take. I put the 5000 in to see how far it would run.
I'll let you know how this works and I'll publish the function if I actually get it to work.
you are welcome. One idea about a change might be to either set v_counter to -1 in front of the loop or to place the line v_counter = v_counter + 1 ; after condition2 within the loop. This way your function would also be able to detect the Close when in_date and in_time fall on the current bar i.e. are at Date[0] and Time[0]. At the moment the function would start with checking the values for the bar one ago.
Good idea. I have noticed that on a live chart, the values returned for today are not correct. My indicator calls a function that determines the highest and lowest of three days. That function calls the function above to get that specific date in time.
I'm currently learning how to use and manipulate ELDate. I'm also going to dig into the Global Dictionary, as you have recommended in other posts.