<jupyter_start><jupyter_text>dfy_corr2<jupyter_code>for x,y in permutations(dfy21_corr.columns[3:],2):
    indstr1 =  get_dictkey(d_industry_tics, x)
    indstr2 =  get_dictkey(d_industry_tics, y)
    ret_x,risk_x,ksh_x = return_risk_stock(dfy, x)
    ret_y,risk_y,ksh_y = return_risk_stock(dfy, y)
    
    if indstr1 == [] or indstr2 == []:
        continue
    
    corr = dfy21_corr.loc[x, y]
    
    if x != y and corr > 0.75 and corr < 0.99 and indstr1 == indstr2:
        #and (ksh_x > 1 or ksh_y > 1): # and 'Bank' not in ''.join(indstr1+indstr2): # and risk_x > 0.7:
        #dfy_coint.loc[x,y] < 0.05: # and risk_x > 0.9:
        
        print(round(corr, 2), x, y, indstr1, indstr2)
              #,round(ret_x,2), round(risk_x,2), round(ret_y,2), round(risk_y,2))
        #round(dfy_coint.loc[x,y],3)) #, round(risk_x,3), round(risk_y,3))
        #plot_LR_rel2tic_d(dfy, x, y)
                    
            
#reloadlibs()
#dfa,dfb = download_yf_tics('AAPL')
#''.join(indstr1+indstr2)
df1, dfm_corr1, dfm_doclh1, nloaded_moex1, err_moex1 = download_moex_mt(['YNDX', 'MAIL'], days_nodata = 250)

#dfm_corr.loc['YNDX', 'MAIL']
dfy_corr.loc['H','CCL']
dfm_corr.loc['MGNT','FIVE']
plot_LR_rel2tic_d(dfy, 'USFD', 'PFGC')
for x,y in permutations(dfm_corr.columns[3:],2):
    #indstr1 =  get_dictkey(d_industry_tics, x)
    #indstr2 =  get_dictkey(d_industry_tics, y)
    #ret_x,risk_x,_ = return_risk_stock(dfy, x)
    #ret_y,risk_y,_ = return_risk_stock(dfy, y)
    
    if x != y and dfm_corr.loc[x, y] > 0.69: # and risk_x > 0.9:
        print(round(dfm_corr.loc[x, y], 2), x, y) #, round(risk_x,3), round(risk_y,3))
dfy_corr.loc['RRGB','BJRI']
plot_LR_rel2tic_d(dfy, 'DHI', 'LEN')
coint_interest =[['ALK', 'AXP'], ['BK','LEVI'],['CPRI', 'NTAP'],['DXC','MAR'], ['EMR','BKNG'], ['HA','ADS'], ['HEAR','PYPL'],
                 ['HAS','MDT'], ['JP','HEAR'], ['LVS','WELL'], ['MFGP', 'LHX'],
                 ['OVV','BEN'], ['PCG','NEM'], ['TOT', 'CVS'], ['VIPS','MRNA'],
                 ['ZGNX','ADBE'], ['ZGNX','AMGN'], ['VIAC', 'MSTR'], ['ZYNE', 'VLO']] 
for p in coint_interest:
    x = p[0]
    y = p[1]
    plot_spred_d(dfy[:-40], x, y) #, legend = x+'/'+y)
    plot_LR_tics_d(dfy, [x,y], grid=True)
    
for x, y in permutations(dfy_coint.columns, 2):
    co = dfy_coint.loc[x, y]
    spred = LR_spred(dfy[x], dfy[y])
    lel = spred.iloc[-1]
    
    if co < 0.01 and abs(lel) > 0.1:
        fco = 'co={0:.3f}'.format(co)
        print(x,y, fco)
        plot_spred_d(dfy, x, y) #, legend = x+'/'+y)
'COST' in all_us_ticks
yfinfo['LOW']
for xy in coint_interest:
    x, y  = xy[0], xy[1]
    co = dfy_coint.loc[x, y]
    spred = LR_spred(dfy[x], dfy[y])
    lel = spred.iloc[-1]
    
    if co < 0.001: # and abs(lel) > 0.2:
        fco = 'co={0:.3f}'.format(co)
        print(x,y, fco)
        plot_spred_d(dfy, x, y) #, legend = x+'/'+y)
plot_spred_d(dfy, 'EHTH', 'RRC')
for x, y in permutations(dfm_coint.columns, 2):
    co = dfm_coint.loc[x, y]
    spred = LR_spred(dfm[x], dfm[y])
    lel = spred.iloc[-1]
    
    if co < 0.001: # and abs(lel) > 0.2:
        fco = 'co={0:.3f}'.format(co)
        print(x,y, fco)
        plot_spred_d(dfm, x, y) #, legend = x+'/'+y)
plot_LR_tics_d(dfy[-70:], cols_wo_dates(dfy))
for x in cols_wo_dates(dfy):
    plot_LR_rel2tic_d(dfy[-120:], x, 'SP500')
invest_idea_usd(dfy, ['TSLA', 'AAPL'], ['F', 'MDT'], datetime.datetime(2021,3,3), 
                datetime.datetime(2021,5,6), vol_short_entry=4000)
for x, y in permutations(dfy_coint.columns, 2):
    co = dfy_coint.loc[x, y]
    spred = LR_spred(dfy[x], dfy[y])
    lel = spred.iloc[-1]
    
    if co < 0.02 and abs(lel) > 0.2 and abs(spred.max())>0.3:
        fco = 'co={0:.3f}'.format(co)
        print(x,y, fco)
        plot_spred(dfy, x, y) #, legend = x+'/'+y)
#return_risk_stock(dfy[-80:], 'BAC')
df_returns = dfy['GS'].pct_change().dropna()

df_returns.plot()
cr = (1+df_returns).cumprod()
cr.plot()
df_returns.std() * np.sqrt(252)
df_returns = np.negative(df_returns)

#df_returns.rolling(window=5).std().plot(figsize=(20, 10), title="30 Day Rolling Standard Deviation");

#import requests
#stock='AAPL'
#prices = requests.get(f'https://financialmodelingprep.com/api/v3/historical-price-full/{stock}?serietype=line').json()
#from yahoofinancials import YahooFinancials
#ticker = 'AAPL'
#yahoo_financials = YahooFinancials(ticker)
#apple_net_income = yahoo_financials.get_net_income()
#utilsforshort = list((set(utilities) ^ set(utilities_noshort)))
#dfbuildh,_ = download_yf_tics(build_house, '2018-01-01', '2021-04-09')
#dfutil,_ = download_yf_tics(utilsforshort, '2018-01-01', '2021-04-09')
#random.sample(set(us_tics), 0)
dfy_ret = dfy[['BAC', 'GS']].pct_change().dropna()
minimize(sharpe_formin, [0.25, 0.25], method = 'SLSQP', bounds=[(0.01,0.5)]*2, args = (dfy_ret, ['BAC', 'GS'], []))
return_risk_portfolio(dfy[-80:], lt, st, res_w)
plot_spred_d(dfm, 'PHOR', 'AGRO')
atr(dfm_doclh, 'LKOH')[['date']]
plot_LR_tics_d(dfy, 'FCX')
reload(gz_mainlib)
reload(gz_plotlib)
reload(gz_rwlib_mt)
reload(gz_const)

from gz_mainlib import *
from gz_plotlib import *
from gz_rwlib_mt import *
from gz_const import *
#текущие позиции
long_tics = ['LGIH', 'KBH', 'LEN', 'PHM', 'DHI']
short_tics = ['NEE', 'CNP', 'SO', 'PCG', 'EIX']

#беты из офиц данных по отношению к SP500 (3 года  помесячные возвраты)
long_betas = [1.555984, 1.828461, 1.516822, 1.439051, 1.644236]
short_betas = [0.167677, 0.966905, 0.423607, 1.408714, 0.608383]

long_prices = [dfy.iloc[-1][x] for x in long_tics]
short_prices = [dfy.iloc[-1][x] for x in short_tics]

dfl, dfs = create_position(long_tics, short_tics, 
                            long_betas, short_betas, long_prices, short_prices, 1700)

wlong = dfl['pos'].tolist()
wshort = dfs['pos'].tolist()

weights = wlong + wshort

ret, risk, sharpe_ratio = return_risk_portfolio(dfy[-100:], long_tics, short_tics, weights)

#ret, risk, sharpe_ratio = return_risk_portfolio(dfy[-100:], long_tics, short_tics, [1/10]*10)

ret, risk
xlwrite_betapos('_beta1.xlsx', 1700, dfl, dfs)
[beta(dfyi, x, 'SP500') for x in short_tics]
[beta(dfyi, x, 'SP500') for x in long_tics]
#import pandas_datareader.data as web
#dflen = web.get_data_yahoo('LEN', '01/01/2018', '01/01/2021', interval='m')
#dflen500 = web.get_data_yahoo('^GSPC', '01/01/2018', '01/01/2021', interval='m')
#ru_tickers
#dfm0 = read_multiple_opencloselowhigh_ru_moex(ru_tickers, t1, now)

#dfrtsi = read_opencloselowhigh_ru_stoc('moex', 'RTSI', t1, now, cols_rename_moex, cols_out_2, boardid='RTSI')
#dfimoex = read_opencloselowhigh_ru_stoc('moex', 'IMOEX', t1, now, cols_rename_moex, cols_out_2, boardid='SNDX')
#dfrvi = read_opencloselowhigh_ru_stoc('moex', 'RVI', t1, now, cols_rename_moex, cols_out_2, boardid='RTSI')

#dfusdrub = read_opencloselowhigh_ru_stoc('moex', 'USD000UTSTOM', t1, now, cols_rename_moex, cols_out_2, boardid='CETS')
#ren = [x.replace('USD000UTSTOM', 'USDRUB') for x in dfusdrub.columns]
#dfusdrub.columns = ren
#dfeurrub = read_opencloselowhigh_ru_stoc('moex', 'EUR_RUB__TOM', t1, now, cols_rename_moex, cols_out_2, boardid='CETS')
#ren = [x.replace('EUR_RUB__TOM', 'EURRUB') for x in dfeurrub.columns]
#dfeurrub.columns = ren
#dfeurusd = read_opencloselowhigh_ru_stoc('moex', 'EURUSD000TOM', t1, now, cols_rename_moex, cols_out_2, boardid='CETS')
#ren = [x.replace('EURUSD000TOM', 'EURUSD') for x in dfeurusd.columns]
#dfeurusd.columns = ren

#dfdax = read_opencloselowhigh_orig_stoc('yahoo', '^GDAXI', t1, now, cols_rename_yahoo, cols_out_2)
#dfdax.columns = ['date', 'DAX_high', 'DAX_low', 'DAX_open', 'DAX_close']

#dfetf = read_multiple_opencloselowhigh_ru_moex(['SBSP', 'VTBA', 'FXIT', 'FXUS'], t1, now, boardid='TQTF')


return_risk_stock(dfm[-70:], 'AFKS')
return_risk_stock(dfm[-70:], 'GMKN')
ddfc = read_invests_rub('frtin.txt', datetime.datetime(2021,3,30), dfm)
ddfc
reload(gz_mainlib)
reload(gz_plotlib)
reload(gz_rwlib_mt)
reload(gz_const)

from gz_mainlib import *
from gz_plotlib import *
from gz_rwlib_mt import *
from gz_const import *
invest_idea_usd(dfy, ['TSLA', 'AAPL'], ['F', 'MDT'], datetime.datetime(2021,3,3), 
                datetime.datetime(2021,5,6), vol_short_entry=4000)
reload(gz_mainlib)
reload(gz_plotlib)
reload(gz_rwlib_mt)
reload(gz_const)

from gz_mainlib import *
from gz_plotlib import *
from gz_rwlib_mt import *
from gz_const import *

ddii = invest_idea_usd(dflogreit, logistic, l_reits_forshort, datetime.datetime(2021,3,1), 
                                     datetime.datetime(2021,5,13), vol_long_entry=1000, vol_short_entry=1000)

print('Earn:',round(ddii['earn'].sum(), 2), 'Vlong:',  round(ddii.loc[ddii['ls']=='long',  'vcurr'].sum(), 2),
                                            'Vshort:', round(ddii.loc[ddii['ls']=='short', 'vcurr'].sum(), 2))
ddii
rolwin = 50
tic = 'SPCE'
ret = dfy[tic].pct_change().dropna()
retmean = ((ret.rolling(window=rolwin)).mean() * 252).dropna()
risk = (ret.rolling(window=rolwin).std() * np.sqrt(252)).dropna()
sharpe = retmean / risk
#plot_d(dfy.loc[retmean.index,'date'], sharpe, title=tic+' sharpe', draw_LRline=False, draw_line0=True)
sharpe.mean()
def plot_rolling_sharpe(df, tic, rolwindow=40, min_sharpe=-1000, max_sharpe=1000,
                       draw_returns=True, draw_risk=True):
    returns = df[tic].pct_change().dropna()
    retmean = ((returns.rolling(window=rolwindow)).mean() * 252).dropna()
    risk = (returns.rolling(window=rolwindow).std() * np.sqrt(252)).dropna()
    sharpe = retmean / risk
    lastsharpe = sharpe.iloc[-rolwindow:].mean()
    lastrisk = risk.iloc[-rolwindow:].mean()
    lastreturn = retmean.iloc[-rolwindow:].mean()
    
    if lastsharpe > min_sharpe and lastsharpe < max_sharpe:
        figax = plot_d(df.loc[risk.index,'date'], sharpe, title=tic+' sharpe=' + str(round(lastsharpe,2)),
                       draw_LRline=False, draw_line0=True)
        
        if draw_returns:
            figax3 = plot_d(df.loc[risk.index,'date'], retmean, title=tic+' returns=' + str(round(lastreturn,2)),
                            draw_LRline=False, draw_line0=True)
        
        if draw_risk:
            figax2 = plot_d(df.loc[risk.index,'date'], risk, title=tic+' risk=' + str(round(lastrisk,2)),
                            draw_LRline=False, draw_line0=True)

f = plot_rolling_sharpe(dfy, 'SPCE', rolwindow=40)
for x in us_tics:
    figax = plot_rolling_sharpe(dfy, x, rolwindow=20, max_sharpe=0.5)
    #plt.close(figax[0])
    
tics = ['SBER', 'GAZP', 'ROSN', 'RUAL', 'GMKN']
tics = cols_wo_dates(dfm)
dftemp = dfm[['date']+tics].copy()
dftemp_ret = dftemp[tics].pct_change().dropna()
cumulative_returns = (1 + dftemp_ret).cumprod()

for x in range(0, len(tics)-1, 5):
    #print(x,x+4)
    #print(cumulative_returns.iloc[:,[x,x+4]])
    cumulative_returns.iloc[:,x:x+4].plot(figsize=(10, 5), title="Cumulative Returns");


#plot_d(dfy['date'], dfy['SPCE'], title='1', x_intervals=5, g_size=(5,3), draw_grid=False, 
#       draw_LRline=True, xaxe_date=True, scatter=False)
plot_norm_d(dfy, ['AAPL', 'SP500', 'TSLA', 'T'])
#t = yf.Ticker('RRC')
#b = t.info #).update({'tic':'RRC'})
#dict_yfinfo = yfinfo_mt_load(us_tics)
#ticslist = list(pd.merge(df_5min, df_instr, on='figi').groupby(['ticker']).count().index)
return_risk_stock(dfy[-70:], 'SPCE')
hdf5_contain('dat.h5')
#print(dfm_coint.loc['POLY', 'PLZL'])
#plot_spred_d(dfm, 'RUAL', 'GMKN')
plot_LR_rel2tic_d(dfy, 'HLT', 'DRI')
'LI' in all_us_ticks
import yahoo_fin.stock_info as si
from yahoo_fin import news

reload(gz_mainlib)
reload(gz_plotlib)
reload(gz_rwlib)
reload(gz_const)
reload(gz_dwnllib)

from gz_mainlib import *
from gz_plotlib import *
from gz_dwnllib import *
from gz_rwlib import *
from gz_const import *

mainver()
plotver()
rwver()
dwnlver()
download_yahoo_doclh('AAPL')
si.get_data('aapl', start_date=datetime.date(2021,6,1))
si.get_quote_table("LGIH")
#news.get_yf_rss("LGIH")
yfinfo['LGIH']
si.get_stats_valuation('LGIH')
quote
si.get_stats_valuation('TSLA')
#si.get_balance_sheet('aapl')
val = val.iloc[:,:2]
 
val.columns = ["Attribute", "Recent"]
val
#sp500_list = si.tickers_sp500()
msft = yf.Ticker("MSFT")
hist = msft.history(period="max")
hist<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1855457470505734, 'fraction_numerical': 0.04034320600610511, 'mean_word_length': 4.573333333333333, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '519797', 'license': 'MIT', 'n_tokens_mistral': 5716, 'n_tokens_neox': 5311, 'n_words': 898, 'path': 'pyandjnotebook/gz71.ipynb', 'repo_name': 'meprosky/gz-stockdata', 'size': '12121'}