lth-elm/TradingView-Webhook-Trading-Bot

Bybit api issue

MnyCaek opened this issue · 5 comments

I have been trying to work around this problem for a few days now. Basically at this point i think ther's an issue in the code?
When i try to send the following API alert:

{
"ticker" : "BTCUSD",
"exchange" : "BYBIT",
"time" : "2022-04-10T01:44:17Z",
"action" : "Buy",
"price" : 44153.23,
"message" : "entry",
"passphrase" : "123",
"subaccount": "MYBYBITACCOUNT"
}

i get the following response:
image

i have also tried removing everything but the message, passphrase, subaccount and ticker and exchange but i still had the same issue. Seems like then if i went in the code itself and edited the open_size = position['size'] to a fixed number it then worked? Anyway, it seems to me like ther's really an issue in orders = order(data) in app.py.

Did something change in the bybit's api that needs changing?

I'd appreciate if anyone could push me in the right direction.

Can you log or print r and type of r right after line 238 and same thing with position between line 243 and 244. Run and tell me what it returns please, maybe something has changed in bybit's api because position['size'] should return a number but apparently it's not the case here so we should print the whole dictionary and see what's actually inside for debugging.

That's exactly what i whent and did yesterday after further testing. The position reports as:

Retrieve positions
{'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 499713, 'risk_id': 1, 'symbol': 'BTCUSD', 'side': 'Buy', 'size': 3, 'position_value': '0.00007489', 'entry_price': '40058.75283749', 'is_isolated': True, 'auto_add_margin': 0, 'leverage': '1', 'effective_leverage': '1', 'position_margin': '0.00007498', 'liq_price': '20080.5', 'bust_price': '20029.5', 'occ_closing_fee': '0.00000009', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '39000', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 3, 'oc_calc_data': '{"blq":0,"slq":3,"slv":"0.00006666","bmp":0,"smp":45004.5005,"bv2c":1.0018,"sv2c":1.0006}', 'order_margin': '0', 'wallet_balance': '0.48751317', 'realised_pnl': '-0.00000006', 'unrealised_pnl': -6.8e-07, 'cum_realised_pnl': '-0.01248683', 'cross_seq': 4984212004, 'position_seq': 0, 'created_at': '2022-04-02T19:55:06.723844426Z', 'updated_at': '2022-04-12T18:43:33.110089552Z', 'tp_sl_mode': 'Full'}

So basically we'r trying to return string but size in the dictionary is integer? That would be my frist guess.

So then if i try and do:

for position in r['result'].values():
            open_size = position['size']

i get the error:
TypeError: 'int' object is not subscriptable

That is how far i got yesterday.

I have fixed it.
Tested just a couple orders for long and short.

What i did was in the example of the exit position:

        for position in r['result']:
            open_size = r['result'].get('size')
            if open_size > 0:
                open_side = r['result'].get('side')
                close_side = 'Sell' if open_side == 'Buy' else 'Buy'

so basically get the size and open side with .get to get the value out of the dictionary.
Idk if this is the correct way but it does close the order/s and enters the new position correctly if we'r calling for 'entry' or it just closes the positions if we'r calling for it to 'exit'.

Glad you made it work ! I think you can write position.get('key') instead of r['result'].get('key') for more legibility and therefore I believe you have to make the change everywhere in the code where a key in a dictionary is called.

But this is still quite surprising because position['key'] and position.get('key') both do the same thing if the key exists, I really wonder what the problem is. What version of python are you using to run that code ?

I'm using Python 3.10.4. It's weird it was not working for me but its okay as its fixed.