-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataimport.py
More file actions
61 lines (51 loc) · 2.02 KB
/
Copy pathdataimport.py
File metadata and controls
61 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import Tuple, Dict, List
import pandas as pd
def read_file(file_path: str) -> Tuple[List[str], int, pd.DataFrame]:
"""
Create a dataframe of all orders.
:param file_path: File path of CSV containing market data.
"""
df = pd.read_csv(file_path)
products = df["product"].unique().tolist()
ticks = df["timestamp"].nunique()
return products, ticks, df
def extract_orders(df: pd.DataFrame, tick: int, product: str) -> Dict[str, Dict[float, int]]:
"""
Create an orderbook for the specified tick from dataframe.
:param df: Dataframe containing market data.
:param tick: Tick to create orderbook for.
:param product: Product to create orderbook for.
"""
row = df[df["timestamp"] == tick*100]
row = row[row["product"] == product]
bid_orders = {} #price:quantity
ask_orders = {} #price:quantity
for i in range(1, 4):
price = row[f"bid_price_{i}"].iloc[0]
bid_orders[price] = row[f"bid_volume_{i}"].iloc[0]
for i in range(1, 4):
price = row[f"ask_price_{i}"].iloc[0]
ask_orders[price] = row[f"ask_volume_{i}"].iloc[0]
return {"BUY": bid_orders,
"SELL": ask_orders}
def extract_bot_orders(df: pd.DataFrame, tick: int, product: str) -> Dict[str, Dict[float, int]]:
"""
Create an orderbook for the specified tick from bot order dataframe.
:param df: Dataframe containing market data.
:param tick: Tick to create orderbook for.
:param product: Product to create orderbook for.
"""
row = df[df["timestamp"] == tick*100]
row = row[row["product"] == product]
bid_orders = {} # price:quantity
ask_orders = {} # price:quantity
bid_price = row["bid_price_1"].iloc[0]
bid_volume = row["bid_volume_1"].iloc[0]
if bid_volume > 0:
bid_orders[bid_price] = bid_volume
ask_price = row["ask_price_1"].iloc[0]
ask_volume = row["ask_volume_1"].iloc[0]
if ask_volume > 0:
ask_orders[ask_price] = ask_volume
return {"BUY": bid_orders,
"SELL": ask_orders}