analyse bitcoin price from CSV data based on blockchain info data not

 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
"""
analyse bitcoin price from CSV data based on blockchain.info's data (not 100% accurate, especially before 2010)
"""
import urllib2
turl = "https://blockchain.info/charts/market-price?showDataPoints=false&timespan=all&show_header=true&daysAverageString=1&scale=0&format=csv&address="
s = urllib2.urlopen(turl).read()
with open('btcdata.csv','w') as f:
f.write(s)
def getd():
with open('btcdata.csv','r') as f:
lines = f.readlines()
return lines
def avgc(li):
s = sum(li[:])
r = s/len(li)
return r
d = getd()
count = 0
sump = 0
firstday = ""
oldyear = ""
avglist = list()
lastavg = None
print '** BTC price information **'
print 'year .. end of year .. avg for year .. increase'
for x in d[:]:
x = x.replace('\n','')
date,price = x.split(',')
price = float(price)
nt = date.split(' ')[0]
if price == 0.0: continue
d,m,y = nt.split('/')
avglist.append(price)
if y != oldyear:
avgy = avgc(avglist)
incr = 0
if lastavg != None:
incr = avgy/lastavg
lastavg = avgy
else:
lastavg = avgy
print y,' ',price,' ',round(avgy,3),' ',round(incr,3),' ',avgy
#print y,' ',round(incr,3)
oldyear = y
avglist = list()
if firstday == "": firstday = date
sump += price
count+=1
avg = round(sump/float(count),2)
print 'total days: ',count
print 'avg: ',avg
print 'first day: ',firstday
print 'last day: ',date