(feat) refactor get data page

This commit is contained in:
cardosofede
2024-04-30 19:59:17 -03:00
parent c371a58ae9
commit 37a608f4e7
2 changed files with 70 additions and 70 deletions

View File

@@ -1,70 +0,0 @@
import time
from subprocess import CalledProcessError
import streamlit as st
import constants
from utils import os_utils
from docker_manager import DockerManager
from utils.st_utils import initialize_st_page
initialize_st_page(title="Get Data", icon="💾", initial_sidebar_state="collapsed")
# Start content here
docker_manager = DockerManager()
c1, c2, c3 = st.columns([2, 2, 0.5])
with c1:
exchange = st.selectbox("Exchange", ["binance_perpetual", "binance"], index=0)
trading_pairs = st.text_input("Trading Pairs (separated with commas)", value="BTC-USDT,ETH-USDT")
with c2:
intervals = st.multiselect("Intervals", options=["1s", "1m", "3m", "5m", "15m", "1h", "4h", "1d"], default=["1m", "3m", "1h"])
days_to_download = st.number_input("Days to Download", value=30, min_value=1, max_value=365, step=1)
with c3:
get_data_button = st.button("Download Candles!")
clean_container_folder_button = st.button("Clean Candles Folder")
if clean_container_folder_button:
st.warning("Cleaning Candles Data folder...", icon="⚠️")
st.write("---")
os_utils.remove_files_from_directory(constants.CANDLES_DATA_PATH)
st.write("### Container folder cleaned.")
st.write("---")
if get_data_button:
candles_container_config = {
"EXCHANGE": exchange,
"TRADING_PAIRS": trading_pairs,
"INTERVALS": ",".join(intervals),
"DAYS_TO_DOWNLOAD": days_to_download,
}
time.sleep(0.5)
docker_manager.create_download_candles_container(candles_config=candles_container_config,
yml_path=constants.DOWNLOAD_CANDLES_CONFIG_YML)
st.info("Downloading candles with a Docker container in the background. "
"When this process is ready you will see the candles inside data/candles", icon="🕓")
st.write("---")
st.write("## ⚙Containers Management")
try:
active_containers = docker_manager.get_active_containers()
c1, c2 = st.columns([0.9, 0.1])
with c1:
if "backtest_get_data" in active_containers:
st.success("Hummingbot Candles Downloader is running")
st.write("Exited Containers:")
st.warning(docker_manager.get_exited_containers())
with c2:
if "backtest_get_data" in active_containers:
stop_containers_button = st.button("Stop Candles Downloader")
if stop_containers_button:
docker_manager.stop_container("backtest_get_data")
clean_exited_containers_button = st.button("Clean Containers")
if clean_exited_containers_button:
docker_manager.clean_exited_containers()
except CalledProcessError as error:
st.write("### Docker is not running. Please start docker in your machine.")

View File

@@ -0,0 +1,70 @@
import streamlit as st
from datetime import datetime, time
import pandas as pd
import plotly.graph_objects as go
from utils.backend_api_client import BackendAPIClient
from utils.st_utils import initialize_st_page
# Initialize Streamlit page
initialize_st_page(title="Download Candles", icon="💾", initial_sidebar_state="collapsed")
backend_api_client = BackendAPIClient.get_instance()
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)
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"])
with c3:
start_date = st.date_input("Start Date", value=datetime(2023, 1, 1))
end_date = st.date_input("End Date", value=datetime(2023, 1, 2))
with c4:
get_data_button = st.button("Get Candles!")
if get_data_button:
start_datetime = datetime.combine(start_date, time.min)
end_datetime = datetime.combine(end_date, time.max)
candles = backend_api_client.get_historical_candles(
connector=connector,
trading_pair=trading_pair,
interval=interval,
start_time=int(start_datetime.timestamp()) * 1000,
end_time=int(end_datetime.timestamp()) * 1000
)
candles_df = pd.DataFrame(candles)
candles_df.index = pd.to_datetime(candles_df["timestamp"], unit='ms')
# Plotting the candlestick chart
fig = go.Figure(data=[go.Candlestick(
x=candles_df.index,
open=candles_df['open'],
high=candles_df['high'],
low=candles_df['low'],
close=candles_df['close'],
increasing_line_color='#2ECC71',
decreasing_line_color='#E74C3C'
)])
fig.update_layout(
height=1000,
title="Candlesticks",
xaxis_title="Time",
yaxis_title="Price",
template="plotly_dark",
showlegend=False
)
fig.update_xaxes(rangeslider_visible=False)
fig.update_yaxes(title_text="Price")
st.plotly_chart(fig, use_container_width=True)
# Generating CSV and download button
csv = candles_df.to_csv(index=False)
filename = f"{connector}_{trading_pair}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}.csv"
st.download_button(
label="Download Candles as CSV",
data=csv,
file_name=filename,
mime='text/csv',
)