mqxym

btc_weekday_movements_analysis.py

Apr 16th, 2024 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | Cryptocurrency | 0 0
  1. import csv
  2. from datetime import datetime
  3. from collections import defaultdict
  4.  
  5. # CSV with format date,start_price,daily_high,daily_low
  6. # Can be downloaded here: https://finance.yahoo.com/quote/BTC-USD/history/
  7. filename = 'BTC-USD.csv'
  8.  
  9. def get_weekday(date_str):
  10.     date_obj = datetime.strptime(date_str, '%Y-%m-%d')
  11.     return date_obj.strftime('%A')
  12.  
  13. # Initialize dictionaries to store counts and cumulative losses
  14. analysis_count = defaultdict(int)
  15. analysis_loss = defaultdict(float)  # Use float for loss values
  16. analysis_gain = defaultdict(float)
  17.  
  18. with open(filename, 'r', newline='') as csvfile:
  19.     reader = csv.reader(csvfile)
  20.     next(reader)  # Skip the header
  21.     for row in reader:
  22.         date_weekday = get_weekday(row[0])
  23.         start_price = float(row[1])
  24.         day_high = float(row[2])
  25.         day_low = float(row[3])
  26.         difference_low = (1 - (day_low / start_price)) * 100 # Calculate the loss
  27.         difference_high = (day_high / start_price - 1) * 100
  28.  
  29.         # Increment the count sum of losses for the weekday
  30.         analysis_count[date_weekday] += 1
  31.         analysis_loss[date_weekday] += difference_low
  32.         analysis_gain[date_weekday] += difference_high
  33.  
  34. # Calculate average for each weekday.
  35. average_loss = {weekday: analysis_loss[weekday] / analysis_count[weekday] for weekday in analysis_loss}
  36. average_gain = {weekday: analysis_gain[weekday] / analysis_count[weekday] for weekday in analysis_gain}
  37.  
  38. print('Average loss:')
  39. for weekday, avg_loss in average_loss.items():
  40.     print(f'{weekday}: {avg_loss:.3f}')  # Formats the average loss to two decimal places
  41.  
  42. print('Average gain:')
  43. for weekday, avg_gain in average_gain.items():
  44.     print(f'{weekday}: {avg_gain:.3f}')
Add Comment
Please, Sign In to add comment