import pandas as pd import matplotlib.pyplot as plt import numpy as np import yfinance…
- User
import pandas as pd import matplotlib.pyplot as plt import numpy as np import yfinance as yf
Fetching data
data = yf.download(tickers="EURUSD=X", period="1mo", interval="1h")['Close']
Dropping NaN values
data.dropna(inplace=True)
Calculating Z-score
x = data y = x.rolling(window=35).mean() z = x.rolling(window=35).std() score = (x - y) / z
Plotting Z-score and closing prices
fig, ax1 = plt.subplots(figsize=(20,10))
color = 'tab:red' ax1.set_xlabel('Date') ax1.set_ylabel('Closing Prices', color=color) ax1.plot(data.index, data, color=color) ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() color = 'tab:blue' ax2.set_ylabel('Z-Score', color=color) ax2.plot(score.index, score, color=color) ax2.axhline(0, color='black') ax2.axhline(1.96, color='red') ax2.axhline(-1.96, color='green') ax2.axhline(2.58, color='red') ax2.axhline(-2.58, color='green') ax2.axhline(3.26, color='red') ax2.axhline(-3.26, color='green') fig.tight_layout() plt.title('Closing Prices And Z-Score') plt.show() الكود الذي سوف اقوم بمشاركته معك اريدك ان تقوم باضافه اليه Variance
- Assistant