Steemit Learning Club S23W2 || Developing Web based TradingDashboad to Replicate trade on multiple Bitget Accounts

in #techclub4 days ago

thumbnail.jpg

Thumbnails Designed through Canva

Hi folks, How are you? Its me your very own Faran Nabeel. I hope that everyone is well and happy. In the previous, I introduce my trading dashboard project, which is built with the use of Flask and integrate with Bitget API. This web-base application allow user to connect multiple accounts, including the Master account and Multiple client accounts, to track the real time balance efficiently.


Introduction

The dashboard organize account into two sections: The Master and all Clients, display separate balance. User can add multiple clients in the dashboard, but only one master account. API keys, include the public key, secret key, and passphrase, are securely store in the and process in Flask backend for authentication. The system use the Bitget API endpoints to fetch balance details, ensure accurate and up to date account insight.

I need to tell you unfortunately, I changed the dashboard frontend because I face some problems such as storing keys in .env file from user input and many others. it is show the board with master balance and master trade.

pic1.PNG

Dashboard Snapshot

This is a new trading dashboard webpage based application. Now it is in dark theme with blue combination. It describe all features like monitor balances of both master and clients, tracking trades of master, and replicate to all connected clients. It show the Master Account Balances and Master running trades, but no trades are list. it will list when i place trade on master account using app.

pic2.PNG

Dashboard Snapshot

This dashboard showing the client account balances after scrolling the page. The page with the title "Client Accounts", and it display the balance for client_1 in account types. As the futures balance is 10 USDT, while other balances of this client are mostly zero. There are left and right arrow buttons at left and right side, for navigate between multiple client accounts.

pic3.PNG

Dashboard Snapshot

In this section, a dashboard shows the client trades. The title at top "Client Trades", and it will display trades for client_1 because i connect only one account right now. Here is three buttons:

Replicate Trade to Clients: This button used for copy the master trade to all connected client accounts.
Place SL/TP on All Clients: This button is SL/TP fetch from master trade parameters and set up Stop-Loss (SL) and Take-Profit (TP) on all client accounts.

Close Positions on All Accounts: This button is close all active trades include master and all clients.

After this, the client trade section, there is TPSL Replicate Results box. This box will show the status of SL/TP replication across all connected client accounts, but now, it say "No TPSL replication results available", means no stop loss and take profit orders have been place yet. Here is the complete overview of this dashboard.


How it's working logically ?

Once the system fetch the open position from the master account, it ensure that all client account replicate the same trades. Here is the variation that how it will perform:

Fetch Position

Now, it is the time to describe how it's work logically. And how it's backend work and perform all the functionalities. This dashboard helps to automate trade management. It also ensure that trades are synchronize across all accounts. Here is the program which describe how it's fetch the master running future trades:

positions.PNG

This code use the "HMAC" with "SHA-256" and the Base64 to sign the API request. It sends GET request to Bitget API to fetch all positions for future account. Then, code generates the timestamp, and create the signature with the use of secret key and request detail, then send the request with header and prints the JSON response. The output should be one like this if you place a trade in your bitget account in the app.

All Positions Data: {'code': '00000', 'msg': 'success', 'requestTime': 1740834947195, 'data': [{'marginCoin': 'SUSDT', 'symbol': 'SBTCSUSDT', 'holdSide': 'long', 'openDelegateSize': '0', 'marginSize': '143.9917', 'available': '0.017', 'locked': '0', 'total': '0.017', 'leverage': '10', 'achievedProfits': '0', 'openPriceAvg': '84701', 'marginMode': 'isolated', 'posMode': 'hedge_mode', 'unrealizedPL': '-3.57', 'liquidationPrice': '76583.182640144666', 'keepMarginRate': '0.004', 'markPrice': '84491', 'marginRatio': '0.047052529631', 'breakEvenPrice': '84802.7022213328', 'totalFee': '', 'deductedFee': '0.8639502', 'grant': '', 'assetMode': 'single', 'autoMargin': 'off', 'takeProfit': '', 'stopLoss': '', 'takeProfitId': '', 'stopLossId': '', 'cTime': '1740834529467', 'uTime': '1740834529467'}]}

The response of this API call confirm the successful API request with the code "00000" which describe success in output. It retrieve all position detail for a BTC/USDT future trade on Bitget. For example, trader hold a long position with a 10x leverage, with entry price of 84,701 USDT, and others.

fetching.PNG

Fetching Position

The script is a part of my trading system project that replicates trade for all client accounts. It build order parameters, place the order with use of place_order_client(), and it is fetch from master running trade and save into data directory to proceed to clients for placing orders.

Placing Orders

The purpose of this code is automate place the future trade order on Bitget using its API. It authenticate and sends the POST request to execute the trade. API credentials are define for authentication as i discussed previous. The generate signature function create a secure HMAC signature to verify the request. It is use the /api/v2/mix/order/place-order the endpoint to place the order on the exchange.

order_place.PNG

Order Place

Order Response: {'code': '00000', 'msg': 'success', 'requestTime': 1740836790834, 'data': {'clientOid': '1740836789613', 'orderId': '1279727743218102276'}}
{"code": "00000", "msg": "success", "requestTime": 1740836790834, "data": {"clientOid": "1740836789613", "orderId": "1279727743218102276"}}

The given output indicate the trade order was successfully place on the exchange. The "code": "00000" and the "msg": "success" confirm the request was process without any error. The "clientOid" represent the unique client order ID which is generated for the trade.

Here we can see, the order is successfully placed on the app using API call, using this we can place future trade on our account.WhatsApp Image 2025-03-01 at 6.48.05 PM.jpeg

Placing SL/TP

This is the general code used for placing take Profit and stop Loss orders on already running trades. The code define a function place_tpsl_order() that construct the POST request to Bitget /api/v2/mix/order/place-tpsl-order endpoint.

sltp.PNG

Placing SL/TP

It send all necessary trade parameters such as margin type of trade, symbol, trigger price, execution price, and others. When it will execute, it place both take profit and stop loss order, ensure the risk management and automatic trade execution.

Placing a Take Profit (Target) Order:
Response JSON:
{
"code": "00000",
"msg": "success",
"requestTime": 1740839132480,
"data": {
"clientOid": "tp_order_001",
"orderId": "127978585564815470592"
} }
Placing a Stop Loss Order:
Response JSON:
{
"code": "00000",
"msg": "success",
"requestTime": 1740839132480,
"data": {
"clientOid": "sl_order_001",
"orderId": "1279737564815470592"
} }

The response confirm that the both take profit and stop loss orders were successfully place on Bitget. The "code": "00000" and the "msg": "success" describe that the request was processed without error.

Closing All Positions

In this section, I will share the basic code to close all positions in the current account and I use the same logic in my website to close all the positions in emergency situation include master and all clients. When user press the Close Positions on All Accounts button, it will close all the position for master and all clients.

close_positions.PNG

Close Positions

This provided code automate the process of closing a position on the Bitget . It also firstly generate the current timestamp for further process and then create a secure HMAC-SHA256 signature with the use of secret key, HTTP method, request endpoint which is '/api/v2/mix/order/close-positions', and the request body. The close_position() function construct the JSON payload with trading pair symbol, product_type, and other things, then send a POST request with proper header for authentication. It will effectively seek risk management tasks.

Response Text: {"code": "00000", "msg": "success", "requestTime": 1740900895871, "data": {"successList": [{"orderId": "1279996619172511750", "clientOid": "1279996619172511757", "symbol": "SETHSUSDT"}], "failureList": [], "result": false}}
Close Position Result: {'code': '00000', 'msg': 'success', 'requestTime': 1740900895871, 'data': {'successList': [{'orderId': '1279996619172511750', 'clientOid': '1279996619172511757', 'symbol': 'SETHSUSDT'}], 'failureList': [], 'result': False}}

The output show API response for close position request. Response indicate the successful call with status code of "00000" means a "success" message.

How it's working functionally ?

Now I wanna like to discuss that how it's working and replicate trade to all clients. Firstly I wanna share the quick look of the website.

gif001.gif

It is the overview of the page and we can see that it shows the master account balance and the master trade box, but We can see it's still empty but when we place the trade, it will show all the details.

gif1.gif

I will use the demo trading for showing that how will it work. When user place the future trade from the exchange app, This show a cryptocurrency future trading interface for SBTC/USDT pair. and option to open either long or short position. And when i place the long position it has executed. and now we can go back to the dashboard and find it's showing or not. Now lets move to Trading dashboard. I also placed the stoploss and target price after this and it will show on the master running trades.


gif2.gif

TradingBoard screenshot show the featuring Master Running Trades, display real time metric such margin, leverage, open price, liquidation price,position status and SL/TP. It support rapid order adjustment.

gif3.gif

As user press the button of "Replicate trade to Clients", it will replicate the master trade to all clients. And show the pop up message if it will replicate successfully to all clients.

gif04.gif

As it is replicated on the client successfully, and showing all necessary details like symbol leverage, unreleased Profit, and position side. If we add more client it will also replicate this trade for next client which is i will add. Now we need to replicate the stop loss and target separately.

fcddf.gif

As i press the Place "SL/Tp on all clients" button, it will fetch the stop loss and target from master trade and place on all clients as a order. And it show the status in the box for trade_01 for client_01.

ggg.gif

And finally Last feature is to close all the position for all account including master and all clients in emergency situation. As I press the the button to close all the position, it will close position, as I shared a live example.


GitHub repository

I create the GitHub repository, and upload all necessary files to understand how will it work, and the basic idea about fetching the balances of both accounts, fetching trade, placing orders, placing SL/TP and closing al l positions.

Now I do like to invite my friends @suboohi, @sualeha, @artist1111, @uzma4882, @josepha and @sameer07 to participate in this challenge. Special thanks to the mentors @kafio @mohammadfaisal @alejos7ven'

Regards,
Faran Nabeel

faran.png

Sort:  
Loading...

Upvoted! Thank you for supporting witness @jswit.

Loading...