Manipulating dates / Dates as parameters / Dating events
From the discussion of the parameter values, here: http://news.cqg.com/blogs/coding/2014/04/parameters-inside-cqg-code, I am guessing that the unique date and time specified in parameters are the usual POSIX values most computers use. Is there a way to extract unique date/time values from an event, like a peak or a cross? And can you define a date/time range, and a date/time difference, in a way that lets you use such ranges or difference in a formula or study? I saw the discussion of the TIMER function, here: http://news.cqg.com/blogs/coding/2013/09/timer-helper-and-cqgs-setreset-function
but I have not been able to figure out how to do similar things for periods that extend over multiple days.
Let me be more concrete. I would like to find programmatically, for each day in a specified date range, say June 15 to Aug 14, the difference between the opening price of a security and the closing price the day before. Can I do that? And if so how?
Second question: I’d like to return the dates and times when certain rare events occur. Say, for example, I want the dates and times when the 12 period moving average crosses the 144-period moving average in the first hour of the daytime trading session for a security. Can I get those date/time values? If so, how?
Adminlaurie (Admin, CQG) commented
Use set reset to look between dates:
SetRst(LocalDay(@)=15 and LocalMonth(@)=6 and LocalYear(@)=2017,LocalDay(@)=14 and LocalMonth(@)=8 and LocalYear(@)=2017)
Pertaining to the second question - we cannot cross reference dates on conditional events and call them up later in a formula.
You can simply use offsets to achieve this.
(Open(@) - Close(@)[-1]) WHEN (SetRst(LocalDay(@)=15 and LocalMonth(@)=6 and LocalYear(@)=2017,LocalDay(@)=14 and LocalMonth(@)=8 and LocalYear(@)=2017) = 1)
When applied to a daily chart, the above will return the Open of any daily bar minus the Close of the previous bar.
In regards to your second question, you can use:
time WHEN (MA(@,Sim,12) XABOVE MA(@,Sim,144))
This will provide the time (900 for 9AM and 2100 for 9PM) when the 2 Moving Averages cross. You can adjust the formula to provide the day when the event occurs by doing the following:
LocalDay(@) WHEN (MA(@,Sim,12) XABOVE MA(@,Sim,144))