From a0459244118f7e49a735728e8cae930ce06e8559 Mon Sep 17 00:00:00 2001 From: Berat Osman Demiralay Date: Sun, 1 Oct 2023 17:36:18 +0300 Subject: [PATCH 1/9] Add Simple Moving Average (SMA) Calculation This commit adds a Python script for calculating the Simple Moving Average (SMA) of a time series data. The script also includes a doctest that verifies the correctness of the SMA calculations for a sample dataset. Usage: - Run the script with your own time series data and specify the window size for SMA calculations. --- financial/simple_moving_average.py | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 financial/simple_moving_average.py diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py new file mode 100644 index 000000000000..a936832ae628 --- /dev/null +++ b/financial/simple_moving_average.py @@ -0,0 +1,59 @@ +""" +Calculate the Simple Moving Average (SMA) for a time series data. +https://en.wikipedia.org/wiki/Moving_average +""" + + +def simple_moving_average(data: list[int], window_size: int) -> list[float | None]: + """ + + + :param data: A list of numerical data points. + :param window_size: An integer representing the size of the SMA window. + :return: A list of SMA values with the same length as the input data. + + The Simple Moving Average (SMA) is a statistical calculation used to + analyze data points by creating + a constantly updated average price over a specific time period. + In finance, SMA is often used in technical + analysis to smooth out price data and identify trends. + + Example: + >>> sma = simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 3) + >>> [round(value, 2) if value is not None else None for value in sma] + [None, None, 12.33, 13.33, 14.0, 14.33, 16.0, 17.0, 18.0, 19.0] + """ + + sma: list[float | None] = [] + + for i in range(len(data)): + if i < window_size - 1: + sma.append(None) # SMA not available for early data points + else: + window = data[i - window_size + 1 : i + 1] + sma_value = sum(window) / window_size + sma.append(sma_value) + return sma + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Example data (replace with your own time series data) + data = [10, 12, 15, 13, 14, 16, 18, 17, 19, 21] + + # Specify the window size for the SMA + window_size = 3 + + # Calculate the Simple Moving Average + sma_values = simple_moving_average(data, window_size) + + # Print the SMA values + print("Simple Moving Average (SMA) Values:") + for i, value in enumerate(sma_values): + if value is not None: + print(f"Day {i + 1}: {value:.2f}") + else: + print(f"Day {i + 1}: Not enough data for SMA") From 2dd9f0b25dde873d97278be81f8e9839226a3302 Mon Sep 17 00:00:00 2001 From: Berat Osman Demiralay Date: Mon, 23 Oct 2023 11:50:57 +0300 Subject: [PATCH 2/9] Update financial/simple_moving_average.py Co-authored-by: Tianyi Zheng --- financial/simple_moving_average.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index a936832ae628..e4bcf963c030 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -6,18 +6,12 @@ def simple_moving_average(data: list[int], window_size: int) -> list[float | None]: """ - + Calculate the simple moving average (SMA) for some given time series data. :param data: A list of numerical data points. :param window_size: An integer representing the size of the SMA window. :return: A list of SMA values with the same length as the input data. - The Simple Moving Average (SMA) is a statistical calculation used to - analyze data points by creating - a constantly updated average price over a specific time period. - In finance, SMA is often used in technical - analysis to smooth out price data and identify trends. - Example: >>> sma = simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 3) >>> [round(value, 2) if value is not None else None for value in sma] From cbbb8e27208ec5cf932f9103cc0b962670148e13 Mon Sep 17 00:00:00 2001 From: Berat Osman Demiralay Date: Mon, 23 Oct 2023 11:51:06 +0300 Subject: [PATCH 3/9] Update financial/simple_moving_average.py Co-authored-by: Tianyi Zheng --- financial/simple_moving_average.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index e4bcf963c030..87b39a1df9eb 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -1,6 +1,9 @@ """ -Calculate the Simple Moving Average (SMA) for a time series data. -https://en.wikipedia.org/wiki/Moving_average +The Simple Moving Average (SMA) is a statistical calculation used to analyze data points +by creating a constantly updated average price over a specific time period. In finance, SMA is +often used in time series analysis to smooth out price data and identify trends. + +Reference: https://en.wikipedia.org/wiki/Moving_average """ From 34dbc9392409bcb52036476e720c944c3e181e0d Mon Sep 17 00:00:00 2001 From: Berat Osman Demiralay Date: Mon, 23 Oct 2023 11:51:13 +0300 Subject: [PATCH 4/9] Update financial/simple_moving_average.py Co-authored-by: Tianyi Zheng --- financial/simple_moving_average.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index 87b39a1df9eb..77af0d4014de 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -7,7 +7,7 @@ """ -def simple_moving_average(data: list[int], window_size: int) -> list[float | None]: +def simple_moving_average(data: list[float], window_size: int) -> list[float | None]: """ Calculate the simple moving average (SMA) for some given time series data. From a2ead5a46a9d0dc0dd5f6d0c2bb9cdd57f78c25a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:51:45 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/simple_moving_average.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index 77af0d4014de..442cd2b279d4 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -1,6 +1,6 @@ """ The Simple Moving Average (SMA) is a statistical calculation used to analyze data points -by creating a constantly updated average price over a specific time period. In finance, SMA is +by creating a constantly updated average price over a specific time period. In finance, SMA is often used in time series analysis to smooth out price data and identify trends. Reference: https://en.wikipedia.org/wiki/Moving_average From 36de41eaee95fddc4404bb07fad27d49dd8595a7 Mon Sep 17 00:00:00 2001 From: Berat Osman Demiralay Date: Mon, 23 Oct 2023 12:04:14 +0300 Subject: [PATCH 6/9] Update simple_moving_average.py --- financial/simple_moving_average.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index 442cd2b279d4..a78e219ef351 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -1,7 +1,8 @@ """ The Simple Moving Average (SMA) is a statistical calculation used to analyze data points -by creating a constantly updated average price over a specific time period. In finance, SMA is -often used in time series analysis to smooth out price data and identify trends. +by creating a constantly updated average price over a specific time period. +In finance, SMA is often used in time series analysis to smooth out price data +and identify trends. Reference: https://en.wikipedia.org/wiki/Moving_average """ From a3cf9d43248f2c6b58f5d3131eca2062938701f8 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Mon, 23 Oct 2023 09:17:57 -0400 Subject: [PATCH 7/9] Update financial/simple_moving_average.py --- financial/simple_moving_average.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index a78e219ef351..b38fa670e74c 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -16,11 +16,19 @@ def simple_moving_average(data: list[float], window_size: int) -> list[float | N :param window_size: An integer representing the size of the SMA window. :return: A list of SMA values with the same length as the input data. - Example: + Examples: >>> sma = simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 3) >>> [round(value, 2) if value is not None else None for value in sma] [None, None, 12.33, 13.33, 14.0, 14.33, 16.0, 17.0, 18.0, 19.0] + >>> simple_moving_average([10, 12, 15], 5) + [None, None, None] + >>> simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 0) + Traceback (most recent call last): + ... + ValueError: Window size must be a positive integer """ + if window_size < 1: + raise ValueError("Window size must be a positive integer") sma: list[float | None] = [] From 656c3b366b3a13c1152862d7c590bdc68e6b4667 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Mon, 23 Oct 2023 09:20:17 -0400 Subject: [PATCH 8/9] Update simple_moving_average.py --- financial/simple_moving_average.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index b38fa670e74c..2b522fcca43d 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -6,9 +6,10 @@ Reference: https://en.wikipedia.org/wiki/Moving_average """ +from collections.abc import Sequence -def simple_moving_average(data: list[float], window_size: int) -> list[float | None]: +def simple_moving_average(data: Sequence[float], window_size: int) -> list[float | None]: """ Calculate the simple moving average (SMA) for some given time series data. From 4a71b43adfae055c1374c95b4aa82d13eb86bc3a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:20:53 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/simple_moving_average.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/financial/simple_moving_average.py b/financial/simple_moving_average.py index 2b522fcca43d..d5d68ffd3dab 100644 --- a/financial/simple_moving_average.py +++ b/financial/simple_moving_average.py @@ -9,7 +9,9 @@ from collections.abc import Sequence -def simple_moving_average(data: Sequence[float], window_size: int) -> list[float | None]: +def simple_moving_average( + data: Sequence[float], window_size: int +) -> list[float | None]: """ Calculate the simple moving average (SMA) for some given time series data.