Advertisement
BAMpas

Untitled

Sep 9th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. ##### GET DATA --> in this case I got it from ALPACA (IEX). save as df in ohlc format
  2.  
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5.  
  6. # Ensure your DataFrame 'df' has a DateTime index
  7. df.index = pd.to_datetime(df.index)
  8.  
  9. # Extract the time of the highest and lowest price for each day
  10. daily_high_times = df.loc[df.groupby(df.index.date)['high'].idxmax()].index
  11. daily_low_times = df.loc[df.groupby(df.index.date)['low'].idxmin()].index
  12.  
  13. # Convert times to numeric format (hours and minutes as float)
  14. high_times = daily_high_times.map(lambda x: x.hour + x.minute / 60)
  15. low_times = daily_low_times.map(lambda x: x.hour + x.minute / 60)
  16.  
  17. # Filter to include only times between 6:30 AM (6.5 in decimal) and 1:00 PM (13.0 in decimal)
  18. high_times_filtered = high_times[(high_times >= 6.5) & (high_times <= 13.0)]
  19. low_times_filtered = low_times[(low_times >= 6.5) & (low_times <= 13.0)]
  20.  
  21. # Plotting the histogram for High Times
  22. plt.figure(figsize=(16, 2))
  23. plt.hist(high_times_filtered, bins=int(6.5*12), color='blue', edgecolor='black')
  24. plt.xlabel('Time of Day (Hours)')
  25. plt.ylabel('Frequency')
  26. plt.title(f'Frequency of Daily High Occurrence Times ({start_date} to {end_date})')
  27. plt.xticks([6.5 + i * 0.5 for i in range(14)],
  28. [f'{int(hour)}:{int((hour - int(hour)) * 60):02d}' for hour in [6.5 + i * 0.5 for i in range(14)]],
  29. rotation=45)
  30. plt.grid(axis='y', alpha=0.75)
  31. plt.show()
  32.  
  33. # Plotting the histogram for Low Times
  34. plt.figure(figsize=(16, 2))
  35. plt.hist(low_times_filtered, bins=int(6.5*12), color='red', edgecolor='black')
  36. plt.xlabel('Time of Day (Hours)')
  37. plt.ylabel('Frequency')
  38. plt.title(f'Frequency of Daily Low Occurrence Times ({start_date} to {end_date})')
  39. plt.xticks([6.5 + i * 0.5 for i in range(14)],
  40. [f'{int(hour)}:{int((hour - int(hour)) * 60):02d}' for hour in [6.5 + i * 0.5 for i in range(14)]],
  41. rotation=45)
  42. plt.grid(axis='y', alpha=0.75)
  43. plt.show()
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement