Introduction
Last week we learned how to use the date
keyword to load dates into GAUSS. Today, we will plot some high-frequency Forex data.
The data
Today's dataset (usdcad_tick.csv) contains tick data for a little over 30,000 observations of the bid price for the USD/CAD currency pair from January 2, 2018.
This file has two variables:
- timestamp: The time of the bid as a POSIX date/time (seconds since Jan 1, 1970).
- bid: The bid price.
The first few observations of the file look like this:
timestamp,bid 1514872801.247, 1.25372 1514872801.497, 1.25376 1514872801.747, 1.25373 1514872802.247, 1.25377 1514872802.497, 1.25377
The fractional portion of the timestamp data represents nanoseconds.
plotTSHF
plotTSHF
is a function which will draw graphs of irregularly spaced and high-frequency time series data. It has three required inputs:
- date_vec
- Tx1 vector containing POSIX date/times for each observation.
- label_unit
- The unit to use for X-axis tick labels. Valid options include:
- milliseconds
- seconds
- minutes
- hours
- days
- months
- quarters
- years
- y
- Tx1 vector or TxP matrix containing the data to plot.
Basic plot
We can plot our dataset with the following code:
// Load all observations of both variables
fname = "usdcad_tick.csv";
data = loadd(fname);
// Split variables into separate vectors
timevec = data[.,1];
bid = data[.,2];
// Plot data
plotTSHF(timevec, "hours", bid);
and we will see a graph that looks like this:
label_unit
Most of the code we used to create this graph should be fairly straightforward, with the possible exception of the label_unit
input.
plotTSHF(timevec, "hours", bid);
In the above line of code, the label_unit
is "hours"
. This tells GAUSS that we would like X-tick labels to be set at even hours.
If we used "days"
we would not see any X-tick labels. This is because our data is from 06:00-18:00 on the same day. Since the time series data does not cross from one day to another, the graph does not contain the start of a day at which to place the tick label.
Customize tick labels
Now we will use plotSetXTicInterval to set the X-ticks to appear every 60 minutes, starting at 06:30.
plotControl
structure? Click here for help.To accomplish this, we will have to take the following steps:
- Set the
label_unit
passed toplotTSHF
to"minutes"
.- This step tells
plotTSHF
to place the X-tick labels at even minutes.
- This step tells
- Set the
tick_interval
input toplotSetXTicInterval
to be 60.- Since the
tick_interval
is in terms of theplotTSHF
label_unit
, settingtick_interval
to 60 will set the X-ticks to be placed every 60 minutes.
- Since the
- Set the
first_labeled
input toplotSetXTicInterval
to be a POSIX date which equals Jan 2, 2018 06:30.- Since
plotTSHF
uses only POSIX date/times, we must specify the first X-tick to label as a POSIX date/time.
- Since
// Load all observations of both variables
fname = "usdcad_tick.csv";
data = loadd(fname);
// Split variables into separate vectors
timevec = data[.,1];
bid = data[.,2];
// Declare 'myPlot' to be a plotControl struct
// and fill with default values
struct plotControl myPlot;
myPlot = plotGetDefaults("xy");
// Convert the DT Scalar date to POSIX
// YYYY MO DD HH MI SS
// 2018 01 02 06 30 00
first_lbl = dttoposix(2018010206300);
// Draw X-tick labels every 60 minutes,
// starting at Jan 2, 2018 at 06:30
plotSetXTicInterval(&myPlot, 60, first_lbl);
// Plot data (notice we pass in the plotControl struct)
plotTSHF(myPlot, timevec, "minutes", bid);
The above code will create a graph like shown below.
Conclusions
Congratulations! You have learned how to:
- Plot high-frequency time series data with
plotTSHF
, using POSIX date/times. - Customize the frequency and starting point of X-tick labels for time series graphs using
plotTSHF
.
Code and data from this blog can be found here.