diff --git a/doc/decimal.adoc b/doc/decimal.adoc index a954151dd..085ee902e 100644 --- a/doc/decimal.adoc +++ b/doc/decimal.adoc @@ -4,7 +4,6 @@ Distributed under the Boost Software License, Version 1.0. https://www.boost.org/LICENSE_1_0.txt //// -= Decimal: IEEE 754 Decimal Floating Point Numbers :toc: left :toclevels: 4 :idprefix: @@ -15,6 +14,9 @@ https://www.boost.org/LICENSE_1_0.txt :leveloffset: +1 += Decimal: IEEE 754 Decimal Floating Point Numbers +Matt Borland and Chris Kormanyos + include::decimal/overview.adoc[] include::decimal/api_reference.adoc[] include::decimal/generic_decimal.adoc[] diff --git a/doc/decimal/examples.adoc b/doc/decimal/examples.adoc index 167a5c225..6c8b9b4de 100644 --- a/doc/decimal/examples.adoc +++ b/doc/decimal/examples.adoc @@ -217,3 +217,14 @@ BID format: 31fc4b40 DPD format: 35f00000 ---- +== Financial Applications + +=== Simple Moving Average + +In the examples folder there is a file named `moving_average.cpp`. +This example shows how to parse historical stock data from file and use it. +This serves as a framework for other calculations for securities. + +=== Currency Conversion +In the examples folder there is a file named `currency_conversion.cpp`. +This example shows how to simply convert currencies based off a given exchange rate. diff --git a/examples/AAPL.csv b/examples/AAPL.csv new file mode 100644 index 000000000..ff54c640b --- /dev/null +++ b/examples/AAPL.csv @@ -0,0 +1,253 @@ +Date,Open,High,Low,Close,Volume +1/2/24,187.15,188.44,183.89,185.64,"82,488,672" +1/3/24,184.22,185.88,183.43,184.25,"58,414,461" +1/4/24,182.15,183.09,180.88,181.91,"71,983,570" +1/5/24,181.99,182.76,180.17,181.18,"62,379,660" +1/8/24,182.09,185.6,181.5,185.56,"59,144,473" +1/9/24,183.92,185.15,182.73,185.14,"42,841,809" +1/10/24,184.35,186.4,183.92,186.19,"46,792,910" +1/11/24,186.54,187.05,183.62,185.59,"49,128,406" +1/12/24,186.06,186.74,185.19,185.92,"40,477,781" +1/16/24,182.16,184.26,180.93,183.63,"65,603,039" +1/17/24,181.27,182.93,180.3,182.68,"47,317,434" +1/18/24,186.09,189.14,185.83,188.63,"78,005,750" +1/19/24,189.33,191.95,188.82,191.56,"68,902,992" +1/22/24,192.3,195.33,192.26,193.89,"60,133,848" +1/23/24,195.02,195.75,193.83,195.18,"42,355,594" +1/24/24,195.42,196.38,194.34,194.5,"53,631,320" +1/25/24,195.22,196.27,193.11,194.17,"54,822,129" +1/26/24,194.27,194.76,191.94,192.42,"44,594,008" +1/29/24,192.01,192.2,189.58,191.73,"47,145,621" +1/30/24,190.94,191.8,187.47,188.04,"55,859,367" +1/31/24,187.04,187.1,184.35,184.4,"55,467,801" +2/1/24,183.99,186.95,183.82,186.86,"64,885,406" +2/2/24,179.86,187.33,179.25,185.85,"102,551,695" +2/5/24,188.15,189.25,185.84,187.68,"69,668,812" +2/6/24,186.86,189.31,186.77,189.3,"43,490,762" +2/7/24,190.64,191.05,188.61,189.41,"53,438,961" +2/8/24,189.39,189.54,187.35,188.32,"40,962,047" +2/9/24,188.65,189.99,188,188.85,"45,155,219" +2/12/24,188.42,188.67,186.79,187.15,"41,781,930" +2/13/24,185.77,186.21,183.51,185.04,"56,529,527" +2/14/24,185.32,185.53,182.44,184.15,"54,630,520" +2/15/24,183.55,184.49,181.35,183.86,"65,434,500" +2/16/24,183.42,184.85,181.67,182.31,"49,752,473" +2/20/24,181.79,182.43,180,181.56,"53,665,551" +2/21/24,181.94,182.89,180.66,182.32,"41,529,672" +2/22/24,183.48,184.96,182.46,184.37,"52,292,207" +2/23/24,185.01,185.04,182.23,182.52,"45,119,680" +2/26/24,182.24,182.76,180.65,181.16,"40,867,422" +2/27/24,181.1,183.92,179.56,182.63,"54,318,848" +2/28/24,182.51,183.12,180.13,181.42,"48,953,941" +2/29/24,181.27,182.57,179.53,180.75,"136,682,594" +3/1/24,179.55,180.53,177.38,179.66,"73,563,078" +3/4/24,176.15,176.9,173.79,175.1,"81,510,094" +3/5/24,170.76,172.04,169.62,170.12,"95,132,359" +3/6/24,171.06,171.24,168.68,169.12,"68,587,711" +3/7/24,169.15,170.73,168.49,169,"71,765,055" +3/8/24,169,173.7,168.94,170.73,"76,267,039" +3/11/24,172.94,174.38,172.05,172.75,"60,139,473" +3/12/24,173.15,174.03,171.01,173.23,"59,825,367" +3/13/24,172.77,173.19,170.76,171.13,"52,488,688" +3/14/24,172.91,174.31,172.05,173,"72,913,516" +3/15/24,171.17,172.62,170.29,172.62,"121,752,703" +3/18/24,175.57,177.71,173.52,173.72,"75,604,180" +3/19/24,174.34,176.61,173.03,176.08,"55,215,238" +3/20/24,175.72,178.67,175.09,178.67,"53,423,102" +3/21/24,177.05,177.49,170.84,171.37,"106,181,297" +3/22/24,171.76,173.05,170.06,172.28,"71,160,141" +3/25/24,170.57,171.94,169.45,170.85,"54,288,328" +3/26/24,170,171.42,169.58,169.71,"57,388,449" +3/27/24,170.41,173.6,170.11,173.31,"60,273,273" +3/28/24,171.75,172.23,170.51,171.48,"65,672,688" +4/1/24,171.19,171.25,169.48,170.03,"46,240,500" +4/2/24,169.08,169.34,168.23,168.84,"49,329,480" +4/3/24,168.79,170.68,168.58,169.65,"47,691,719" +4/4/24,170.29,171.92,168.82,168.82,"53,704,391" +4/5/24,169.59,170.39,168.95,169.58,"42,104,832" +4/8/24,169.03,169.2,168.24,168.45,"37,425,512" +4/9/24,168.7,170.08,168.35,169.67,"42,451,207" +4/10/24,168.8,169.09,167.11,167.78,"49,709,340" +4/11/24,168.34,175.46,168.16,175.04,"91,070,281" +4/12/24,174.26,178.36,174.21,176.55,"101,670,898" +4/15/24,175.36,176.63,172.5,172.69,"73,531,766" +4/16/24,171.75,173.76,168.27,169.38,"73,711,242" +4/17/24,169.61,170.65,168,168,"50,901,207" +4/18/24,168.03,168.64,166.55,167.04,"43,122,898" +4/19/24,166.21,166.4,164.08,165,"68,149,375" +4/22/24,165.52,167.26,164.77,165.84,"48,116,441" +4/23/24,165.35,167.05,164.92,166.9,"49,537,762" +4/24/24,166.54,169.3,166.21,169.02,"48,251,840" +4/25/24,169.53,170.61,168.15,169.89,"50,558,328" +4/26/24,169.88,171.34,169.18,169.3,"44,838,352" +4/29/24,173.37,176.03,173.1,173.5,"68,169,422" +4/30/24,173.33,174.99,170,170.33,"65,934,781" +5/1/24,169.58,172.71,169.11,169.3,"50,383,152" +5/2/24,172.51,173.42,170.89,173.03,"94,214,922" +5/3/24,186.65,187,182.66,183.38,"163,224,094" +5/6/24,182.35,184.2,180.42,181.71,"78,569,672" +5/7/24,183.45,184.9,181.32,182.4,"77,305,766" +5/8/24,182.85,183.07,181.45,182.74,"45,057,090" +5/9/24,182.56,184.66,182.11,184.57,"48,982,969" +5/10/24,184.9,185.09,182.13,183.05,"50,759,500" +5/13/24,185.44,187.1,184.62,186.28,"72,044,805" +5/14/24,187.51,188.3,186.29,187.43,"52,393,621" +5/15/24,187.91,190.65,187.37,189.72,"70,399,992" +5/16/24,190.47,191.1,189.66,189.84,"52,845,230" +5/17/24,189.51,190.81,189.18,189.87,"41,282,930" +5/20/24,189.33,191.92,189.01,191.04,"44,361,281" +5/21/24,191.09,192.73,190.92,192.35,"42,309,398" +5/22/24,192.27,192.82,190.27,190.9,"34,648,551" +5/23/24,190.98,191,186.63,186.88,"51,005,922" +5/24/24,188.82,190.58,188.04,189.98,"36,326,980" +5/28/24,191.51,193,189.1,189.99,"52,280,047" +5/29/24,189.61,192.25,189.51,190.29,"53,068,020" +5/30/24,190.76,192.18,190.63,191.29,"49,947,941" +5/31/24,191.44,192.57,189.91,192.25,"75,158,281" +6/3/24,192.9,194.99,192.52,194.03,"50,080,539" +6/4/24,194.64,195.32,193.03,194.35,"47,471,449" +6/5/24,195.4,196.9,194.87,195.87,"54,156,793" +6/6/24,195.69,196.5,194.17,194.48,"41,181,754" +6/7/24,194.65,196.94,194.14,196.89,"53,103,910" +6/10/24,196.9,197.3,192.15,193.12,"97,262,078" +6/11/24,193.65,207.16,193.63,207.15,"172,373,297" +6/12/24,207.37,220.2,206.9,213.07,"198,134,297" +6/13/24,214.74,216.75,211.6,214.24,"97,862,727" +6/14/24,213.85,215.17,211.3,212.49,"70,122,750" +6/17/24,213.37,218.95,212.72,216.67,"93,728,305" +6/18/24,217.59,218.63,213,214.29,"79,943,250" +6/20/24,213.93,214.24,208.85,209.68,"86,172,445" +6/21/24,210.39,211.89,207.11,207.49,"246,421,406" +6/24/24,207.72,212.7,206.59,208.14,"80,727,008" +6/25/24,209.15,211.38,208.61,209.07,"56,713,871" +6/26/24,211.5,214.86,210.64,213.25,"66,213,195" +6/27/24,214.69,215.74,212.35,214.1,"49,772,711" +6/28/24,215.77,216.07,210.3,210.62,"82,542,719" +7/1/24,212.09,217.51,211.92,216.75,"60,402,930" +7/2/24,216.15,220.38,215.1,220.27,"58,046,180" +7/3/24,220,221.55,219.03,221.55,"37,369,801" +7/5/24,221.65,226.45,221.65,226.34,"60,412,406" +7/8/24,227.09,227.85,223.25,227.82,"59,085,859" +7/9/24,227.93,229.4,226.37,228.68,"48,169,820" +7/10/24,229.3,233.08,229.25,232.98,"62,627,688" +7/11/24,231.39,232.39,225.77,227.57,"64,710,621" +7/12/24,228.92,232.64,228.68,230.54,"53,046,527" +7/15/24,236.48,237.23,233.09,234.4,"62,631,246" +7/16/24,235,236.27,232.33,234.82,"43,234,281" +7/17/24,229.45,231.46,226.64,228.88,"57,345,879" +7/18/24,230.28,230.44,222.27,224.18,"66,034,594" +7/19/24,224.82,226.8,223.28,224.31,"49,151,449" +7/22/24,227.01,227.78,223.09,223.96,"48,201,840" +7/23/24,224.37,226.94,222.68,225.01,"39,960,262" +7/24/24,224,224.8,217.13,218.54,"61,777,578" +7/25/24,218.93,220.85,214.62,217.49,"51,391,199" +7/26/24,218.7,219.49,216.01,217.96,"41,601,352" +7/29/24,216.96,219.3,215.75,218.24,"36,311,781" +7/30/24,219.19,220.33,216.12,218.8,"41,643,840" +7/31/24,221.44,223.82,220.63,222.08,"50,036,262" +8/1/24,224.37,224.48,217.02,218.36,"62,501,000" +8/2/24,219.15,225.6,217.71,219.86,"105,568,602" +8/5/24,199.09,213.5,196,209.27,"119,548,602" +8/6/24,205.3,209.99,201.07,207.23,"69,660,484" +8/7/24,206.9,213.64,206.39,209.82,"63,516,422" +8/8/24,213.11,214.2,208.83,213.31,"47,161,152" +8/9/24,212.1,216.78,211.97,216.24,"42,201,648" +8/12/24,216.07,219.51,215.6,217.53,"38,028,090" +8/13/24,219.01,221.89,219.01,221.27,"44,155,328" +8/14/24,220.57,223.03,219.7,221.72,"41,960,566" +8/15/24,224.6,225.35,222.76,224.72,"46,414,008" +8/16/24,223.92,226.83,223.65,226.05,"44,340,238" +8/19/24,225.72,225.99,223.04,225.89,"40,687,809" +8/20/24,225.77,227.17,225.45,226.51,"30,299,029" +8/21/24,226.52,227.98,225.05,226.4,"34,765,480" +8/22/24,227.79,228.34,223.9,224.53,"43,695,320" +8/23/24,225.66,228.22,224.33,226.84,"38,677,246" +8/26/24,226.76,227.28,223.89,227.18,"30,602,211" +8/27/24,226,228.85,224.89,228.03,"35,934,559" +8/28/24,227.92,229.86,225.68,226.49,"38,052,168" +8/29/24,230.1,232.92,228.88,229.79,"51,906,301" +8/30/24,230.19,230.4,227.48,229,"52,990,770" +9/3/24,228.55,229,221.17,222.77,"50,087,200" +9/4/24,221.66,221.78,217.48,220.85,"43,840,199" +9/5/24,221.63,225.48,221.52,222.38,"36,615,398" +9/6/24,223.95,225.24,219.77,220.82,"48,423,008" +9/9/24,220.82,221.27,216.71,220.91,"67,179,969" +9/10/24,218.92,221.48,216.73,220.11,"51,591,031" +9/11/24,221.46,223.09,217.89,222.66,"44,587,070" +9/12/24,222.5,223.55,219.82,222.77,"37,498,230" +9/13/24,223.58,224.04,221.91,222.5,"36,766,621" +9/16/24,216.54,217.22,213.92,216.32,"59,357,434" +9/17/24,215.75,216.9,214.5,216.79,"45,519,340" +9/18/24,217.55,222.71,217.54,220.69,"59,894,930" +9/19/24,224.99,229.82,224.63,228.87,"66,781,320" +9/20/24,229.97,233.09,227.62,228.2,"318,679,906" +9/23/24,227.34,229.45,225.81,226.47,"54,146,020" +9/24/24,228.65,229.35,225.73,227.37,"43,556,070" +9/25/24,224.93,227.29,224.02,226.37,"42,308,719" +9/26/24,227.3,228.5,225.41,227.52,"36,636,711" +9/27/24,228.46,229.52,227.3,227.79,"34,025,969" +9/30/24,230.04,233,229.65,233,"54,793,391" +10/1/24,229.52,229.65,223.74,226.21,"63,285,047" +10/2/24,225.89,227.37,223.02,226.78,"32,880,609" +10/3/24,225.14,226.81,223.32,225.67,"34,044,160" +10/4/24,227.9,228,224.13,226.8,"37,345,102" +10/7/24,224.5,225.69,221.33,221.69,"39,505,352" +10/8/24,224.3,225.98,223.25,225.77,"31,855,689" +10/9/24,225.23,229.75,224.83,229.54,"33,591,090" +10/10/24,227.78,229.5,227.17,229.04,"28,183,539" +10/11/24,229.3,229.41,227.34,227.55,"31,759,189" +10/14/24,228.7,231.73,228.6,231.3,"39,882,090" +10/15/24,233.61,237.49,232.37,233.85,"64,751,367" +10/16/24,231.6,232.12,229.84,231.78,"34,082,238" +10/17/24,233.43,233.85,230.52,232.15,"32,993,809" +10/18/24,236.18,236.18,234.01,235,"46,431,473" +10/21/24,234.45,236.85,234.45,236.48,"36,254,473" +10/22/24,233.89,236.22,232.6,235.86,"38,846,578" +10/23/24,234.08,235.14,227.76,230.76,"52,286,980" +10/24/24,229.98,230.82,228.41,230.57,"31,109,500" +10/25/24,229.74,233.22,229.57,231.41,"38,802,301" +10/28/24,233.32,234.73,232.55,233.4,"36,087,129" +10/29/24,233.1,234.33,232.32,233.67,"35,417,246" +10/30/24,232.61,233.47,229.55,230.1,"47,070,910" +10/31/24,229.34,229.83,225.37,225.91,"64,370,090" +11/1/24,220.97,225.35,220.27,222.91,"65,276,738" +11/4/24,220.99,222.79,219.71,222.01,"44,944,473" +11/5/24,221.8,223.95,221.14,223.45,"28,111,340" +11/6/24,222.61,226.07,221.19,222.72,"54,561,121" +11/7/24,224.63,227.88,224.57,227.48,"42,137,688" +11/8/24,227.17,228.66,226.41,226.96,"38,328,820" +11/11/24,225,225.7,221.5,224.23,"42,005,602" +11/12/24,224.55,225.59,223.36,224.23,"40,398,301" +11/13/24,224.01,226.65,222.76,225.12,"48,566,219" +11/14/24,225.02,228.87,225,228.22,"44,923,941" +11/15/24,226.4,226.92,224.27,225,"47,923,699" +11/18/24,225.25,229.74,225.17,228.02,"44,686,020" +11/19/24,226.98,230.16,226.66,228.28,"36,211,770" +11/20/24,228.06,229.93,225.89,229,"35,169,566" +11/21/24,228.88,230.16,225.71,228.52,"42,108,328" +11/22/24,228.06,230.72,228.06,229.87,"38,168,246" +11/25/24,231.46,233.25,229.74,232.87,"90,152,828" +11/26/24,233.33,235.57,233.33,235.06,"45,986,191" +11/27/24,234.47,235.69,233.81,234.93,"33,498,441" +11/29/24,234.81,237.81,233.97,237.33,"28,481,381" +12/2/24,237.27,240.79,237.16,239.59,"48,137,102" +12/3/24,239.81,242.76,238.9,242.65,"38,861,020" +12/4/24,242.87,244.11,241.25,243.01,"44,383,941" +12/5/24,243.99,244.54,242.13,243.04,"40,033,879" +12/6/24,242.91,244.63,242.08,242.84,"36,870,621" +12/9/24,241.83,247.24,241.75,246.75,"44,649,230" +12/10/24,246.89,248.21,245.34,247.77,"36,914,809" +12/11/24,247.96,250.8,246.26,246.49,"45,205,809" +12/12/24,246.89,248.74,245.68,247.96,"32,777,531" +12/13/24,247.82,249.29,246.24,248.13,"33,155,289" +12/16/24,247.99,251.38,247.65,251.04,"51,694,754" +12/17/24,250.08,253.83,249.78,253.48,"51,356,359" +12/18/24,252.16,254.28,247.74,248.05,"56,774,102" +12/19/24,247.5,252,247.09,249.79,"60,882,262" +12/20/24,248.04,255,245.69,254.49,"147,495,297" +12/23/24,254.77,255.65,253.45,255.27,"40,858,770" +12/24/24,255.49,258.21,255.29,258.2,"23,234,711" +12/26/24,258.19,260.1,257.63,259.02,"27,262,980" +12/27/24,257.83,258.7,253.06,255.59,"42,355,320" +12/30/24,252.23,253.5,250.75,252.2,"35,557,539" +12/31/24,252.44,253.28,249.43,250.42,"39,480,719" \ No newline at end of file diff --git a/examples/currency_conversion.cpp b/examples/currency_conversion.cpp new file mode 100644 index 000000000..4617d516e --- /dev/null +++ b/examples/currency_conversion.cpp @@ -0,0 +1,28 @@ +// Copyright 2025 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +using namespace boost::decimal; + +auto convert_currency(decimal64 amount, decimal64 exchange_rate) -> decimal64 +{ + return amount * exchange_rate; +} + +int main() +{ + const auto usd_amount = strtod64("1000.50", nullptr); + const auto usd_to_eur_rate = strtod64("0.92", nullptr); + + const decimal64 eur_amount = convert_currency(usd_amount, usd_to_eur_rate); + constexpr decimal64 exact_eur_amount(92046, -2); + + std::cout << "USD: " << std::fixed << std::setprecision(2) << usd_amount << "\n"; + std::cout << "EUR: " << std::fixed << std::setprecision(2) << eur_amount << "\n"; + + return !(eur_amount == exact_eur_amount); +} diff --git a/examples/moving_average.cpp b/examples/moving_average.cpp new file mode 100644 index 000000000..3ac45ca85 --- /dev/null +++ b/examples/moving_average.cpp @@ -0,0 +1,188 @@ +// Copyright 2025 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + + +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::decimal; + +auto where_file(const std::string& test_vectors_filename) -> std::string +{ + // Try to open the file in each of the known relative paths + // in order to find out where it is located. + + // Boost-root + std::string test_vectors_filename_relative = "libs/decimal/examples/" + test_vectors_filename; + + std::ifstream in_01(test_vectors_filename_relative.c_str()); + + const bool file_01_is_open { in_01.is_open() }; + + + if(file_01_is_open) + { + in_01.close(); + } + else + { + // Local test directory or IDE + test_vectors_filename_relative = "../examples/" + test_vectors_filename; + + std::ifstream in_02(test_vectors_filename_relative.c_str()); + + const bool file_02_is_open { in_02.is_open() }; + + if(file_02_is_open) + { + in_02.close(); + } + else + { + // test/cover + test_vectors_filename_relative = "../../examples/" + test_vectors_filename; + + std::ifstream in_03(test_vectors_filename_relative.c_str()); + + const bool file_03_is_open { in_03.is_open() }; + + if(file_03_is_open) + { + in_03.close(); + } + else + { + // CMake builds + test_vectors_filename_relative = "../../../../libs/decimal/examples/" + test_vectors_filename; + + std::ifstream in_04(test_vectors_filename_relative.c_str()); + + const bool file_04_is_open { in_04.is_open() }; + + if(file_04_is_open) + { + in_04.close(); + } + else + { + // Try to open the file from the absolute path. + test_vectors_filename_relative = test_vectors_filename; + + std::ifstream in_05(test_vectors_filename_relative.c_str()); + + const bool file_05_is_open { in_05.is_open() }; + + if(file_05_is_open) + { + in_05.close(); + } + else + { + // Clion Cmake builds + test_vectors_filename_relative = "../../../libs/decimal/examples/" + test_vectors_filename; + + std::ifstream in_06(test_vectors_filename_relative.c_str()); + + const bool file_06_is_open { in_06.is_open() }; + if (file_06_is_open) + { + in_06.close(); + } + else + { + test_vectors_filename_relative = ""; + } + } + } + } + } + } + + return test_vectors_filename_relative; +} + +struct daily_data +{ + std::string date; + decimal64 open; + decimal64 high; + decimal64 low; + decimal64 close; + decimal64 volume; +}; + +// Function to split a CSV line into daily_data +auto parse_csv_line(const std::string& line) -> daily_data +{ + std::stringstream ss(line); + std::string token; + daily_data data; + + // Parse each column + std::getline(ss, data.date, ','); + std::getline(ss, token, ','); + from_chars(token.c_str(), token.c_str() + token.size(), data.open); + + std::getline(ss, token, ','); + from_chars(token.c_str(), token.c_str() + token.size(), data.high); + + std::getline(ss, token, ','); + from_chars(token.c_str(), token.c_str() + token.size(), data.low); + + std::getline(ss, token, ','); + from_chars(token.c_str(), token.c_str() + token.size(), data.close); + + std::getline(ss, token, ','); + from_chars(token.c_str(), token.c_str() + token.size(), data.volume); + + return data; +} + +int main() +{ + std::vector stock_data; + const int window_size = 30; + + // Open and read the CSV file + std::ifstream file(where_file("AAPL.csv")); + std::string line; + + // Skip header line + std::getline(file, line); + + // Read data + while (std::getline(file, line)) { + stock_data.push_back(parse_csv_line(line)); + } + + // Calculate and print 30-day moving averages + std::cout << "Date,30-Day Moving Average\n"; + + size_t loop_count = 0; // Trivial counter to ensure this ran in the CI + for (size_t i = window_size - 1; i < stock_data.size(); ++i) + { + decimal64 sum(0); + + // Calculate sum for the window + for (size_t j = 0; j < window_size; ++j) { + sum += stock_data[i - j].close; + } + + // Calculate average + decimal64 moving_avg = sum / decimal64(window_size); + + // Print result + std::cout << stock_data[i].date << "," + << std::fixed << std::setprecision(2) << moving_avg << "\n"; + + ++loop_count; + } + + return loop_count == 0U; +} diff --git a/test/Jamfile b/test/Jamfile index f3eb7cde1..bddeec3c7 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -150,3 +150,5 @@ run ../examples/bit_conversions.cpp ; run ../examples/charconv.cpp ; run ../examples/literals.cpp ; run ../examples/rounding_mode.cpp ; +run ../examples/moving_average.cpp ; +run ../examples/currency_conversion.cpp ;