0

I am trying to create a program that will post orders on binance and than wait until orders close. So we post 3 orders and when only one order left we do something. So we need just to "start" this function and move on. I am trying to use async but my code in function just not executed and it saying that coroutine 'Position.new_position' was never awaited. My code:

import requests
import pandas as pd
from binance.client import Client
from binance.helpers import round_step_size
import info
import math
import asyncio
from discord.ext import commands
import discord
import logging


client = Client(api_key=info.api, api_secret=info.secret_api, testnet=False)
DS = commands.Bot(command_prefix='/', intents = discord.Intents.all())
DS.remove_command("help")


def get_qsize(symbol):
    info = client.futures_exchange_info()
    for item in info['symbols']:
        if(item['symbol'] == symbol):
            for f in item['filters']:
                if f['filterType'] == 'LOT_SIZE':
                    return f['stepSize']

def get_pricesize(symbol):
    info = client.futures_exchange_info()
    for item in info['symbols']:
        if(item['symbol'] == symbol):
            for f in item['filters']:
                if f['filterType'] == 'PRICE_FILTER':
                    return f['tickSize']

for i in info.tokens:
        try:
            client.futures_change_margin_type(symbol=i, marginType='ISOLATED')
        except:
            continue

pos_list = {}

class Position:
    async def new_position(side, symbol, price, stopl, takep):
        await asyncio.sleep(60)
        print('OK')
        if side == "BUY":
            close_side = "SELL"
        else:
            close_side = "BUY"

        #calculating quaintity
        b=client.futures_account_balance()
        b = pd.DataFrame.from_dict(b)
        b = b.loc[b['asset']=='USDT']
        balance = float(b['balance'].values) * 0.05
        q = balance / price * info.laverage

        #rounding
        q_size = get_qsize(symbol)
        price_size = get_pricesize(symbol)
        price = round_step_size(float(price), float(price_size))
        stopl = round_step_size(float(stopl), float(price_size))
        takep = round_step_size(float(takep), float(price_size))
        q = round_step_size(q, q_size)
        take_q = round_step_size(q/2, q_size)

        #opening order
        client.futures_change_leverage(symbol=symbol, leverage=info.laverage)
        buyorder = client.futures_create_order(symbol=symbol, side=side, type="MARKET", quantity=q, isIsolated='TRUE')
        stop = client.futures_create_order(symbol=symbol, side="SELL", type="STOP_MARKET", stopPrice=stopl, closePosition="true")
        take = client.futures_create_order(symbol=symbol, side="SELL", type="TAKE_PROFIT_MARKET", stopPrice=takep, quantity=take_q)
        
        #monitoring
        a = True
        while a == True:
            print('OK')
            await asyncio.sleep(60)
            orders = client.futures_get_open_orders(symbol=symbol)
            if len(orders) == 1:
                try:
                    client.futures_cancel_order(symbol=symbol, orderId=take['orderId'], timestamp='true')
                    a = False
                except:
                    a = False
    

    def close_position(position):
        #post close order
        close = client.futures_create_order(symbol=position.symbol, side=position.close_side, type="MARKET", quantity=position.q, reduceOnly='true')
        #close orders
        try:
            client.futures_cancel_order(symbol=position.symbol, orderId=position.stop['orderId'], timestamp='true')
        except:
            pass
        try:
            client.futures_cancel_order(symbol=position.symbol, orderId=position.take['orderId'], timestamp='true')
        except:
            pass



@DS.command(aliases=["open"])
async def open_position(ctx, side, symbol, price, stopl, takep):
    global pos_list
    print(price)
    position = Position.new_position(side, symbol.replace("PERP", ""), float(price), float(stopl), float(takep))
    pos_list[symbol] = position
    print(pos_list)

@DS.command(aliases=["close"])
async def close_position(ctx, symbol):
    global pos_list
    symbol = symbol.replace("PERP", "")
    Position.close_position(pos_list[symbol])

DS.run(info.discord_token)
k k
  • 3
  • 1
  • Does this answer your question? [Learning asyncio: "coroutine was never awaited" warning error](https://stackoverflow.com/questions/54441424/learning-asyncio-coroutine-was-never-awaited-warning-error) – Hannon qaoud Sep 28 '22 at 14:05
  • Yes and no now my code in function executes but it is waiting function to finish, not move on. I mean code under " position = asyncio.run(Position.new_position(side, symbol.replace("PERP", ""), float(price), float(stopl), float(takep)))" not executes:/ – k k Sep 28 '22 at 14:13

0 Answers0