-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyseValeurs.py
196 lines (161 loc) · 6.75 KB
/
analyseValeurs.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
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
187
188
189
190
191
192
193
194
195
196
import json
import numpy as np
import pandas as pd
toDo=[[0,"annonces de crash/solde"]]
print "TO DO :",toDo
# lire les donnees du fichier a chaque fois qu'il y a une nouvelle valeur et mettre les dernieres dans des tableaux 5,13 et la derniere dans lastValue
# https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,DASH&tsyms=USD
# Celle la envoi des requetes toutes les 3 sec
# Il faut arriver a faire le tableau suivant ou taper dans un fichier ou l'on prend les x dernieres valeur
if(False):
def printit():
threading.Timer(3.0, printit).start()
f = urllib.request.urlopen("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR")
print(f.read())
account_sid = "AC59592f7f0fb983ee92bb4d0aacfec1e2"
auth_token = "9bf81090628c3206b39c8648eac91e1a"
client = Client(account_sid, auth_token)
json_text = """
[
{"BTC":{"USD":2167.85},"ETH":{"USD":167.88},"DASH":{"USD":102.31}},
{"BTC":{"USD":2253.12},"ETH":{"USD":177.76},"DASH":{"USD":109.17}},
{"BTC":{"USD":2251.47},"ETH":{"USD":177.71},"DASH":{"USD":109.12}},
{"BTC":{"USD":2251.47},"ETH":{"USD":177.71},"DASH":{"USD":109.12}},
{"BTC":{"USD":2251.47},"ETH":{"USD":177.71},"DASH":{"USD":109.12}},
{"BTC":{"USD":2167.85},"ETH":{"USD":167.88},"DASH":{"USD":102.31}},
{"BTC":{"USD":2407.05},"ETH":{"USD":222.67},"DASH":{"USD":134.09}},
{"BTC":{"USD":2405.47},"ETH":{"USD":222.86},"DASH":{"USD":134.09}},
{"BTC":{"USD":2404.51},"ETH":{"USD":222.83},"DASH":{"USD":134.09}},
{"BTC":{"USD":2404.23},"ETH":{"USD":222.86},"DASH":{"USD":134.09}},
{"BTC":{"USD":2404.22},"ETH":{"USD":222.99},"DASH":{"USD":134.09}},
{"BTC":{"USD":2403.5},"ETH":{"USD":222.85},"DASH":{"USD":134.09}},
{"BTC":{"USD":2403.79},"ETH":{"USD":222.94},"DASH":{"USD":134.09}},
{"BTC":{"USD":2403.74},"ETH":{"USD":222.8},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.18},"ETH":{"USD":222.82},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.37},"ETH":{"USD":222.82},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.37},"ETH":{"USD":222.82},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.37},"ETH":{"USD":223.09},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.37},"ETH":{"USD":223.09},"DASH":{"USD":133.96}},
{"BTC":{"USD":2404.15},"ETH":{"USD":223.09},"DASH":{"USD":133.96}},
{"BTC":{"USD":2404.15},"ETH":{"USD":223.09},"DASH":{"USD":133.96}},
{"BTC":{"USD":2404.31},"ETH":{"USD":223.02},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.76},"ETH":{"USD":222.98},"DASH":{"USD":133.96}},
{"BTC":{"USD":2403.76},"ETH":{"USD":222.98},"DASH":{"USD":133.96}},
{"BTC":{"USD":2405.12},"ETH":{"USD":222.86},"DASH":{"USD":133.96}},
{"BTC":{"USD":2404.76},"ETH":{"USD":222.91},"DASH":{"USD":133.99}},
{"BTC":{"USD":2404.76},"ETH":{"USD":222.91},"DASH":{"USD":133.99}}
]
"""
a = json.loads(json_text)
# je recupere les valeurs du texte
values = [(each["BTC"].get("USD"), each["ETH"].get("USD"), each["DASH"].get("USD")) for each in a]
print "each : ", each
fiveLast = np.array(values[-5:])
lastValue = values[-1]
df = pd.DataFrame(values, columns=['BTC', 'ETH', 'DASH'])
# on calcule les moyennes sma, ema
# SMA:
def calculateSMA(prices):
return prices.tail(5).mean()
MeanBTC = calculateSMA(df['BTC'])
MeanETH = calculateSMA(df['ETH'])
MeanDASH = calculateSMA(df['DASH'])
fiveLastMean = np.array([["",'BTC','ETH','DASH'],
['Mean',MeanBTC,MeanETH,MeanDASH]])
# EMA: {Close - EMA(previous day)} x multiplier + EMA(previous day).
# I have to store those values
# If I have the time I can manage it by indexing on it.
# we take the closing prices for the first 22 days ## Why the first and not the last ?
# closingPricesBTC = df['BTC'].iloc[1:22].mean()
closingPricesETH = df['ETH'].iloc[-1:22].mean()
closingPricesDASH = df['DASH'].iloc[-1:22].mean()
# taking the following day s (day 23) closing price multiplied by k, then multiply the previous day s moving average by (1-k) and add the two.
def calculateEMA(todayPrice,numberOfDays,EMAYesterday):
k = float(2)/(numberOfDays+1)
return todayPrice * k + EMAYesterday *(1-k)
def calculateAllEMA(df):
ema=[]
for i in range(0, len(df)-24):
EMAYesterday = df.iloc[1+i:22+i].mean()
k = float(2)/(22+1)
# print(str(i)+" "+str(len(ema)))
ema.append(df.iloc[23 + i]*k+EMAYesterday*(1-k))
return ema[-1]
MeanExpBTC = calculateAllEMA(df['BTC'])
MeanExpETH = calculateAllEMA(df['ETH'])
MeanExpDASH = calculateAllEMA(df['DASH'])
MeanExp = np.array([["",'BTC','ETH','DASH'],
['Mean',MeanExpBTC,MeanExpETH,MeanExpDASH]])
#print "emaBTC : "
#print emaBTC
#print emaBTC["ETH"]
# on lance une alerte d'achat ou de vente en cas de pb
results = np.array([["",'BTC','ETH','DASH'],
['Action',"","",""]])
print "i: "
for i in range(0,len(lastValue)+1):
print i
for i in range(1,len(lastValue)+1) :
if ((float(fiveLastMean[1,i]) > (float(lastValue[i-1]))) and (float (MeanExp[1,i]) >float(lastValue[i-1]))):
# On dirait que and ne marche pas
#if (float(fiveLastMean[1,i]) > float(lastValue[i-1])):
# if (float (MeanExp[1,i]) >float(lastValue[i-1])):
print "(fiveLastMean[1,i]) : ",float(fiveLastMean[1,i])
print "MeanExp[1,i] : ",MeanExp[1,i]
print "***"
print "(float(lastValue[i-1]))", (float(lastValue[i-1]))
print "sell",fiveLastMean[0,i]
results[1,i]="sell"
elif((float(fiveLastMean[1,i])< (float(lastValue[i-1]))) and (float (MeanExp[1,i])< float(lastValue[i-1]))):
#if(float(fiveLastMean[1,i])< float(lastValue[i-1])):
# if(MeanExp[1,i]< float(lastValue[i-1])):
results[1,i]="buy"
print "---------------------------------------------------------"
print "lastValue : "
print(lastValue)
print "fiveLastMean : "
print(fiveLastMean)
print "MeanExp : "
print(MeanExp)
print "----------"
print "result : "
print results
buyExist = False
sellExist = False
print "---------------------------------------------------------"
j=0
k=0
# Creer deux tableaux vides de string
buyValue =(len(results[1])-1)*[""]
sellValue = (len(results[1])-1)*[""]
# on parcourt la deuxieme ligne
for i in range(1,len(results[1])):
if results[1][i] == "buy":
buyExist = True
# il me faut l'indice a acheter
buyValue[k] = results[0][i]
k=k+1
if results[1][i] == "sell":
sellExist = True
sellValue[j] = results[0][i]
j=j+1
#if ((sellExist != 0) and (buyExist !=0)):
# break
print "Vendre ",sellValue, " contre ",buyValue
# le prochain chantier est de mesurer lequel est le plus prometteur
# Ensuite on va faire un systeme pour prevenir quand un cours s'est casse la gueule
if(False):
# il faut ajouter les parties qu'on echange : ex BTC -> ETH , donc peut-etre attendre
# Envoi de texto
message = client.api.account.messages.create(to="+33620050318",
from_="+33644601266",
body="Vendre "
#,sellValue, " contre ",buyValue
)
# Connection a elasticsearch
es = Elasticsearch(
hosts=['localhost'],
http_auth=('elastic', 'changeme'),
port=9200,
ca_certs=certifi.where()
)