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 from scipy.signal import argrelextrema
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 window = 35 rolling_mean = x.rolling(window=window).mean() rolling_std = x.rolling(window=window).std() score = (x - rolling_mean) / rolling_std
def detect_extrema(series, order=5): “”“Detect local minima and maxima”“” local_min = argrelextrema(series.values, np.less_equal, order=order)[0] local_max = argrelextrema(series.values, np.greater_equal, order=order)[0] return local_min, local_max
def calculate_probability(z_scores, level): “”“Calculate probability of z-scores beyond a given level”“” total_count = len(z_scores) beyond_count = np.sum(np.abs(z_scores) > level) return beyond_count / total_count
Detect local minima (point bas) and local maxima (point haut)
local_min, local_max = detect_extrema(x, order=5)
Calculate probability for significance levels
significance_levels = [1.96, 2.58, 3.26] probabilities = {level: calculate_probability(score, level) for level in significance_levels}
Plotting
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, label=‘Closing Price’) ax1.scatter(data.index[local_min], data.iloc[local_min], marker=‘^’, color=‘green’, alpha=1, label=‘Point Bas (Local Min)’) ax1.scatter(data.index[local_max], data.iloc[local_max], marker=‘v’, color=‘red’, alpha=1, label=‘Point Haut (Local Max)’) 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, label=‘Z-Score’) ax2.axhline(0, color=‘black’, linestyle=‘–’, label=‘Zero Revision Level’) for level in significance_levels: ax2.axhline(level, color=‘red’, linestyle=‘–’, label=f’Significance Level {level}‘) ax2.axhline(-level, color=‘green’, linestyle=’–')
ax2.tick_params(axis=‘y’, labelcolor=color) fig.tight_layout()
Adding title and legends
plt.title(‘Closing Prices And Z-Score with Extremes and Significance Levels’) ax1.legend(loc=‘upper left’) ax2.legend(loc=‘upper right’)
plt.show()
Print probabilities
print(“Probabilities of Z-Score beyond significance levels:”) for level, prob in probabilities.items(): print(f"Level {level}: {prob:.2%}")
- Assistant
- User
Change the symbols in the code from ' to "
- Assistant
- User
- Assistant
- User
turning point zero revision level This is the last model that I gave you. I want you to enter some additions or algorithms that give 0 reversal. This means that if you enter from a place in the market, it will not be reflected. The important thing is that I want the reversal level and the zero reversal level.
- Assistant
- User
File "", line 8 data = yf.download(tickers=“GC=F”, period=“1mo”, interval=“1h”)[“Close”] ^ SyntaxError: invalid character '“' (U+201C) Please correct me because I use COLAB GOOGLE
- Assistant