Skip to content

Commit 2c15137

Browse files
committed
Create python script to construct central limit order book from Tradier market stream data
1 parent b3cb88e commit 2c15137

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

limit_order_book.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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}');

market_txt_to_json.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
#
4+
# Fetch command line argument (e.g. the name of the text file with market stream data to convert into json)
5+
#
6+
7+
input_file="$1"
8+
9+
output_file="${input_file%.txt}.json"
10+
11+
12+
#
13+
# Count input file lines
14+
#
15+
16+
total_lines=$(wc -l < "$input_file")
17+
18+
19+
#
20+
# Begin JSON File Formatting
21+
#
22+
23+
24+
echo "[" > "$output_file"; # json files begin with closed interval square bracket
25+
26+
27+
#
28+
# Read the input file line-by-line
29+
#
30+
31+
current_line=0
32+
while IFS= read -r line; do
33+
((current_line++))
34+
35+
#
36+
# Check for final line of file
37+
#
38+
39+
if [ "$current_line" -eq "$total_lines" ]; then
40+
# Last line: append without a comma
41+
echo "$line" >> "$output_file"
42+
else
43+
# Not the last line: append with a comma
44+
echo "$line," >> "$output_file"
45+
fi
46+
done < "$input_file"
47+
48+
49+
echo "]" >> "$output_file"; # json files end with closed interval square bracket
50+
51+
echo "Conversion completed. The JSON file is saved as $output_file"
52+

0 commit comments

Comments
 (0)