create account

Programming Project Part #3 by vatman

View this thread on: hive.blogpeakd.comecency.com
· @vatman ·
$11.95
Programming Project Part #3
Today we're going to build the GUI for this bot.
Here’s how the Tkinter GUI is made and what it looks like, plus a quick recap of how the bot works.

The idea is to have the first window ask for the Binance API keys and Telegram tokens. Once we hit "Save," the GUI will verify the Binance credentials. If they're valid, it will create and save a config file for the bot to read in the future.

<div class="pull-left"> https://images.hive.blog/DQme5F88bJaZsM7fkKo8WmBjQZww9ChfjuGWywDKbVzDZKe/Captsure.PNG </div> <div class="pull-right"> https://images.hive.blog/DQmaeoV9UaaPyeny161czAZQRMWbjWesysSXpwfpwZSV8Hd/Capture.PNG </div> 

--- 

Once that’s done, we can select the coin we want to track, the target amount to maintain, the min notional (meaning the minimum amount allowed to buy/sell), and the % of profit we want to make on each buy.

So for example:
If we have a target of $100 in BTC and the price drops, we still own the same amount of BTC, but the dollar value we hold is now less—say $99. The bot should then buy $1 worth of BTC to bring us back up to target.

Now, if we set our profit % to 20%, it will only sell once that $1 buy reaches $1.20—ensuring a profit.

There are two main scenarios:

If the price consistently drops

If the price consistently rises

# 1: If the price drops consistently
Example: We own $100 in BTC and set a budget of $50. If the price of BTC drops and our holding is now worth $99, we use $1 from our budget to buy more. We now hold slightly more BTC (still worth around $100), and our budget is $49.

This continues until the budget is spent. The idea is that every time the price drops, we track the buys.

The bot can run as often as you want—say, every 10 seconds. If the balance drops by $1 in that window, it buys. If it's checking every 10 minutes instead, the price might have dropped more, and we’d buy a bigger chunk.

# 2: If the price rises consistently
In this case, I decided the bot should sell as much as it wants above the target. So if we have $100 in BTC and it grows to $110, the bot just sells the extra $10.

However, if we have a pending profit-sell (a buy we made earlier that hasn't reached its profit target yet), we don’t want to include that amount in the sell. So really, we’re tracking two things:

The difference between the current value and the target, and

Any open buy positions still waiting to hit their profit %

Here's a quick look at the files it creates and how they’re structured:

<div class="pull-left"> https://images.hive.blog/DQmYjr4qihrxzYsnFYuzewHb5bQYuVVf88WUGQMJJkKq2RF/Casdapture.PNG </div> <div class="pull-right"> https://images.hive.blog/DQmX84rV33Zdy949KhVNQeoyo3Ks8wuRdr1Tvu5u7L2D1Zv/Captasdure.PNG </div>
In these images, you can see how the credentials.json file creates a format that’s readable for the bot, and what a coin parameter file looks like so the bot knows what to do.

Honestly, I don’t know if this is going to turn into a good trading bot. I know Binance already has something similar on their site, but I started this project before I knew that—and I want to finish it just to have something of my own that (hopefully) makes a profit.

Side note: it’s kind of nice to understand how it all works and have it running locally. Even though it’s still using Binance’s APIs, it just feels safer.

heres the code of the bot incase someone wants to copy and paste it : 

      import tkinter as tk
      from tkinter import messagebox, ttk
      import json, os
      from binance.client import Client

                 CREDENTIALS_PATH = "credentials.json"
                 COINS_PATH = "coins.json"
           class RebalancerGUI:
           def __init__(self, root):
        self.root = root
        self.root.title("Simple Rebalancing Bot Setup")

        self.api_key = tk.StringVar()
        self.api_secret = tk.StringVar()
        self.telegram_token = tk.StringVar()
        self.telegram_chat_id = tk.StringVar()

        self.symbol_var = tk.StringVar()
        self.target_var = tk.StringVar()
        self.budget_var = tk.StringVar()
        self.min_notional_var = tk.StringVar()
        self.profit_var = tk.StringVar()

        self.client = None
        self.pairs = []

        self.show_credentials_screen()

    def show_credentials_screen(self):
        self.clear_root()

        tk.Label(self.root, text="Binance API Key").pack()
        tk.Entry(self.root, textvariable=self.api_key).pack()

        tk.Label(self.root, text="Binance API Secret").pack()
        tk.Entry(self.root, textvariable=self.api_secret, show="*").pack()

        tk.Label(self.root, text="Telegram Bot Token (optional)").pack()
        tk.Entry(self.root, textvariable=self.telegram_token).pack()

        tk.Label(self.root, text="Telegram Chat ID (optional)").pack()
        tk.Entry(self.root, textvariable=self.telegram_chat_id).pack()

        tk.Button(self.root, text="Save and Continue", command=self.verify_credentials).pack(pady=10)

    def verify_credentials(self):
        try:
            self.client = Client(self.api_key.get(), self.api_secret.get())
            self.client.get_account()  # test credentials

            creds = {
                "binance_api": self.api_key.get(),
                "binance_secret": self.api_secret.get(),
                "telegram_token": self.telegram_token.get(),
                "telegram_chat_id": self.telegram_chat_id.get()
            }

            with open(CREDENTIALS_PATH, "w") as f:
                json.dump(creds, f, indent=2)

            self.fetch_usdt_pairs()
            self.show_config_screen()

        except Exception as e:
            messagebox.showerror("Error", f"Invalid Binance credentials:\n{e}")

    def fetch_usdt_pairs(self):
        info = self.client.get_exchange_info()
        self.pairs = sorted([s['symbol'] for s in info['symbols'] if s['symbol'].endswith("USDT") and s['status'] == 'TRADING'])

    def show_config_screen(self):
        self.clear_root()

        tk.Label(self.root, text="Select USDT Pair").pack()
        dropdown = ttk.Combobox(self.root, textvariable=self.symbol_var, values=self.pairs)
        dropdown.pack()

        tk.Label(self.root, text="Target Value (USDT)").pack()
        tk.Entry(self.root, textvariable=self.target_var).pack()

        tk.Label(self.root, text="Dip Budget (USDT)").pack()
        tk.Entry(self.root, textvariable=self.budget_var).pack()

        tk.Label(self.root, text="Min Notional (manual)").pack()
        tk.Entry(self.root, textvariable=self.min_notional_var).pack()

        tk.Label(self.root, text="Profit %").pack()
        tk.Entry(self.root, textvariable=self.profit_var).pack()

        tk.Button(self.root, text="Save Coin Config", command=self.save_coin_config).pack(pady=10)

    def save_coin_config(self):
        try:
            data = {
                "symbol": self.symbol_var.get(),
                "target": float(self.target_var.get()),
                "budget": float(self.budget_var.get()),
                "min_notional": float(self.min_notional_var.get()),
                "profit_pct": float(self.profit_var.get())
            }

            with open(COINS_PATH, "w") as f:
                json.dump([data], f, indent=2)

            messagebox.showinfo("Saved", "Coin config saved to coins.json")
        except ValueError:
            messagebox.showerror("Error", "Please enter valid numbers")

    def clear_root(self):
        for widget in self.root.winfo_children():
            widget.destroy()if __name__ == "__main__":
    root = tk.Tk()
    app = RebalancerGUI(root)
    root.mainloop()
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 221 others
properties (23)
authorvatman
permlinkprogramming-project-part-3
categoryprogramming
json_metadata"{"tags":["programming","python","development","bitcoin","passiveincome","tutorial","tradingbot","binance"],"image":["https://images.hive.blog/DQme5F88bJaZsM7fkKo8WmBjQZww9ChfjuGWywDKbVzDZKe/Captsure.PNG","https://images.hive.blog/DQmaeoV9UaaPyeny161czAZQRMWbjWesysSXpwfpwZSV8Hd/Capture.PNG","https://images.hive.blog/DQmYjr4qihrxzYsnFYuzewHb5bQYuVVf88WUGQMJJkKq2RF/Casdapture.PNG","https://images.hive.blog/DQmX84rV33Zdy949KhVNQeoyo3Ks8wuRdr1Tvu5u7L2D1Zv/Captasdure.PNG"],"app":"hiveblog/0.1","format":"markdown","description":"Today we're going to build the GUI for this bot. Here’s how the Tkinter GUI is made and what it looks like, plus a quick recap of how......."}"
created2025-05-14 23:09:57
last_update2025-05-14 23:09:57
depth0
children5
last_payout2025-05-21 23:09:57
cashout_time1969-12-31 23:59:59
total_payout_value5.992 HBD
curator_payout_value5.956 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,586
author_reputation16,946,770,159,570
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,734,617
net_rshares34,307,696,199,747
author_curate_reward""
vote details (285)
@hivebuzz ·
Congratulations @vatman! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/https://hivebuzz.me/@vatman/upvoted.png?202505141953"></td><td>You received more than 2000 upvotes.<br>Your next target is to reach 2250 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@vatman) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

👍  
properties (23)
authorhivebuzz
permlinknotify-1747264364
categoryprogramming
json_metadata{"image":["https://hivebuzz.me/notify.t6.png"]}
created2025-05-14 23:12:45
last_update2025-05-14 23:12:45
depth1
children0
last_payout2025-05-21 23:12:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length622
author_reputation369,400,114,746,106
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,734,644
net_rshares4,599,179,083
author_curate_reward""
vote details (1)
@hivepago ·
So this thing trades for you and takes profits?  I might try it.  You didn't say where to paste the code or how to run it though.  I don't think that is enough code to be the whole program either. Is it?
👍  
properties (23)
authorhivepago
permlinkswbhqj
categoryprogramming
json_metadata{"app":"hiveblog/0.1"}
created2025-05-15 19:20:42
last_update2025-05-15 19:20:42
depth1
children1
last_payout2025-05-22 19:20:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length203
author_reputation6,988,979,842,187
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,753,398
net_rshares4,296,624,458
author_curate_reward""
vote details (1)
@vatman ·
Hey, thanks for the question.  in my other posts about this project I talk about how its a python program. I'm using "visual code" to work on this but if you don't have that you should be able to paste this code block into a text file and then rename it from (*.txt) to (*.py) and it should run.

PS: its not the whole trading bot, this is just the first and second window that creates the files for the bot to read & run in the future
properties (22)
authorvatman
permlinkswbjar
categoryprogramming
json_metadata{"app":"hiveblog/0.1"}
created2025-05-15 19:54:27
last_update2025-05-15 19:54:27
depth2
children0
last_payout2025-05-22 19:54:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length435
author_reputation16,946,770,159,570
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,753,981
net_rshares0
@stemsocial ·
re-vatman-programming-project-part-3-20250515t045041578z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Consider setting @stemsocial as a beneficiary of this post's rewards if you would like to support the community and contribute to its mission of promoting science and education on Hive.&nbsp;<br />&nbsp;<br />
</div>
👍  
properties (23)
authorstemsocial
permlinkre-vatman-programming-project-part-3-20250515t045041578z
categoryprogramming
json_metadata{"app":"STEMsocial"}
created2025-05-15 04:50:42
last_update2025-05-15 04:50:42
depth1
children1
last_payout2025-05-22 04:50:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length646
author_reputation22,918,491,691,707
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,738,038
net_rshares4,209,726,230
author_curate_reward""
vote details (1)
@vatman ·
Wow, STEMsocial seems like a vey cool community I'm looking forward to posting on it, thanks for the comment.
properties (22)
authorvatman
permlinkswbj2z
categoryprogramming
json_metadata{"app":"hiveblog/0.1"}
created2025-05-15 19:49:48
last_update2025-05-15 19:49:48
depth2
children0
last_payout2025-05-22 19:49:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length109
author_reputation16,946,770,159,570
root_title"Programming Project Part #3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,753,939
net_rshares0