Skip to content

Instantly share code, notes, and snippets.

@HHIR
Last active January 22, 2022 12:25
Show Gist options
  • Save HHIR/a78627b13517dc514fc4bd04d313b93f to your computer and use it in GitHub Desktop.
Save HHIR/a78627b13517dc514fc4bd04d313b93f to your computer and use it in GitHub Desktop.
stream Unicorn data to OHLC format
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
import os
import time
from unicorn_fy.unicorn_fy import UnicornFy
import talib
import pandas as pd
import pandas_ta as pta
import numpy as np
import os
ubwa = BinanceWebSocketApiManager(exchange="binance.com")
ubwa.create_stream(['kline_1m'], ['bnbbtc'])
closes, highs, lows, opens, data, time_stamp = [], [], [], [], [], []
# closes = np.array(closes)
index1 = 0
while True:
oldest_data_from_stream_buffer = ubwa.pop_stream_data_from_stream_buffer()
closes, highs, lows, openes, time_stampes = [], [], [], [], []
if oldest_data_from_stream_buffer:
unicorn_fied_stream_data = UnicornFy.binance_com_websocket(oldest_data_from_stream_buffer)
# print (unicorn_fied_stream_data)
try:
index1 = index1 + 1
time_stamp = float(unicorn_fied_stream_data['event_time']) #CLOSE PRICE
# print (time_stamp)
openx = float(unicorn_fied_stream_data['kline']['open_price']) #CLOSE PRICE
close = float(unicorn_fied_stream_data['kline']['close_price']) #CLOSE PRICE
high = float(unicorn_fied_stream_data['kline']['high_price']) #HIGH PRICE
low = float(unicorn_fied_stream_data['kline']['low_price']) #LOW PRICE
# time.sleep(1)
QW = [[ time_stamp, openx, close, high, low]]
df = pd.DataFrame(QW)
# df = QW
df.columns = ['date', 'Open', 'High', 'Low', 'Close']
format = '%Y-%m-%d %H:%M:%S'
df['date'] = pd.to_datetime(df['date'], format=format)
df = df.set_index(pd.DatetimeIndex(df['date']))
df["Open"] = pd.to_numeric(df["Open"],errors='coerce')
df["High"] = pd.to_numeric(df["High"],errors='coerce')
df["Low"] = pd.to_numeric(df["Low"],errors='coerce')
df["Close"] = pd.to_numeric(df["Close"],errors='coerce')
print (df)
time.sleep(1)
except:
print("Something went wrong")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment