netflypsb commited on
Commit
517c0ba
·
verified ·
1 Parent(s): bc22d0d

Create indicators/sma.py

Browse files
Files changed (1) hide show
  1. indicators/sma.py +21 -0
indicators/sma.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ def calculate_sma(data, column='close', period=21):
4
+ """
5
+ Calculates the Simple Moving Average (SMA) for a specified column in the data DataFrame.
6
+
7
+ Args:
8
+ data (pd.DataFrame): The input DataFrame containing the price data.
9
+ column (str): The name of the column to calculate the SMA on. Default is 'close'.
10
+ period (int): The period over which to calculate the SMA. Default is 21.
11
+
12
+ Returns:
13
+ pd.Series: A pandas Series containing the SMA for the specified period.
14
+ """
15
+ sma = data[column].rolling(window=period, min_periods=1).mean()
16
+ return sma
17
+
18
+ # Example usage:
19
+ # Assuming `data` is a pandas DataFrame with a 'close' column containing closing prices:
20
+ # sma21 = calculate_sma(data, column='close', period=21)
21
+ # data['SMA_21'] = sma21