Time iteration

<< Click to Display Table of Contents >>

Navigation:  Details >

Time iteration

Previous pageReturn to chapter overviewNext page

Gekko has a GekkoTime object (in reality a struct for speed) that permits fast and easy iteration over time periods and other things. For instance, for quarters, it must be able to iterate 2020q3, 2020q4, 2021q1, 2021q2, etc. There are also methods for comparing Gekko dates, and counting observations between two dates.

The GekkoTime struct looks like this:

public struct GekkoTime

{    

  public readonly short super;   //year

  public readonly short sub;     //quarter, month

  public readonly short subsub; //day

  public readonly EFreq freq;

}

 

 

So the struct contains a frequency indicator freq (of enumeration EFreq), which can be annual, quarterly, monthly, daily or undated (weeks coming up). The fields of a GekkoTime struct can not be changed, so think of it as a fixed object after construction.

The super field is always the year, and if the frequency is annual, sub and subsub will be = 0. Looping over an annual frequency is easy, just contstruct a new GekkoTime struct where 1 is added to the super field. If the super field is set to -12345, this indicates that the struct should be thought of as null (not-initialized), check this with the IsNull() method (structs cannot be set to null).

There are some conversion methods:

FromDateTimeToUnixDays()

FromDateTimeToGekkoTime()

FromExcelDateToDateTime()

 

FromYYYYMMDDToDateTime()

 

FromGekkoTimeToDateTime()

 

FromStringToGekkoTime()

 

FromGekkoTimeToDifferentFormatsForWriting()

 

These methods are reasonably straightforward. Else, Observations() counts periods between two dates, and methods like StrictlyLargerThan() or LargerThanOrEqual() can be used to test inequalitites corresponding to > or >=.

The Add() method can add any number of periods to a date (also negative periods). Regarding the method ConvertFreqs(), see here.

The GekkoTimeIterator class is used for looping (there is also a GekkoTimeIteratorBackwards class). The time iterator is very practical and used many places in the code. An example:

GekkoTime t1 = new GekkoTime(EFreq.Q, 2020, 3);
GekkoTime t2 = new GekkoTime(EFreq.Q, 2021, 2);
foreach (GekkoTime t in new GekkoTimeIterator(t1, t2))
{
    //t will loop over 2020q3, 2020q4, 2021q1, 2021q2
}

 

When looping over for instance quarters, some integer division and integer modulo is used, to account for a jump like 2020q4 to 2021q1. These operations are very fast, and much faster than for instance using C# DateTime objects (which can denote a point in time down to milliseconds).