Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i coded a trading bot it buys coin at market rate and sell it after 5 seconds it was working but when I installed some modules because it is showing some latnce of about 4 seconds. I didn't touched the code but suddenly it stoped working it is now showing error in
line 41
print(f"Selling {qty} {coin_name.upper()}")
^
SyntaxError: invalid syntax
"

What I have tried:

Python
from binance.client import Client
from binance.exceptions import BinanceAPIException
import json
import sys
import time
import math

api_key = '--------------------------------------'
api_secret = '----------------------------'
client = Client(api_key, api_secret)
amount = sys.argv[1]
coin_name = sys.argv[2]
SELL_DELAY = 4

symbol_info = client.get_symbol_info(coin_name.upper() + 'BNB')


def place_order(coin_symbol, amnt):
    order = None
    try:
        order = client.order_market_buy(
            symbol=coin_symbol,
            quoteOrderQty=amnt)

    except BinanceAPIException as e:
        print(e.status_code)
        print(e.message)

    return order


def sell_order():
    sell = None
    try:
        balance = client.get_asset_balance(asset=coin_name.upper())['free']
        qty = float(balance)*0.9995
        step_size = symbol_info['filters'][2]['stepSize']
        precision = int(round(-math.log(float(step_size), 10), 0))
        qty = float(round(qty, precision))

        print(f"Selling {qty} {coin_name.upper()}")

        sell = client.order_market_sell(
            symbol=coin_name.upper() + 'BNB',
            quantity=qty)

    except BinanceAPIException as e:
        print(e.status_code)
        print(e.message)

    return sell


def main():
    order = place_order(coin_name.upper() + 'BNB', amount)

    if order is not None:
        order_time_raw = str(order['transactTime'])
        order_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(order_time_raw[0:10])))

        print("----------------------------------------------------------------")
        print("\nYou purchased {0} for Amount {1}\n".format(coin_name.upper(), amount))
        print("----------------------------------------------------------------")
        print("Order Details:\n")
        print(f"Symbol: {order['symbol']}")
        print(f"Status: {order['status']}")
        print(f"Executed Quantity: {order['executedQty']}")
        print(f"Original Quantity: {order['fills'][0]['qty']}")
        print(f"Symbol Price: {order['fills'][0]['price']}")
        print(f"BNB Spent: {order['cummulativeQuoteQty']}")
        print(f"Executed at: {order_time}")

        print('Waiting For 5 Seconds\n')

        time.sleep(SELL_DELAY)

        print('Selling Now\n')

    sell = sell_order()

    if sell is not None:
        sell_time_raw = str(sell['transactTime'])
        sell_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(sell_time_raw[0:10])))
        print("----------------------------------------------------------------")
        print("\nYou Sold {0} for Amount {1}\n".format(coin_name.upper(), amount))
        print("----------------------------------------------------------------")
        print("Sale Details:\n")
        print(f"Symbol: {sell['symbol']}")
        print(f"Status: {sell['status']}")
        print(f"Executed Quantity: {sell['executedQty']}")
        print(f"Original Quantity: {sell['fills'][0]['qty']}")
        print(f"Symbol Price: {sell['fills'][0]['price']}")
        print(f"BNB Earned: {sell['cummulativeQuoteQty']}")
        print(f"Executed at: {sell_time}")

        print("\n----------------------------------------------------------------")
        print("[+] Happy Trading [+]")
        print("----------------------------------------------------------------")


if __name__ == "__main__":
    main()
Posted
Updated 23-Oct-21 0:21am
v3
Comments
Patrice T 23-Oct-21 4:01am    
Show exact error message and position of error.
Asjad Adil 23-Oct-21 4:41am    
line 41
print(f"Selling {qty} {coin_name.upper()}")
^
SyntaxError: invalid syntax
Patrice T 23-Oct-21 5:16am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 23-Oct-21 6:19am    
I just tried that statement and it works fine. There must be something else that is causing the problem. Are there any other error messages? If not, you will need to use the debugger to find out why it is failing.
Asjad Adil 23-Oct-21 6:51am    
it was working fine but suddenly it start showing error after installed some pips

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900