Search code examples
tradingbinancebinance-api-client

Post an order to Binance SPOT Trading via API


I'm trying to write two API calls to Binance SPOT trading to POST an order and not sure how I should proceed, I'm newbie on this, and documentation is a bit confusing.

I want to post an order to buy BTCBUSD, current price 12000, and execute buy order ONLY if price is higher than 12250, and once it is executed (buy), sell ALL it if price is below 12100, and I want to 6000BUSD.

{
  "symbol": "BTCBUSD",
  "side": BUY,
  "type": LIMIT
  "quantity": 6000,
  "price": 12250,
  "stopPrice": 12100
}

I'm not sure if this call is valid to achieve what I want or should I do two calls with next parameters.

{
  "symbol": "BTCBUSD",
  "side": BUY,
  "type": LIMIT
  "quantity": 6000,
  "price": 12250
}

{
  "symbol": "BTCBUSD",
  "side": SELL,
  "type": STOP_LOSS
  "price": 12250
}

My doubt in this scenario is LIMIT will buy when price is reached (documentation talks about BUY price reach or below) and if in SELL order, if you want to sell the entire 'account' from BTC to BUSD, if you don't specify a quantity it sells ALL?

And the latest, a BUY order that doesn't have funds? It simply fails when it is executed? for example, I post two orders:

  1. BUY BTCBUSD > 10000BUSD, current price 12000, buy at price 12500
  2. BUY ETHBUSD > 10000BUSD, current price 2000, buy at price 2500

My total funds in BUSD are 10000. Funds in BUSD will be freeze/blocked meanwhile nothing is bought because I have one order, second would get an error. Or order are posted, and funds will be checked when order needs to be executed?


Solution

  • I hope this is help u can adjust everything how u want, Now these codes for BTCBUSD and if u type price 12250 for buy price this code waiting for until current price hit the 12250 and calculate automatically +%5.

    I used the numbers you gave for better understanding But if u start it like this as it is already triggered now bec 12250 is now lower than current price dont forget to change

    DONT FORGET TO CHANGE 'price variable'

    DONT FORGET TO CHANGE 'symbol variable' if u want for ETH

    DONT FORGET TO CHANGE 'Key variable for ETH' if u want for ETH change like:

    key ="https://api.binance.com/api/v3/ticker/price?symbol=ETHBUSD"
    
        #=====IMPORT======
        import os
        import time
        import json
        import requests
        import asyncio
        from itertools import count
        from binance.client import Client
        
        
        #=====API-DATA======
        symbol='BTCBUSD'                       
        api_='API'            #Your API
        api_key='SECRET KEY'  #Your Secret_KEY 
    
        client = Client(api_key=api_, api_secret=api_key,testnet = False)
        
        #==GET-PRICE==#
        key ="https://api.binance.com/api/v3/ticker/price?symbol=BTCBUSD" 
        data = requests.get(key)                                          
        data = data.json()
        current = float(data['price'])
        #now we get live price from binance for BTCBUSD.
        
        price = 12250                        
        #Type whatever u want this is represent buying price AND requested price
        
        #=====AUTO-TP-======
        hesap1= price*0.05                   #'0.05'means is now its set for '+%5 TP' if u want to change TP ratio u can play with '0.05'
        hesap2= hesap1+price                 #'current price + %5' example if BTCBUSD current is 100 now we add 5 and 100+5=105 this is for TP price
        hesap2= round(hesap2,2)              #Final tp price with rounded
        #=====AUTO-SL======
        hesap3= price*0.025                  #same thing but now its set for -%2,5
        hesap4= price-hesap3                 #now we subtract from current price like '100-2,5= 97,5'
        hesap4= round(hesap4,2)              #Final sl price with rounded
        
        
        #===QUANTITY-CALC====
        balance = float(6000)      #this represent your balance
        qua = balance/current      #this means if u want to buy at 12000: 6000/12000= and this is '0.5 BTC'
        qua = round(qua,5)
        print('| current:',current,'| Balance:',balance,'| Quantity',qua,
            '| TP at:',hesap2,'| SL at:',hesap4)
        
        #NOW PRICE CATHCER
        while True:
            print('Waiting for price','| current:',current)
            time.sleep(1)              #its sleep this loop for 1 sec. İf u want u can delete.
            if current >= price :      #if current equal or bigger than 12250 its sent order at 12250 and set %5 TP, Set -2,5 SL
                #=====LONG-BUY======                 
                BUYorder=client.create_order(
                    symbol=symbol,
                    side='BUY',
                    type='LIMIT',
                    quantity=qua,
                    price=(price),
                    timeInForce='GTC')
                print("1-Orders sent")
        
                #=====LONG-TP%5======                  
                TPsell=client.create_order(
                    symbol=symbol,
                    side='SELL',
                    type='TAKE_PROFIT_MARKET',
                    stopPrice=hesap2,
                    closePosition='true')   
                print("2-TP set at +%5")
        
                #=====LONG-SL%2,5======                
                SLsell=client.create_order(
                    symbol=symbol,
                    side='SELL',
                    type='STOP_MARKET',
                    stopPrice=hesap4,  #
                    closePosition='true')   
                print("3-SL set at -%2,5")
                break
        
        print('END')
    

    And if u run this output is like that:

    | current: 15820.75 | Balance: 6000.0 | Quantity 0.37925 | TP at: 12862.5 | SL at: 11943.75
    Waiting for price | current: 15820.75
    Waiting for price | current: 15819.50
    Waiting for price | current: 15821.62