Zscore_Crypto / main_with_plot.py
gjin10969
initialize
e97cf97
import pandas as pd
import numpy as np
import ccxt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import time
import json
from datetime import datetime
import pytz
# Prompt for the symbol
symbols = input('Please input Symbol: ')
timeframe = input("please input time frame: ")
# Initialize Binance Futures API
binance = ccxt.binance({
'options': {'defaultType': 'future'}, # Specify futures
})
def fetch_and_calculate_zscore(symbol, timeframe=timeframe, limit=200, rolling_window=30):
# Fetch OHLCV data
data = binance.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
# Convert timestamp to UTC datetime format
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms', utc=True)
# Calculate rolling mean, std, and Z-Score
df['mean'] = df['close'].rolling(window=rolling_window).mean()
df['std'] = df['close'].rolling(window=rolling_window).std()
df['z_score'] = (df['close'] - df['mean']) / df['std']
# Initialize signal columns
df['buy_signal'] = 0
df['sell_signal'] = 0
# Variables to track thresholds
in_sell_signal = False
in_buy_signal = False
signal_triggered = False # Track if any signal was triggered
# Iterate through the dataframe to track signals
for i in range(1, len(df)):
current_z = df.loc[i, 'z_score']
previous_z = df.loc[i - 1, 'z_score']
# Handle Z-score crossing extreme thresholds for sell signal
if not in_sell_signal:
# Z-score crosses above 1.85 (potential sell signal)
if current_z > 1.85 and previous_z <= 1.85:
#print(f"Sell signal candidate at index {i}, Z-score = {current_z}")
in_sell_signal = True
# Handle Z-score crossing extreme thresholds for buy signal
if not in_buy_signal:
# Z-score crosses below -1.85 (potential buy signal)
if current_z < -1.85 and previous_z >= -1.85:
#print(f"Buy signal candidate at index {i}, Z-score = {current_z}")
in_buy_signal = True
# Keep the signal active if the Z-score remains within the range
if in_sell_signal:
# Sell signal is triggered between 1.85 and 1
if 1 <= current_z <= 1.85:
df.loc[i, 'sell_signal'] = 1 # Sell signal active
#print(f"Sell signal active at index {i}, Z-score = {current_z}")
signal_triggered = True
# Exit sell signal if Z-score falls below 1
elif current_z < 1:
in_sell_signal = False
#print(f"Sell signal exited at index {i}, Z-score = {current_z}")
if in_buy_signal:
# Buy signal is triggered between -1.85 and -1
if -1.85 <= current_z <= -1:
df.loc[i, 'buy_signal'] = 1 # Buy signal active
#print(f"Buy signal active at index {i}, Z-score = {current_z}")
signal_triggered = True
# Exit buy signal if Z-score rises above -1
elif current_z > -1:
in_buy_signal = False
#print(f"Buy signal exited at index {i}, Z-score = {current_z}")
return df
utc_time = datetime.utcnow()
philippine_tz = pytz.timezone('Asia/Manila')
philippine_time = pytz.utc.localize(utc_time).astimezone(philippine_tz)
# Format the time in your preferred format
formatted_ph_time = philippine_time.strftime("%Y-%m-%d %H:%M:%S")
# Function to update signals in JSON
# Function to update signals in JSON with real-time Z-Score
def update_signal_json(symbol, df, json_data):
# Extract latest data point
latest_data = df.iloc[-1]
# Prepare new entry with real-time Z-Score
signal_entry = {
"symbol": symbol,
"time_frame": timeframe,
"date_and_time": latest_data['timestamp'].strftime("%Y-%m-%d %H:%M:%S"),
"realtime_ph_time": formatted_ph_time, # Add the local Philippine time (UTC+8)
"current_price": latest_data['close'],
"zscore": latest_data['z_score']
}
# Remove previous entries for this symbol
json_data = [entry for entry in json_data if entry['symbol'] != symbol]
# Add the latest entry
json_data.append(signal_entry)
return json_data
# Function to plot data
def plot_data(btcdom_df, pair_df, btc_df, ax):
ax.clear() # Clear previous plotspython /home/gjin/Documents/zscore/main_with_plot.py
# Plot Z-Scores for all pairs
ax.plot(btcdom_df['timestamp'], btcdom_df['z_score'], label="BTCDOM/USDT Z-Score", color='blue', linestyle='-')
ax.plot(pair_df['timestamp'], pair_df['z_score'], label=f"{symbols}/USDT Z-Score", color='orange', linestyle='-')
ax.plot(btc_df['timestamp'], btc_df['z_score'], label="BTC/USDT Z-Score", color='gray', linestyle='-')
# Add thresholds
ax.axhline(y=2, color='red', linestyle='--', label='Overbought Threshold')
ax.axhline(y=-2, color='green', linestyle='--', label='Oversold Threshold')
# Plot Buy and Sell signals for BTCDOM/USDT
ax.scatter(btcdom_df[btcdom_df['buy_signal'] == 1]['timestamp'], btcdom_df[btcdom_df['buy_signal'] == 1]['z_score'],
marker='^', color='green', label='BTCDOM Buy Signal')
ax.scatter(btcdom_df[btcdom_df['sell_signal'] == 1]['timestamp'], btcdom_df[btcdom_df['sell_signal'] == 1]['z_score'],
marker='v', color='red', label='BTCDOM Sell Signal')
# Plot signals for the other pair
ax.scatter(pair_df[pair_df['buy_signal'] == 1]['timestamp'], pair_df[pair_df['buy_signal'] == 1]['z_score'],
marker='^', color='green', alpha=0.5, label=f"{symbols} Buy Signal")
ax.scatter(pair_df[pair_df['sell_signal'] == 1]['timestamp'], pair_df[pair_df['sell_signal'] == 1]['z_score'],
marker='v', color='red', alpha=0.5, label=f"{symbols} Sell Signal")
# Format plot
ax.set_title(f"Z-Scores Signals {timeframe} for {symbols}/USDT Futures", fontsize=16)
ax.set_xlabel("Time (UTC)", fontsize=12)
ax.set_ylabel("Z-Score", fontsize=12)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M"))
ax.legend(loc="upper left")
ax.grid(True)
plt.xticks(rotation=45)
# Real-time monitoring
# Real-time monitoring
def run_real_time():
json_data = []
try:
with open('signals.json', 'r') as file:
json_data = json.load(file)
except FileNotFoundError:
pass
fig, ax = plt.subplots(figsize=(14, 7))
plt.ion() # Interactive plotting
while True:
btcdom_symbol = 'BTCDOM/USDT'
pair_symbol = f'{symbols}/USDT'
btc_symbol = 'BTC/USDT'
btcdom_df = fetch_and_calculate_zscore(btcdom_symbol)
pair_df = fetch_and_calculate_zscore(pair_symbol)
btc_df = fetch_and_calculate_zscore(btc_symbol)
# Update JSON for BTCDOM
json_data = update_signal_json(btcdom_symbol, btcdom_df, json_data)
# Update JSON for selected pair
json_data = update_signal_json(pair_symbol, pair_df, json_data)
# Update JSON for BTC/USDT
json_data = update_signal_json(btc_symbol, btc_df, json_data)
# Save updated JSON
with open('signals.json', 'w') as file:
json.dump(json_data, file, indent=4)
# Update plot
plot_data(btcdom_df, pair_df, btc_df, ax)
plt.draw()
plt.pause(60) # Update every 60 seconds
time.sleep(60)
# Run the real-time system
run_real_time()