1
+ # mrk_syf_stream.json
2
+ # {"type":"quote","symbol":"MRK","bid":125.62,"bidsz":2,"bidexch":"M","biddate":"1713545563000","ask":125.64,"asksz":1,"askexch":"Q","askdate":"1713545563000"},
3
+ from stock_trader import *
4
+
5
+
6
+ print (pd .json_normalize (tradier_exchange_list ).T );
7
+
8
+
9
+ #
10
+ # Convert json file -> Central Limit Order Book
11
+ #
12
+
13
+ def create_clob (file , symbol = '' , exchange = '' ):
14
+ stream_data = pd .read_json (file ); # print(f'STREAM DATA\n{stream_data.head(10)}\n');
15
+
16
+ stream_data = stream_data .query ("type == 'quote'" )[['bidexch' , 'biddate' , 'bidsz' , 'bid' , 'ask' , 'asksz' , 'askdate' , 'askexch' , 'symbol' ]];
17
+
18
+
19
+
20
+ # if symbol is not None:
21
+ if len (symbol ) > 0 :
22
+ stream_data = stream_data .query ("symbol == @symbol" );
23
+
24
+ # if exchange is not None:
25
+ if len (exchange ) > 0 :
26
+ stream_data = stream_data .query ("bidexch == @exchange" );
27
+ stream_data = stream_data .query ("askexch == @exchange" );
28
+
29
+ #
30
+ # Market stream data returns unix timestamps to the millisecond -> Convert to datetime
31
+ #
32
+
33
+ stream_data ['biddate' ] = pd .to_datetime (stream_data ['biddate' ], unit = 'ms' );
34
+ stream_data ['askdate' ] = pd .to_datetime (stream_data ['askdate' ], unit = 'ms' );
35
+
36
+ return stream_data ;
37
+
38
+
39
+
40
+ #
41
+ # Plot change in bid/ask through trading day
42
+ #
43
+
44
+ def plot_bid_ask (clob ):
45
+ plt .figure (figsize = (14 ,7 ));
46
+ plt .plot (clob ['biddate' ], clob ['bid' ], label = 'Bid Price' );
47
+ plt .plot (clob ['askdate' ], clob ['ask' ], label = 'Ask Price' );
48
+ plt .fill_between (clob ['biddate' ], clob ['bid' ], clob ['ask' ], color = 'gray' , alpha = 3 / 10 , label = 'Spread' );
49
+ plt .title (f"{ list (clob ['symbol' ])[0 ]} Bid/Ask" );
50
+ plt .xlabel ('Time' ); plt .ylabel ('Price' );
51
+ plt .legend ();
52
+ plt .show ();
53
+
54
+
55
+ df_clob = create_clob (file = 'market_streams/market_stream_april22.json' , symbol = 'ICE' , exchange = 'N' ); print (f'DF CLOB\n { df_clob } ' );
0 commit comments