-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add Simple Moving Average (SMA) Calculation #9300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a045924
Add Simple Moving Average (SMA) Calculation
Berat-O 2dd9f0b
Update financial/simple_moving_average.py
Berat-O cbbb8e2
Update financial/simple_moving_average.py
Berat-O 34dbc93
Update financial/simple_moving_average.py
Berat-O a2ead5a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36de41e
Update simple_moving_average.py
Berat-O a3cf9d4
Update financial/simple_moving_average.py
tianyizheng02 656c3b3
Update simple_moving_average.py
tianyizheng02 4a71b43
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]: | ||
Berat-O marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
|
||
: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. | ||
Berat-O marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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] = [] | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.