(feat) add macd bb

This commit is contained in:
cardosofede
2024-04-17 21:36:47 -03:00
parent 0ef7a7a0eb
commit 4a8b787718
3 changed files with 167 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ def main_page():
Page("pages/pmm_simple/app.py", "PMM Simple", "👨‍🏫"), Page("pages/pmm_simple/app.py", "PMM Simple", "👨‍🏫"),
Page("pages/dman_maker_v2/app.py", "D-Man Maker V2", "🤖"), Page("pages/dman_maker_v2/app.py", "D-Man Maker V2", "🤖"),
Page("pages/bollinger_v1/app.py", "Bollinger V1", "📈"), Page("pages/bollinger_v1/app.py", "Bollinger V1", "📈"),
Page("pages/macd_bb_v1/app.py", "MACD_BB V1", "📉"),
Page("pages/xemm_controller/app.py", "XEMM Controller", "⚡️"), Page("pages/xemm_controller/app.py", "XEMM Controller", "⚡️"),
Page("pages/position_builder/app.py", "Position Builder", "🔭"), Page("pages/position_builder/app.py", "Position Builder", "🔭"),
# Page("pages/master_conf/app.py", "Credentials", "🗝️"), # Page("pages/master_conf/app.py", "Credentials", "🗝️"),

View File

@@ -0,0 +1,19 @@
# D-Man Maker V2
## Features
- **Interactive Configuration**: Configure market making parameters such as spreads, amounts, and order levels through an intuitive web interface.
- **Visual Feedback**: Visualize order spread and amount distributions using dynamic Plotly charts.
- **Backend Integration**: Save and deploy configurations directly to a backend system for active management and execution.
### Using the Tool
1. **Configure Parameters**: Use the Streamlit interface to input parameters such as connector type, trading pair, and leverage.
2. **Set Distributions**: Define distributions for buy and sell orders, including spread and amount, either manually or through predefined distribution types like Geometric or Fibonacci.
3. **Visualize Orders**: View the configured order distributions on a Plotly graph, which illustrates the relationship between spread and amount.
4. **Export Configuration**: Once the configuration is set, export it as a YAML file or directly upload it to the Backend API.
5. **Upload**: Use the "Upload Config to BackendAPI" button to send your configuration to the backend system. Then can be used to deploy a new bot.
## Troubleshooting
- **UI Not Loading**: Ensure all Python dependencies are installed and that the Streamlit server is running correctly.
- **API Errors**: Check the console for any error messages that may indicate issues with the backend connection.
For more detailed documentation on the backend API and additional configurations, please refer to the project's documentation or contact the development team.

147
pages/macd_bb_v1/app.py Normal file
View File

@@ -0,0 +1,147 @@
import streamlit as st
import pandas as pd
import pandas_ta as ta
import plotly.graph_objects as go
import yaml
from hummingbot.connector.connector_base import OrderType
from plotly.subplots import make_subplots
from CONFIG import BACKEND_API_HOST, BACKEND_API_PORT
from utils.backend_api_client import BackendAPIClient
from utils.st_utils import initialize_st_page
# Initialize the Streamlit page
initialize_st_page(title="MACD_BB V1", icon="📊", initial_sidebar_state="expanded")
@st.cache_data
def get_candles(connector_name, trading_pair, interval, max_records):
backend_client = BackendAPIClient(BACKEND_API_HOST, BACKEND_API_PORT)
return backend_client.get_candles(connector_name, trading_pair, interval, max_records)
@st.cache_data
def add_indicators(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold, macd_fast, macd_slow, macd_signal):
# Bollinger Bands
df.ta.bbands(length=bb_length, std=bb_std, append=True)
# MACD
df.ta.macd(fast=macd_fast, slow=macd_slow, signal=macd_signal, append=True)
# Decision Logic
bbp = df[f"BBP_{bb_length}_{bb_std}"]
macdh = df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"]
macd = df[f"MACD_{macd_fast}_{macd_slow}_{macd_signal}"]
long_condition = (bbp < bb_long_threshold) & (macdh > 0) & (macd < 0)
short_condition = (bbp > bb_short_threshold) & (macdh < 0) & (macd > 0)
df["signal"] = 0
df.loc[long_condition, "signal"] = 1
df.loc[short_condition, "signal"] = -1
return df
st.write("## Configuration")
c1, c2, c3, c4 = st.columns(4)
with c1:
connector_name = st.text_input("Connector Name", value="binance_perpetual")
trading_pair = st.text_input("Trading Pair", value="WLD-USDT")
with c2:
interval = st.selectbox("Candle Interval", ["1m", "3m", "5m", "15m", "30m"], index=1)
max_records = st.number_input("Max Records", min_value=100, max_value=10000, value=1000)
with c3:
macd_fast = st.number_input("MACD Fast", min_value=1, value=21)
macd_slow = st.number_input("MACD Slow", min_value=1, value=42)
macd_signal = st.number_input("MACD Signal", min_value=1, value=9)
with c4:
bb_length = st.number_input("BB Length", min_value=2, value=100)
bb_std = st.number_input("BB Std Dev", min_value=0.1, value=2.0, step=0.1)
bb_long_threshold = st.number_input("BB Long Threshold", value=0.0)
bb_short_threshold = st.number_input("BB Short Threshold", value=1.0)
# Fetch and process data
candle_data = get_candles(connector_name, trading_pair, interval, max_records)
df = pd.DataFrame(candle_data)
df.index = pd.to_datetime(df['timestamp'], unit='ms')
df = add_indicators(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold, macd_fast, macd_slow, macd_signal)
# Prepare data for signals
signals = df[df['signal'] != 0]
buy_signals = signals[signals['signal'] == 1]
sell_signals = signals[signals['signal'] == -1]
# Define your color palette
tech_colors = {
'upper_band': '#4682B4',
'middle_band': '#FFD700',
'lower_band': '#32CD32',
'buy_signal': '#1E90FF',
'sell_signal': '#FF0000',
}
# Create a subplot with 3 rows
fig = make_subplots(rows=3, cols=1, shared_xaxes=True,
vertical_spacing=0.05, # Adjust spacing to make the plot look better
subplot_titles=('Candlestick with Bollinger Bands', 'MACD Line and Histogram', 'Trading Signals'),
row_heights=[0.5, 0.3, 0.2]) # Adjust heights to give more space to candlestick and MACD
# Candlestick and Bollinger Bands
fig.add_trace(go.Candlestick(x=df.index,
open=df['open'],
high=df['high'],
low=df['low'],
close=df['close'],
name="Candlesticks", increasing_line_color='#2ECC71', decreasing_line_color='#E74C3C'),
row=1, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df[f"BBL_{bb_length}_{bb_std}"], line=dict(color='blue'), name='Lower Band'), row=1, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df[f"BBM_{bb_length}_{bb_std}"], line=dict(color='red'), name='Middle Band'), row=1, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df[f"BBU_{bb_length}_{bb_std}"], line=dict(color='green'), name='Upper Band'), row=1, col=1)
# MACD Line and Histogram
fig.add_trace(go.Scatter(x=df.index, y=df[f"MACD_{macd_fast}_{macd_slow}_{macd_signal}"], line=dict(color='orange'), name='MACD Line'), row=2, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df[f"MACDs_{macd_fast}_{macd_slow}_{macd_signal}"], line=dict(color='purple'), name='MACD Signal'), row=2, col=1)
fig.add_trace(go.Bar(x=df.index, y=df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"], name='MACD Histogram', marker_color=df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"].apply(lambda x: '#FF6347' if x < 0 else '#32CD32')), row=2, col=1)
# Signals plot
fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals['close'], mode='markers',
marker=dict(color=tech_colors['buy_signal'], size=20, symbol='triangle-up'),
name='Buy Signal'), row=1, col=1)
fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals['close'], mode='markers',
marker=dict(color=tech_colors['sell_signal'], size=20, symbol='triangle-down'),
name='Sell Signal'), row=1, col=1)
# Trading Signals
fig.add_trace(go.Scatter(x=signals.index, y=signals['signal'], mode='markers', marker=dict(color=signals['signal'].map({1: '#1E90FF', -1: '#FF0000'}), size=10), name='Trading Signals'), row=3, col=1)
# Update layout settings for a clean look
fig.update_layout(height=1000, title="MACD and Bollinger Bands Strategy", xaxis_title="Time", yaxis_title="Price", template="plotly_dark", showlegend=True)
fig.update_xaxes(rangeslider_visible=False, row=1, col=1)
fig.update_xaxes(rangeslider_visible=False, row=2, col=1)
fig.update_xaxes(rangeslider_visible=False, row=3, col=1)
# Display the chart
st.plotly_chart(fig, use_container_width=True)
c1, c2, c3 = st.columns([2, 2, 1])
with c1:
config_base = st.text_input("Config Base", value=f"macd_bb_v1-{connector_name}-{trading_pair.split('-')[0]}")
with c2:
config_tag = st.text_input("Config Tag", value="1.1")
# Save the configuration
id = f"{config_base}-{config_tag}"
config = {
"id": id,
"connector_name": connector_name,
"trading_pair": trading_pair,
"interval": interval,
"bb_length": bb_length,
"bb_std": bb_std,
"bb_long_threshold": bb_long_threshold,
"bb_short_threshold": bb_short_threshold,
"macd_fast": macd_fast,
"macd_slow": macd_slow,
"macd_signal": macd_signal,
}