-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombat_nat.py
162 lines (133 loc) · 5.02 KB
/
combat_nat.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
import matplotlib.pyplot as plt
import pandas as pd
import yfinance as yf
import numpy as np
pd.set_option("mode.chained_assignment",None)
print(" +-+-+-+-+ ")
print(" |C|O|D|E|")
print(" +-+-+-+-+ \n")
print(" +-+-+ ")
print(" |B|Y| ")
print(" +-+-+ \n")
print(" +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ")
print(" |O|U|G|I|_|L|E|S|G|_|A|F|A|R|D| ")
print(" +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ \n")
def tranform(matrice): #supprime colonnes inutiles et ajoute specialdate
matrice = pd.DataFrame(matrice)
A = np.random.random()
B = np.random.random()
C = np.random.random()
D = np.random.random()
nombre = int((A*B+C+D*A)*100000)
nom = "save" +str(nombre) + ".csv"
matrice.to_csv(nom)
matrice = pd.read_csv(nom)
matrice["Valeur"] = matrice["Close"]
matrice["specialdate"] = 0
for i in range(0, len(matrice)):
matrice["specialdate"][i] = int(str(matrice["Date"][i][:4]) + str(matrice["Date"][i][5:7]) + str(matrice["Date"][i][8:10]))
for i in range(0, len(matrice)):
matrice["Date"][i] = str(matrice["specialdate"][i])[0:4] + "-" + str(matrice["specialdate"][i])[4:6]
matrice = matrice.drop(["Volume", "Low", "Open", "High", "Adj Close", "Close"], axis=1)
matrice.to_csv(nom)
return matrice
def Firstmonth(matrice): #garde le premier jour du mois
temp = matrice
rejection = []
mois = str(matrice["specialdate"][0])[4:6]
for i in range(1,len(matrice)):
if int(mois) == int(str(matrice["specialdate"][i])[4:6]):
rejection.append(i)
else:
if (int(mois) == 12) and (int(str(matrice["specialdate"][i])[4:6]) == 1):
mois = str("01")
else:
mois = str(matrice["specialdate"][i])[4:6]
temp.drop(rejection,inplace=True)
temp = temp.reset_index(drop=True)
for i in range(0, len(temp)):
temp["specialdate"][i] = str(temp["specialdate"][i])[:6]
return temp
def finmatrice(matrice): #calculation de l'évolution de 100€
temp1 = matrice
temp1["valorisation"] = 0
temp1["valorisation"][0] = 100
temp1["unit"] = 100/temp1["Valeur"][0]
for i in range(1,len(temp1)):
temp1["valorisation"][i] = temp1["unit"][i] * temp1["Valeur"][i]
return temp1
def creer_liste(): #liste des tickers
matrice = []
T = True
while T == True:
in1 = input("quel est le ticker, si fin alors rentrer 007 : ")
if in1 != "007":
matrice.append(in1)
else:
T = False
print("\n")
return matrice
def nomfinal(): #nom final pour le ficher .csv
T = True
while T:
nomfichersave = input("quel est le nom que à donner au ficher csv sous la forme XXX.csv ? ")
format = nomfichersave[len(nomfichersave) - 4:]
if format == '.csv' and not len(nomfichersave) < 5:
T = False
else:
print("Le nom n'est pas correct, il manque le '.csv', ou il est trop court")
print("\n\n")
return nomfichersave
def demandeDate(): #la date pour commencer la bataille
T = True
while T:
int1 = input("entrer la date de depart sous format YYYY-MM-DD : ")
temp1 = (int(int1[0:4]) >= 1850) and (int(int1[0:4]) <= 2023)
temp2 = (int(int1[5:7]) >= 1) and (int(int1[5:7]) <= 12)
temp3 = (int(int1[8:10]) >= 1) and (int(int1[8:10]) <= 31)
if ((temp1 and temp2) and temp3) == True:
T = False
print("La date {0} est correcte".format(int1))
return int1
else:
print("La date insérée est incorrecte")
def telechargement(ticker, date):
matrice = []
if len(ticker) > 1:
for i in range(0,len(ticker)):
temp = yf.download(ticker[i], start=date, interval="1d")
matrice.append(temp)
else:
matrice = yf.download(ticker, start=date, interval="1d")
return matrice
def MyBattle():
liste = creer_liste()
date = demandeDate()
print("\n")
matrice = telechargement(liste, date)
NbTicker = len(matrice)
for i in range(0,NbTicker):
matrice[i] = tranform(matrice[i])
matrice[i] = Firstmonth(matrice[i])
matrice[i] = finmatrice(matrice[i])
print("\n")
nameFile = nomfinal()
Final = pd.DataFrame(columns=liste)
for i in range(0, NbTicker):
Final[liste[i]] = matrice[i]["valorisation"]
Final["Date"]=0
for i in range(0, len(matrice[0])):
Final["Date"][i] = matrice[0]["Date"][i]
print("\n")
column = Final.pop("Date")
Final.insert(0,"Date", column)
PER = []
for i in range(0, len(Final)):
Final["Date"][i]=(float(Final["Date"][i][:4])+float(float(Final["Date"][i][5:])/13))
color = ["b","g","r", "c", "m","k","y"]
for i in range(0, len(liste)):
plot = plt.plot(Final["Date"], Final[liste[i]], c = color[i])
plt.figlegend(liste)
plt.show()
#Final.to_csv(nameFile)
MyBattle()