Mtemi / Bybit-Trading-Bot-Integrated-with-TradingView-Webhook-Alerts

Python based Trading Bot that uses TradingView.com webhook JSON alerts to place orders(buy/sell/close/manage positions/TP/SL/TS etc.) on Bybit.com.
https://crypttops.com
383 stars 107 forks source link

Can't make trades #18

Closed bhsatterwhite1 closed 3 years ago

bhsatterwhite1 commented 3 years ago

I'm still struggling to get this system to post a trade. Not sure what is holding me up.

See code below.

127.0.0.1 - - [20/Jul/2021 01:48:35] "POST /webhook HTTP/1.1" 500 - Data as Literal {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Data Read From File {'type': 'market', 'side': 'buy', 'amount': '0.01', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Updated Data {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} [Alert Received] POST Received/Updated Data: {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Sending: ETHUSD market buy 0.1 Trading Amount: 0.1 Side: buy Type: market Wallet Balance ---------------------------------------------------------- Wallet Balance------------------------------------------------(((( {'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': {'ETH': {'equity': 2.61628957, 'available_balance': 2.61628957, 'used_margin': 0, 'order_margin': 0, 'position_margin': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'wallet_balance': 2.61628957, 'realised_pnl': 0, 'unrealised_pnl': 0, 'cum_realised_pnl': 0, 'given_cash': 0, 'service_cash': 0}}, 'time_now': '1626763770.165515', 'rate_limit_status': 118, 'rate_limit_reset_ms': 1626763770160, 'rate_limit': 120} 2.61628957 Place Order Amount Int Amount Percentage 0.001 Wallet Balance 2.61628957 Leverage ---------------------------------------------------------- { "ret_code": 0, "ret_msg": "ok", "ext_code": "", "result": { "BTCUSD": { "leverage": 100 }, "ETHUSD": { "leverage": 25 }, "EOSUSD": { "leverage": 50 }, "XRPUSD": { "leverage": 50 } }, "ext_info": null, "time_now": "1626763770.485808", "rate_limit_status": 110, "rate_limit_reset_ms": 1626763770496, "rate_limit": 120 } Leverage Value 25 Position ---------------------------------------------------------- Position Result Value [{'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 1, 'symbol': 'BTCUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 0, 'leverage': '100', 'effective_leverage': '100', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.0115075,"sv2c":0.0114925}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 3872611797, 'position_seq': 0, 'created_at': '2021-07-19T13:40:14.567438091Z', 'updated_at': '2021-07-20T06:35:17.255468085Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 21, 'symbol': 'EOSUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 0, 'leverage': '50', 'effective_leverage': '50', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.021515,"sv2c":0.021485}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 1252426809, 'position_seq': 0, 'created_at': '2021-07-17T14:27:36.008753308Z', 'updated_at': '2021-07-20T06:35:17.253319064Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 11, 'symbol': 'ETHUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': True, 'auto_add_margin': 0, 'leverage': '25', 'effective_leverage': '25', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.04153,"sv2c":0.04147}', 'order_margin': '0', 'wallet_balance': '2.61628957', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 2590142356, 'position_seq': 0, 'created_at': '2021-07-17T14:27:35.996668921Z', 'updated_at': '2021-07-20T06:35:17.253213024Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 31, 'symbol': 'XRPUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 1, 'leverage': '50', 'effective_leverage': '50', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.021515,"sv2c":0.021485}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 1399427815, 'position_seq': 0, 'created_at': '2021-06-20T20:57:35.653496369Z', 'updated_at': '2021-07-20T06:35:17.253218425Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}] Position Result None Position Take Profit 0 Position Stop Loss 0 Order ID ---------------------------------------------------------- Example Order ID 0 { "ret_code": 0, "ret_msg": "OK", "ext_code": "", "ext_info": "", "result": [ { "symbol": "ETHUSD", "bid_price": "1757.6", "ask_price": "1757.7", "last_price": "1757.60", "last_tick_direction": "PlusTick", "prev_price_24h": "1904.40", "price_24h_pcnt": "-0.077084", "high_price_24h": "1917.45", "low_price_24h": "1720.05", "prev_price_1h": "1748.40", "price_1h_pcnt": "0.005261", "mark_price": "1757.29", "index_price": "1757.26", "open_interest": 35665590, "open_value": "16039.00", "total_turnover": "213631951.07", "turnover_24h": "139353.46", "total_volume": 112991309973, "volume_24h": 252864449, "funding_rate": "0.0001", "predicted_funding_rate": "0.0001", "next_funding_time": "2021-07-20T08:00:00Z", "countdown_hour": 2, "delivery_fee_rate": "0", "predicted_delivery_price": "", "delivery_time": "" } ], "time_now": "1626763776.214818" } Tickers [{'symbol': 'ETHUSD', 'bid_price': '1757.6', 'ask_price': '1757.7', 'last_price': '1757.60', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1904.40', 'price_24h_pcnt': '-0.077084', 'high_price_24h': '1917.45', 'low_price_24h': '1720.05', 'prev_price_1h': '1748.40', 'price_1h_pcnt': '0.005261', 'mark_price': '1757.29', 'index_price': '1757.26', 'open_interest': 35665590, 'open_value': '16039.00', 'total_turnover': '213631951.07', 'turnover_24h': '139353.46', 'total_volume': 112991309973, 'volume_24h': 252864449, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2021-07-20T08:00:00Z', 'countdown_hour': 2, 'delivery_fee_rate': '0', 'predicted_delivery_price': '', 'delivery_time': ''}] Last Price 1757.60 Position Entry Price 0 Bid Price Value Two 0.0 Order Amount 4598.390548232 Order Amount in USD 4.5983905482319996 Active Order Real Time ---------------------------------------------------------- Real time active order result {'current_page': 1, 'last_page': 1, 'data': []} Real time active order result Intented Results {'current_page': 1, 'last_page': 1, 'data': []} [2021-07-20 01:49:36,714] ERROR in app: Exception on /webhook [POST] Traceback (most recent call last): File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app response = self.full_dispatch_request() File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request rv = self.dispatch_request() File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/home/pi/Bot/webhook-bot.py", line 50, in webhook send_order(datas) File "/home/pi/Bot/actions.py", line 1368, in send_order if position_side == 'None' and data['stopLoss']=='None' and data['trailingStop']=='None': KeyError: 'stopLoss' 127.0.0.1 - - [20/Jul/2021 01:49:36] "POST /webhook HTTP/1.1" 500 - Data as Literal {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Data Read From File {'type': 'market', 'side': 'buy', 'amount': '0.01', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Updated Data {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} [Alert Received] POST Received/Updated Data: {'type': 'market', 'side': 'buy', 'amount': '0.1', 'symbol': 'ETHUSD', 'key': 'bf110da8a214b863838dad8c7e1f0d4609a356a8460803dc6e14a2c1'} Sending: ETHUSD market buy 0.1 Trading Amount: 0.1 Side: buy Type: market Wallet Balance ---------------------------------------------------------- Wallet Balance------------------------------------------------(((( {'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': {'ETH': {'equity': 2.61628957, 'available_balance': 2.61628957, 'used_margin': 0, 'order_margin': 0, 'position_margin': 0, 'occ_closing_fee': 0, 'occ_funding_fee': 0, 'wallet_balance': 2.61628957, 'realised_pnl': 0, 'unrealised_pnl': 0, 'cum_realised_pnl': 0, 'given_cash': 0, 'service_cash': 0}}, 'time_now': '1626763829.989898', 'rate_limit_status': 119, 'rate_limit_reset_ms': 1626763829982, 'rate_limit': 120} 2.61628957 Place Order Amount Int Amount Percentage 0.001 Wallet Balance 2.61628957 Leverage ---------------------------------------------------------- { "ret_code": 0, "ret_msg": "ok", "ext_code": "", "result": { "BTCUSD": { "leverage": 100 }, "ETHUSD": { "leverage": 25 }, "EOSUSD": { "leverage": 50 }, "XRPUSD": { "leverage": 50 } }, "ext_info": null, "time_now": "1626763830.306705", "rate_limit_status": 111, "rate_limit_reset_ms": 1626763830317, "rate_limit": 120 } Leverage Value 25 Position ---------------------------------------------------------- Position Result Value [{'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 1, 'symbol': 'BTCUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 0, 'leverage': '100', 'effective_leverage': '100', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.0115075,"sv2c":0.0114925}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 3872611797, 'position_seq': 0, 'created_at': '2021-07-19T13:40:14.567438091Z', 'updated_at': '2021-07-20T06:35:17.255468085Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 21, 'symbol': 'EOSUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 0, 'leverage': '50', 'effective_leverage': '50', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.021515,"sv2c":0.021485}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 1252426809, 'position_seq': 0, 'created_at': '2021-07-17T14:27:36.008753308Z', 'updated_at': '2021-07-20T06:35:17.253319064Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 11, 'symbol': 'ETHUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': True, 'auto_add_margin': 0, 'leverage': '25', 'effective_leverage': '25', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.04153,"sv2c":0.04147}', 'order_margin': '0', 'wallet_balance': '2.61628957', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 2590142356, 'position_seq': 0, 'created_at': '2021-07-17T14:27:35.996668921Z', 'updated_at': '2021-07-20T06:35:17.253213024Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}, {'data': {'id': 0, 'position_idx': 0, 'mode': 0, 'user_id': 234826, 'risk_id': 31, 'symbol': 'XRPUSD', 'side': 'None', 'size': 0, 'position_value': '0', 'entry_price': '0', 'is_isolated': False, 'auto_add_margin': 1, 'leverage': '50', 'effective_leverage': '50', 'position_margin': '0', 'liq_price': '0', 'bust_price': '0', 'occ_closing_fee': '0', 'occ_funding_fee': '0', 'take_profit': '0', 'stop_loss': '0', 'trailing_stop': '0', 'position_status': 'Normal', 'deleverage_indicator': 0, 'oc_calc_data': '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.021515,"sv2c":0.021485}', 'order_margin': '0', 'wallet_balance': '0', 'realised_pnl': '0', 'unrealised_pnl': 0, 'cum_realised_pnl': '0', 'cross_seq': 1399427815, 'position_seq': 0, 'created_at': '2021-06-20T20:57:35.653496369Z', 'updated_at': '2021-07-20T06:35:17.253218425Z', 'tp_sl_mode': 'Full'}, 'is_valid': True}] Position Result None Position Take Profit 0 Position Stop Loss 0 Order ID ---------------------------------------------------------- Example Order ID 0 { "ret_code": 0, "ret_msg": "OK", "ext_code": "", "ext_info": "", "result": [ { "symbol": "ETHUSD", "bid_price": "1757.35", "ask_price": "1757.4", "last_price": "1757.35", "last_tick_direction": "MinusTick", "prev_price_24h": "1904.40", "price_24h_pcnt": "-0.077215", "high_price_24h": "1917.45", "low_price_24h": "1720.05", "prev_price_1h": "1748.40", "price_1h_pcnt": "0.005118", "mark_price": "1758.74", "index_price": "1758.71", "open_interest": 35625756, "open_value": "16039.00", "total_turnover": "213631976.71", "turnover_24h": "139354.76", "total_volume": 112991355037, "volume_24h": 252863173, "funding_rate": "0.0001", "predicted_funding_rate": "0.0001", "next_funding_time": "2021-07-20T08:00:00Z", "countdown_hour": 2, "delivery_fee_rate": "0", "predicted_delivery_price": "", "delivery_time": "" } ], "time_now": "1626763836.031523" } Tickers [{'symbol': 'ETHUSD', 'bid_price': '1757.35', 'ask_price': '1757.4', 'last_price': '1757.35', 'last_tick_direction': 'MinusTick', 'prev_price_24h': '1904.40', 'price_24h_pcnt': '-0.077215', 'high_price_24h': '1917.45', 'low_price_24h': '1720.05', 'prev_price_1h': '1748.40', 'price_1h_pcnt': '0.005118', 'mark_price': '1758.74', 'index_price': '1758.71', 'open_interest': 35625756, 'open_value': '16039.00', 'total_turnover': '213631976.71', 'turnover_24h': '139354.76', 'total_volume': 112991355037, 'volume_24h': 252863173, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2021-07-20T08:00:00Z', 'countdown_hour': 2, 'delivery_fee_rate': '0', 'predicted_delivery_price': '', 'delivery_time': ''}] Last Price 1757.35 Position Entry Price 0 Bid Price Value Two 0.0 Order Amount 4597.7364758395 Order Amount in USD 4.5977364758395 Active Order Real Time ---------------------------------------------------------- Real time active order result {'current_page': 1, 'last_page': 1, 'data': []} Real time active order result Intented Results {'current_page': 1, 'last_page': 1, 'data': []} [2021-07-20 01:50:36,535] ERROR in app: Exception on /webhook [POST] Traceback (most recent call last): File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app response = self.full_dispatch_request() File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request rv = self.dispatch_request() File "/home/pi/.local/share/virtualenvs/Bot-QKn0Xe1W/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/home/pi/Bot/webhook-bot.py", line 50, in webhook send_order(datas) File "/home/pi/Bot/actions.py", line 1368, in send_order if position_side == 'None' and data['stopLoss']=='None' and data['trailingStop']=='None': KeyError: 'stopLoss' 127.0.0.1 - - [20/Jul/2021 01:50:36] "POST /webhook HTTP/1.1" 500 -

CapsLockeD commented 3 years ago

I was having similar issues.

  1. Make sure you are reading from the webhook not the file. I had to comment out line 44 and uncomment line 42 in webhook-bot.py. *
  2. Find the if statement you are hitting in actions.py and copy it to the first one just to test. I also had so much problems with it that I added print statements to every if statement I thought I was hitting.
crypttops commented 3 years ago

You are placing order of 4.5977364758395 when you balance is 2.6. I don't know if it can place such an, order. Also look at your JSON, the amount parameter is 0.1???? Put something like 97 and that is a percentage of your balance.

joseeyw commented 3 years ago

The payload from your trading view has a issue stopLoss is missing. Make sure you include the stopLoss as a parameter.

bhsatterwhite1 commented 3 years ago

I made all of those changes you guys recommended. I'm still not getting trades and I'm still getting that 500 at the bottom of the code there, which I suspect is the reason the API isnt taking the trade. It says something there about trailing stops but I dont want to use trailing stops. Should I put trailing stop as 0 in my Json file?

joseeyw commented 3 years ago

Okay if you dont want to use the trailingStop or any other parameter, include it on the payload but set it to None for example... "trailingStop":"None"

bhsatterwhite1 commented 3 years ago

I got rid of all of the errors but nothing seems to change the order amount. Its always 5000 something whether I change the quantity in the json or webhook. Nothing changes this. I'm soo close!!

bhsatterwhite1 commented 3 years ago

Here is the code.

(Bot-QKn0Xe1W) pi@pi-desktop:~/Bot$ python3 webhook-bot.py

Mtemi commented 3 years ago

check for current update

Mtemi commented 3 years ago

Fixed as per current updates. Follow feedback on Discord