diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..795a139 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:latest + +WORKDIR /usr/src/app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD [ "python", "./app.py" ] diff --git a/app.py b/app.py new file mode 100644 index 0000000..2f943c6 --- /dev/null +++ b/app.py @@ -0,0 +1,62 @@ +import os +from threading import Thread +import json +from rethinkdb import RethinkDB +from pybit.unified_trading import WebSocket +from time import sleep + +r = RethinkDB() + +# RethinkDB settings +RDB_HOST = os.environ.get('RETHINKDB_URL') +RDB_PORT = os.environ.get('RETHINKDB_PORT') +DB_NAME = "finfree" +TABLE_NAME = 'conditions' + +# Initialize RethinkDB connection +rdb_conn = r.connect(host=RDB_HOST, port=RDB_PORT, db=DB_NAME) + +# WebSocket setup +WS_URL = "wss://stream.bybit.com/v5/public/linear" +ws = WebSocket( + testnet=True, + channel_type="linear", +) + +subscribed = set() + +def fetch_symbols(): + try: + symbols = r.table(TABLE_NAME).pluck('symbol').run(rdb_conn) + return list(set([symbol['symbol'] for symbol in symbols])) + except Exception as e: + print(f"Error fetching symbols: {e}") + return [] + +def handle_message(message): + print(message) + +def watch_symbols_table(): + feed = r.table(TABLE_NAME).changes().run(rdb_conn) + for change in feed: + print(f"Change detected: {change}") + if change['new_val'] and not change['old_val']: # New symbol added + symbol = change['new_val']['symbol'] + subscribe_to_symbol(symbol) + +def subscribe_to_symbol(symbol): + if symbol not in subscribed: + subscribed.add(symbol) + ws.ticker_stream( + symbol=symbol, + callback=handle_message + ) + +if __name__ == "__main__": + for symbol in fetch_symbols(): + subscribe_to_symbol(symbol) + + watch_symbols_table() + + while True: + sleep(1) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d9efde2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +rethinkdb +pybit \ No newline at end of file