<jupyter_start><jupyter_text>bid ask spreads and mid prices(n levels)<jupyter_code>for i in range(10):
    ind = str(i)
    df['spreed_'+ind] = df['ask_p_'+ind] - df['bid_p_'+ind]
    df['mid_p'+ind] = (df['ask_p_'+ind] + df['bid_p_'+ind]) / 2
df.head()<jupyter_output><empty_output><jupyter_text>price difference<jupyter_code>for i in range(9):
    ind = str(i+1)
    df['ask_p_diff_'+ind+'_0'] = df['ask_p_'+ind] - df['ask_p_0']
    df['bid_p_diff_'+ind+'_0'] = df['bid_p_'+ind] - df['bid_p_0']
    ind1 = str(i)
    df['ask_p_diff_'+ind+'_'+ind1] = df['ask_p_'+ind] - df['ask_p_'+ind1]
    df['bid_p_diff_'+ind+'_'+ind1] = df['bid_p_'+ind] - df['bid_p_'+ind1]
df.head()<jupyter_output><empty_output><jupyter_text>mean prices and volumes<jupyter_code>l1, l2, l3, l4 = [], [], [], []
for i in range(10):
    ind = str(i)
    l1.append('ask_p_'+ind)
    l2.append('bid_p_'+ind)
    l3.append('ask_vol_'+ind)
    l4.append('bid_vol_'+ind)
df['ask_p_mean'] = df[l1].mean(axis=1)
df['bid_mean'] = df[l2].mean(axis=1)
df['ask_vol_mean'] = df[l3].mean(axis=1)
df['bid_vol_mean'] = df[l4].mean(axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>accumulated difference<jupyter_code>l = []
template = 'spreed_{0}'
for i in range(10):
    l.append(template.format(i))
df['accum_spreed'] = df[l].sum(axis=1)
df.head()
for i in range(10):
    ind = str(i)
    df['spreed_vol_'+ind] = df['ask_vol_'+ind] - df['bid_vol_'+ind]
l = []
template = 'spreed_vol_{0}'
for i in range(10):
    l.append(template.format(i))
df['accum_spreed_vol'] = df[l].sum(axis=1)
df.head()
import numpy as np
df['y'] = np.where(df['mid_p0'].shift(1) > df['mid_p0'] , 1, 0)
df['y'] = np.where(df['mid_p0'].shift(1) < df['mid_p0'] , -1, df['y'])
df['y'].value_counts()<jupyter_output><empty_output><jupyter_text>Web-socket downloading data<jupyter_code>import swagger_client 
import os

access_key, secret_key =  os.environ["BITMEX_ACCESS_KEY"], os.environ["BITMEX_ACCESS_SECRET_KEY"]

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
from swagger_client.bitmex_websocket import BitMEXWebsocket

from bitmex_websocket import BitMEXWebsocket
access_key, secret_key =  os.environ["BITMEX_ACCESS_KEY"], os.environ["BITMEX_ACCESS_SECRET_KEY"]
url = "https://www.bitmex.com/api/v1"
ws = BitMEXWebsocket(endpoint=url, symbol="XBTUSD", api_key=access_key, api_secret=secret_key)
import csv
def transform(ob, size=10):
    if len(ob) != size*2:
        raise Exception('Wrong size of order book')
    asks = ob[:size][::-1]
    bids = ob[size:]
    return asks, bids
for i in range(24*60*60/5):
    with open('xbtusd_ob.csv', 'wa') as f:
        writer = csv.writer(f)
        
import time
cnt = 0
import json
with open('xbtusd_ob.csv', 'a') as f:
    for i in range(17280):
        ob = ws.market_depth()
        asks = filter(lambda item: item['side'] == 'Sell', ob)
        asks = sorted(asks, key=lambda a: a['price'])[:10]
        bids = filter(lambda item: item['side'] == 'Buy', ob)
        bids = sorted(bids, key=lambda b: b['price'], reverse=True)[:10]
        row = {}
        for index, val in enumerate(asks):
            ind = str(index)
            row['ask_p_' + ind] = val['price']
            row['ask_vol_' + ind] = val['size']
        for index, val in enumerate(bids):
            ind = str(index)
            row['bid_p_' + ind] = val['price']
            row['bid_vol_' + ind] = val['size']
        json.dump(row, f)
        f.write('\n')
        time.sleep(5)
import json
import pandas as pd
%matplotlib inline
obs = []
with open('xbtusd_ob5.json', 'r') as f:
    for line in f:
        obs.append(json.loads(line))
f.close()

df = pd.DataFrame(obs)
df.describe()
df.to_csv('xbtusdt_orderbook5.csv')
df[['ask_p_0', 'bid_p_0']].plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18309492847854356, 'fraction_numerical': 0.020286085825747726, 'mean_word_length': 4.319502074688796, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '366648', 'license': 'MIT', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1574, 'n_words': 285, 'path': 'Untitled1.ipynb', 'repo_name': 'magicalne/cryptocurrency', 'size': '3845'}