robswc / tradingview-webhooks-bot

a framework 🏗 for trading with tradingview webhooks!
http://tvwb.robswc.me
GNU General Public License v3.0
619 stars 185 forks source link

What am I doing wrong here, I have followed all steps correctly #15

Open Man-ingo opened 4 years ago

Man-ingo commented 4 years ago

ualenvs/trading-hnTPjx37/bin/activate (trading) ubuntu@ip-172-31-34-546:~/tradingview/trading$ python webhook-bot.py

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "webhook-bot.py", line 35, in webhook send_order(data) File "/home/ubuntu/tradingview/trading/actions.py", line 49, in send_order order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price'])) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1079, in create_order response = self.privatePostOrder(self.extend(request, params)) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 435, in inner return entry(_self, **inner_kwargs) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 458, in request return self.fetch2(path, api, method, params, headers, body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 454, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 551, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1154, in handle_errors raise BadRequest(feedback) ccxt.base.errors.BadRequest: bitmex {"error":{"message":"'price' must be a number.","name":"HTTPError"}} 127.0.0.1 - - [08/Mar/2020 17:30:04] "POST /webhook HTTP/1.1" 500 - [Alert Received] POST Received: {'type': 'market', 'side': 'sell', 'amount': '10', 'symbol': 'BTC/USD', 'price': 'None', 'key': '292f4a2921fa2153eb13862c051448f5b4e87caf04ba0287999f68bc'} Sending: BTC/USD market sell 10 None [2020-03-08 17:30:11,478] ERROR in app: Exception on /webhook [POST] Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 539, in fetch response.raise_for_status() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.bitmex.com/api/v1/order

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "webhook-bot.py", line 35, in webhook send_order(data) File "/home/ubuntu/tradingview/trading/actions.py", line 49, in send_order order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price'])) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1079, in create_order response = self.privatePostOrder(self.extend(request, params)) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 435, in inner return entry(_self, **inner_kwargs) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 458, in request return self.fetch2(path, api, method, params, headers, body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 454, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 551, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1154, in handle_errors raise BadRequest(feedback) ccxt.base.errors.BadRequest: bitmex {"error":{"message":"'price' must be a number.","name":"HTTPError"}} 127.0.0.1 - - [08/Mar/2020 17:30:11] "POST /webhook HTTP/1.1" 500 - [Alert Received] POST Received: {'type': 'market', 'side': 'sell', 'amount': '10', 'symbol': 'BTC/USD', 'price': 'None', 'key': '292f4a2921fa2153eb13862c051448f5b4e87caf04ba0287999f68bc'} Sending: BTC/USD market sell 10 None [2020-03-08 17:30:18,889] ERROR in app: Exception on /webhook [POST] Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 539, in fetch response.raise_for_status() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.bitmex.com/api/v1/order

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "webhook-bot.py", line 35, in webhook send_order(data) File "/home/ubuntu/tradingview/trading/actions.py", line 49, in send_order order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price'])) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1079, in create_order response = self.privatePostOrder(self.extend(request, params)) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 435, in inner return entry(_self, **inner_kwargs) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 458, in request return self.fetch2(path, api, method, params, headers, body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 454, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 551, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1154, in handle_errors raise BadRequest(feedback) ccxt.base.errors.BadRequest: bitmex {"error":{"message":"'price' must be a number.","name":"HTTPError"}} 127.0.0.1 - - [08/Mar/2020 17:30:18] "POST /webhook HTTP/1.1" 500 - [Alert Received] POST Received: {'type': 'market', 'side': 'sell', 'amount': '10', 'symbol': 'BTC/USD', 'price': 'None', 'key': '292f4a2921fa2153eb13862c051448f5b4e87caf04ba0287999f68bc'} Sending: BTC/USD market sell 10 None [2020-03-08 17:30:26,319] ERROR in app: Exception on /webhook [POST] Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 539, in fetch response.raise_for_status() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.bitmex.com/api/v1/order

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "webhook-bot.py", line 35, in webhook send_order(data) File "/home/ubuntu/tradingview/trading/actions.py", line 49, in send_order order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price'])) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1079, in create_order response = self.privatePostOrder(self.extend(request, params)) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 435, in inner return entry(_self, **inner_kwargs) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 458, in request return self.fetch2(path, api, method, params, headers, body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 454, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/base/exchange.py", line 551, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "/home/ubuntu/.local/share/virtualenvs/trading-hnTPjx37/lib/python3.7/site-packages/ccxt/bitmex.py", line 1154, in handle_errors raise BadRequest(feedback) ccxt.base.errors.BadRequest: bitmex {"error":{"message":"'price' must be a number.","name":"HTTPError"}} 127.0.0.1 - - [08/Mar/2020 17:30:26] "POST /webhook HTTP/1.1" 500 -

 ngrok command shell below

ngrok by @inconshreveable (Ctrl+C to quit)

Session Status online
Account Enock (Plan: Free)
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://60fb567d.ngrok.io -> http://localhost:5000 Forwarding https://60fb567d.ngrok.io -> http://localhost:5000

Connections ttl opn rt1 rt5 p50 p90
10 0 0.01 0.01 2.03 2.08

HTTP Requests


POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR
POST /webhook 500 INTERNAL SERVER ERROR

robswc commented 4 years ago

hey, not sure entirely, I'll look at it again when I get the chance but this line:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.bitmex.com/api/v1/order

are you able to place an order on Bitmex through the API without the bot?

What does your action function look like? (omitting key and info)

Man-ingo commented 4 years ago
  1. Are you able to place an order on Bitmex through the API without the bot? Yes I do even I have been using their bot market maker it worked fine.

  2. What does your action function look like? (omitting key and info):

import ccxt import ast

def parse_webhook(webhook_data):

"""
This function takes the string from tradingview and turns it into a python dict.
:param webhook_data: POST data from tradingview, as a string.
:return: Dictionary version of string.
"""

data = ast.literal_eval(webhook_data)
return data

def calc_price(given_price):

"""
Will use this function to calculate the price for limit orders.
:return: calculated limit price
"""

if given_price == None:
    price = given_price
else:
    price = given_price
return price

def send_order(data):

"""
This function sends the order to the exchange using ccxt.
:param data: python dict, with keys as the API parameters.
:return: the response from the exchange.
"""

# Replace kraken with your exchange of choice.
exchange = ccxt.bitmex({
    # Inset your API key and secrets for exchange in question.
    'apiKey': '-removed key',
    'secret': 'removed secret',
    'enableRateLimit': True,
})

# Send the order to the exchange, using the values from the tradingview alert.
print('Sending:', data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']))
order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']))
# This is the last step, the response from the exchange will tell us if it made it and what errors pop up if not.
print('Exchange Response:', order)
Man-ingo commented 4 years ago

I am running on EC2 Amazon AWS

itsXactlY commented 4 years ago

EDIT: Fixed my Market order stuff. Just had to remove calc_price(data['price'] from actions.py

Same problem here, all setup so far, doing a "limit" order works fine on bitmex testnet - no issues can give buy/sell oders just fine.

But as soon i want to use "market" orders, im getting this:

Request: POST https://testnet.bitmex.com/api/v1/order {'Content-Type': 'application/json', 'api-key': 'onzChTx9UPQE-_GIdGaTGCTI', 'api-expires': '1592325731', 'api-signature': '', 'Accept-Encoding': 'gzip, deflate'} {"symbol":"XBTUSD","side":"Buy","orderQty":"10","ordType":"Market","price":"9435"}

Response: POST https://testnet.bitmex.com/api/v1/order 400 {'Date': 'Tue, 16 Jun 2020 16:42:06 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '56', 'Connection': 'keep-alive', 'Set-Cookie': '=; Expires=Tue, 23 Jun 2020 16:42:06 GMT; Path=/, =/l+/+/6uz+/+//=; Expires=Tue, 23 Jun 2020 16:42:06 GMT; Path=/; SameSite=None; Secure, AWSALB=+/3IKN7CaQS8W3/Ok7c3J9fUxLM7z/ZON+/aNACN2sEWWYY8bsAdWUixm+cYO3THx; Expires=Tue, 23 Jun 2020 16:42:06 GMT; Path=/, AWSALBCORS=+///+/+; Expires=Tue, 23 Jun 2020 16:42:06 GMT; Path=/; SameSite=None; Secure', 'X-RateLimit-Limit': '60', 'X-RateLimit-Remaining': '59', 'X-RateLimit-Reset': '1592325727', 'X-Powered-By': 'Profit', 'ETag': 'W/"38-VA4Y4az2ZWIZFNiNZx+BrZha5y4"', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'} {"error":{"message":"Invalid price","name":"HTTPError"}}
[2020-06-16 18:42:06,145] ERROR in app: Exception on /webhook [POST]
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/base/exchange.py", line 539, in fetch
    response.raise_for_status()
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://testnet.bitmex.com/api/v1/order

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "webhook-bot.py", line 38, in webhook
    send_order(data)
  File "/root/tradingview-webhooks-bot/actions.py", line 55, in send_order
    order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']))
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/bitmex.py", line 1079, in create_order
    response = self.privatePostOrder(self.extend(request, params))
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/base/exchange.py", line 435, in inner
    return entry(_self, **inner_kwargs)
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/base/exchange.py", line 458, in request
    return self.fetch2(path, api, method, params, headers, body)
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/base/exchange.py", line 454, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/base/exchange.py", line 551, in fetch
    self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "/root/.local/share/virtualenvs/tradingview-webhooks-bot-kiAJApaF/lib/python3.7/site-packages/ccxt/bitmex.py", line 1148, in handle_errors
    raise exact[message](feedback)
ccxt.base.errors.InvalidOrder: bitmex {"error":{"message":"Invalid price","name":"HTTPError"}}
127.0.0.1 - - [16/Jun/2020 18:42:06] "POST /webhook HTTP/1.1" 500 -

Testing with: curl -H 'Content-Type: text/plain; charset=utf-8' -d '{"type": "market", "side": "buy", "amount": "10", "symbol": "BTC/USD", "price": "9435", "key": "mykey"}' -X POST http://xyz.ngrok.io/webhook