Click here to Skip to main content
15,848,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code itself:
Python
import redis, sqlite3, time
from flask import Flask, render_template, request, g, current_app

app = Flask(__name__)

r = redis.Redis(host='localhost', port=5000, db=0)

conn = sqlite3.connect('trade.db')
cursor = conn.cursor()
cursor.execute("""
    CREATE TABLE IF NOT EXISTS signals (
        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, 
        ticker,
        order_action,
        order_contracts,
        order_price
    )
""")
conn.commit()

def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect('trade.db')
        g.db.row_factory = sqlite3.Row

    return g.db

@app.route('/', methods=["GET"])
def dashboard():
    db = get_db()
    cursor = db.cursor()
    cursor.execute("""
        SELECT * FROM signals
    """)
    signals = cursor.fetchall()

    return render_template('dashboard.html', signals=signals)

@app.route("/webhook", methods=["GET", "POST"])
def webhook():
    data = request.data

    if data:
        r.publish('tradingview', data)

        data_dict = request.json

        db = get_db()
        cursor = db.cursor()
        cursor.execute("""
            INSERT INTO signals (ticker, order_action, order_contracts, order_price) 
            VALUES (?, ?, ?, ?)
        """, (data_dict['ticker'], 
                data_dict['strategy']['order_action'], 
                data_dict['strategy']['order_contracts'],
                data_dict['strategy']['order_price']))

        db.commit()

        return data

    return {
        "code": "success"
    }

if __name__ == "__main__":
    app.run()
    
        

The error the console gives me:
127.0.0.1 - - [01/Sep/2021 17:50:09] code 400, message Bad request syntax ('*3')
127.0.0.1 - - [01/Sep/2021 17:50:09] "*3" HTTPStatus.BAD_REQUEST -
[2021-09-01 17:50:09,963] ERROR in app: Exception on /webhook [POST]
Traceback (most recent call last):
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Santiago\Desktop\API TWS\TradingView IB Integration\tradingview integration ib\webappog.py", line 44, in webhook
    r.publish('tradingview', data)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 3098, in publish
    return self.execute_command('PUBLISH', channel, message)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 901, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 915, in parse_response
    response = connection.read_response()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 739, in read_response
    response = self._parser.read_response()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 324, in read_response
    raw = self._buffer.readline()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 256, in readline
    self._read_from_socket()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 201, in _read_from_socket
    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.
127.0.0.1 - - [01/Sep/2021 17:50:09] "POST /webhook HTTP/1.1" 500 -
127.0.0.1 - - [01/Sep/2021 17:50:17] code 400, message Bad request syntax ('*3')
127.0.0.1 - - [01/Sep/2021 17:50:17] "*3" HTTPStatus.BAD_REQUEST -
[2021-09-01 17:50:17,671] ERROR in app: Exception on /webhook [POST]
Traceback (most recent call last):
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Santiago\Desktop\API TWS\TradingView IB Integration\tradingview integration ib\webappog.py", line 44, in webhook
    r.publish('tradingview', data)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 3098, in publish
    return self.execute_command('PUBLISH', channel, message)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 901, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\client.py", line 915, in parse_response
    response = connection.read_response()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 739, in read_response
    response = self._parser.read_response()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 324, in read_response
    raw = self._buffer.readline()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 256, in readline
    self._read_from_socket()
  File "C:\Users\Santiago\anaconda3\lib\site-packages\redis\connection.py", line 201, in _read_from_socket
    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.
127.0.0.1 - - [01/Sep/2021 17:50:17] "POST /webhook HTTP/1.1" 500 -


What I have tried:

I have tried changing the "Get" and "Post" methods but still get the errors.
Posted
Updated 1-Sep-21 23:31pm
v2
Comments
Richard MacCutchan 1-Sep-21 12:15pm    
The message is telling you that the text in the POST message is not a valid request. So use the debugger to find out exactly what is being sent.
Scieoner 1-Sep-21 14:12pm    
How do I use the debugger?? I mean what should I get off of it please help
Scieoner 1-Sep-21 14:16pm    
Is it something related to this?

def route(self, rule, **options):
"""A decorator that is used to register a view function for a
given URL rule. This does the same thing as :meth:`add_url_rule`
but is intended for decorator usage::

@app.route('/')
def index():
return 'Hello World'

For more information refer to :ref:`url-route-registrations`.

:param rule: the URL rule as string
:param endpoint: the endpoint for the registered URL rule. Flask
itself assumes the name of the view function as
endpoint
:param options: the options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object. A change
to Werkzeug is handling of method options. methods
is a list of methods this rule should be limited
to (``GET``, ``POST`` etc.). By default a rule
just listens for ``GET`` (and implicitly ``HEAD``).
Starting with Flask 0.6, ``OPTIONS`` is implicitly
added and handled by the standard request handling.
Richard MacCutchan 2-Sep-21 4:36am    
Possibly, but it is impossible to know without capturing more information, which is why I suggested using the debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900