main.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import functions
  2. import config
  3. import imp
  4. while True:
  5. # ------------------------------------------- #
  6. # PART 1: Updating the notion trades database #
  7. # ------------------------------------------- #
  8. # Fetches the list of all trades stored in notion
  9. print("Fetching Data from Notion...", end=" ", flush=True)
  10. trades = functions.fetch_format_notion_trades(config.notion_db_id_trades)
  11. # Generates a list with unique tickers and no duplicates to reduce workload for the yfinance api
  12. print("Creating a list of unique tickers...", end=" ", flush=True)
  13. tickers = functions.filter_list_of_tickers(trades)
  14. # Configuration dependent execution:
  15. if config.calculate_benchmark == True:
  16. # Add the benchmark-ticker to the list of tickers to download data from yfinance from
  17. print("Adding benchmark-ticker...", end="", flush=True)
  18. tickers = functions.add_benchmark_ticker(tickers, config.ticker_benchmark)
  19. # Fetches & formats the complete history per ticker from yfinance
  20. print("Fetching & formating yfinance data", end="", flush=True)
  21. yf_data = functions.fetch_format_yf_data(tickers)
  22. # Calculates & stores a history per trade
  23. print("Calculating the history per trade...", end=" ", flush=True)
  24. history_per_trade = functions.calc_history_per_trade(trades, yf_data)
  25. # Configuration dependent execution:
  26. if config.update_notion == True:
  27. # Selects the most current values from the history per trade and overwrites them in the "trades" feteched from notion
  28. print("Selecting the most current values...", end=" ", flush=True)
  29. trades = functions.select_current_value_per_trade(trades, history_per_trade)
  30. # Updates the values in the notion database
  31. print("Updating the notion trades database", end="", flush=True)
  32. functions.push_notion_trades_update(trades)
  33. # ------------------------------------------------ #
  34. # PART 2: Updating the notion investments database #
  35. # ------------------------------------------------ #
  36. # Fetches the list of entries in the investment-overview database stored in notion
  37. print("Fetching & formating notion investments...", end=" ", flush=True)
  38. investments = functions.fetch_format_notion_investments(config.notion_db_id_investments)
  39. # Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name
  40. print("Calculating history per ticker...", end=" ", flush=True)
  41. history_per_ticker = functions.calc_history_per_ticker(history_per_trade, tickers, trades)
  42. # Configuration dependent execution:
  43. if config.update_notion == True:
  44. # Selects the most current values from the history per ticker and overwrites them in the "investments" feteched from notion
  45. print("Calculating current value per ticker...", end=" ", flush=True)
  46. investments = functions.select_current_value_per_ticker(investments, history_per_ticker)
  47. # Updates the values in the notion database
  48. print("Updating the notion ticker database", end="", flush=True)
  49. functions.push_notion_investment_update(investments)
  50. # ----------------------------------------- #
  51. # PART 3: Calculating Benchmark performance #
  52. # ----------------------------------------- #
  53. # Configuration dependent execution:
  54. if config.calculate_benchmark == True:
  55. # Creating benchmark trades
  56. print("Creating 'benchmark trades'...", end="", flush=True)
  57. benchmark_trades = functions.create_benchmark_trades(trades, yf_data)
  58. # Calculating benchmark trades
  59. print("Calculating the history per benchmark-trade...", end=" ", flush=True)
  60. history_per_benchmark_trade = functions.calc_history_per_trade(benchmark_trades, yf_data)
  61. # Calculates & stores a history for the benchmark
  62. print("Calculating benchmark-history overall...", end=" ", flush=True)
  63. history_benchmark = functions.calc_history_per_ticker(history_per_benchmark_trade, tickers, benchmark_trades)
  64. # Merging the benchmark_history into the ticker_history
  65. print("Merging the benchmark-history into the ticker-history...", end=" ", flush=True)
  66. history_per_ticker = functions.merge_histories(history_per_ticker, history_benchmark)
  67. # --------------------------------- #
  68. # PART 4: Updating the TRMNL Screen #
  69. # --------------------------------- #
  70. # Configuration dependent execution:
  71. if config.update_TRMNL == True:
  72. # Creates a list containing one date per week
  73. print("Creating a list with one entry per week...", end=" ", flush=True)
  74. list_filtered_dates = functions.create_list_filtered_dates(trades, config.trmnl_granularity)
  75. # Filter a weekly snapshot from the history per ticker
  76. print("Filtering the history per ticker to weekly values...", end=" ", flush=True)
  77. history_per_ticker_filtered = functions.filter_history_by_list(history_per_ticker, list_filtered_dates)
  78. # Prepare a new TRMNL update
  79. print("Constructing a TERMNL update object...", end=" ", flush=True)
  80. trmnl_update_object = functions.prep_trmnl_chart_udpate(
  81. history_per_ticker_filtered,
  82. series_to_show_1="total",
  83. data_to_show_1="current_value",
  84. series_to_show_2="benchmark",
  85. data_to_show_2="current_value"
  86. )
  87. # Push the update to TRMNL
  88. print("Updating a TERMNL screen...", end=" ", flush=True)
  89. functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_1, config.trmnl_headers)
  90. # Prepare a new TRMNL update
  91. print("Constructing a TERMNL update object...", end=" ", flush=True)
  92. trmnl_update_object = functions.prep_trmnl_chart_udpate(
  93. history_per_ticker_filtered,
  94. series_to_show_1="total",
  95. data_to_show_1="current_irr",
  96. series_to_show_2="benchmark",
  97. data_to_show_2="current_irr"
  98. )
  99. # Push the update to TRMNL
  100. print("Updating a TERMNL screen...", end=" ", flush=True)
  101. functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_2, config.trmnl_headers)
  102. # Prepare a new TRMNL update
  103. print("Constructing a TERMNL update object...", end=" ", flush=True)
  104. trmnl_update_object = functions.prep_trmnl_chart_udpate(
  105. history_per_ticker_filtered,
  106. series_to_show_1="total",
  107. data_to_show_1="total_performanance",
  108. series_to_show_2="benchmark",
  109. data_to_show_2="total_performanance"
  110. )
  111. # Push the update to TRMNL
  112. print("Updating a TERMNL screen...", end=" ", flush=True)
  113. functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_3, config.trmnl_headers)
  114. # --------------------------- #
  115. # PART 5: Cool off and repeat #
  116. # --------------------------- #
  117. # Clear variables
  118. trades = {}
  119. yf_data = {}
  120. history_per_trade = {}
  121. tickers = []
  122. # Reload config
  123. imp.reload(config)
  124. # Logging
  125. print("Completed cycle at: {}".format(functions.datetime.datetime.now()))
  126. print("Waiting a few minutes before the next execution")
  127. print("---------------------------------------------------------------------------")
  128. # Wait for api-cooldown
  129. functions.time.sleep(config.programm_cooldown_time * 60)