|
| 1 | +// Copyright 2025 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#include "where_file.hpp" |
| 6 | +#include <boost/decimal.hpp> |
| 7 | +#include <iostream> |
| 8 | +#include <iomanip> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | +#include <fstream> |
| 12 | +#include <sstream> |
| 13 | + |
| 14 | +#if defined(__clang__) |
| 15 | +# pragma clang diagnostic push |
| 16 | +# pragma clang diagnostic ignored "-Wfloat-equal" |
| 17 | +# pragma clang diagnostic ignored "-Wsign-conversion" |
| 18 | +#elif defined(__GNUC__) |
| 19 | +# pragma GCC diagnostic push |
| 20 | +# pragma GCC diagnostic ignored "-Wfloat-equal" |
| 21 | +# pragma GCC diagnostic ignored "-Wsign-conversion" |
| 22 | +#endif |
| 23 | + |
| 24 | +#include <boost/math/statistics/univariate_statistics.hpp> |
| 25 | + |
| 26 | +#if defined(__clang__) |
| 27 | +# pragma clang diagnostic pop |
| 28 | +#elif defined(__GNUC__) |
| 29 | +# pragma GCC diagnostic pop |
| 30 | +#endif |
| 31 | + |
| 32 | +using namespace boost::decimal; |
| 33 | + |
| 34 | +struct daily_data |
| 35 | +{ |
| 36 | + std::string date; |
| 37 | + decimal64 open; |
| 38 | + decimal64 high; |
| 39 | + decimal64 low; |
| 40 | + decimal64 close; |
| 41 | + decimal64 volume; |
| 42 | +}; |
| 43 | + |
| 44 | +// Function to split a CSV line into daily_data |
| 45 | +auto parse_csv_line(const std::string& line) -> daily_data |
| 46 | +{ |
| 47 | + std::stringstream ss(line); |
| 48 | + std::string token; |
| 49 | + daily_data data; |
| 50 | + |
| 51 | + // Parse each column |
| 52 | + std::getline(ss, data.date, ','); |
| 53 | + std::getline(ss, token, ','); |
| 54 | + from_chars(token, data.open); |
| 55 | + |
| 56 | + std::getline(ss, token, ','); |
| 57 | + from_chars(token, data.high); |
| 58 | + |
| 59 | + std::getline(ss, token, ','); |
| 60 | + from_chars(token, data.low); |
| 61 | + |
| 62 | + std::getline(ss, token, ','); |
| 63 | + from_chars(token, data.close); |
| 64 | + |
| 65 | + std::getline(ss, token, ','); |
| 66 | + from_chars(token, data.volume); |
| 67 | + |
| 68 | + return data; |
| 69 | +} |
| 70 | + |
| 71 | +int main() |
| 72 | +{ |
| 73 | + std::vector<daily_data> stock_data; |
| 74 | + |
| 75 | + // Open and read the CSV file |
| 76 | + std::ifstream file(where_file("AAPL.csv")); |
| 77 | + std::string line; |
| 78 | + |
| 79 | + // Skip header line |
| 80 | + std::getline(file, line); |
| 81 | + |
| 82 | + // Read data |
| 83 | + while (std::getline(file, line)) |
| 84 | + { |
| 85 | + stock_data.push_back(parse_csv_line(line)); |
| 86 | + } |
| 87 | + |
| 88 | + // Get the closing prices for the entire year |
| 89 | + std::vector<decimal64> closing_prices; |
| 90 | + for (const auto& day : stock_data) |
| 91 | + { |
| 92 | + closing_prices.emplace_back(day.close); |
| 93 | + } |
| 94 | + |
| 95 | + const auto mean_closing_price = boost::math::statistics::mean(closing_prices); |
| 96 | + const auto median_closing_price = boost::math::statistics::median(closing_prices); |
| 97 | + const auto variance_closing_price = boost::math::statistics::variance(closing_prices); |
| 98 | + const auto std_dev_closing_price = sqrt(variance_closing_price); |
| 99 | + |
| 100 | + // 2-Sigma Bollinger Bands |
| 101 | + const auto upper_band = mean_closing_price + 2 * std_dev_closing_price; |
| 102 | + const auto lower_band = mean_closing_price - 2 * std_dev_closing_price; |
| 103 | + |
| 104 | + std::cout << std::fixed << std::setprecision(2) |
| 105 | + << " Mean Closing Price: " << mean_closing_price << '\n' |
| 106 | + << " Standard Deviation: " << std_dev_closing_price << '\n' |
| 107 | + << "Upper Bollinger Band: " << upper_band << '\n' |
| 108 | + << "Lower Bollinger Band: " << lower_band << std::endl; |
| 109 | + |
| 110 | + // Mean = 207.21 |
| 111 | + // Median = 214.27 |
| 112 | + return mean_closing_price > median_closing_price; |
| 113 | +} |
| 114 | + |
0 commit comments