
import functions
import config
import imp

while True:

	# ------------------------------------------- #
	# PART 1: Updating the notion trades database #
	# ------------------------------------------- #
	# Fetches the list of all trades stored in notion
	print("Fetching Data from Notion...", end=" ", flush=True)
	trades = functions.fetch_format_notion_trades(config.notion_db_id_trades)

	# Generates a list with unique tickers and no duplicates to reduce workload for the yfinance api
	print("Creating a list of unique tickers...", end=" ", flush=True)
	tickers = functions.filter_list_of_tickers(trades)

	# Configuration dependent execution:
	if config.calculate_benchmark == True:
		
		# Add the benchmark-ticker to the list of tickers to download data from yfinance from
		print("Adding benchmark-ticker...", end="", flush=True)
		tickers = functions.add_benchmark_ticker(tickers, config.ticker_benchmark)

	# Fetches & formats the complete history per ticker from yfinance
	print("Fetching & formating yfinance data", end="", flush=True)
	yf_data = functions.fetch_format_yf_data(tickers)

	# Calculates & stores a history per trade
	print("Calculating the history per trade...", end=" ", flush=True)
	history_per_trade = functions.calc_history_per_trade(trades, yf_data)

	# Configuration dependent execution:
	if config.update_notion == True:

		# Selects the most current values from the history per trade and overwrites them in the "trades" feteched from notion
		print("Selecting the most current values...", end=" ", flush=True)
		trades = functions.select_current_value_per_trade(trades, history_per_trade)

		# Updates the values in the notion database
		print("Updating the notion trades database", end="", flush=True)
		functions.push_notion_trades_update(trades)



	# ------------------------------------------------ #
	# PART 2: Updating the notion investments database #
	# ------------------------------------------------ #
	# Fetches the list of entries in the investment-overview database stored in notion
	print("Fetching & formating notion investments...", end=" ", flush=True)
	investments = functions.fetch_format_notion_investments(config.notion_db_id_investments)

	# Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name
	print("Calculating history per ticker...", end=" ", flush=True)
	history_per_ticker = functions.calc_history_per_ticker(history_per_trade, tickers, trades)

	# Configuration dependent execution:
	if config.update_notion == True:
		
		# Selects the most current values from the history per ticker and overwrites them in the "investments" feteched from notion
		print("Calculating current value per ticker...", end=" ", flush=True)
		investments = functions.select_current_value_per_ticker(investments, history_per_ticker)

		# Updates the values in the notion database
		print("Updating the notion ticker database", end="", flush=True)
		functions.push_notion_investment_update(investments)


	# ----------------------------------------- #
	# PART 3: Calculating Benchmark performance #
	# ----------------------------------------- #
	# Configuration dependent execution:
	if config.calculate_benchmark == True:

		# Creating benchmark trades
		print("Creating 'benchmark trades'...", end="", flush=True)
		benchmark_trades = functions.create_benchmark_trades(trades, yf_data)

		# Calculating benchmark trades
		print("Calculating the history per benchmark-trade...", end=" ", flush=True)
		history_per_benchmark_trade = functions.calc_history_per_trade(benchmark_trades, yf_data)

		# Calculates & stores a history for the benchmark
		print("Calculating benchmark-history overall...", end=" ", flush=True)
		history_benchmark = functions.calc_history_per_ticker(history_per_benchmark_trade, tickers, benchmark_trades)

		# Merging the benchmark_history into the ticker_history
		print("Merging the benchmark-history into the ticker-history...", end=" ", flush=True)
		history_per_ticker = functions.merge_histories(history_per_ticker, history_benchmark)



	# --------------------------------- #
	# PART 4: Updating the TRMNL Screen #
	# --------------------------------- #
	# Configuration dependent execution:
	if config.update_TRMNL == True:

		# Creates a list containing one date per week
		print("Creating a list with one entry per week...", end=" ", flush=True)
		list_filtered_dates = functions.create_list_filtered_dates(trades, config.trmnl_granularity)

		# Filter a weekly snapshot from the history per ticker
		print("Filtering the history per ticker to weekly values...", end=" ", flush=True)
		history_per_ticker_filtered = functions.filter_history_by_list(history_per_ticker, list_filtered_dates)

		# Prepare a new TRMNL update
		print("Constructing a TERMNL update object...", end=" ", flush=True)
		trmnl_update_object = functions.prep_trmnl_chart_udpate(
			history_per_ticker_filtered,
			series_to_show_1="total",
			data_to_show_1="current_value",
			series_to_show_2="benchmark",
			data_to_show_2="current_value"
		)

		# Push the update to TRMNL
		print("Updating a TERMNL screen...", end=" ", flush=True)
		functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_1, config.trmnl_headers)

		# Prepare a new TRMNL update
		print("Constructing a TERMNL update object...", end=" ", flush=True)
		trmnl_update_object = functions.prep_trmnl_chart_udpate(
			history_per_ticker_filtered,
			series_to_show_1="total",
			data_to_show_1="current_irr",
			series_to_show_2="benchmark",
			data_to_show_2="current_irr"
		)

		# Push the update to TRMNL
		print("Updating a TERMNL screen...", end=" ", flush=True)
		functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_2, config.trmnl_headers)

		# Prepare a new TRMNL update
		print("Constructing a TERMNL update object...", end=" ", flush=True)
		trmnl_update_object = functions.prep_trmnl_chart_udpate(
			history_per_ticker_filtered,
			series_to_show_1="total",
			data_to_show_1="total_performanance",
			series_to_show_2="benchmark",
			data_to_show_2="total_performanance"
		)

		# Push the update to TRMNL
		print("Updating a TERMNL screen...", end=" ", flush=True)
		functions.push_trmnl_update_chart(trmnl_update_object, config.trmnl_url_chart_3, config.trmnl_headers)



	# --------------------------- #
	# PART 5: Cool off and repeat #
	# --------------------------- #
	# Clear variables
	trades = {}
	yf_data = {}
	history_per_trade = {}
	tickers = []

	# Reload config
	imp.reload(config)

	# Logging
	print("Completed cycle at: {}".format(functions.datetime.datetime.now()))
	print("Waiting a few minutes before the next execution")
	print("---------------------------------------------------------------------------")

	# Wait for api-cooldown
	functions.time.sleep(config.programm_cooldown_time * 60)