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?

Comments

  • 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.
  • Thanks BidnOff4! That is very helpful.

    But I still want to know how to calculate the difference between the opening price and the closing price the previous day. I can individually specify the date for the closing and the following day's date for the opening, but I don't know how to get a series of the differences for every day.
  • Hi Andrew,

    You can simply use offsets to achieve this.
    (Open(@)[0] - 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:= ((LocalHour(@)*100)+Minute(@));
    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))
Sign In or Register to comment.