-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
111 lines (87 loc) · 3.22 KB
/
analysis.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
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
'''
This module acquires bots data from the database and perform numerous analysis.
'''
from config import db_bots, db_market
import sys
from datetime import datetime
# Function counts buys and sells in
def countBuysAndSells(query={}):
buy, sell = 0, 0
for bot in db_bots.find(query):
ops = bot['history']['operations']
for op in ops:
if op[1] > 0:
buy += 1
else:
sell += 1
return buy, sell
# Function returns a list of bots with evaluation above bar.
def filterByEvalutaion(bar=100000, query={}):
res = []
for bot in db_bots.find(query):
hist = bot['history']['evaluation']
if(len(hist) < 2):
pass
else:
lastHist = hist[-1][1]
if lastHist > bar:
res.append(bot)
return res
def printBuysAndSellsCounts(query={}):
for bot in db_bots.find(query):
buy, sell = 0, 0
ops = bot['history']['operations']
for op in ops:
if op[1] > 0:
buy += 1
else:
sell += 1
print(bot['id'], bot['name'], ' : ', buy, 'buys and', sell, 'sells')
def printGoodAndBad(query={}):
good, bad, better = 0, 0, 0
for bot in db_bots.find(query):
hist = bot['history']['evaluation']
if(len(hist) < 2):
pass
else:
lastHist = hist[-1][1]
secLastHist = hist[-2][1]
good += 1 if lastHist > 100000 else 0
bad += 1 if lastHist < 100000 else 0
better += 1 if lastHist > secLastHist else 0
print("among 2000 robots,", good, 'are winning,',
bad, 'are losing', better, 'are improving')
def printHist(query={}):
bot = db_bots.find_one(query)
recents, recents_str = bot['history']['operations'], ''
[timestamp, shares, symb, opPrice, cash] = [0, 0, 0, 0, 0]
for each in recents:
[timestamp, shares, symb, opPrice, cash] = each
date = datetime.fromtimestamp(timestamp/1000).date()
shares = int(shares)
avgCost = (bot['portfolio'][each[2]]['avgcost'])
gain = -int(shares)*round((opPrice-avgCost),3)
if shares < 0:
recents_str += '\n' + \
"{4} \033[91m Selled \033[00m {0} shares of {1} at {2}. Gain: {3}".format(
-int(shares), symb, opPrice, gain, date)
else:
recents_str += '\n' + \
"{3} \033[92m Buyed \033[00m {0} shares of {1} at {2}.".format(
int(shares), symb, opPrice, date)
recents_str += '\r'
print("ID: {0} Name: {1}{2} Value: {3}".format(
bot['id'] % 100000, bot['name'], (30-len(bot['name'])) *
' ', bot['history']['evaluation'][-1][1]
))
print(recents_str)
def winners_stats(bar=100000):
winners = filterByEvalutaion(bar=bar)
avgGainRate = sum([winner['history']['evaluation'][-1][1]/100000 for winner in winners]) / len(winners)
print("Average Gain Rate for the winners: {0}".format(avgGainRate))
res = filterByEvalutaion(bar=108000)
for bot in res:
print(bot['chars'], bot['history']['evaluation'][-1][-1])
printBuysAndSellsCounts(query={'_id': bot['_id']})
printGoodAndBad()
winners_stats(bar=108000)