-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
84 lines (75 loc) · 2.89 KB
/
main.py
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
# A script that generates ahr999 index data into a JSON file.
from pycoingecko import CoinGeckoAPI
from scipy import stats
import math,time,json,datetime
now = round(time.time())
nybf = now - 24*60*60*365*10 #10 year before
cg = CoinGeckoAPI()
scdata = cg.get_coin_market_chart_range_by_id('bitcoin','usd',str(nybf),str(now)) # source data
scdict = {}
for d in scdata['prices']:
datadate = datetime.datetime.utcfromtimestamp(d[0]/1000).strftime('%Y-%m-%d')
scdict[datadate]=d[1] #存两百天价格和日期的dict
avgut = scdata['prices'][199][0]/1000 #200天 start unix time
avguts= avgut #用来被减掉200天,计算平均数
avghm = datetime.datetime.utcfromtimestamp(avgut).strftime('%Y-%m-%d') #200天 human time
avghms = avghm
thps = scdict[avghm]
pcls = []
for i in range(200):
if avghm in scdict:
pcls.append(scdict[avghm])
avgut -= 24*60*60
avghm = datetime.datetime.utcfromtimestamp(avgut).strftime('%Y-%m-%d')
thavg = round(stats.gmean(pcls),2) #200天平均值
day = ((avguts- 1230940800) / (24 * 60 * 60) )
logprice = round(10 ** (5.84 * math.log(day, 10) - 17.01),2)
ahr999 = round((thps/thavg)*(thps/logprice),4)
avguts += 24*60*60
avgut = avguts
pcls = []
print("日期:",avghms)
print("200日平均值:",thavg)
print('拟合价格:',logprice)
print('天数:',day)
print("当天价格:",thps)
print("ahr999:",ahr999)
alltimedict={}
while avguts <= now:
avghm = datetime.datetime.utcfromtimestamp(avgut).strftime('%Y-%m-%d') #200天 human time
avghms = avghm
if avghm in scdict:
thps = scdict[avghm]
for i in range(200):
if avghm in scdict:
pcls.append(scdict[avghm])
else:
pass#print("not exist:",avguts)
avgut -= 24*60*60
avghm = datetime.datetime.utcfromtimestamp(avgut).strftime('%Y-%m-%d') #200天 human time
thavg = round(stats.gmean(pcls),2)
day = ((avguts- 1230940800) / (24 * 60 * 60) )
logprice = round(10 ** (5.84 * math.log(day, 10) - 17.01),2)
ahr999 = round((thps/thavg)*(thps/logprice),4)
# print("日期:",avghms)
# print("ahr999:",ahr999)
# print("200日平均值:",thavg)
# print('拟合价格:',logprice)
# print("当天价格:",round(thps,2))
# print("\n")
dt = datetime.datetime.strptime(avghms, "%Y-%m-%d")
dt = datetime.datetime.timestamp(dt)
dt *= 1000
print(avghms, dt)
alltimedict[dt]=ahr999
avguts+=24*60*60
avgut = avguts
pcls=[]
ls1 = list(alltimedict)
ls2 = list(alltimedict.values())
ls4 = [] # A list that contains a lot of lists (e.g [[timestamp,price],[timestamp,price],...])
for i in range(len(alltimedict)):
ls3 = [ls1[i],ls2[i]]
ls4.append(ls3)
with open("data.json", "w+") as f: # Check out the data.json file to see the result
f.write(str(ls4))