Ethereum

import pandas as pd import numpy as np import matplotlib.pyplot

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf

# Stock data download (for example, Apple stock)
stock_symbol = 'AAPL'
data = yf.download(stock_symbol, start='2020-01-01', end='2025-01-01')

# Calculate Short and Long Moving Averages
short_window = 40
long_window = 100

data['Short_MA'] = data['Close'].rolling(window=short_window, min_periods=1).mean()
data['Long_MA'] = data['Close'].rolling(window=long_window, min_periods=1).mean()

# Generate signals
data['Signal'] = 0
data['Signal'][short_window:] = np.where(data['Short_MA'][short_window:] > data['Long_MA'][short_window:], 1, 0)
data['Position'] = data['Signal'].diff()

# Plotting the data
plt.figure(figsize=(12,6))
plt.plot(data['Close'], label='Close Price')
plt.plot(data['Short_MA'], label=f'{short_window} Days Moving Average')
plt.plot(data['Long_MA'], label=f'{long_window} Days Moving Average')

plt.scatter(data.index[data['Position'] == 1], data['Short_MA'][data['Position'] == 1], marker='^', color='g', label='Buy Signal', alpha=1)
plt.scatter(data.index[data['Position'] == -1], data['Short_MA'][data['Position'] == -1], marker='v', color='r', label='Sell Signal', alpha=1)

plt.title(f'{stock_symbol} Moving Average Crossover Strategy')
plt.legend(loc='best')
plt.show()

Wyłączenie odpowiedzialności