Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
59 changes: 59 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.16)

project(YFinancePP)

set(CMAKE_CXX_STANDARD 17)

if (MSVC)
# warning level 4
add_compile_options(/W4)
else()
# additional warnings
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(Boost 1.81.0 REQUIRED)

find_package(Doxygen
REQUIRED dot
OPTIONAL_COMPONENTS mscgen dia)

if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message("Doxygen build started")

# note the option ALL which allows to build the docs together with the application
add_custom_target( doc_doxygen ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM )
else (DOXYGEN_FOUND)
message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)



include(FetchContent)

FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
GIT_TAG 0817715923c9705e68994eb52ef9df3f6845beba) # The commit hash for 1.10.x. Replace with the latest from: https://github.com/libcpr/cpr/releases
FetchContent_MakeAvailable(cpr)

FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
FetchContent_MakeAvailable(json)


add_executable(yfdemo demo/yfdemo.cpp
cpp/base.cpp
cpp/methods.cpp
cpp/requests.cpp
cpp/utils.cpp
)
target_include_directories(yfdemo PRIVATE hpp cpr::cpr nlohmann_json::nlohmann_json)
target_link_libraries(yfdemo PRIVATE cpr::cpr nlohmann_json::nlohmann_json)
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@
### Quick Start
This is a quick and simple repository which is still in development. Its aim is to simplify the interaction between the **_Yahoo Finance_** API and **_C++_**.

This project uses [CMake](https://cmake.org/) as its build system.

It depends on two other projects:

* [C++ Requests: Curl for People (cpr)](https://github.com/libcpr/cpr)
* [JSON for Modern C++](https://github.com/nlohmann/json)

This project uses the [Boost Libraries](http://www.boost.org/).

If you don't have [C++ Requests: Curl for People (cpr)](https://github.com/libcpr/cpr) and/or [JSON for Modern C++](https://github.com/nlohmann/json) you can use CMake's FetchContent to add them. Just add something like

```cmake
include(FetchContent)

FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
GIT_TAG 0817715923c9705e68994eb52ef9df3f6845beba) # The commit hash for 1.10.x. Replace with the latest from: https://github.com/libcpr/cpr/releases
FetchContent_MakeAvailable(cpr)

FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
FetchContent_MakeAvailable(json)
```

to your CMakeLists.txt. Check out this project's [CMakeLists.txt](CMakeLists.txt) to see how to do it.


### Version Updates
**_v-0.1 (09-30-2022)_**:
Added the `get_quotes` and `get_options` member functions for the `Symbol` class.
Expand Down
18 changes: 9 additions & 9 deletions cpp/base.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../hpp/base.h"
#include "../hpp/utils.h"
#include "../hpp/methods.h"
#include <base.h>
#include <utils.h>
#include <methods.h>


namespace yfinance {
Expand Down Expand Up @@ -59,7 +59,7 @@ namespace yfinance {
unsigned int size = quotemap["unix"].size();
std::vector<float> open(size), high(size), low(size), close(size);
std::vector<long int> volume(size);
std::vector<time_t> unix(size);
std::vector<time_t> _unix(size);

std::transform(quotemap["open"].begin(), quotemap["open"].end(), open.begin(),
[](const std::string& token) { return std::stof(token); });
Expand All @@ -71,7 +71,7 @@ namespace yfinance {
[](const std::string& token) { return std::stof(token); });
std::transform(quotemap["volume"].begin(), quotemap["volume"].end(), volume.begin(),
[](const std::string& token) { return std::stoll(token); });
std::transform(quotemap["unix"].begin(), quotemap["unix"].end(), unix.begin(),
std::transform(quotemap["unix"].begin(), quotemap["unix"].end(), _unix.begin(),
[](const std::string& token) { return (time_t)std::stoll(token); });

return Structures::Quotes(
Expand All @@ -80,7 +80,7 @@ namespace yfinance {
std::move(low),
std::move(close),
std::move(volume),
std::move(unix)
std::move(_unix)
);
}
else {
Expand Down Expand Up @@ -113,8 +113,8 @@ namespace yfinance {
auto& raw = rjson["optionChain"]["result"][0]
["options"][0][kind];

unsigned int size = raw.size();
for (int i = 0; i < size; i++) {
size_t size = raw.size();
for (size_t i = 0; i < size; i++) {
Structures::Option option;
for (auto& [key, val] : raw[i].items()) {
// As the response from YFINANCE API may be
Expand Down Expand Up @@ -202,7 +202,7 @@ namespace yfinance {
throw std::runtime_error(error_message);
}

};
}

nlohmann::json Symbol::get_summary(
const std::string&& module
Expand Down
5 changes: 3 additions & 2 deletions cpp/methods.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../hpp/methods.h"
#include <algorithm>
#include <methods.h>


namespace Methods {
Expand All @@ -24,5 +25,5 @@ namespace Methods {
it++;
}
return indexes;
};
}
}
6 changes: 3 additions & 3 deletions cpp/requests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/requests.h"
#include "../hpp/structures.h"
#include <requests.h>
#include <structures.h>


namespace Requests {
Expand All @@ -13,7 +13,7 @@ namespace Requests {
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
};
}

Structures::Response Requests(
std::string&& url,
Expand Down
4 changes: 2 additions & 2 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/utils.h"
#include "../hpp/structures.h"
#include <utils.h>
#include <structures.h>


namespace Utils {
Expand Down
4 changes: 2 additions & 2 deletions demo/news.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/base.h"
#include "../hpp/benchmark.h"
#include <base.h>
#include <benchmark.h>


void news() {
Expand Down
4 changes: 2 additions & 2 deletions demo/options.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/base.h"
#include "../hpp/benchmark.h"
#include <base.h>
#include <benchmark.h>


void options(int argc, char** argv) {
Expand Down
4 changes: 2 additions & 2 deletions demo/quotes.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/base.h"
#include "../hpp/benchmark.h"
#include <base.h>
#include <benchmark.h>


void quotes(int argc, char** argv) {
Expand Down
4 changes: 2 additions & 2 deletions demo/summary.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../hpp/base.h"
#include "../hpp/benchmark.h"
#include <base.h>
#include <benchmark.h>


void summary(int argc, char** argv) {
Expand Down
88 changes: 88 additions & 0 deletions demo/yfdemo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file main.cpp
* @author Oliver Ofenloch (57812959+ofenloch@users.noreply.github.com)
* @brief
* @version 0.1
* @date 2023-12-04
*
* @copyright Copyright (c) 2023
*
*/

#include <base.h>
#include <benchmark.h>
int main(int /*argc*/, char * /*argv*/[])
{
int iReturnCode = 0;

//
// demo/news.cpp
//
yfinance::Symbol* symbol;
symbol = new yfinance::Symbol("AAPL");
// Getting news for the symbol;
auto news = symbol->get_news();
// Printing the first news;
std::cout << news[0];
// // Benchmarking;
// auto f = std::bind(&yfinance::Symbol::get_news, symbol);
// auto benchmarkResult = Benchmarking::Timeit(10, f);
// std::cout << benchmarkResult;



//
// demo/options.cpp
//
// Initialize the Ticker object:
yfinance::Symbol *tk;
tk = new yfinance::Symbol("GS");
// Loading options into struct:
auto options = tk->get_options();
// Print Option:
std::cout << options["calls"][0];
// // Benchmarking:
// auto f = std::bind(&yfinance::Symbol::get_options, tk);
// auto timeit = Benchmarking::Timeit(10, f);
// // Printing benchmark results:
// std::cout << timeit;

//
// demo/quotes.cpp
//
// Initialize the Ticker object:
//yfinance::Symbol* tk;
tk = new yfinance::Symbol("AAPL");
// Loading quotes into struct:
auto quotes = tk->get_quotes("1d");
// Print Quotes content:
std::cout << quotes;
// // Benchmarking:
// auto f = std::bind(
// &yfinance::Symbol::get_quotes, tk,
// std::placeholders::_1, std::placeholders::_2,
// std::placeholders::_3, std::placeholders::_4);
// auto timeit = Benchmarking::Timeit(100, f, "1h", -1, -1, ",");
// // Printing benchmark results:
// std::cout << timeit;

//
// demo/summary.cpp
//
// Initialize the Ticker object:
//yfinance::Symbol* tk;
tk = new yfinance::Symbol("GS");
// Loading assetProfile into struct:
auto profile = tk->get_profile();
// Print assetProfile:
std::cout << profile;
// Loading Module (quoteSummary):
auto quoteSummary = tk->get_summary("financialData");
// Print Module:
for (auto& [k, v] : quoteSummary.items())
std::cout << k << ':' << v << "\n";



return iReturnCode;
}
12 changes: 12 additions & 0 deletions docs/Doxyfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PROJECT_NAME = "@CMAKE_PROJECT_NAME@"
PROJECT_NUMBER = @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@
STRIP_FROM_PATH = @PROJECT_SOURCE_DIR@
INPUT = @doxy_main_page@ \
@PROJECT_SOURCE_DIR@ \
@PROJECT_BINARY_DIR@
FILE_PATTERNS = *.cpp \
*.h \
*.hpp \
*.cc
RECURSIVE = YES
USE_MDFILE_AS_MAINPAGE = @doxy_main_page@
13 changes: 8 additions & 5 deletions hpp/base.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once
#ifndef _BASE_H_
#define _BASE_H_
#include <boost/algorithm/string.hpp>
#include "boost/lexical_cast.hpp"
#include <boost/lexical_cast.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <string>
#include <regex>
#include <cpr/cpr.h>
#include "../hpp/structures.h"
#include "../hpp/utils.h"
#include <structures.h>
#include <utils.h>
using json = nlohmann::json;


Expand Down Expand Up @@ -50,4 +51,6 @@ namespace yfinance {
);

};
}
}

#endif // #ifndef _BASE_H_
12 changes: 7 additions & 5 deletions hpp/benchmark.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "structures.h"
#include "utils.h"
#ifndef _BENCHMARK_H_
#define _BENCHMARK_H_
#include <structures.h>
#include <utils.h>


namespace Benchmarking {
Expand All @@ -25,5 +26,6 @@ namespace Benchmarking {
milliseconds sum = std::accumulate(durations.begin(), durations.end(), milliseconds(0));
milliseconds avg = sum / durations.size();
return Structures::TimeitResult(iters, min, max, avg, sum);
};
}
}
}
#endif // #ifndef _BENCHMARK_H_
8 changes: 6 additions & 2 deletions hpp/methods.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#ifndef _METHODS_H_
#define _METHODS_H_

#include <string>
#include <vector>

Expand All @@ -14,4 +16,6 @@ namespace Methods {
const std::vector<std::string>& v,
const std::vector<std::string>& z
);
}
}

#endif // #ifndef _METHODS_H_
Loading