main.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import functions
  2. import config
  3. while True:
  4. # ------------------------------------------- #
  5. # PART 1: Updating the notion trades database #
  6. # ------------------------------------------- #
  7. # Fetches the list of all trades stored in notion
  8. print("Fetching Data from Notion...", end=" ", flush=True)
  9. trades = functions.fetch_format_notion_trades(config.notion_db_id_trades)
  10. # Generates a list with unique tickers and no duplicates to reduce workload for the yfinance api
  11. print("Creating a list of unique tickers...", end=" ", flush=True)
  12. tickers = functions.filter_list_of_tickers(trades)
  13. # Fetches & formats the complete history per ticker from yfinance
  14. print("Fetching & formating yfinance data", end="", flush=True)
  15. yf_data = functions.fetch_format_yf_data(tickers)
  16. # Calculates & stores a history per trade
  17. print("Calculating the history per trade...", end=" ", flush=True)
  18. history_per_trade = functions.calc_history_per_trade(trades, yf_data)
  19. # Configuration dependent execution:
  20. if config.update_notion == True:
  21. # Selects the most current values from the history per trade and overwrites them in the "trades" feteched from notion
  22. print("Selecting the most current values...", end=" ", flush=True)
  23. trades = functions.select_current_value_per_trade(trades, history_per_trade)
  24. # Updates the values in the notion database
  25. print("Updating the notion trades database", end="", flush=True)
  26. functions.push_notion_trades_update(trades)
  27. # ------------------------------------------------ #
  28. # PART 2: Updating the notion investments database #
  29. # ------------------------------------------------ #
  30. # Fetches the list of entries in the investment-overview database stored in notion
  31. print("Fetching & formating notion investments...", end=" ", flush=True)
  32. investments = functions.fetch_format_notion_investments(config.notion_db_id_investments)
  33. # Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name
  34. print("Calculating history per ticker...", end=" ", flush=True)
  35. history_per_ticker = functions.calc_history_per_ticker(history_per_trade, tickers, trades)
  36. # Configuration dependent execution:
  37. if config.update_notion == True:
  38. # Selects the most current values from the history per ticker and overwrites them in the "investments" feteched from notion
  39. print("Calculating current value per ticker...", end=" ", flush=True)
  40. investments = functions.select_current_value_per_ticker(investments, history_per_ticker)
  41. # Updates the values in the notion database
  42. print("Updating the notion ticker database", end="", flush=True)
  43. functions.push_notion_investment_update(investments)
  44. # --------------------------------- #
  45. # PART 3: Updating the TRMNL Screen #
  46. # --------------------------------- #
  47. # Configuration dependent execution:
  48. if config.update_TRMNL == True:
  49. # Creates a list containing one date per week
  50. print("Creating a list with one entry per week...", end=" ", flush=True)
  51. list_filtered_dates = functions.create_list_filtered_dates(trades, config.trmnl_granularity)
  52. # Filter a weekly snapshot from the history per ticker
  53. print("Filtering the history per ticker to weekly values...", end=" ", flush=True)
  54. history_per_ticker_filtered = functions.filter_history_by_list(history_per_ticker, list_filtered_dates)
  55. # Prepare a new TRMNL update
  56. print("Constructing a TERMNL update object...", end=" ", flush=True)
  57. trmnl_update_object = functions.prep_trmnl_chart_udpate(
  58. history_per_ticker_filtered,
  59. series_to_show_1="total",
  60. data_to_show_1="current_value",
  61. series_to_show_2="DBPG.DE",
  62. data_to_show_2="current_value"
  63. )
  64. # Push the update to TRMNL
  65. print("Updating a TERMNL screen...", end=" ", flush=True)
  66. functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_1, config.trmnl_headers)
  67. # Prepare a new TRMNL update
  68. print("Constructing a TERMNL update object...", end=" ", flush=True)
  69. trmnl_update_object = functions.prep_trmnl_chart_udpate(
  70. history_per_ticker_filtered,
  71. series_to_show_1="total",
  72. data_to_show_1="current_irr",
  73. series_to_show_2="DBPG.DE",
  74. data_to_show_2="current_irr"
  75. )
  76. # Push the update to TRMNL
  77. print("Updating a TERMNL screen...", end=" ", flush=True)
  78. functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_2, config.trmnl_headers)
  79. # --------------------------- #
  80. # PART 4: Cool off and repeat #
  81. # --------------------------- #
  82. # Logging
  83. print("Completed cycle at: {}".format(functions.datetime.datetime.now()))
  84. print("Waiting a few minutes before the next execution")
  85. print("---------------------------------------------------------------------------")
  86. # Clear variables
  87. trades = {}
  88. yf_data = {}
  89. history_per_trade = {}
  90. tickers = []
  91. # Wait for api-cooldown
  92. functions.time.sleep(config.programm_cooldown_time * 60)