PeakeCoin BNB Trading Bot - Stop Loss Logic

in tunes •  2 days ago 

So here working on the @PeakeCoin project we have had a couple things to hold us off a little bit. Since its currently just me expanding my own work portfolio, so hopefully I can score some side jobs and not have to really have ajob anymore, lol.

Seriously though lets get back to business.

I was not losing money, but the logic was only set to a percentage that was off of the last buy/sell price. Leaving it at that point doesn't really guarantee a profit for any of the transactions.

So with the existing trading bot https://ecency.com/hive-167922/@paulmoon410/peakcoin-bnb-trading-bot only had the logic of percentage offset of the last orderbook

TOKEN = "SWAP.BNB"
SPREAD = 0.01  # 3% above/below for placing orders
TRADE_QTY = 0.001
SLEEP_TIME = 60  # seconds

def trading_bot():
    while True:
        book = get_orderbook_top(TOKEN)
        if not book:
            print("❌ Failed to fetch orderbook.")
            time.sleep(SLEEP_TIME)
            continue

        buy_price = book["highestBid"] * (1 - SPREAD)
        sell_price = book["lowestAsk"] * (1 + SPREAD)


What I've been trying to accomplish is more along these lines.

TRADE_HISTORY_FILE = "trade_history.json"
MIN_PROFIT_MARGIN = 0.015  # 1.5%

def load_trade_history():
    if os.path.exists(TRADE_HISTORY_FILE):
        with open(TRADE_HISTORY_FILE, "r") as f:
            return json.load(f)
    return {}

def save_trade_history(data):
    with open(TRADE_HISTORY_FILE, "w") as f:
        json.dump(data, f, indent=2)

def update_trade_history(token, action, price):
    history = load_trade_history()
    if token not in history:
        history[token] = {}
    history[token][action] = price
    save_trade_history(history)

Within the logic of the stop/loss code

    history = load_trade_history()

    if order_type == "sell":
        last_buy = history.get(token, {}).get("buy", 0)
        min_sell_price = last_buy * (1 + MIN_PROFIT_MARGIN)
        if price < min_sell_price:
            print(f"🚫 Skipping SELL order — price too low to make profit. Minimum: {min_sell_price}")
            return False
    elif order_type == "buy":
        # Optional: prevent buying above previous sell price
        last_sell = history.get(token, {}).get("sell", float("inf"))
        max_buy_price = last_sell * (1 - MIN_PROFIT_MARGIN)
        if price > max_buy_price:
            print(f"🚫 Skipping BUY order — price too high to profit. Maximum: {max_buy_price}")
            return False


I'll update the Github place_order.py with the @peakecoin.bnb bot within the next few hours. I haven't run a new bot with the code I've posted here to date. If it doesn't work, I'll post the corrective pieces of the code. I'll post them tomorrow if I fix it, or if you fix it let me know.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Upvoted! Thank you for supporting witness @jswit.