HMRZ-framework / hmr_periods_visuakizer.py
algorembrant's picture
Upload 6 files
736dbb9 verified
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import io
# 1. The CSV Data (embedded so you can run this immediately)
csv_data = """Start_Datetime,End_Datetime,Max_Leverage,Events
2026-02-25 05:28,2026-02-25 07:10,1000,
2026-02-25 14:45,2026-02-25 15:01,200,
2026-02-25 17:45,2026-02-25 18:01,200,
2026-02-25 23:15,2026-02-25 23:31,200,
2026-02-26 05:28,2026-02-26 07:10,1000,
2026-02-26 17:45,2026-02-26 18:01,200,
2026-02-26 21:15,2026-02-26 21:31,200,
2026-02-27 05:28,2026-02-27 07:10,1000,
2026-02-27 15:30,2026-02-27 16:01,200,
2026-02-27 16:40,2026-02-27 17:01,200,
2026-02-27 20:45,2026-02-27 21:01,200,
2026-02-27 21:15,2026-02-27 21:31,200,PPI MoM | PPI YoY | Core PPI MoM
2026-02-27 22:30,2026-02-27 23:01,200,Chicago PMI | Construction Spending MoM"""
# Load and format dates
df_hmr = pd.read_csv(io.StringIO(csv_data))
df_hmr['Start_Datetime'] = pd.to_datetime(df_hmr['Start_Datetime'])
df_hmr['End_Datetime'] = pd.to_datetime(df_hmr['End_Datetime'])
# 2. Generate Mock Price Data (to simulate XAU/USD movements)
date_rng = pd.date_range(start='2026-02-25 00:00', end='2026-02-28 00:00', freq='5min')
np.random.seed(42) # For reproducibility
# Simulate a gold chart starting around $2,030
price_data = 2030 + np.cumsum(np.random.randn(len(date_rng)) * 0.4)
df_price = pd.DataFrame({'Datetime': date_rng, 'Price': price_data})
# 3. Setting up the Plot
fig, ax = plt.subplots(figsize=(14, 7))
# Plot the mock XAU/USD line
ax.plot(df_price['Datetime'], df_price['Price'], color='#DAA520', label='XAU/USD (Mock Price)', linewidth=1.5)
# 4. Loop through the data to draw HMR Zones
for index, row in df_hmr.iterrows():
start = row['Start_Datetime']
end = row['End_Datetime']
leverage = row['Max_Leverage']
events = row['Events']
# Conditional coloring based on leverage restrictions
if leverage == 200:
zone_color = 'red'
alpha_val = 0.2
label_text = '1:200 HMR (Strict)' if index == 1 else "" # Prevent duplicate legend entries
else:
zone_color = 'blue'
alpha_val = 0.1
label_text = '1:1000 Margin' if index == 0 else ""
# axvspan creates the shaded vertical zones
ax.axvspan(start, end, color=zone_color, alpha=alpha_val, label=label_text)
# Add text annotation if there's an economic event tied to this period
if pd.notna(events):
# Truncate text to fit nicely and plot near the top of the chart
short_event = events.split('|')[0].strip() + '...'
ax.text(start, ax.get_ylim()[1] * 0.998, short_event,
rotation=90, verticalalignment='top', fontsize=9, color='darkred', weight='bold')
# 5. Formatting the Chart
ax.set_title('XAU/USD Algorithmic View with HMR Zones', fontsize=16, fontweight='bold')
ax.set_ylabel('Price (USD)', fontsize=12)
ax.set_xlabel('Date & Time', fontsize=12)
# Format the X-axis to cleanly display dates and hours
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d\n%H:%M'))
plt.xticks(rotation=0)
# Clean up the legend (remove empty label artifacts)
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax.legend(by_label.values(), by_label.keys(), loc='upper left')
plt.grid(True, linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()