Quantcast
Channel: Active questions tagged feed - Stack Overflow
Viewing all articles
Browse latest Browse all 539

Python websocket feed save to postgres database is not working

$
0
0

So I am trying to use in python 3.8 a websocket feed from coinbase pro to save data into postgres database.I was following this guide:

Guide I was following (page 19)

I used part of the code logic and modified it from page 19.However, the code works it reads the information correctly and outputs it as you can see in the picture.

The picture of the code and output

The issue is that the code runs without errors, but it is not saving data to database like it is ignoring this code cur.execute(rep,vars). And that is where I am confused, why?

I would appreciate any feedback on this and suggestions on other guides that would be better to use.

import asynciofrom copra.websocket import Channel, Clientfrom datetime import datetimeimport sysimport psycopg2import sshtunnelsshtunnel.SSH_TIMEOUT = 5.0sshtunnel.TUNNEL_TIMEOUT = 5.0with sshtunnel.SSHTunnelForwarder(('ssh.pythonanywhere.com'),    ssh_username='username', ssh_password='password',    remote_bind_address=('id.postgres.pythonanywhere-services.com', port),    local_bind_address=('127.0.0.1', 50000)    ) as tunnel:    conn = psycopg2.connect(        user='super', password='password',        host='127.0.0.1', port=tunnel.local_bind_port,        database='request',    )    cur = conn.cursor()    class Ticker(Client):        def on_message(self, message):            if message['type'] == 'ticker' and 'time' in message:                print(message)                spread = float(message['best_ask']) - float(message['best_bid'])                rep = u"""INSERT INTO BTCEUR (pair,time,price,size,side,best_ask,best_bid,spread) VALUES (%s,%s,%s,%s,%s,%s,%s,%s);"""                vars = (message['product_id'], message['time'], message['price'], message['last_size'], message['side'], message['best_ask'], message['best_bid'], spread)                return cur.execute(rep,vars)    product_id = 'BTC-EUR'    loop = asyncio.get_event_loop()    channel = Channel('ticker', product_id)    ticker = Ticker(loop, channel)    try:        loop.run_forever()    except KeyboardInterrupt:        loop.run_until_complete(ticker.close())        loop.close()    conn.commit()    cur.close()

Viewing all articles
Browse latest Browse all 539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>