Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##### GET DATA --> in this case I got it from ALPACA (IEX). save as df in ohlc format
- import pandas as pd
- import matplotlib.pyplot as plt
- # Ensure your DataFrame 'df' has a DateTime index
- df.index = pd.to_datetime(df.index)
- # Extract the time of the highest and lowest price for each day
- daily_high_times = df.loc[df.groupby(df.index.date)['high'].idxmax()].index
- daily_low_times = df.loc[df.groupby(df.index.date)['low'].idxmin()].index
- # Convert times to numeric format (hours and minutes as float)
- high_times = daily_high_times.map(lambda x: x.hour + x.minute / 60)
- low_times = daily_low_times.map(lambda x: x.hour + x.minute / 60)
- # Filter to include only times between 6:30 AM (6.5 in decimal) and 1:00 PM (13.0 in decimal)
- high_times_filtered = high_times[(high_times >= 6.5) & (high_times <= 13.0)]
- low_times_filtered = low_times[(low_times >= 6.5) & (low_times <= 13.0)]
- # Plotting the histogram for High Times
- plt.figure(figsize=(16, 2))
- plt.hist(high_times_filtered, bins=int(6.5*12), color='blue', edgecolor='black')
- plt.xlabel('Time of Day (Hours)')
- plt.ylabel('Frequency')
- plt.title(f'Frequency of Daily High Occurrence Times ({start_date} to {end_date})')
- plt.xticks([6.5 + i * 0.5 for i in range(14)],
- [f'{int(hour)}:{int((hour - int(hour)) * 60):02d}' for hour in [6.5 + i * 0.5 for i in range(14)]],
- rotation=45)
- plt.grid(axis='y', alpha=0.75)
- plt.show()
- # Plotting the histogram for Low Times
- plt.figure(figsize=(16, 2))
- plt.hist(low_times_filtered, bins=int(6.5*12), color='red', edgecolor='black')
- plt.xlabel('Time of Day (Hours)')
- plt.ylabel('Frequency')
- plt.title(f'Frequency of Daily Low Occurrence Times ({start_date} to {end_date})')
- plt.xticks([6.5 + i * 0.5 for i in range(14)],
- [f'{int(hour)}:{int((hour - int(hour)) * 60):02d}' for hour in [6.5 + i * 0.5 for i in range(14)]],
- rotation=45)
- plt.grid(axis='y', alpha=0.75)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement