Skip to content

Commit

Permalink
Merge pull request #407 from VanekPetr/patch/data-loading-outside-models
Browse files Browse the repository at this point in the history
patch: data path as an input into TradeBot class
  • Loading branch information
VanekPetr authored Jan 26, 2025
2 parents 3f0d5cc + 48f9929 commit 8d1f592
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
6 changes: 5 additions & 1 deletion funnel/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import dash
import dash_bootstrap_components as dbc
import numpy as np
Expand All @@ -10,7 +13,8 @@
from .models.main import TradeBot

logger.setup_logging()
algo = TradeBot()
ROOT_DIR = Path(__file__).parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))


def load_page():
Expand Down
7 changes: 6 additions & 1 deletion funnel/dashboard/app_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import flask
from dash import dcc
from dash.dependencies import Input, Output, State
Expand All @@ -11,7 +14,9 @@
page_mobile_layout,
)

algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))



def get_callbacks(app):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import dash_bootstrap_components as dbc
from dash import dash_table, dcc, html

Expand All @@ -13,7 +16,8 @@
SUB_TITLE,
)

algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))


optionML = html.Div(
Expand Down
6 changes: 5 additions & 1 deletion funnel/dashboard/components_and_styles/backtest_page.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import cvxpy
import dash_bootstrap_components as dbc
from dash import dcc, html
Expand All @@ -14,7 +17,8 @@
SUB_TITLE,
)

algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))


optionBacktest = html.Div(
Expand Down
6 changes: 5 additions & 1 deletion funnel/dashboard/components_and_styles/lifecycle_page.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import dash_bootstrap_components as dbc
from dash import dcc, html

Expand All @@ -13,7 +16,8 @@
SUB_TITLE,
)

algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))


options_lifecycle = html.Div(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

import dash_bootstrap_components as dbc
from dash import dcc, html

Expand All @@ -12,7 +15,8 @@
SUB_TITLE,
)

algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))


optionGraph = html.Div(
Expand Down
33 changes: 17 additions & 16 deletions funnel/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

pio.renderers.default = "browser"

# that's unfortunate but will be addressed later
ROOT_DIR = Path(__file__).parent.parent
# Load our data
weekly_returns = pd.read_parquet(
os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip")
)
tickers = [pair[0] for pair in weekly_returns.columns.values]
names = [pair[1] for pair in weekly_returns.columns.values]
# # that's unfortunate but will be addressed later
# ROOT_DIR = Path(__file__).parent.parent
# # Load our data
# weekly_returns = pd.read_parquet(
# os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip")
# )
# tickers = [pair[0] for pair in weekly_returns.columns.values]
# names = [pair[1] for pair in weekly_returns.columns.values]


class TradeBot:
Expand All @@ -45,14 +45,14 @@ class TradeBot:
optimization suggesting optimal portfolio of assets.
"""

def __init__(self):
self.tickers = tickers
self.names = names
self.weeklyReturns = weekly_returns
self.min_date = str(weekly_returns.index[0])
self.max_date = str(weekly_returns.index[-1])
def __init__(self, data_path: str) -> None:
self.weeklyReturns = pd.read_parquet(data_path)
self.tickers = [pair[0] for pair in self.weeklyReturns.columns.values]
self.names = [pair[1] for pair in self.weeklyReturns.columns.values]
self.min_date = str(self.weeklyReturns.index[0])
self.max_date = str(self.weeklyReturns.index[-1])

weekly_returns.columns = tickers
self.weeklyReturns.columns = self.tickers

@staticmethod
def __plot_backtest(
Expand Down Expand Up @@ -881,7 +881,8 @@ def lifecycle_scenario_analysis(

if __name__ == "__main__":
# INITIALIZATION OF THE CLASS
algo = TradeBot()
ROOT_DIR = Path(__file__).parent.parent
algo = TradeBot(os.path.join(ROOT_DIR, "financial_data/all_etfs_rets.parquet.gzip"))

# Get top performing assets for given periods and measure
top_assets = algo.get_top_performing_assets(
Expand Down

0 comments on commit 8d1f592

Please sign in to comment.