Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created November 25, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymorast/362c73ee284fd1ad1f7c968c5fbd392e to your computer and use it in GitHub Desktop.
Save anthonymorast/362c73ee284fd1ad1f7c968c5fbd392e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include "matplotlib.h" // plotting
#include "yfapi.h" // data API
namespace plt = matplotlibcpp;
using namespace std;
/*
Transforms a primitve array into a std::vector of the
approriate type.
*/
vector<float> array_to_vector(float* arr, size_t size)
{
vector<float> vec;
for(int i = 0; i < size; i++)
{
vec.push_back(arr[i]);
}
return vec;
}
/*
Makes the closing data indepent of the actual trading price
of the ETFs by squeezing the values between 0 and 1.
*/
float** normalize_close(datatable::DataTable<float> data)
{
float* cdata = data.get_column("Close");
float min = MAXFLOAT, max = -MAXFLOAT;
for(int i = 0; i < data.nrows(); i++)
{
min = cdata[i] < min ? cdata[i] : min;
max = cdata[i] > max ? cdata[i] : max;
}
delete cdata;
float** new_data = new float*[1];
float* close = data.get_column("Close");
for(int i = 0; i < data.nrows(); i++)
{
close[i] = (close[i] - min) / (max - min);
}
new_data[0] = close;
return new_data;
}
/*
An easy way to retrieve the data from the API, normalize the close prices, and
reformat into a matplotlibcpp acceptable format.
*/
vector<float> get_close_vector_for_ticker(string ticker, string start_date, string end_date, yfapi::YahooFinanceAPI api)
{
datatable::DataTable<float> data = api.get_ticker_data(ticker, start_date, end_date);
float** normal_data = normalize_close(data);
vector<float> vec_data = array_to_vector(normal_data[0], data.nrows());
delete[] normal_data;
return vec_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment