Time Series Analysis

Position detection, thereholds

bool quotek::quant::above(quotek::data::records &recs, float value, float thereshold)

above tells if the values contained in the provided dataset are mostly above or below value. accuracy will depend on provided thereshold (between 0 and 1) , 1 meaning that all the values of dataset must be higher than value to return true, 0.5 meaning that half the values must be higher.

Return
true if dataset is above value, false otherwise.
Parameters
  • recs -

    dataset to work on.

  • value -

    value to test dataset against.

  • thereshold -

    integer in [0,1] that indicates the percentage of value in the dataset that must be above value.

bool quotek::quant::cross(quotek::data::records &recs1, quotek::data::records &recs2)

Detects if 2 value time-series cross each other.

Return
true if 2 datasets cross each other, false otherwise.
Parameters
  • recs1 -

    first values dataset.

  • recs2 -

    second values dataset.

int quotek::quant::cross_ex(quotek::data::records &recs1, quotek::data::records &recs2)

Alternate version of bool cross() , but adds information in return value. If time-series don’t cross, the function returns 0. If recs1 is on top, then it returns 1. If recs2 is on top, it returns 2.

Return
0 if no cross, 1 if cross and recs1 is on top, 2 if cross and recs2 is on top.
Parameters
  • recs1 -

    first values time-series.

  • recs2 -

    second values time-series.

Basic analysis

float quotek::quant::min(quotek::data::records &recs)

min() returns the smallest value contained in the provided recs dataset to work on.

Return
the smallest value stored in recs dataset.
Parameters
  • recs -

    dataset to work on.

float quotek::quant::max(quotek::data::records &recs)

max() return the largest value contained in the provided

Return
the largest value stored in recs dataset.
Parameters
  • recs -

    dataset to work on.

float quotek::quant::average(quotek::data::records &recs)

This function returns an average of the values contained in the provided recs dataset.

Return
the mean value of the dataset, as a float value.
Parameters
  • recs -

    dataset to work on.

float quotek::quant::weighted_average(quotek::data::records &recs, std::vector<int> &weights)

This function returns a weight average of the values contained in the provided recs dataset.

Return
the mean value of the dataset, as a float value.
Parameters
  • recs -

    dataset to work on.

  • weights -

    weights vector.

float quotek::quant::variance(quotek::data::records &recs, bool sampled)

Returns the variance of the values contained in the provided recs dataset.

Return
variance of the recs dataset, as a float value.
Parameters
  • recs -

    dataset to work on.

  • sampled -

    when you need to make a variance estimate, elimites bias by dividing by N-1 instead of N.

float quotek::quant::variance_q(quotek::data::records &recs, bool sampled, float average)

variance_q is a performance-shaped function that will compute variance of the values for the provided recs dataset but also provided that you already know the average of the values from a previous compute.

Return
variance of the recs dataset, as a float value.
Parameters
  • recs -

    dataset to work on.

  • sampled -

    when you need to make a variance estimate, elimites bias by dividing by N-1 instead of N.

  • average -

    pre-computed average of the dataset (for instance coming from previous calculus).

float quotek::quant::covariance(quotek::data::records &recs1, quotek::data::records &recs2)

Computes covariance of values between two datasets recs1 and recs2.

Return
the covariance between the 2 provided datasets, as a float.
Parameters
  • recs1 -

    the first dataset to work on.

  • recs2 -

    the second dataset to work on.

float quotek::quant::covariance_q(quotek::data::records &recs1, quotek::data::records &recs2, float average_1, float average_2)

Quick version of covariance, provided that you already know the averages of the 2 datasets.

Return
the covariance between the 2 provided datasets, as a float.
Parameters
  • recs1 -

    the first dataset to work on.

  • recs2 -

    the second dataset to work on.

  • average_1 -

    pre-computed average of first dataset.

  • average_2 -

    prec-computed average of second dataset.

float quotek::quant::standard_deviation(quotek::data::records &recs, bool sample)

Computes the standard deviation for the values contained in the recs dataset.

Return
the standard deviation of the provided dataset, as a float.
Parameters
  • recs -

    dataset to work on. if true, computes sample standard deviation, population standard deviation otherwise.

float quotek::quant::normal_cumulative_distribution(float value)

This function returns the cumulative normal density (PHI) for a given value.

Return
CDF for value.
Parameters
  • value -

    The value to compute PHI from.

float quotek::quant::standard_normal_distribution(float value)

This function returns the standard normal distribution of a given value.

Return
Normal distribution for value.
Parameters
  • value -

    The value to compute SND from.

Moving Averages

std::vector<float> quotek::quant::moving_average(quotek::data::records &recs, int periods)
std::vector<float> quotek::quant::exponential_moving_average(quotek::data::records &recs, int periods)

Computes the exponential moving average (EMA) of the provided dataset for n periods. As a reminder, EMA is is a special kind of moving average giving more weight to recent values. eg if you want to compute an EMA100 for the provided dataset, use exponential_moving_average(recs,50)

Return
a vector of floats containing the graph values of the moving average for the dataset.
Parameters
  • recs -

    dataset to work on.

  • periods -

    the number of dataset values that must be aggregated together to compute a single point of the EMA.

std::vector<float> quotek::quant::weighted_moving_average(quotek::data::records &recs, int periods)

Computes the weighted moving average (WMA) of the provided dataset for n periods. WMA gives even more weight to recent values than EMA. Note: the weight vector for this function is {1,2,..periods-1,periods}

Parameters
  • recs -

    dataset to work on.

  • periods -

    Number of periods for the moving average.

std::vector<float> quotek::quant::triple_exponential_moving_average(quotek::data::records &recs, int periods)

Computes the Triple exponential moving average (TEMA/TRIX) of the provided dataset for n periods. TEMA is a tech indicator that tries to deal with the lag.

Return
a vector of floats containing the graph values of the TEMA for the dataset.
Parameters
  • recs -

    dataset to work on.

  • periods -

    the number of dataset values that must be aggregated together to compute a single point of the TEMA.

Advanced Statistical Analysis

affine quotek::quant::linear_regression(quotek::data::records &recs)

Computes the linear regression of the values contained in the dataset using an ordinary least squares method, and gives the result as an affine structure such as the line equation is y = affine.a * x + affine.b Note about linear regression: in order to avoid troublesome scaling problems, it is computed which sequence(1..dataset_size) as x coordinates. So in order to work properly, it assumes constant interval on x between points of the dataset.

Return
affine structure containing slope and intercept values.
Parameters
  • recs -

    dataset to work on.

void quotek::quant::linear_regression(quotek::data::records &recs, std::vector<float> &result)

linear_regression computes the linear regression of the provided dataset. Note about linear regression: in order to avoid troublesome scaling problems, it is computed which sequence(1..dataset_size) as x coordinates. So in order to work properly, it assumes constant interval on x between points of the dataset.

Return
none.
Parameters
  • recs -

    dataset to work on.

  • result -

    reference, to store the result of the linear regression.

std::vector<float> quotek::quant::polynomial_regression(quotek::data::records &recs)

This fucntion computes a polynomial regression from a provided recs dataset.

Return
vector of floats representing the polynomial regression of dataset.
Parameters
  • recs -

    dataset to work on.

std::vector<float> quotek::quant::autocorrelation(quotek::data::records recs)

autocorrelation is a very useful function that can help to discover seasonalities in time series data. Underlying mecanism for computing autocorrelation uses Fast Fourier Transform.

Return
computed autocorrelation graph.
Parameters
  • recs -

    dataset to compute autocorrelation for.

Other Forecasting Methods

trend_p quotek::quant::trend_percentages(quotek::data::records &recs)

Computes the bull, bear and neutral ratios for the provided dataset. ratios are provided in percentage.

Return
a trend_p structure containing the bull, bear and neutral trend percentages for the dataset.
Parameters
  • recs -

    dataset to work on.

float quotek::quant::risk_reward_ratio(float expected_profit, int stop, int leverage)

This function returns the risk/reward ratio for a potential trade having the provided caracteristics.

Parameters
  • expected_profit -

    profit in asset’s currency expected for a trade.

  • stop -

    value in BPS where you intend to place your stop value.

  • leverageexprimed -

    in protit per BPS, you intend to add on your trade.

Dedicated Data Structures

struct quotek::quant::affine

This structure aims to store slope-intercept equations parameters.

Public Members

float a

slope

float b

intercept

struct quotek::quant::trinom

trinom is a struct that aims to store 2nd degree polynom parameters.

struct quotek::quant::_greeks

greeks is a structure meant to store greek values involved in portfolio modeling.

Public Members

float delta

stores delta part of the greeks

float gamma

stores gamma part of the greeks

float theta

stores theta part of the greeks

float vega

stores vega part of the greeks

float rho

stores rho part of the greeks

struct quotek::quant::pivot
struct quotek::quant::fibo_ret

fibo_ret is a structure that stores the different fibonacci retracements for a time-serie

Public Members

float p0

Stores level 0%

float p23

Stores level 23.6 %

float p38

Stores level 38.2 %

float p50

Stores level 50%. Please note that this is not really a fibo level but since it is highly used, it is also computed.

float p61

Stores level 61.8 %

float p100

Stores level 100 %

struct quotek::quant::fibo_ext

fibo_ext is a structure that stores the different fibonacci extensions for a time-series

Public Members

float p100

Stores level 0%

float p127

Stores level 127.2 %

float p161

Stores level 161.8%.

float p200

Stores level 200 %

float p261

Stores level 261.8 %

struct quotek::quant::trend_p

This structure stores trend percentage analysis.

Public Members

float bull

stores bull percentage of a dataset

float bear

stores bear percentage of a dataset.

float neutral

stores neutral percentage of a dataset.

Example

In the following example we will see how to use the statistical analysis functions listed above.

#include <quotek/record.hpp>
#include <quotek/quant.hpp>
using namespace quotek::data;
using namespace quotek::quant;

int main() {


  /* First we create fake data. */

  std::vector<long> tstamps = { 1437143900,
                                1437143901,
                                1437143902,
                                1437143903,
                                1437143904,
                                1437143905,
                                1437143906,
                                1437143907,
                                1437143908 };

  std::vector<float> vals = { 4530, 4575, 4890, 4235, 4144, 4356, 4588, 4720, 4989 };


  /* Then we create a records container from these data. */
  records r1 = records(vals);

  for (int i=0;i< r1.size(); i++ ) {
    r1[i].timestamp = tstamps[i];

  }

  /* We find the minimum value inside the r1 time-series, value is 4144. */
  float minval = min(r1);

  /* Now we compute the 5 periods Exponential moving average for the r1 time-series.
     Result is the following: {4552.5, 4560, 4670, 4525, 4398, 4384, 4452, 4541.33 }. */

  records r3 = EMA(r1,5);

  /* Finally We compute the linear regression of the r1 time-series. Affine parameters are
  (29.8, 4409.56) */

  affine af = linear_regression(r1);
}