Skip to content

Commit 766c61c

Browse files
committed
Adds observation (orderbook,trades) and reward
1 parent 4ece45e commit 766c61c

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

RL/observation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
from stats_columns import stats_columns
5+
6+
7+
class Observation:
8+
def __init__(self, client):
9+
self.client = client
10+
11+
def new(self):
12+
# stats = __ob_stats__(client)
13+
orderbook = self.__ob_orderbook__()
14+
trades = self.__ob_trades__()
15+
return np.concatenate([trades, orderbook])
16+
17+
def __ob_orderbook__(self):
18+
raw_orderbook = self.client.orderbook(
19+
params={"symbol": "XBTUSD", "depth": "20"})
20+
df_orderbook = pd.DataFrame(raw_orderbook).drop(
21+
["symbol", "id"], axis=1)
22+
df_orderbook = df_orderbook.sort_values(by="side", ascending=False)
23+
return df_orderbook[["price", "size"]].values.flatten()
24+
25+
def __ob_stats__(self):
26+
return pd.DataFrame(self.client.instruments(
27+
params={"symbol": "XBTUSD",
28+
"columns": ",".join(stats_columns())}))
29+
30+
def __ob_trades__(self):
31+
df = pd.DataFrame(self.client.trades_bucketed(
32+
params={"binSize": "1m",
33+
"symbol": "XBTUSD",
34+
"count": "2",
35+
"reverse": "true",
36+
"partial": "true"}))
37+
df = df.select_dtypes(exclude=["object"])
38+
self.last_vwap = df["vwap"][0]
39+
return df.apply(lambda x: x * np.array([0.75, 0.25])).sum().values

RL/reward.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pandas as pd
2+
3+
4+
class Reward:
5+
def __init__(self, client):
6+
self.client = client
7+
8+
def __last_o_book__(self, o_book, side=None):
9+
if side:
10+
o_book = o_book[o_book["side"] == side]
11+
return o_book[o_book["timestamp"] == o_book["timestamp"].max()]
12+
13+
def get(self, prev_act, vwap):
14+
orders = self.client.get_orders(params={"symbol": "XBTUSD",
15+
"reverse": "true",
16+
"count": "10"})
17+
18+
df_orders = pd.DataFrame(orders)[["cumQty",
19+
"price",
20+
"side",
21+
"timestamp"]]
22+
df_orders["timestamp"] = pd.to_datetime(df_orders["timestamp"])
23+
24+
self.last_order = self.__last_o_book__(df_orders)
25+
self.last_buy = self.__last_o_book__(df_orders, "Buy").values[0]
26+
self.last_sell = self.__last_o_book__(df_orders, "Sell").values[0]
27+
28+
if prev_act != self.last_order["side"].values[0] or prev_act == "Hold":
29+
return 0
30+
elif prev_act == "Buy":
31+
return vwap - self.last_buy
32+
elif prev_act == "Sell":
33+
return self.last_sell - self.last_buy

RL/stats_columns.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
def stats_columns():
2+
return ['optionStrikePcnt',
3+
'optionStrikeRound',
4+
'optionStrikePrice',
5+
'optionMultiplier',
6+
'maxOrderQty',
7+
'maxPrice',
8+
'lotSize',
9+
'tickSize',
10+
'multiplier',
11+
'underlyingToPositionMultiplier',
12+
'underlyingToSettleMultiplier',
13+
'quoteToSettleMultiplier',
14+
'isQuanto',
15+
'isInverse',
16+
'initMargin',
17+
'maintMargin',
18+
'riskLimit',
19+
'riskStep',
20+
'limit',
21+
'capped',
22+
'taxed',
23+
'deleverage',
24+
'makerFee',
25+
'takerFee',
26+
'settlementFee',
27+
'insuranceFee',
28+
'fundingRate',
29+
'indicativeFundingRate',
30+
'prevClosePrice',
31+
'limitDownPrice',
32+
'limitUpPrice',
33+
'bankruptLimitDownPrice',
34+
'bankruptLimitUpPrice',
35+
'prevTotalVolume',
36+
'totalVolume',
37+
'volume',
38+
'volume24h',
39+
'prevTotalTurnover',
40+
'totalTurnover',
41+
'turnover',
42+
'turnover24h',
43+
'homeNotional24h',
44+
'foreignNotional24h',
45+
'prevPrice24h',
46+
'vwap',
47+
'highPrice',
48+
'lowPrice',
49+
'lastPrice',
50+
'lastPriceProtected',
51+
'lastChangePcnt',
52+
'bidPrice',
53+
'midPrice',
54+
'askPrice',
55+
'impactBidPrice',
56+
'impactMidPrice',
57+
'impactAskPrice',
58+
'hasLiquidity',
59+
'openInterest',
60+
'openValue',
61+
'fairBasisRate',
62+
'fairBasis',
63+
'fairPrice',
64+
'markPrice',
65+
'indicativeTaxRate',
66+
'indicativeSettlePrice',
67+
'optionUnderlyingPrice',
68+
'settledPrice']

0 commit comments

Comments
 (0)