netflypsb commited on
Commit
1c344c6
·
verified ·
1 Parent(s): ac7b430

Update indicators/sma.py

Browse files
Files changed (1) hide show
  1. indicators/sma.py +29 -26
indicators/sma.py CHANGED
@@ -1,42 +1,45 @@
1
- # indicators/sma.py
2
-
3
  import pandas as pd
4
 
5
- def calculate_sma(data, period):
6
  """
7
- Calculates the Simple Moving Average (SMA) for a given period.
8
 
9
  Parameters:
10
- - data: DataFrame containing stock prices with a 'Close' column (DataFrame).
11
- - period: The period over which to calculate the SMA (int).
12
 
13
  Returns:
14
- - sma: The calculated SMA as a Series (pd.Series).
15
  """
16
- sma = data['Close'].rolling(window=period, min_periods=1).mean()
17
- return sma
18
 
19
- def add_sma_columns(data):
20
  """
21
- Adds SMA columns for the 21 and 50 periods to the input DataFrame.
22
 
23
  Parameters:
24
- - data: DataFrame containing stock prices. Must include a 'Close' column (DataFrame).
25
 
26
- Modifies:
27
- - data: The input DataFrame is modified in-place, adding two new columns: 'SMA_21' and 'SMA_50'.
28
  """
29
- data['SMA_21'] = calculate_sma(data, 21)
30
- data['SMA_50'] = calculate_sma(data, 50)
 
 
 
 
 
 
31
 
32
- # Example usage
33
  if __name__ == "__main__":
34
- # Assuming 'data' is a DataFrame that contains stock price data including a 'Close' column.
35
- # For the sake of example, let's create a dummy DataFrame.
36
- dates = pd.date_range(start="2023-01-01", end="2023-02-28", freq='D')
37
- prices = pd.Series([i * 0.01 for i in range(len(dates))], index=dates)
38
- data = pd.DataFrame(prices, columns=['Close'])
39
-
40
- # Add SMA columns
41
- add_sma_columns(data)
42
- print(data.head()) # Display the first few rows to verify the SMA calculations
 
 
 
 
1
  import pandas as pd
2
 
3
+ def calculate_sma(data, window):
4
  """
5
+ Calculate the Simple Moving Average (SMA) for the given data.
6
 
7
  Parameters:
8
+ - data (pd.Series): The stock data (typically closing prices).
9
+ - window (int): The period over which to calculate the SMA.
10
 
11
  Returns:
12
+ - pd.Series: The calculated SMA values.
13
  """
14
+ return data.rolling(window=window, min_periods=1).mean()
 
15
 
16
+ def calculate_21_50_sma(data):
17
  """
18
+ Calculate both the 21-period and 50-period SMAs for the given stock data.
19
 
20
  Parameters:
21
+ - data (pd.DataFrame): The stock data, expected to have a 'Close' column.
22
 
23
+ Returns:
24
+ - pd.DataFrame: The input data frame with added columns for the 21-period and 50-period SMAs.
25
  """
26
+ if 'Close' not in data.columns:
27
+ raise ValueError("Data frame must contain a 'Close' column.")
28
+
29
+ # Calculate the SMAs
30
+ data['SMA_21'] = calculate_sma(data['Close'], 21)
31
+ data['SMA_50'] = calculate_sma(data['Close'], 50)
32
+
33
+ return data
34
 
 
35
  if __name__ == "__main__":
36
+ # Example usage
37
+ # Generate a sample DataFrame with 'Close' prices
38
+ dates = pd.date_range(start='2023-01-01', periods=100, freq='D')
39
+ close_prices = pd.Series((100 + np.random.randn(100).cumsum()), index=dates)
40
+ sample_data = pd.DataFrame({'Close': close_prices})
41
+
42
+ # Calculate the 21 and 50 period SMAs
43
+ sma_data = calculate_21_50_sma(sample_data)
44
+
45
+ print(sma_data.head()) # Print the first few rows to verify