Skip to content

Commit

Permalink
(feat) general precommit run
Browse files Browse the repository at this point in the history
  • Loading branch information
cardosofede committed Jul 16, 2024
1 parent 759c286 commit 9967322
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 338 deletions.
7 changes: 4 additions & 3 deletions frontend/components/backtesting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import streamlit as st
from datetime import datetime, timedelta

import streamlit as st


def backtesting_section(inputs, backend_api_client):
st.write("### Backtesting")
Expand All @@ -13,7 +14,8 @@ def backtesting_section(inputs, backend_api_client):
end_date = st.date_input("End Date", default_end_time,
help="End date is inclusive, make sure that you are not including the current date.")
with c3:
backtesting_resolution = st.selectbox("Backtesting Resolution", options=["1m", "3m", "5m", "15m", "30m", "1h", "1s"], index=0)
backtesting_resolution = st.selectbox("Backtesting Resolution",
options=["1m", "3m", "5m", "15m", "30m", "1h", "1s"], index=0)
with c4:
trade_cost = st.number_input("Trade Cost (%)", min_value=0.0, value=0.06, step=0.01, format="%.2f")
with c5:
Expand All @@ -33,7 +35,6 @@ def backtesting_section(inputs, backend_api_client):
except Exception as e:
st.error(e)
return None

if len(backtesting_results["processed_data"]) == 0:
st.error("No trades were executed during the backtesting period.")
return None
Expand Down
8 changes: 6 additions & 2 deletions frontend/components/bots_file_explorer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from streamlit_elements import mui

import constants
from backend.utils.os_utils import get_directories_from_directory, get_python_files_from_directory, \
get_yml_files_from_directory, get_log_files_from_directory
from backend.utils.os_utils import (
get_directories_from_directory,
get_log_files_from_directory,
get_python_files_from_directory,
get_yml_files_from_directory,
)
from frontend.components.file_explorer_base import FileExplorerBase


Expand Down
1 change: 1 addition & 0 deletions frontend/components/datagrid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from streamlit_elements import mui

from .dashboard import Dashboard


Expand Down
70 changes: 43 additions & 27 deletions frontend/pages/backtesting/analyze/analyze.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from hummingbot.core.data_type.common import PositionMode, TradeType, OrderType
import json
import os
from decimal import Decimal

import streamlit as st
from hummingbot.core.data_type.common import OrderType, PositionMode, TradeType
from hummingbot.data_feed.candles_feed.candles_factory import CandlesConfig
from hummingbot.strategy_v2.strategy_frameworks.data_types import OrderLevel, TripleBarrierConf
from hummingbot.strategy_v2.strategy_frameworks.directional_trading import DirectionalTradingBacktestingEngine
from hummingbot.strategy_v2.utils.config_encoder_decoder import ConfigEncoderDecoder

import constants
import os
import json
import streamlit as st
from decimal import Decimal

from backend.utils.optuna_database_manager import OptunaDBManager
from backend.utils.os_utils import load_controllers
from frontend.st_utils import initialize_st_page
from frontend.visualization.graphs import BacktestingGraphs
from frontend.visualization.strategy_analysis import StrategyAnalysis
from frontend.st_utils import initialize_st_page

initialize_st_page(title="Analyze", icon="🔬")

Expand Down Expand Up @@ -56,18 +56,26 @@ def initialize_session_state_vars():
filters_column, scatter_column = st.columns([1, 6])
with filters_column:
accuracy = st.slider("Accuracy", min_value=0.0, max_value=1.0, value=[0.4, 1.0], step=0.01)
net_profit = st.slider("Net PNL (%)", min_value=merged_df["net_pnl_pct"].min(), max_value=merged_df["net_pnl_pct"].max(),
net_profit = st.slider("Net PNL (%)", min_value=merged_df["net_pnl_pct"].min(),
max_value=merged_df["net_pnl_pct"].max(),
value=[merged_df["net_pnl_pct"].min(), merged_df["net_pnl_pct"].max()], step=0.01)
max_drawdown = st.slider("Max Drawdown (%)", min_value=merged_df["max_drawdown_pct"].min(), max_value=merged_df["max_drawdown_pct"].max(),
value=[merged_df["max_drawdown_pct"].min(), merged_df["max_drawdown_pct"].max()], step=0.01)
total_positions = st.slider("Total Positions", min_value=merged_df["total_positions"].min(), max_value=merged_df["total_positions"].max(),
value=[merged_df["total_positions"].min(), merged_df["total_positions"].max()], step=1)
max_drawdown = st.slider("Max Drawdown (%)", min_value=merged_df["max_drawdown_pct"].min(),
max_value=merged_df["max_drawdown_pct"].max(),
value=[merged_df["max_drawdown_pct"].min(), merged_df["max_drawdown_pct"].max()],
step=0.01)
total_positions = st.slider("Total Positions", min_value=merged_df["total_positions"].min(),
max_value=merged_df["total_positions"].max(),
value=[merged_df["total_positions"].min(), merged_df["total_positions"].max()],
step=1)
net_profit_filter = (merged_df["net_pnl_pct"] >= net_profit[0]) & (merged_df["net_pnl_pct"] <= net_profit[1])
accuracy_filter = (merged_df["accuracy"] >= accuracy[0]) & (merged_df["accuracy"] <= accuracy[1])
max_drawdown_filter = (merged_df["max_drawdown_pct"] >= max_drawdown[0]) & (merged_df["max_drawdown_pct"] <= max_drawdown[1])
total_positions_filter = (merged_df["total_positions"] >= total_positions[0]) & (merged_df["total_positions"] <= total_positions[1])
max_drawdown_filter = (merged_df["max_drawdown_pct"] >= max_drawdown[0]) & (
merged_df["max_drawdown_pct"] <= max_drawdown[1])
total_positions_filter = (merged_df["total_positions"] >= total_positions[0]) & (
merged_df["total_positions"] <= total_positions[1])
with scatter_column:
bt_graphs = BacktestingGraphs(merged_df[net_profit_filter & accuracy_filter & max_drawdown_filter & total_positions_filter])
bt_graphs = BacktestingGraphs(
merged_df[net_profit_filter & accuracy_filter & max_drawdown_filter & total_positions_filter])
# Show and compare all of the study trials
st.plotly_chart(bt_graphs.pnl_vs_maxdrawdown(), use_container_width=True)
# Get study trials
Expand Down Expand Up @@ -107,11 +115,11 @@ def initialize_session_state_vars():
# TODO: Add support for boolean fields in optimize tab
field_value = st.checkbox(field_name, value=field_value)
else:
raise ValueError(f"Field type {field_type} not supported")
raise ValueError("Field type {field_type} not supported")
else:
if field_name == "candles_config":
st.write("---")
st.write(f"## Candles Config:")
st.write("## Candles Config:")
candles = []
for i, candles_config in enumerate(field_value):
st.write(f"#### Candle {i}:")
Expand All @@ -130,7 +138,7 @@ def initialize_session_state_vars():
field_value = candles
elif field_name == "order_levels":
new_levels = []
st.write(f"## Order Levels:")
st.write("## Order Levels:")
for order_level in field_value:
st.write(f"### Level {order_level['level']} {order_level['side'].name}")
ol_c1, ol_c2 = st.columns([5, 1])
Expand All @@ -139,30 +147,38 @@ def initialize_session_state_vars():
c21, c22, c23, c24, c25 = st.columns(5)
triple_barrier_conf_level = order_level["triple_barrier_conf"]
with c21:
take_profit = st.number_input("Take profit", value=float(triple_barrier_conf_level["take_profit"]),
take_profit = st.number_input("Take profit",
value=float(triple_barrier_conf_level["take_profit"]),
key=f"{order_level['level']}_{order_level['side'].name}_tp")
with c22:
stop_loss = st.number_input("Stop Loss", value=float(triple_barrier_conf_level["stop_loss"]),
stop_loss = st.number_input("Stop Loss",
value=float(triple_barrier_conf_level["stop_loss"]),
key=f"{order_level['level']}_{order_level['side'].name}_sl")
with c23:
time_limit = st.number_input("Time Limit", value=triple_barrier_conf_level["time_limit"],
key=f"{order_level['level']}_{order_level['side'].name}_tl")
with c24:
ts_ap = st.number_input("Trailing Stop Activation Price", value=float(triple_barrier_conf_level["trailing_stop_activation_price_delta"]),
key=f"{order_level['level']}_{order_level['side'].name}_tsap", format="%.4f")
ts_ap = st.number_input("Trailing Stop Activation Price", value=float(
triple_barrier_conf_level["trailing_stop_activation_price_delta"]),
key=f"{order_level['level']}_{order_level['side'].name}_tsap",
format="%.4f")
with c25:
ts_td = st.number_input("Trailing Stop Trailing Delta", value=float(triple_barrier_conf_level["trailing_stop_trailing_delta"]),
key=f"{order_level['level']}_{order_level['side'].name}_tstd", format="%.4f")
ts_td = st.number_input("Trailing Stop Trailing Delta", value=float(
triple_barrier_conf_level["trailing_stop_trailing_delta"]),
key=f"{order_level['level']}_{order_level['side'].name}_tstd",
format="%.4f")
with ol_c2:
st.write("#### Position config:")
c31, c32 = st.columns(2)
with c31:
order_amount = st.number_input("Order amount USD", value=float(order_level["order_amount_usd"]),
order_amount = st.number_input("Order amount USD",
value=float(order_level["order_amount_usd"]),
key=f"{order_level['level']}_{order_level['side'].name}_oa")
with c32:
cooldown_time = st.number_input("Cooldown time", value=order_level["cooldown_time"],
key=f"{order_level['level']}_{order_level['side'].name}_cd")
triple_barrier_conf = TripleBarrierConf(stop_loss=Decimal(stop_loss), take_profit=Decimal(take_profit),
triple_barrier_conf = TripleBarrierConf(stop_loss=Decimal(stop_loss),
take_profit=Decimal(take_profit),
time_limit=time_limit,
trailing_stop_activation_price_delta=Decimal(ts_ap),
trailing_stop_trailing_delta=Decimal(ts_td),
Expand Down Expand Up @@ -225,4 +241,4 @@ def initialize_session_state_vars():
add_volume=add_volume)

except FileNotFoundError:
st.warning(f"The requested candles could not be found.")
st.warning("The requested candles could not be found.")
2 changes: 1 addition & 1 deletion frontend/pages/backtesting/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import streamlit as st
from streamlit_elements import elements, mui

from frontend.components.dashboard import Dashboard
from frontend.components.controllers_file_explorer import ControllersFileExplorer
from frontend.components.dashboard import Dashboard
from frontend.components.directional_strategy_creation_card import DirectionalStrategyCreationCard
from frontend.components.editor import Editor
from frontend.st_utils import initialize_st_page
Expand Down
5 changes: 3 additions & 2 deletions frontend/pages/backtesting/optimize/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import streamlit as st
from streamlit_elements import elements, mui

from backend.utils import os_utils
from frontend.components.dashboard import Dashboard
from frontend.components.editor import Editor
from frontend.components.optimization_creation_card import OptimizationCreationCard
from frontend.components.optimization_run_card import OptimizationRunCard
from frontend.components.optimizations_file_explorer import OptimizationsStrategiesFileExplorer
from backend.utils import os_utils
from frontend.st_utils import initialize_st_page

initialize_st_page(title="Optimize", icon="🧪")


def run_optuna_dashboard():
os_utils.execute_bash_command(f"optuna-dashboard sqlite:///data/backtesting/backtesting_report.db")
os_utils.execute_bash_command("optuna-dashboard sqlite:///data/backtesting/backtesting_report.db")
time.sleep(5)
webbrowser.open("http://127.0.0.1:8080/dashboard", new=2)

Expand Down
9 changes: 6 additions & 3 deletions frontend/pages/data/download_candles/app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import streamlit as st
from datetime import datetime, time

import pandas as pd
import plotly.graph_objects as go
import streamlit as st

from frontend.st_utils import initialize_st_page, get_backend_api_client
from frontend.st_utils import get_backend_api_client, initialize_st_page

# Initialize Streamlit page
initialize_st_page(title="Download Candles", icon="💾")
backend_api_client = get_backend_api_client()

c1, c2, c3, c4 = st.columns([2, 2, 2, 0.5])
with c1:
connector = st.selectbox("Exchange", ["binance_perpetual", "binance", "gate_io", "gate_io_perpetual", "kucoin", "ascend_ex"], index=0)
connector = st.selectbox("Exchange",
["binance_perpetual", "binance", "gate_io", "gate_io_perpetual", "kucoin", "ascend_ex"],
index=0)
trading_pair = st.text_input("Trading Pair", value="BTC-USDT")
with c2:
interval = st.selectbox("Interval", options=["1m", "3m", "5m", "15m", "1h", "4h", "1d", "1s"])
Expand Down
11 changes: 9 additions & 2 deletions frontend/pages/data/token_spreads/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import streamlit as st
import plotly.express as px
import streamlit as st

import CONFIG
from backend.services.coingecko_client import CoinGeckoClient
from backend.services.miner_client import MinerClient
Expand All @@ -11,22 +12,27 @@
cg_utils = CoinGeckoClient()
miner_utils = MinerClient()


@st.cache_data
def get_all_coins_df():
return cg_utils.get_all_coins_df()


@st.cache_data
def get_all_exchanges_df():
return cg_utils.get_all_exchanges_df()


@st.cache_data
def get_miner_stats_df():
return miner_utils.get_miner_stats_df()


@st.cache_data
def get_coin_tickers_by_id_list(coins_id: list):
return cg_utils.get_coin_tickers_by_id_list(coins_id)


with st.spinner(text='In progress'):
exchanges_df = get_all_exchanges_df()
coins_df = get_all_coins_df()
Expand All @@ -43,7 +49,8 @@ def get_coin_tickers_by_id_list(coins_id: list):
coins_id = coins_df.loc[coins_df["name"].isin(tokens), "id"].tolist()

coin_tickers_df = get_coin_tickers_by_id_list(coins_id)
coin_tickers_df["coin_name"] = coin_tickers_df.apply(lambda x: coins_df.loc[coins_df["id"] == x.token_id, "name"].item(), axis=1)
coin_tickers_df["coin_name"] = coin_tickers_df.apply(
lambda x: coins_df.loc[coins_df["id"] == x.token_id, "name"].item(), axis=1)

exchanges = st.multiselect(
"Select the exchanges to analyze:",
Expand Down
18 changes: 12 additions & 6 deletions frontend/pages/data/tvl_vs_mcap/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import streamlit as st
import pandas as pd
import plotly.express as px
import streamlit as st
from defillama import DefiLlama

from frontend.st_utils import initialize_st_page
Expand All @@ -12,16 +12,21 @@
MIN_TVL = 1000000.
MIN_MCAP = 1000000.


@st.cache_data
def get_tvl_mcap_data():
llama = DefiLlama()
df = pd.DataFrame(llama.get_all_protocols())
tvl_mcap_df = df.loc[(df["tvl"]>0) & (df["mcap"]>0), ["name", "tvl", "mcap", "chain", "category", "slug"]].sort_values(by=["mcap"], ascending=False)
return tvl_mcap_df[(tvl_mcap_df["tvl"] > MIN_TVL) & (tvl_mcap_df["mcap"]> MIN_MCAP)]
tvl_mcap_df = df.loc[
(df["tvl"] > 0) & (df["mcap"] > 0), ["name", "tvl", "mcap", "chain", "category", "slug"]].sort_values(
by=["mcap"], ascending=False)
return tvl_mcap_df[(tvl_mcap_df["tvl"] > MIN_TVL) & (tvl_mcap_df["mcap"] > MIN_MCAP)]


def get_protocols_by_chain_category(protocols: pd.DataFrame, group_by: list, nth: list):
return protocols.sort_values('tvl', ascending=False).groupby(group_by).nth(nth).reset_index()


with st.spinner(text='In progress'):
tvl_mcap_df = get_tvl_mcap_data()

Expand Down Expand Up @@ -57,14 +62,15 @@ def get_protocols_by_chain_category(protocols: pd.DataFrame, group_by: list, nth
groupby = st.selectbox('Group by:', [['chain', 'category'], ['category', 'chain']])
nth = st.slider('Top protocols by Category', min_value=1, max_value=5)

proto_agg = get_protocols_by_chain_category(tvl_mcap_df[tvl_mcap_df["chain"].isin(chains)], groupby, np.arange(0, nth, 1).tolist())
proto_agg = get_protocols_by_chain_category(tvl_mcap_df[tvl_mcap_df["chain"].isin(chains)],
groupby, np.arange(0, nth, 1).tolist())
groupby.append("slug")
sunburst = px.sunburst(
proto_agg,
path=groupby,
values='tvl',
height=800,
title="SunBurst",
template="plotly_dark",)
template="plotly_dark", )

st.plotly_chart(sunburst, use_container_width=True)
st.plotly_chart(sunburst, use_container_width=True)
Loading

0 comments on commit 9967322

Please sign in to comment.