forked from brandonrobertz/BitcoinTradingAlgorithmToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogtick.py
More file actions
186 lines (166 loc) · 5.73 KB
/
Copy pathlogtick.py
File metadata and controls
186 lines (166 loc) · 5.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
import json
import re
import time
import sys
import eventlet
from eventlet.green import urllib2
import csv
import datetime
# function to fetch our URL
def fetch(url):
while(True):
try:
r = urllib2.urlopen(url, timeout=10)
code = r.getcode()
if( code == 200):
return url, r.read()
else:
print "Error", code
except Exception as e:
print "WOAH!!!", url, e
# verify gox json API response
# DEFINE PRICE AS last price
def gox_tick_last( body):
try:
j = json.loads( body)
except ValueError, e:
print "BTC-e error", e, url
if j["result"] == "success":
last = float( j["data"]["last"]["value"])
buy = float( j["data"]["buy"]["value"])
sell = float( j["data"]["sell"]["value"])
# convert gox timestamp to regular
t = float( j["data"]["now"])/1000000
bs = get_buy_sell( last, buy, sell)
return { "price":last, "type":bs, "time":t}
else:
print "Error", j
# verify/process btc-e ticker json API response
# this defines price as last
def btce_tick_last(body):
try:
j = json.loads( body)
except ValueError, e:
print "BTC-e error", e, url
buy = float( j["ticker"]["buy"])
buy = float( j["ticker"]["buy"])
sell = float( j["ticker"]["sell"])
last = float( j["ticker"]["last"])
vol = float( j["ticker"]["vol"])
bs = get_buy_sell( last, buy, sell)
t = float( j["ticker"]["server_time"])
bs = get_buy_sell( last,
float(j["ticker"]["buy"]),
float(j["ticker"]["sell"]))
return { "price":last, "type":bs, "time":t, "vol":vol}
# figure out whether it's a buy or sell based on three prices
def get_buy_sell(last, buy, sell):
if( last == buy):
return "buy"
elif( last == sell):
return "sell"
else:
return "?"
# compress a string using BZ2 and Base64 ... remove newlines
def compress(json_str):
return re.sub("\n", "", json_str.encode("bz2").encode("base64"))
# undo a Base64'd BZ2 string
def decompress(encoded_str):
return encoded_str.decode("base64").decode("bz2")
# get BTC-e litecoin market depth
def ltc_depth( body):
return { "info":compress(body), "time":time.time()}
# get Mt. Gox last Bitcoin trade value
def get_gox_btc_last( body):
j = gox_tick( body)
def ts2dt( timestamp):
return datetime.datetime.fromtimestamp( timestamp).strftime("%Y-%m-%d %H:%M:%S")
# get current market activity as a csv line
def process_responses( url_body_list):
# run through responses and parse accordingly
processed = {}
# THIS IS AN UGLY HACK ... but we're using a global to store the volume
# and then tack it onto the end of our log to maintain compatability
ltc_vol = 0
for resp in url_body_list:
u = resp["url"]
b = resp["body"]
# gox ticker
if( u == "http://data.mtgox.com/api/2/BTCUSD/money/ticker"):
g = gox_tick_last( b)
processed["gox_price"] = g["price"]
processed["gox_type"] = g["type"]
processed["gox_time"] = ts2dt( g["time"])
# btc-e ltc depth
elif( u == "http://btc-e.com/api/2/ltc_usd/depth"):
g = ltc_depth( b)
processed["ltc_depth"] = g["info"]
processed["ltc_depth_time"] = ts2dt( g["time"])
# btc-e LAST (not avg)
elif( u == "http://btc-e.com/api/2/ltc_usd/ticker"):
g = btce_tick_last( b)
processed["ltc_usd_price"] = g["price"]
processed["ltc_type"] = g["type"]
processed["ltc_time"] = ts2dt( g["time"])
processed["ltc_24h_vol"] = g["vol"]
elif( u == "http://btc-e.com/api/2/btc_usd/ticker"):
g = btce_tick_last( b)
processed["ltc_btc_price"] = g["price"]
processed["ltc_btc_type"] = g["type"]
processed["ltc_btc_time"] = ts2dt( g["time"])
elif( u == "http://btc-e.com/api/2/ltc_btc/ticker"):
g = btce_tick_last( b)
processed["btc_usd_price"] = g["price"]
processed["btc_type"] = g["type"]
processed["btc_time"] = ts2dt( g["time"])
# return our list
return processed
def usage():
print "USAGE: %s FILENAME\n" % sys.argv[0]
sys.exit()
def process_args():
if len(sys.argv) != 2:
usage()
return sys.argv[1]
if __name__ == "__main__":
# process argv or die
logname = str(time.time())+".csv" #process_args()
# urls and set up
urls = ["http://data.mtgox.com/api/2/BTCUSD/money/ticker",
"http://btc-e.com/api/2/btc_usd/ticker",
"http://btc-e.com/api/2/ltc_btc/ticker",
"http://btc-e.com/api/2/ltc_usd/ticker",
"http://btc-e.com/api/2/ltc_usd/depth"]
pool = eventlet.GreenPool()
# prepare log
f = open(logname, "wb")
w = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
w.writerow( [ "gox_last", "gox_buysell", "gox_time",
"btc_usd_last", "btc_usd_buysell", "btc_usd_time",
"ltc_btc_last", "ltc_btc_buysell", "ltc_btc_time",
"ltc_last", "ltc_buysell", "ltc_time",
"ltc_depth", "ltc_depth_time", "ltc_24h_volume" ])
try:
while(True):
# get URLs in parallel
responses = []
for url, body in pool.imap(fetch, urls):
responses.append({"url":url, "body":body})
#process our responses
processed = process_responses( responses)
p = processed
# insert timestamp and write to log
print datetime.datetime.fromtimestamp(
time.time()).strftime('%Y-%m-%d %H:%M:%S')
w.writerow( [ p["gox_price"], p["gox_type"], p["gox_time"],
p["btc_usd_price"], p["btc_type"], p["btc_time"],
p["ltc_btc_price"], p["ltc_btc_type"], p["ltc_btc_time"],
p["ltc_usd_price"], p["ltc_type"], p["ltc_time"],
p["ltc_depth"], p["ltc_depth_time"],
p["ltc_24h_vol"] ])
f.flush()
time.sleep(3)
except KeyboardInterrupt:
f.close()
print "Caught Ctl-Z/C exiting!\nL8r."