from webpage_background import *
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
from matplotlib.image import imread
img = imread('dataframe.png')
import warnings
warnings.filterwarnings('ignore')
# current quarter start
print(pd.date_range(end=pd.Timestamp.now(), periods=1, freq='QS'))
# current quarter end
print(pd.date_range(start=pd.Timestamp.now(), periods=1, freq='Q'))
ndays,tdays=daysuntil()
print("There are", ndays, " normal and ", tdays, " trading days until the next quarter.")
DatetimeIndex(['2023-07-01 09:29:14.886762'], dtype='datetime64[ns]', freq='QS-JAN') DatetimeIndex(['2023-09-30 09:29:14.887216'], dtype='datetime64[ns]', freq='Q-DEC') There are 70 normal and 50 trading days until the next quarter.
average_rank_neo = get_average_rank(symbols_sw)
[*********************100%***********************] 257 of 257 completed
7 Failed downloads:
['FDCB.SW', 'BSSANB.SW', 'SUPCON.SW', 'GSI.SW', 'ZHT.SW', 'DFSH.SW', 'YJET.SW']: Exception('%ticker%: No timezone found, symbol may be delisted')
[*********************100%***********************] 10 of 10 completed Units per stock ADEN.SW 3 ARYN.SW 4 DUFN.SW 2 EFGN.SW 9 KARN.SW 4 MOBN.SW 26 SCR.SW 19 SKAN.SW 6 VPBN.SW 23 YTME.SW 0 Name: 0, dtype: int64 Total Units 96 Expected annual return: 6.5% Annual volatility: 13.9% Sharpe Ratio: 0.32 (0.06469321822427757, 0.1390768170430547, 0.32135634949455066) [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
print('Swiss Important Stocks ',dt_string)
print(average_rank_neo)
Swiss Important Stocks 23/07/2023 09:29:14
symbols relvol breakout30 breakout52 truerange frog final_rank
238 VPBN.SW 0.720056 1 1 4.101371 -0.099206 16.2
20 ARYN.SW 0.422234 1 1 7.442922 -0.099206 13.2
69 DUFN.SW 0.728079 1 1 5.271852 -0.083333 9.6
198 SKAN.SW 0.557694 1 1 7.499739 -0.071429 17.4
120 KARN.SW 1.526518 1 1 7.675323 -0.071429 15.0
5 ADEN.SW 0.641805 1 1 7.400674 -0.055556 5.4
72 EFGN.SW 1.301376 1 1 4.736111 -0.039683 10.2
248 YTME.SW 1.266012 0 0 21.280984 -0.035714 12.0
148 MOBN.SW 0.694211 1 1 3.238564 -0.027778 15.6
188 SCR.SW 0.000000 1 1 5.752380 -0.027778 13.8
average_rank_neo = get_average_rank(stock_list)
[*********************100%***********************] 135 of 135 completed
12 Failed downloads:
['ARCUS.OL', 'NPRO.OL', 'MRCEL.OL', 'SBANK.OL', 'NRS.OL', 'SOLON.OL', 'FJORD.OL', 'FKRFT.OL', 'BANO.OL', 'OCY.OL']: Exception('%ticker%: No timezone found, symbol may be delisted')
['NOR.OL', 'NTS.OL']: Exception('%ticker%: No data found, symbol may be delisted')
[*********************100%***********************] 10 of 10 completed Units per stock AKRBP.OL 6 AKSO.OL 3 CADLR.OL 14 DNB.OL 25 KAHOT.OL 3 RECSI.OL 3 SCHA.OL 8 STB.OL 17 SUBC.OL 4 YAR.OL 12 Name: 0, dtype: int64 Total Units 95 Expected annual return: 16.5% Annual volatility: 19.9% Sharpe Ratio: 0.73 (0.165309354517166, 0.19857660662829874, 0.731754646151045) [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
print('OsloBors Important Stocks ',dt_string)
print(average_rank_neo)
OsloBors Important Stocks 23/07/2023 09:29:14
symbols relvol breakout30 breakout52 truerange frog final_rank
118 STB.OL 0.596078 1 1 4.280756 -0.123016 15.6
32 CADLR.OL 0.194112 1 1 6.912679 -0.099206 18.0
119 SUBC.OL 0.459353 1 1 7.715179 -0.091270 2.4
107 SCHA.OL 0.843390 0 0 9.986961 -0.087302 11.4
36 DNB.OL 1.170322 1 1 5.510634 -0.051587 14.4
134 YAR.OL 0.543721 1 1 6.899368 -0.043651 16.2
56 KAHOT.OL 0.564951 1 1 10.480341 -0.031746 3.6
96 RECSI.OL 1.218552 1 0 9.651280 -0.027778 9.0
12 AKSO.OL 1.075701 1 1 9.949501 -0.019841 0.6
11 AKRBP.OL 0.897682 1 0 6.009049 -0.019841 6.6
img = imread('dataframe.png')
fig_im, axim = plt.subplots(figsize=(18,6))
axim.imshow(img,aspect='auto')
plt.show()
average_rank = get_average_rank(symbols_pa)
[*********************100%***********************] 40 of 40 completed
14 Failed downloads:
['EI.PA', 'MT.PA', 'GSZ.PA', 'ALU.PA', 'TEC.PA', 'PP.PA', 'DEXB.PA', 'EAD.PA', 'LG.PA', 'FTE.PA']: Exception('%ticker%: No price data found, symbol may be delisted (1d 2020-01-01 -> 2023-07-23)')
['EDF.PA', 'STM.PA']: Exception('%ticker%: No data found, symbol may be delisted')
['UG.PA', 'FP.PA']: Exception('%ticker%: No timezone found, symbol may be delisted')
[*********************100%***********************] 10 of 10 completed Units per stock AC.PA 7 ACA.PA 11 BNP.PA 8 CA.PA 19 DG.PA 9 EN.PA 13 LR.PA 8 RNO.PA 6 SGO.PA 8 SU.PA 5 Name: 0, dtype: int64 Total Units 94 Expected annual return: 12.1% Annual volatility: 20.1% Sharpe Ratio: 0.51 (0.12131573885738411, 0.2005918150103192, 0.5050841124906919) [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
print('CAC40 Important Stocks ',dt_string)
print(average_rank)
CAC40 Important Stocks 23/07/2023 09:29:14 symbols relvol breakout30 breakout52 truerange frog final_rank 15 EN.PA 0.681511 1 1 4.812119 -0.170635 4.2 1 ACA.PA 0.733923 1 1 5.957227 -0.134921 4.8 6 BNP.PA 0.987594 1 1 6.976336 -0.115079 5.4 0 AC.PA 0.588291 1 1 8.039725 -0.111111 10.2 33 SU.PA 0.867013 1 1 5.558842 -0.103175 12.0 11 DG.PA 0.781190 1 1 5.362917 -0.103175 9.0 31 SGO.PA 0.540168 1 1 7.334962 -0.099206 3.0 21 LR.PA 0.883397 1 1 5.453641 -0.099206 8.4 7 CA.PA 0.969929 1 1 5.876706 -0.095238 3.6 29 RNO.PA 0.765737 1 1 10.216693 -0.087302 1.8
average_rank = get_average_rank(symbols_ist)
print('BIST100 Important Stocks',dt_string)
print(average_rank)
[*********************100%***********************] 100 of 100 completed [*********************100%***********************] 10 of 10 completed Units per stock ECZYT.IS 10 ENJSA.IS 12 FROTO.IS 6 IPEKE.IS 9 ISMEN.IS 10 MGROS.IS 8 OTKAR.IS 11 SELEC.IS 14 SOKM.IS 8 TTRAK.IS 7 Name: 0, dtype: int64 Total Units 95 Expected annual return: 100.7% Annual volatility: 33.7% Sharpe Ratio: 2.93 (1.006881027875822, 0.33667036269049805, 2.931297605138663) [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
BIST100 Important Stocks 23/07/2023 09:29:14
symbols relvol breakout30 breakout52 truerange frog final_rank
24 ECZYT.IS 4.129043 1 1 14.351569 -0.150794 8.4
63 MGROS.IS 1.146623 1 1 16.176524 -0.130952 14.4
77 SELEC.IS 1.492940 1 1 14.137672 -0.126984 12.0
90 TTRAK.IS 1.245950 1 1 18.165511 -0.119048 3.0
34 FROTO.IS 1.772138 1 1 12.624336 -0.107143 18.0
46 IPEKE.IS 2.319349 0 0 12.720281 -0.107143 15.0
80 SOKM.IS 0.946643 1 1 14.933616 -0.099206 16.8
50 ISMEN.IS 1.426573 1 1 23.669489 -0.099206 2.4
69 OTKAR.IS 1.850998 1 1 16.536143 -0.091270 5.4
28 ENJSA.IS 1.702054 1 1 16.211790 -0.071429 10.8
def convert_prices(tickers, currency, end_date=None):
# Fetch stock prices for all tickers from Yahoo Finance
stock_data = yf.download(tickers, start='2020-01-01', end=end_date)
stock_data = stock_data.fillna(method='ffill')
Volume= stock_data['Volume']
# Extract the adjusted close prices for each ticker
adj_close_data = pd.DataFrame()
for ticker in tickers:
adj_close_data[ticker] = stock_data['Adj Close'][ticker]
# Fetch exchange rate data from Yahoo Finance
exchange_data = yf.download(currency, start='2020-01-01', end=end_date)
exchange_data = exchange_data.fillna(method='ffill')
# Calculate exchange rate for each day
exchange_data['Adj Close ' + currency] = 1 / exchange_data['Adj Close']
# Join adjusted close data and exchange rate data based on date
merged_data = adj_close_data.join(exchange_data['Adj Close ' + currency], how='inner')
# Convert prices to specified currency
for ticker in tickers:
merged_data[ticker] = merged_data[ticker] * merged_data['Adj Close ' + currency]
return merged_data, Volume
result_sp500 = yf.download(symbols_sp500, start='2020-01-01', end=None)
result_sp500 = result_sp500.fillna(method='ffill')
Volume_sp500= result_sp500['Volume']
result_sp500 = result_sp500['Adj Close']
[*********** 23% ] 48 of 212 completed
Failed to get ticker 'BRK/A' reason: Expecting value: line 1 column 1 (char 0)
[**********************47% ] 99 of 212 completed
Failed to get ticker 'BRK/B' reason: Expecting value: line 1 column 1 (char 0)
[*********************100%***********************] 212 of 212 completed
2 Failed downloads:
['BRK/A', 'BRK/B']: Exception('%ticker%: No timezone found, symbol may be delisted')
result, Volume = convert_prices(symbols_pa, 'USDEUR=X', end_date=None)
result_Oslo, Volume_Oslo = convert_prices(stock_list, 'USDNOK=X', end_date=None)
result_SW, Volume_SW = convert_prices(symbols_sw, 'USDCHF=X', end_date=None)
[*********************100%***********************] 40 of 40 completed
14 Failed downloads:
['GSZ.PA', 'TEC.PA', 'MT.PA', 'EI.PA', 'ALU.PA', 'PP.PA', 'DEXB.PA', 'EAD.PA', 'LG.PA', 'FTE.PA']: Exception('%ticker%: No price data found, symbol may be delisted (1d 2020-01-01 -> 2023-07-23)')
['EDF.PA', 'STM.PA']: Exception('%ticker%: No data found, symbol may be delisted')
['UG.PA', 'FP.PA']: Exception('%ticker%: No timezone found, symbol may be delisted')
[*********************100%***********************] 1 of 1 completed [*********************100%***********************] 135 of 135 completed
12 Failed downloads:
['ARCUS.OL', 'NPRO.OL', 'MRCEL.OL', 'SBANK.OL', 'NRS.OL', 'SOLON.OL', 'FKRFT.OL', 'FJORD.OL', 'BANO.OL', 'OCY.OL']: Exception('%ticker%: No timezone found, symbol may be delisted')
['NOR.OL', 'NTS.OL']: Exception('%ticker%: No data found, symbol may be delisted')
[*********************100%***********************] 1 of 1 completed [*********************100%***********************] 257 of 257 completed
7 Failed downloads:
['FDCB.SW', 'BSSANB.SW', 'SUPCON.SW', 'GSI.SW', 'DFSH.SW', 'ZHT.SW', 'YJET.SW']: Exception('%ticker%: No timezone found, symbol may be delisted')
[*********************100%***********************] 1 of 1 completed
import pandas as pd
# Merge two data frames: result and result_2
merged_data = pd.merge(result, result_Oslo, how='outer', left_index=True, right_index=True)
merged_data = pd.merge(merged_data, result_SW, how='outer', left_index=True, right_index=True)
merged_data = pd.merge(merged_data, result_sp500, how='outer', left_index=True, right_index=True)
merged_Vol= pd.merge(Volume, Volume_Oslo, how='outer', left_index=True, right_index=True)
merged_Vol= pd.merge(merged_Vol, Volume_SW, how='outer', left_index=True, right_index=True)
merged_Vol= pd.merge(merged_Vol, Volume_sp500, how='outer', left_index=True, right_index=True)
# Forward-fill NaN values
merged_data.ffill(inplace=True)
# Print the merged and filled data frame
print(merged_data.shape)
print(merged_Vol.shape)
(920, 647) (920, 644)
# Compare columns of two data frames
df1_columns = set(merged_data.columns)
df2_columns = set(merged_Vol.columns)
# Find columns not in common
columns_not_in_common = df1_columns.symmetric_difference(df2_columns)
# Print the columns not in common
print("Columns not in common:")
for column in columns_not_in_common:
merged_data = merged_data.drop(columns=[column])
print(merged_data.shape)
Columns not in common: (920, 644)
top_50_stocks_neo=neo_rank_stocks_by_return(merged_data,merged_Vol)
plotter(top_50_stocks_neo.iloc[:,0].values.tolist())
[*********************100%***********************] 10 of 10 completed Units per stock ABNB 4 AKRBP.OL 12 AKSO.OL 7 BX 5 KAHOT.OL 4 LECN.SW 7 MIKN.SW 25 SCHW 12 SUBC.OL 15 VK.PA 4 Name: 0, dtype: int64 Total Units 95 Expected annual return: 27.4% Annual volatility: 21.2% Sharpe Ratio: 1.19 (0.2738627510384993, 0.21244146159952723, 1.194977426379485) [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
#GLOBAL COMPARISON OS + PA + SW + US
top_50_stocks_neo
| symbols | relvol | breakout30 | breakout52 | truerange | frog | final_rank | |
|---|---|---|---|---|---|---|---|
| 168 | SUBC.OL | 0.209704 | 1 | 1 | 10.088080 | -0.087302 | 5.4 |
| 149 | KAHOT.OL | 0.325032 | 1 | 1 | 12.162404 | -0.055556 | 6.6 |
| 605 | SCHW | NaN | 0 | 0 | 7.271825 | -0.051587 | 8.4 |
| 324 | MIKN.SW | 1.243973 | 1 | 1 | 6.996241 | -0.043651 | 13.2 |
| 36 | VK.PA | 0.810386 | 0 | 0 | 9.829210 | -0.027778 | 14.4 |
| 310 | LECN.SW | 5.913383 | 1 | 1 | 9.740706 | -0.027778 | 3.6 |
| 434 | ABNB | NaN | 1 | 1 | 9.860749 | -0.023810 | 16.8 |
| 469 | BX | NaN | 1 | 1 | 9.455960 | -0.007937 | 15.6 |
| 43 | AKSO.OL | 0.386474 | 1 | 1 | 11.628397 | -0.007937 | 2.4 |
| 135 | AKRBP.OL | 0.532988 | 0 | 0 | 8.991531 | -0.007937 | 12.6 |
# #todo for 3.9+ python!!!!
# import yfinance_cache as yfc
# def download_history(tickers, start=None, end=None):
# if isinstance(tickers, str):
# tickers = [tickers]
# # Initialize the YahooFinanceCached object
# # Download historical data for each ticker
# cntr = 0
# for ticker in tickers:
# if cntr is 0:
# toti1=yfc.Ticker('QQQ')
# data1 = toti1.history(start=start, end=end, interval='1d',adjust_splits=True, adjust_divs=True)
# data1.index = data1.index.date
# data1 = data1.groupby(data1.index).first()
# data1 = data1.fillna(method='ffill')
# volume1 = data1['Volume']
# data1 = data1['Close']
# data1.name='QQQ'
# volume1.name='QQQ'
# cntr = 1
# else:
# try:
# if start and end:
# toti=yfc.Ticker(ticker)
# data = toti.history(start=start, end=end, interval='1d',adjust_splits=True, adjust_divs=True)
# else:
# toti=yfc.Ticker(ticker)
# data = toti.history(interval='1d',adjust_splits=True, adjust_divs=True)
# data.index = data.index.date
# data = data.groupby(data.index).first()
# data = data.fillna(method='ffill')
# volume = data['Volume']
# data = data['Close']
# volume.name=ticker
# data.name=ticker
# data1 = pd.merge(data1, data, how='outer', left_index=True, right_index=True)
# volume1 = pd.merge(volume1, volume, how='outer', left_index=True, right_index=True)
# except:
# print(ticker, 'symbol error')
# # Merge the dataframes into a single dataframe
# #merged_data = pd.concat(dataframes, axis=1)
# return data1, volume1
# xx, vol = download_history(symbols_sp500, start='2020-01-01', end='2023-01-01')
# xx
# xx = yf.download('LEPU.SW', start='2020-01-01', end='2023-01-01')
# xx['Close'].plot()