(feat) adapt components to support defaults from session state

This commit is contained in:
cardosofede
2024-05-25 01:48:45 -05:00
parent 91d02f3f03
commit 0e2bfc03f2
8 changed files with 156 additions and 60 deletions

View File

@@ -0,0 +1,17 @@
import streamlit as st
from CONFIG import BACKEND_API_HOST, BACKEND_API_PORT
from backend.services.backend_api_client import BackendAPIClient
backend_api_client = BackendAPIClient.get_instance(host=BACKEND_API_HOST, port=BACKEND_API_PORT)
def get_default_config_loader(controller_name: str):
use_default_config = st.checkbox("Use default config", value=True)
all_configs = backend_api_client.get_all_controllers_config()
if use_default_config:
st.session_state["default_config"] = {}
else:
configs = [config for config in all_configs if config["controller_name"] == controller_name]
default_config = st.selectbox("Select a config", [config["id"] for config in configs])
st.session_state["default_config"] = next((config for config in all_configs if config["id"] == default_config), {})

View File

@@ -1,22 +1,38 @@
import streamlit as st
from hummingbot.connector.connector_base import OrderType
from frontend.components.st_inputs import get_distribution, normalize, distribution_inputs
def get_dca_distribution_inputs():
with st.expander("DCA Distribution", expanded=True):
st.header("DCA Settings")
buy_order_levels = st.number_input("Number of Order Levels", min_value=1, value=3,
with st.expander("DCA Builder", expanded=True):
default_config = st.session_state.get("default_config", {})
dca_spreads = default_config.get("dca_spreads", [0.01, 0.02, 0.03])
dca_amounts = default_config.get("dca_amounts", [0.2, 0.5, 0.3])
tp = default_config.get("take_profit", 0.01) * 100
sl = default_config.get("stop_loss", 0.02) * 100
time_limit = default_config.get("time_limit", 60 * 6 * 60) // 60
ts_ap = default_config.get("trailing_stop", {}).get("activation_price", 0.018) * 100
ts_delta = default_config.get("trailing_stop", {}).get("trailing_delta", 0.002) * 100
levels_def = len(dca_spreads)
c1, c2 = st.columns([0.67, 0.33])
with c1:
st.header("DCA Distribution")
buy_order_levels = st.number_input("Number of Order Levels", min_value=1, value=levels_def,
help="Enter the number of order levels (e.g., 2).")
if buy_order_levels > levels_def:
dca_spreads += [0.01 + max(dca_spreads)] * (buy_order_levels - levels_def)
dca_amounts += [0.2 + max(dca_amounts)] * (buy_order_levels - levels_def)
elif buy_order_levels < levels_def:
dca_spreads = dca_spreads[:buy_order_levels]
dca_amounts = dca_amounts[:buy_order_levels]
col_spreads, col_amounts = st.columns(2)
with col_spreads:
buy_spread_dist_type, buy_spread_start, buy_spread_base, buy_spread_scaling, buy_spread_step, buy_spread_ratio, buy_manual_spreads = distribution_inputs(
col_spreads, "Spread", buy_order_levels)
col_spreads, "Spread", buy_order_levels, dca_spreads)
with col_amounts:
buy_amount_dist_type, buy_amount_start, buy_amount_base, buy_amount_scaling, buy_amount_step, buy_amount_ratio, buy_manual_amounts = distribution_inputs(
col_amounts, "Amount", buy_order_levels)
col_amounts, "Amount", buy_order_levels, dca_amounts)
# Generate distributions
spread_distributions = get_distribution(buy_spread_dist_type, buy_order_levels, buy_spread_start,
@@ -31,26 +47,27 @@ def get_dca_distribution_inputs():
orders_amount_normalized = normalize(amount_distributions)
spreads_normalized = [spread - spread_distributions[0] for spread in spread_distributions]
st.write("---")
c1, c2, c3, c4, c5 = st.columns(5)
with c1:
sl = st.number_input("Stop Loss (%)", min_value=0.0, max_value=100.0, value=2.0, step=0.1,
help="Enter the stop loss as a percentage (e.g., 2.0 for 2%).") / 100
# c1, c2, c3, c4, c5 = st.columns(5)
with c2:
tp = st.number_input("Take Profit (%)", min_value=0.0, max_value=100.0, value=1.0, step=0.1,
st.header("Risk Management")
sl = st.number_input("Stop Loss (%)", min_value=0.0, max_value=100.0, value=sl, step=0.1,
help="Enter the stop loss as a percentage (e.g., 2.0 for 2%).") / 100
# with c2:
tp = st.number_input("Take Profit (%)", min_value=0.0, max_value=100.0, value=tp, step=0.1,
help="Enter the take profit as a percentage (e.g., 3.0 for 3%).") / 100
with c3:
time_limit = st.number_input("Time Limit (minutes)", min_value=0, value=60 * 6,
# with c3:
time_limit = st.number_input("Time Limit (minutes)", min_value=0, value=time_limit,
help="Enter the time limit in minutes (e.g., 360 for 6 hours).") * 60
with c4:
ts_ap = st.number_input("Trailing Stop Act. Price (%)", min_value=0.0, max_value=100.0, value=1.8,
# with c4:
ts_ap = st.number_input("Trailing Stop Act. Price (%)", min_value=0.0, max_value=100.0, value=ts_ap,
step=0.1,
help="Enter the tr ailing stop activation price as a percentage (e.g., 1.0 for 1%).") / 100
with c5:
ts_delta = st.number_input("Trailing Stop Delta (%)", min_value=0.0, max_value=100.0, value=0.2, step=0.1,
# with c5:
ts_delta = st.number_input("Trailing Stop Delta (%)", min_value=0.0, max_value=100.0, value=ts_delta, step=0.1,
help="Enter the trailing stop delta as a percentage (e.g., 0.3 for 0.3%).") / 100
return {
"dca_spreads": [spread /100 for spread in spreads_normalized],
"dca_spreads": [spread / 100 for spread in spreads_normalized],
"dca_amounts": orders_amount_normalized,
"stop_loss": sl,
"take_profit": tp,

View File

@@ -4,31 +4,45 @@ import streamlit as st
def get_directional_trading_general_inputs():
with st.expander("General Settings", expanded=True):
c1, c2, c3, c4, c5, c6, c7 = st.columns(7)
default_config = st.session_state.get("default_config", {})
connector_name = default_config.get("connector_name", "kucoin")
trading_pair = default_config.get("trading_pair", "WLD-USDT")
leverage = default_config.get("leverage", 20)
total_amount_quote = default_config.get("total_amount_quote", 1000)
max_executors_per_side = default_config.get("max_executors_per_side", 5)
cooldown_time = default_config.get("cooldown_time", 60 * 60) / 60
position_mode = 0 if default_config.get("position_mode", "HEDGE") == "HEDGE" else 1
candles_connector_name = default_config.get("candles_connector_name", "kucoin")
candles_trading_pair = default_config.get("candles_trading_pair", "WLD-USDT")
interval = default_config.get("interval", "3m")
intervals = ["1m", "3m", "5m", "15m", "1h", "4h", "1d", "1s"]
interval_index = intervals.index(interval)
with c1:
connector_name = st.text_input("Connector", value="binance",
connector_name = st.text_input("Connector", value=connector_name,
help="Enter the name of the exchange to trade on (e.g., binance_perpetual).")
candles_connector_name = st.text_input("Candles Connector", value="binance",
candles_connector_name = st.text_input("Candles Connector", value=candles_connector_name,
help="Enter the name of the exchange to get candles from (e.g., binance_perpetual).")
with c2:
trading_pair = st.text_input("Trading Pair", value="WLD-USDT",
trading_pair = st.text_input("Trading Pair", value=trading_pair,
help="Enter the trading pair to trade on (e.g., WLD-USDT).")
candles_trading_pair = st.text_input("Candles Trading Pair", value="WLD-USDT",
candles_trading_pair = st.text_input("Candles Trading Pair", value=candles_trading_pair,
help="Enter the trading pair to get candles for (e.g., WLD-USDT).")
with c3:
leverage = st.number_input("Leverage", value=20,
leverage = st.number_input("Leverage", value=leverage,
help="Set the leverage to use for trading (e.g., 20 for 20x leverage). Set it to 1 for spot trading.")
interval = st.selectbox("Candles Interval", ("1m", "3m", "5m", "15m", "1h", "4h", "1d"), index=1,
interval = st.selectbox("Candles Interval", ("1m", "3m", "5m", "15m", "1h", "4h", "1d"), index=interval_index,
help="Enter the interval for candles (e.g., 1m).")
with c4:
total_amount_quote = st.number_input("Total amount of quote", value=1000,
total_amount_quote = st.number_input("Total amount of quote", value=total_amount_quote,
help="Enter the total amount in quote asset to use for trading (e.g., 1000).")
with c5:
max_executors_per_side = st.number_input("Max Executors Per Side", value=5,
max_executors_per_side = st.number_input("Max Executors Per Side", value=max_executors_per_side,
help="Enter the maximum number of executors per side (e.g., 5).")
with c6:
cooldown_time = st.number_input("Cooldown Time (minutes)", value=10,
cooldown_time = st.number_input("Cooldown Time (minutes)", value=cooldown_time,
help="Time between accepting a new signal in minutes (e.g., 60).") * 60
with c7:
position_mode = st.selectbox("Position Mode", ("HEDGE", "ONEWAY"), index=0,
position_mode = st.selectbox("Position Mode", ("HEDGE", "ONEWAY"), index=position_mode,
help="Enter the position mode (HEDGE/ONEWAY).")
return connector_name, trading_pair, leverage, total_amount_quote, max_executors_per_side, cooldown_time, position_mode, candles_connector_name, candles_trading_pair, interval

View File

@@ -2,30 +2,49 @@ import streamlit as st
from frontend.components.st_inputs import get_distribution, normalize, distribution_inputs
def get_executors_distribution_inputs():
def get_executors_distribution_inputs(default_spreads=[0.01, 0.02], default_amounts=[0.2, 0.8]):
default_config = st.session_state.get("default_config", {})
buy_spreads = default_config.get("buy_spreads", default_spreads)
sell_spreads = default_config.get("sell_spreads", default_spreads)
buy_amounts_pct = default_config.get("buy_amounts_pct", default_amounts)
sell_amounts_pct = default_config.get("sell_amounts_pct", default_amounts)
buy_order_levels_def = len(buy_spreads)
sell_order_levels_def = len(sell_spreads)
with st.expander("Executors Configuration", expanded=True):
col_buy, col_sell = st.columns(2)
with col_buy:
st.header("Buy Order Settings")
buy_order_levels = st.number_input("Number of Buy Order Levels", min_value=1, value=2,
buy_order_levels = st.number_input("Number of Buy Order Levels", min_value=1, value=buy_order_levels_def,
help="Enter the number of buy order levels (e.g., 2).")
with col_sell:
st.header("Sell Order Settings")
sell_order_levels = st.number_input("Number of Sell Order Levels", min_value=1, value=2,
sell_order_levels = st.number_input("Number of Sell Order Levels", min_value=1, value=sell_order_levels_def,
help="Enter the number of sell order levels (e.g., 2).")
if buy_order_levels > buy_order_levels_def:
buy_spreads += [0.01 + max(buy_spreads)] * (buy_order_levels - buy_order_levels_def)
buy_amounts_pct += [0.2 + max(buy_amounts_pct)] * (buy_order_levels - buy_order_levels_def)
elif buy_order_levels < buy_order_levels_def:
buy_spreads = buy_spreads[:buy_order_levels]
buy_amounts_pct = buy_amounts_pct[:buy_order_levels]
if sell_order_levels > sell_order_levels_def:
sell_spreads += [0.01 + max(sell_spreads)] * (sell_order_levels - sell_order_levels_def)
sell_amounts_pct += [0.2 + max(sell_amounts_pct)] * (sell_order_levels - sell_order_levels_def)
elif sell_order_levels < sell_order_levels_def:
sell_spreads = sell_spreads[:sell_order_levels]
sell_amounts_pct = sell_amounts_pct[:sell_order_levels]
col_buy_spreads, col_buy_amounts, col_sell_spreads, col_sell_amounts = st.columns(4)
with col_buy_spreads:
buy_spread_dist_type, buy_spread_start, buy_spread_base, buy_spread_scaling, buy_spread_step, buy_spread_ratio, buy_manual_spreads = distribution_inputs(
col_buy_spreads, "Spread", buy_order_levels)
col_buy_spreads, "Spread", buy_order_levels, buy_spreads)
with col_buy_amounts:
buy_amount_dist_type, buy_amount_start, buy_amount_base, buy_amount_scaling, buy_amount_step, buy_amount_ratio, buy_manual_amounts = distribution_inputs(
col_buy_amounts, "Amount", buy_order_levels)
col_buy_amounts, "Amount", buy_order_levels, buy_amounts_pct)
with col_sell_spreads:
sell_spread_dist_type, sell_spread_start, sell_spread_base, sell_spread_scaling, sell_spread_step, sell_spread_ratio, sell_manual_spreads = distribution_inputs(
col_sell_spreads, "Spread", sell_order_levels)
col_sell_spreads, "Spread", sell_order_levels, sell_spreads)
with col_sell_amounts:
sell_amount_dist_type, sell_amount_start, sell_amount_base, sell_amount_scaling, sell_amount_step, sell_amount_ratio, sell_manual_amounts = distribution_inputs(
col_sell_amounts, "Amount", sell_order_levels)
col_sell_amounts, "Amount", sell_order_levels, sell_amounts_pct)
# Generate distributions
buy_spread_distributions = get_distribution(buy_spread_dist_type, buy_order_levels, buy_spread_start,

View File

@@ -58,7 +58,7 @@ class LaunchStrategyV2(Dashboard.Item):
"markets": {},
"candles_config": [],
"controllers_config": self._controller_config_selected,
"config_update_interval": 20,
"config_update_interval": 10,
"script_file_name": "v2_with_controllers.py",
"time_to_cash_out": None,
}

View File

@@ -4,38 +4,51 @@ import streamlit as st
def get_market_making_general_inputs(custom_candles=False):
with st.expander("General Settings", expanded=True):
c1, c2, c3, c4, c5, c6, c7 = st.columns(7)
default_config = st.session_state.get("default_config", {})
connector_name = default_config.get("connector_name", "kucoin")
trading_pair = default_config.get("trading_pair", "WLD-USDT")
leverage = default_config.get("leverage", 20)
total_amount_quote = default_config.get("total_amount_quote", 1000)
position_mode = 0 if default_config.get("position_mode", "HEDGE") == "HEDGE" else 1
cooldown_time = default_config.get("cooldown_time", 60 * 60) / 60
executor_refresh_time = default_config.get("executor_refresh_time", 60 * 60) / 60
candles_connector = None
candles_trading_pair = None
interval = None
with c1:
connector_name = st.text_input("Connector", value="binance",
connector_name = st.text_input("Connector", value=connector_name,
help="Enter the name of the exchange to trade on (e.g., binance_perpetual).")
with c2:
trading_pair = st.text_input("Trading Pair", value="WLD-USDT",
trading_pair = st.text_input("Trading Pair", value=trading_pair,
help="Enter the trading pair to trade on (e.g., WLD-USDT).")
with c3:
leverage = st.number_input("Leverage", value=20,
leverage = st.number_input("Leverage", value=leverage,
help="Set the leverage to use for trading (e.g., 20 for 20x leverage). Set it to 1 for spot trading.")
with c4:
total_amount_quote = st.number_input("Total amount of quote", value=1000,
total_amount_quote = st.number_input("Total amount of quote", value=total_amount_quote,
help="Enter the total amount in quote asset to use for trading (e.g., 1000).")
with c5:
position_mode = st.selectbox("Position Mode", ("HEDGE", "ONEWAY"), index=0,
position_mode = st.selectbox("Position Mode", ("HEDGE", "ONEWAY"), index=position_mode,
help="Enter the position mode (HEDGE/ONEWAY).")
with c6:
cooldown_time = st.number_input("Stop Loss Cooldown Time (minutes)", value=60,
cooldown_time = st.number_input("Stop Loss Cooldown Time (minutes)", value=cooldown_time,
help="Specify the cooldown time in minutes after having a stop loss (e.g., 60).") * 60
with c7:
executor_refresh_time = st.number_input("Executor Refresh Time (minutes)", value=60,
executor_refresh_time = st.number_input("Executor Refresh Time (minutes)", value=executor_refresh_time,
help="Enter the refresh time in minutes for executors (e.g., 60).") * 60
if custom_candles:
candles_connector = default_config.get("candles_connector", "kucoin")
candles_trading_pair = default_config.get("candles_trading_pair", "WLD-USDT")
interval = default_config.get("interval", "3m")
intervals = ["1m", "3m", "5m", "15m", "1h", "4h", "1d"]
interval_index = intervals.index(interval)
with c1:
candles_connector = st.text_input("Candles Connector", value="binance",
candles_connector = st.text_input("Candles Connector", value=candles_connector,
help="Enter the name of the exchange to get candles from (e.g., binance_perpetual).")
with c2:
candles_trading_pair = st.text_input("Candles Trading Pair", value="WLD-USDT",
candles_trading_pair = st.text_input("Candles Trading Pair", value=candles_trading_pair,
help="Enter the trading pair to get candles for (e.g., WLD-USDT).")
with c3:
interval = st.selectbox("Candles Interval", ("1m", "3m", "5m", "15m", "1h", "4h", "1d"), index=1,
interval = st.selectbox("Candles Interval", intervals, index=interval_index,
help="Enter the interval for candles (e.g., 1m).")
return connector_name, trading_pair, leverage, total_amount_quote, position_mode, cooldown_time, executor_refresh_time, candles_connector, candles_trading_pair, interval

View File

@@ -3,25 +3,36 @@ from hummingbot.connector.connector_base import OrderType
def get_risk_management_inputs():
default_config = st.session_state.get("default_config", {})
sl = default_config.get("stop_loss", 0.05) * 100
tp = default_config.get("take_profit", 0.02) * 100
time_limit = default_config.get("time_limit", 60 * 12 * 60) // 60
ts_ap = default_config.get("trailing_stop", {}).get("activation_price", 0.018) * 100
ts_delta = default_config.get("trailing_stop", {}).get("trailing_delta", 0.002) * 100
take_profit_order_type = OrderType(default_config.get("take_profit_order_type", 2))
order_types = [OrderType.LIMIT, OrderType.MARKET]
order_type_index = order_types.index(take_profit_order_type)
with st.expander("Risk Management", expanded=True):
c1, c2, c3, c4, c5, c6 = st.columns(6)
with c1:
sl = st.number_input("Stop Loss (%)", min_value=0.0, max_value=100.0, value=5.0, step=0.1,
sl = st.number_input("Stop Loss (%)", min_value=0.0, max_value=100.0, value=sl, step=0.1,
help="Enter the stop loss as a percentage (e.g., 2.0 for 2%).") / 100
with c2:
tp = st.number_input("Take Profit (%)", min_value=0.0, max_value=100.0, value=2.0, step=0.1,
tp = st.number_input("Take Profit (%)", min_value=0.0, max_value=100.0, value=tp, step=0.1,
help="Enter the take profit as a percentage (e.g., 3.0 for 3%).") / 100
with c3:
time_limit = st.number_input("Time Limit (minutes)", min_value=0, value=60 * 12,
time_limit = st.number_input("Time Limit (minutes)", min_value=0, value=time_limit,
help="Enter the time limit in minutes (e.g., 360 for 6 hours).") * 60
with c4:
ts_ap = st.number_input("Trailing Stop Act. Price (%)", min_value=0.0, max_value=100.0, value=1.8,
ts_ap = st.number_input("Trailing Stop Act. Price (%)", min_value=0.0, max_value=100.0, value=ts_ap,
step=0.1,
help="Enter the tr ailing stop activation price as a percentage (e.g., 1.0 for 1%).") / 100
with c5:
ts_delta = st.number_input("Trailing Stop Delta (%)", min_value=0.0, max_value=100.0, value=0.2, step=0.1,
ts_delta = st.number_input("Trailing Stop Delta (%)", min_value=0.0, max_value=100.0, value=ts_delta, step=0.1,
help="Enter the trailing stop delta as a percentage (e.g., 0.3 for 0.3%).") / 100
with c6:
take_profit_order_type = st.selectbox("Take Profit Order Type", (OrderType.LIMIT, OrderType.MARKET),
index=order_type_index,
help="Enter the order type for taking profit (LIMIT/MARKET).")
return sl, tp, time_limit, ts_ap, ts_delta, take_profit_order_type

View File

@@ -9,7 +9,7 @@ def normalize(values):
return [val / total for val in values]
def distribution_inputs(column, dist_type_name, levels=3):
def distribution_inputs(column, dist_type_name, levels=3, default_values=None):
if dist_type_name == "Spread":
dist_type = column.selectbox(
f"Type of {dist_type_name} Distribution",
@@ -46,9 +46,14 @@ def distribution_inputs(column, dist_type_name, levels=3):
elif dist_type == "Linear":
step = column.number_input(f"{dist_type_name} End", value=1.0,
key=f"{column}_{dist_type_name.lower()}_end")
else:
if default_values:
manual_values = [column.number_input(f"{dist_type_name} for level {i + 1}", value=value * 100.0,
key=f"{column}_{dist_type_name.lower()}_{i}") for i, value in
enumerate(default_values)]
else:
manual_values = [column.number_input(f"{dist_type_name} for level {i + 1}", value=i + 1.0,
key=f"{column}_{dist_type_name.lower()}_{i}") for i in range(levels)]
key=f"{column}_{dist_type_name.lower()}_{i}") for i, value in range(levels)]
start = None # As start is not relevant for Manual type
return dist_type, start, base, scaling_factor, step, ratio, manual_values