Python:import yfinance as yf import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from tabulate import tabulate # Fetch Apple's daily stock data daily_data = yf.download('AAPL', start='2020-01-01') # Fetch Apple's weekly stock data weekly_data = yf.download('AAPL', start='2020-01-01', interval='1wk') # Function to calculate Ichimoku components def calculate_ichimoku(data): high_window = 9 low_window = 9 base_window = 26 span_b_window = 52 displacement = 26 data['Tenkan-sen'] = (data['High'].rolling(window=high_window).max() + data['Low'].rolling(window=high_window).min()) / 2 data['Kijun-sen'] = (data['High'].rolling(window=base_window).max() + data['Low'].rolling(window=base_window).min()) / 2 data['Senkou Span A'] = ((data['Tenkan-sen'] + data['Kijun-sen']) / 2).shift(displacement) data['Senkou Span B'] = (data['High'].rolling(window=span_b_window).max() + data['Low'].rolling(window=span_b_window).min()) / 2 data['Chikou Span'] = data['Close'].shift(-displacement) return data # Calculate Ichimoku for daily data daily_data = calculate_ichimoku(daily_data) # Calculate Ichimoku for weekly data weekly_data = calculate_ichimoku(weekly_data) # Set up Seaborn style sns.set(style="whitegrid") # Create a Seaborn plot for daily Ichimoku Cloud plt.figure(figsize=(14, 7)) plt.plot(daily_data['Close'], label='Close Price', color='black', linewidth=1.5) plt.plot(daily_data['Tenkan-sen'], label='Tenkan-sen', color='red', linewidth=1.5) plt.plot(daily_data['Kijun-sen'], label='Kijun-sen', color='blue', linewidth=1.5) plt.plot(daily_data['Senkou Span A'], label='Senkou Span A', color='green', linewidth=1.5) plt.plot(daily_data['Senkou Span B'], label='Senkou Span B', color='orange', linewidth=1.5) plt.plot(daily_data['Chikou Span'], label='Chikou Span', color='purple', linewidth=1.5) # Fill the cloud plt.fill_between(daily_data.index, daily_data['Senkou Span A'], daily_data['Senkou Span B'], where=daily_data['Senkou Span A'] >= daily_data['Senkou Span B'], color='lightgreen', alpha=0.5) plt.fill_between(daily_data.index, daily_data['Senkou Span A'], daily_data['Senkou Span B'], where=daily_data['Senkou Span A'] < daily_data['Senkou Span B'], color='lightcoral', alpha=0.5) plt.title('Ichimoku Cloud - Daily Apple Stock') plt.xlabel('Date') plt.ylabel('Price') plt.legend() plt.grid() plt.show() # Prepare multitimeframe trends table def trend_analysis(data): most_recent = data.iloc[-1] trend = "" if most_recent['Close'] > most_recent['Senkou Span A'] and most_recent['Close'] > most_recent['Senkou Span B']: trend = "Bullish" elif most_recent['Close'] < most_recent['Senkou Span A'] and most_recent['Close'] < most_recent['Senkou Span B']: trend = "Bearish" else: trend = "Sideways" return trend daily_trend = trend_analysis(daily_data) weekly_trend = trend_analysis(weekly_data) # Create a summary table summary = pd.DataFrame({ 'Timeframe': ['Daily', 'Weekly'], 'Current Trend': [daily_trend, weekly_trend] }) # Print the summary table print(tabulate(summary, headers='keys', tablefmt='psql'))
We use essential cookies to make this site work, and optional cookies to enhance your experience.