-
-
Save quantra-go-algo/8a398ec1bc372f9c45d900fca3a62174 to your computer and use it in GitHub Desktop.
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
| # 1) IMPORT LIBRARIES | |
| import yfinance as yf # to fetch historical stock data | |
| import pandas as pd # for DataFrame & rolling calculations | |
| import matplotlib.pyplot as plt # for plotting | |
| # 2) DOWNLOAD TESLA (TSLA) DATA | |
| # - 'start' is inclusive, 'end' is exclusive | |
| tesla = yf.download( | |
| tickers='TSLA', | |
| start='2023-01-01', | |
| end='2025-04-25', | |
| progress=False | |
| ) | |
| # 3) EXTRACT CLOSE PRICE SERIES | |
| close = tesla['Close'] | |
| # 4) CALCULATE 30-DAY SIMPLE MOVING AVERAGE (SMA) | |
| sma30 = close.rolling(window=30).mean() | |
| # 5) CALCULATE 30-DAY TRIANGULAR MOVING AVERAGE (TMA) | |
| # TMA = SMA of the SMA | |
| tma30 = sma30.rolling(window=30).mean() | |
| # 6) PLOT CLOSE, SMA AND TMA | |
| plt.figure(figsize=(12, 6)) | |
| # raw close price | |
| plt.plot(close.index, close, label='Close Price', linewidth=1) | |
| # 30-day SMA | |
| plt.plot(sma30.index, sma30, label='30-Day SMA', linewidth=1) | |
| # 30-day TMA | |
| plt.plot(tma30.index, tma30, label='30-Day TMA', linewidth=1) | |
| # 7) FORMAT THE PLOT | |
| plt.title('Tesla Close Price with 30-Day SMA & TMA\n2023–2025') | |
| plt.xlabel('Date') | |
| plt.ylabel('Price (USD)') | |
| plt.legend() | |
| plt.grid(True) | |
| plt.tight_layout() | |
| # 8) DISPLAY THE PLOT | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment