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
| import os
import requests
import datetime
from datetime import date
from datetime import datetime
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
nbAppleStock = 189
nbMicrosoftStock = 100
AppleInit = 127.84
MicrosoftInit = 228.0599
EURUSDInit = 1.19517400
InitialWalletInvested = (nbAppleStock * AppleInit + nbMicrosoftStock * MicrosoftInit)/EURUSDInit
fees = 0.0047 * InitialWalletInvested
feesApple = 0.0047 * AppleInit*nbAppleStock / EURUSDInit
feesMicrosoft = 0.0047 * MicrosoftInit*nbMicrosoftStock / EURUSDInit
fees = 0
feesApple = 0
feesMicrosoft = 0
url1='https://fr.finance.yahoo.com/quote/AAPL/history/'
url2='https://fr.finance.yahoo.com/quote/MSFT/history/'
url3='https://fr.finance.yahoo.com/quote/EURUSD=X/'
url4='https://finance.yahoo.com/quote/AAPL/history?p=AAPL'
url5='https://finance.yahoo.com/quote/MSFT/history?p=MSFT'
url6='https://finance.yahoo.com/quote/EURUSD%3DX/history?p=EURUSD%3DX'
url7='https://www.boursedirect.fr/fr/marche/nasdaq-ngs-global-select-market/apple-inc-AAPL-USD-XNGS/seance'
url8='https://www.boursedirect.fr/fr/marche/nasdaq-ngs-global-select-market/microsoft-corporation-MSFT-USD-XNGS/seance'
page1 = requests.get(url1)
page2 = requests.get(url2)
page3 = requests.get(url3)
page4 = requests.get(url4)
page5 = requests.get(url5)
page6 = requests.get(url6)
page7 = requests.get(url7)
page8 = requests.get(url8)
soup1 = BeautifulSoup(page1.text, 'html.parser')
soup2 = BeautifulSoup(page2.text, 'html.parser')
soup3 = BeautifulSoup(page3.text, 'html.parser')
soup4 = BeautifulSoup(page4.text, 'html.parser')
soup5 = BeautifulSoup(page5.text, 'html.parser')
soup6 = BeautifulSoup(page6.text, 'html.parser')
soup7 = BeautifulSoup(page7.text, 'html.parser')
soup8 = BeautifulSoup(page8.text, 'html.parser')
#print(soup7.prettify())
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(now.strftime("%H"))
print("Current Time =", current_time)
if (int(now.strftime("%H"))<15) | (int(now.strftime("%H"))>22) :
AppleStockPriceString = soup7.find('tbody',{'class':"bd-streaming-select-value-trades"}).findAll("td")[2:3][0]
AppleStockPriceString = str(AppleStockPriceString)
AppleStockPriceString=AppleStockPriceString.replace("<td>","").replace("</td>","")
MicrosoftStockPriceString = soup8.find('tbody',{'class':"bd-streaming-select-value-trades"}).findAll("td")[2:3][0]
MicrosoftStockPriceString = str(MicrosoftStockPriceString)
MicrosoftStockPriceString=MicrosoftStockPriceString.replace("<td>","").replace("</td>","")
else :
AppleStockPriceString = soup1.find('div',{'class': 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
MicrosoftStockPriceString = soup2.find('div',{'class': 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
EURUSDString = soup3.find('div',{'class': 'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
AppleStockPriceString = AppleStockPriceString.replace(",",".")
MicrosoftStockPriceString = MicrosoftStockPriceString.replace(",",".")
EURUSDString = EURUSDString.replace(",",".")
AppleStockPrice = float(AppleStockPriceString)
MicrosoftStockPrice = float(MicrosoftStockPriceString)
EURUSD = float(EURUSDString)
print("EURUSD: " + EURUSDString)
print("AppleStockPrice: " + str(AppleStockPrice) + "$")
print("MicrosoftStockPrice: " + str(MicrosoftStockPrice) + "$")
EURUSDPerformance = 1/(1 + (EURUSD/EURUSDInit)-1) - 1
print("EURUSD Performance SI: " + str(round(EURUSDPerformance * 100,2)) + "%")
WalletPresentValue = (nbAppleStock * AppleStockPrice + nbMicrosoftStock * MicrosoftStockPrice)/EURUSD
print("Fees: " + str(fees) + "")
Performance =(WalletPresentValue/(InitialWalletInvested + fees) - 1)
print("Overall Performance SI: " + str(round((Performance)*100,2)) + "%")
print("Apple Performance SI: " + str(round((((1 + AppleStockPrice/(AppleInit) - 1))*(1-feesApple/(nbAppleStock*AppleInit))*(1+EURUSDPerformance)-1)*100,2))+ "%")
PNLApple = round(nbAppleStock* AppleInit *((1 +(AppleStockPrice/AppleInit -1))*(1+ EURUSDPerformance))/EURUSDInit - nbAppleStock * AppleInit/EURUSDInit - feesApple, 2)
print("P&L on Apple: " + str(PNLApple) + "")
print("Microsoft Performance SI: " + str(round((((1 + MicrosoftStockPrice/(MicrosoftInit) - 1))*(1-feesMicrosoft/(nbMicrosoftStock*MicrosoftInit))*(1+EURUSDPerformance)-1)*100,2)) + "%")
PNLMicrosoft = round(nbMicrosoftStock* MicrosoftInit *((1 +(MicrosoftStockPrice/MicrosoftInit -1))*(1+ EURUSDPerformance))/EURUSDInit - nbMicrosoftStock * MicrosoftInit/EURUSDInit - feesMicrosoft, 2)
print("P&L on Microsoft: " + str(PNLMicrosoft) + "")
print("P&L: " + str(round(PNLApple + PNLMicrosoft,2)))
print("Invested Wallet Present Value in : " + str(round(WalletPresentValue,2)))
Cash = 11637
print("Total Compte Titres in : " + str(round(Cash + WalletPresentValue,2)))
table = soup4.find('table')
table_rows = table.find_all('tr')
res = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
df1 = pd.DataFrame(res, columns=["Date", "Ouverture", "Élevé ", "Faible ","Clôture*","Cours de clôture ajusté**","Volume"])
df1 = df1.drop(["Ouverture", "Élevé ", "Faible ","Cours de clôture ajusté**","Volume"], axis=1)
df1 = df1.drop_duplicates("Date", keep='first', inplace=False)
df1['Clôture*'] = df1['Clôture*'].str.replace(',','.')
df1["Clôture*"] = pd.to_numeric(df1["Clôture*"], downcast="float")
df1["Date"] = df1["Date"].astype(str)
df1["Date"].describe()
indexNames = df1[ df1['Date'].str.contains("split") ].index
df1.drop(indexNames , inplace=True)
df1['Date'] = df1['Date'].astype('datetime64[ns]')
df1.plot(figsize=(10,2), x ='Date', y='Clôture*', kind = 'line')
plt.title('Apple Stock Prices in $')
plt.xlabel('Dates')
plt.ylabel('Stock Price in $')
plt.show()
df1Wallet = df1[df1["Date"] >= '2020-09-01']
table = soup5.find('table')
table_rows = table.find_all('tr')
res = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
df2 = pd.DataFrame(res, columns=["Date", "Ouverture", "Élevé ", "Faible ","Clôture*","Cours de clôture ajusté**","Volume"])
df2 = df2.drop(["Ouverture", "Élevé ", "Faible ","Cours de clôture ajusté**","Volume"], axis=1)
df2 = df2.drop_duplicates("Date", keep='first', inplace=False)
df2['Clôture*'] = df2['Clôture*'].str.replace(',','.')
df2["Clôture*"] = pd.to_numeric(df2["Clôture*"], downcast="float")
df2["Date"] = df2["Date"].astype(str)
df2["Date"].describe()
indexNames = df2[ df2['Date'].str.contains("split") ].index
df2.drop(indexNames , inplace=True)
df2['Date'] = df2['Date'].astype('datetime64[ns]')
df2.plot(figsize=(10,2), x ='Date', y='Clôture*', kind = 'line')
plt.title('Microsfot Stock Prices in $')
plt.xlabel('Dates')
plt.ylabel('Stock Price in $')
plt.show()
df2Wallet = df2[df2["Date"] >= '2020-09-01']
table = soup6.find('table')
table_rows = table.find_all('tr')
res = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
df3 = pd.DataFrame(res, columns=["Date", "Ouverture", "Élevé ", "Faible ","Clôture*","Cours de clôture ajusté**","Volume"])
df3 = df3.drop(["Ouverture", "Élevé ", "Faible ","Cours de clôture ajusté**","Volume"], axis=1)
df3 = df3.drop_duplicates("Date", keep='first', inplace=False)
df3['Clôture*'] = df3['Clôture*'].str.replace(',','.')
df3['Clôture*'] = df3["Clôture*"].apply (pd.to_numeric, errors='coerce')
df3 = df3.dropna()
df3["Clôture*"] = pd.to_numeric(df3["Clôture*"], downcast="float")
df3["Date"] = df3["Date"].astype(str)
df3["Date"].describe()
df3['Date'] = df3['Date'].astype('datetime64[ns]')
df3.plot(figsize=(10,2), x ='Date', y='Clôture*', kind = 'line')
plt.title('EURUSD Exchange Rate')
plt.xlabel('Dates')
plt.ylabel('EURUSD Exchange Rate')
plt.show()
df3Wallet = df3[df3["Date"] >= '2020-09-01']
Wallet = pd.merge(df1Wallet, df2Wallet, on='Date')
Wallet = pd.merge(Wallet, df3Wallet, on = 'Date')
Wallet['Wallet'] = (Wallet["Clôture*_x"]*nbAppleStock + Wallet["Clôture*_y"]*nbMicrosoftStock)/Wallet["Clôture*"]
Wallet['Performance SI'] = (Wallet['Wallet']-InitialWalletInvested)/InitialWalletInvested
Wallet['Date'] =pd.to_datetime(Wallet.Date)
Wallet = Wallet.drop(["Clôture*_x","Clôture*_y","Clôture*"], axis=1)
Wallet = pd.concat([pd.DataFrame([[pd.to_datetime('2020-08-31'), float(InitialWalletInvested), float(0)]],columns=Wallet.columns),Wallet],ignore_index=True)
Wallet = pd.concat([pd.DataFrame([[pd.to_datetime(date.today()), float(WalletPresentValue), Performance]],columns=Wallet.columns),Wallet],ignore_index=True)
Wallet['Performance SI']=Wallet['Performance SI']*100
Wallet = Wallet.sort_values(by='Date')
Wallet.plot(figsize=(10,2), x ='Date', y='Wallet', kind = 'line')
plt.title('Ludo Wallet')
plt.xlabel('Dates')
plt.ylabel('Value in ')
plt.show()
Wallet.plot(figsize=(10,2), x ='Date', y='Performance SI', kind = 'line')
plt.title('Ludo Wallet Performance SI')
plt.xlabel('Dates')
plt.ylabel('Performance in %')
plt.show()
os.system('jupyter-nbconvert --to PDFviaHTML Wallet12latest.ipynb')
os.system('explorer.exe "Wallet12latest.pdf"') |
Partager