def run(self): while True: # Get the work from the queue and expand the tuple link = self.queue.get() try: download_link(link) finally: self.queue.task_done()
def single_threaded_execution(): start_ts = time() download_directory = setup_download_dir() links = get_links(CLIENT_ID) count = len(links) for link in links: download_link(download_directory, link) logging.info( 'Time Taken to Sequentially download %s images took %s Seconds', count, time() - start_ts)
def main(): st.title('PSSLAI Probability of Membership Predictor') st.subheader('Please upload the CSV file.') data_file = st.file_uploader("Click below to browse", type=['csv']) if st.button("Predict!"): if data_file is not None: file_details = { "Filename": data_file.name, "FileType": data_file.type, "FileSize in Bytes": data_file.size } st.write(file_details) X_validation = pd.read_csv(data_file) ml = modelPredict() xgbmodel, lbl_rank, lbl_bos, lbl_type = ml.load_model() X_validation['rank'] = lbl_rank.transform(X_validation['rank']) X_validation['alphalist_type'] = lbl_type.transform( X_validation['alphalist_type']) X_validation['branch_of_service'] = lbl_bos.transform( X_validation['branch_of_service']) X_validation['net_pay'] = np.log(X_validation['net_pay']) # st.write(lbl_rank) X_validation['membership'] = xgbmodel.predict(X_validation) X_validation['rank'] = lbl_rank.inverse_transform( X_validation['rank']) X_validation['alphalist_type'] = lbl_type.inverse_transform( X_validation['alphalist_type']) X_validation['branch_of_service'] = lbl_bos.inverse_transform( X_validation['branch_of_service']) X_validation['net_pay'] = np.exp(X_validation['net_pay']) st.dataframe(X_validation) if st.button('Download results to CSV file'): tmp_download_link = download_link( X_validation, 'YOUR_DF.csv', 'Click here to download your data!') st.write(tmp_download_link) st.markdown(tmp_download_link, unsafe_allow_html=True) else: st.write( 'ERROR! Cannot predict probability of membership without uploading a file.' )
def run(self): while True: dir, link = queue.get() download_link(dir, link) queue.task_done()
def run(self): while True: directory, link = self.queue.get() download_link(directory, link) self.queue.task_done()
queries, please refer to the \ <a {n_tab} href="https://github.com/simonchuth/seq_listing_patent_tool">\ project GitHub repo, </a> or contact \ <a {n_tab} href="https://www.linkedin.com/in/simonchuth/">\ Simon Chu </a>') st.markdown(into_text, unsafe_allow_html=True) app_name = st.text_input('Applicant Name') inv_title = st.text_input('Title of Invention') file_ref = st.text_input('File Reference') df = pd.DataFrame(sample_dict) tmp_download_link = download_link( df, 'template.csv', 'Download csv template for sequence listing') st.markdown(tmp_download_link, unsafe_allow_html=True) ncbi_text = (f'Some common species names were given in the template. For the \ scientific name of other species, please refer to sources such \ as the <a {n_tab} href="https://www.ncbi.nlm.nih.gov/taxonomy">\ NCBI Taxonomy Database</a>') st.markdown(ncbi_text, unsafe_allow_html=True) uploaded_file = st.file_uploader("Choose a file", type=['csv']) if uploaded_file is not None: uploaded_file.seek(0) df = pd.read_csv(uploaded_file)
""" Download images synchronous """ from utils import get_links, download_link from time import time if __name__ == '__main__': start_time = time() for link in get_links(): download_link(link) print("Total time download: {}s".format(time() - start_time))
if fund_maturity is None: maturity = st.date_input('Maturity Date') else: fund_maturity = date.fromisoformat(fund_maturity) maturity = st.date_input('Maturity Date', value=fund_maturity) else: maturity = None if st.button('Update'): ss.wealth_manager.fund_edit_info(fund_name, remarks, platform, maturity) st.success(f'Information for {fund_name} updated') elif action == 'Import/Export Data': st.header('Update Current Valuation of funds') json_export = ss.wealth_manager.export_data() st.subheader('Export Data') today_date = today() export_link = download_link(json_export, f'wealth_manager_{today_date}.json', 'Download json data') st.markdown(export_link, unsafe_allow_html=True) data_loader()
def app(): global input_df st.title('Home') st.write( 'First adjust the backtest parameters on the left, then launch the backtest by pressing the button below.' ) st.sidebar.header("Backtest parameters") with st.form("input_params"): session_state.startdate = st.sidebar.date_input( 'start date', value=session_state.startdate, min_value=datetime.strptime('1900-01-01', '%Y-%m-%d'), max_value=date.today(), key='startdate', help='start date of the backtest') session_state.enddate = st.sidebar.date_input( 'end date', value=session_state.enddate, min_value=datetime.strptime('1900-01-01', '%Y-%m-%d'), max_value=date.today(), key='enddate', help='end date of the backtest') session_state.initial_cash = st.sidebar.number_input( "initial cash", min_value=0.0, max_value=None, value=session_state.initial_cash, step=1000.0, format='%f', key='initial_cash', help='initial cash') session_state.contribution = st.sidebar.number_input( "contribution or withdrawal", min_value=None, max_value=None, value=session_state.contribution, format='%f', step=0.01, key='contribution', help= 'contribution or withdrawal. Can be specified as % of the portfolio value or in absolute terms.' ) session_state.leverage = st.sidebar.number_input( "leverage", min_value=1.0, max_value=None, step=0.01, value=session_state.leverage, format='%f', key='leverage', help='daily leverage to apply to assets returns') session_state.expense_ratio = st.sidebar.number_input( "expense ratio", min_value=0.0, max_value=1.0, step=0.01, value=session_state.expense_ratio, format='%f', key='expense_ratio', help='annual expense ratio') st.sidebar.subheader("Assets") if session_state.historic == "Yahoo Finance (daily prices)": idx = 0 elif session_state.historic == "Historical DB (daily prices)": idx = 1 else: idx = 2 session_state.historic = st.sidebar.radio( 'data source', ("Yahoo Finance (daily prices)", "Historical DB (daily prices)", "Historical DB (yearly prices)"), index=idx, key='historic', help='choose the data source') if session_state.historic == "Yahoo Finance (daily prices)": historic_cd = None elif session_state.historic == "Historical DB (daily prices)": historic_cd = 'medium' elif session_state.historic == "Historical DB (yearly prices)": historic_cd = 'long' session_state.shares = st.sidebar.text_area( "assets to backtest", value=session_state.shares, height=None, max_chars=None, key="shares", help='tickers in a comma separated list (e.g. "SPY,TLT,GLD")') session_state.shareclass = st.sidebar.text_area( "assets class (for Yahoo Finance only)", value=session_state.shareclass, height=None, max_chars=None, key="shareclass", help= 'class of each asset (e.g. `equity,bond_lt,gold`). Possibilities are `equity, bond_lt, bond_it, gold, commodity`, where "bond_lt" and "bond_it" are long and intermediate duration bonds, respectively. __This argument is mandatory when the data source is Yahoo Finance.' ) session_state.weights = st.sidebar.text_area( "asset weights", value=session_state.weights, height=None, max_chars=None, key="weights", help= 'list of portfolio weights for each asset specified (e.g. `0.35,0.35,0.30`). The weights need to sum to 1. When weights are specified a custom weights strategy is used that simply loads the weights specified. Alternative is to use a pre-defined strategy.' ) session_state.benchmark = st.sidebar.text_input( "benchmark", value=session_state.benchmark, max_chars=None, key='benchmark', help='ticker of a benchmark') session_state.indicator = st.sidebar.checkbox( "signal assets", value=session_state.indicator, key='indicators', help='load the signal assets needed for the rotation strategy') st.sidebar.subheader("Strategies") session_state.riskparity = st.sidebar.checkbox( 'risk parity', value=session_state.riskparity, key='riskparity', help= 'Dynamic allocation of weights according to the risk parity methodology (see https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/). Here the risk parity is run at portfolio level.' ) session_state.riskparity_nested = st.sidebar.checkbox( 'risk parity nested', value=session_state.riskparity_nested, key='riskparity_nested', help= 'Dynamic allocation of weights according to the risk parity methodology (see https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/). Here the risk parity is run first at asset classe level (for assets belonging to the same asset class) and then at portfolio level.' ) session_state.rotationstrat = st.sidebar.checkbox( 'asset rotation', value=session_state.rotationstrat, key='rotationstrat', help= 'Asset rotation strategy that buy either gold, bonds or equities based on a signal (see https://seekingalpha.com/article/4283733-simple-rules-based-asset-rotation-strategy). To use this strategy tick the box signal assets.' ) session_state.uniform = st.sidebar.checkbox( 'uniform', value=session_state.uniform, key='uniform', help= 'Static allocation uniform across asset classes. Assets are allocated uniformly within the same asset class.' ) session_state.vanillariskparity = st.sidebar.checkbox( 'static risk parity', value=session_state.vanillariskparity, key='vanillariskparity', help= 'Static allocation to asset classes where weights are taken from https://www.theoptimizingblog.com/leveraged-all-weather-portfolio/ (see section "True Risk Parity").' ) session_state.onlystocks = st.sidebar.checkbox( 'only equity', value=session_state.onlystocks, key='onlystocks', help= 'Static allocation only to the equity class. Assets are allocated uniformly within the equity class.' ) session_state.sixtyforty = st.sidebar.checkbox( '60% equities 40% bonds', value=session_state.sixtyforty, key='sixtyforty', help= 'Static allocation 60% to the equity class, 20% to the Long Term Bonds class and 20% to the Short Term Bonds class. Assets are allocated uniformly within the asset classes.' ) session_state.trend_u = st.sidebar.checkbox( 'trend uniform', value=session_state.trend_u, key='trend_u', help= 'First weights are assigned according to the "uniform" strategy. Then, if the current asset price is smaller than the simple moving average, the weight is set to zero (leave as cash).' ) session_state.absmom_u = st.sidebar.checkbox( 'absolute momentum uniform', value=session_state.absmom_u, key='absmom_u', help= 'First weights are assigned according to the "uniform" strategy. Then, if the asset return over the period (momentum) is less than 0, the weight is set to zero (leave as cash).' ) session_state.relmom_u = st.sidebar.checkbox( 'relative momentum uniform', value=session_state.relmom_u, key='relmom_u', help= 'First assets are ranked based on their return over the period (momentum) and divided in two classes. The portfolio is formed by the assets belonging to the higher return class. Then, weights are assigned to this portfolio according to the "uniform" strategy.' ) session_state.momtrend_u = st.sidebar.checkbox( 'relative momentum & trend uniform', value=session_state.momtrend_u, key='momtrend_u', help= 'First weights are assigned according to the "uniform" strategy. Second, assets are ranked based on their return over the period (momentum) and divided in two classes. For the assets belonging to the lower return class, the weight is set to zero (leave as cash). Finally, a trend filter is then applied to assets with positive weight: if the current asset price is smaller than the simple moving average, the weight is set to zero (leave as cash).' ) session_state.trend_rp = st.sidebar.checkbox( 'trend risk parity', value=session_state.trend_rp, key='trend_rp', help= 'First weights are assigned according to the "riskparity" strategy. Then, if the current asset price is smaller than the simple moving average, the weight is set to zero (leave as cash).' ) session_state.absmom_rp = st.sidebar.checkbox( 'absolute momentum risk parity', value=session_state.absmom_rp, key='absmom_rp', help= 'First weights are assigned according to the "riskparity" strategy. Then, if the asset return over the period (momentum) is less than 0, the weight is set to zero (leave as cash).' ) session_state.relmom_rp = st.sidebar.checkbox( 'relative momentum risk parity', value=session_state.relmom_rp, key='relmom_rp', help= 'First assets are ranked based on their return over the period (momentum) and divided in two classes. The portfolio is formed by the assets belonging to the higher return class. Then, weights are assigned to this portfolio according to the "risk parity" strategy.' ) session_state.momtrend_rp = st.sidebar.checkbox( 'relative momentum & trend risk parity', value=session_state.momtrend_rp, key='momtrend_rp', help= 'First weights are assigned according to the "riskparity" strategy. Second, assets are ranked based on their return over the period (momentum) and divided in two classes. For the assets belonging to the lower return class, the weight is set to zero (leave as cash). Finally, a trend filter is then applied to assets with positive weight: if the current asset price is smaller than the simple moving average, the weight is set to zero (leave as cash).' ) session_state.GEM = st.sidebar.checkbox( 'Global equity momentum', value=session_state.GEM, key='GEM', help= 'Global equity momentum strategy. Needs only 4 assets of classes equity, equity_intl, bond_lt, money_market. example: `VEU,IVV,BIL,AGG equity_intl,equity,money_market,bond_lt`. See https://blog.thinknewfound.com/2019/01/fragility-case-study-dual-momentum-gem/' ) session_state.acc_dualmom = st.sidebar.checkbox( 'Accelerating Dual Momentum', value=session_state.acc_dualmom, key='acc_dualmom', help= 'Accelerating Dual Momentum. Needs only 3 assets of classes equity, equity_intl, bond_lt. example: VFINX,VINEX,VUSTX, shareclass equity,equity_intl,bond_lt. See https://engineeredportfolio.com/2018/05/02/accelerating-dual-momentum-investing/' ) session_state.acc_dualmom2 = st.sidebar.checkbox( 'Accelerating Dual Momentum (extended)', value=session_state.acc_dualmom2, key='acc_dualmom2', help= 'Accelerating Dual Momentum (extended). Needs only 4 assets of classes equity, equity_intl, bond_lt, gold. example: VFINX,VINEX,VUSTX,GLD shareclass equity,equity_intl,bond_lt,gold.' ) st.sidebar.subheader("HTML Report") # session_state.create_report = st.sidebar.checkbox('create PDF report', value=session_state.create_report, # key='create_report', help=None) session_state.report_name = st.sidebar.text_input( "report name", value=session_state.report_name, max_chars=None, key='report_name', help=None) session_state.user = st.sidebar.text_input( "user name", value=session_state.user, max_chars=None, key='user', help='user generating the report') session_state.memo = st.sidebar.text_input( "report memo", value=session_state.memo, max_chars=None, key='memo', help='description of the report') #launch_btn = st.button("Launch backtest") launch_btn = st.form_submit_button("Launch backtest") if launch_btn: params['startdate'] = session_state.startdate params['enddate'] = session_state.enddate params['initial_cash'] = session_state.initial_cash params['contribution'] = session_state.contribution params['leverage'] = session_state.leverage params['expense_ratio'] = session_state.expense_ratio params['historic'] = historic_cd params['shares'] = session_state.shares params['shareclass'] = session_state.shareclass params['weights'] = session_state.weights params['benchmark'] = session_state.benchmark params['indicator'] = session_state.indicator params['riskparity'] = session_state.riskparity params['riskparity_nested'] = session_state.riskparity_nested params['rotationstrat'] = session_state.rotationstrat params['uniform'] = session_state.uniform params['vanillariskparity'] = session_state.vanillariskparity params['onlystocks'] = session_state.onlystocks params['sixtyforty'] = session_state.sixtyforty params['trend_u'] = session_state.trend_u params['absmom_u'] = session_state.absmom_u params['relmom_u'] = session_state.relmom_u params['momtrend_u'] = session_state.momtrend_u params['trend_rp'] = session_state.trend_rp params['absmom_rp'] = session_state.absmom_rp params['relmom_rp'] = session_state.relmom_rp params['momtrend_rp'] = session_state.momtrend_rp params['GEM'] = session_state.GEM params['acc_dualmom'] = session_state.acc_dualmom params['acc_dualmom2'] = session_state.acc_dualmom2 params['create_report'] = session_state.create_report params['report_name'] = session_state.report_name params['user'] = session_state.user params['memo'] = session_state.memo # advanced params params['DAYS_IN_YEAR'] = session_state.DAYS_IN_YEAR params[ 'DAYS_IN_YEAR_BOND_PRICE'] = session_state.DAYS_IN_YEAR_BOND_PRICE params['reb_days_days'] = session_state.reb_days_days params['reb_days_years'] = session_state.reb_days_years params['reb_days_custweights'] = session_state.reb_days_custweights params[ 'lookback_period_short_days'] = session_state.lookback_period_short_days params[ 'lookback_period_short_years'] = session_state.lookback_period_short_years params[ 'lookback_period_short_custweights'] = session_state.lookback_period_short_custweights params[ 'lookback_period_long_days'] = session_state.lookback_period_long_days params[ 'lookback_period_long_years'] = session_state.lookback_period_long_years params[ 'lookback_period_long_custweights'] = session_state.lookback_period_long_custweights params[ 'moving_average_period_days'] = session_state.moving_average_period_days params[ 'moving_average_period_years'] = session_state.moving_average_period_years params[ 'moving_average_period_custweights'] = session_state.moving_average_period_custweights params['momentum_period_days'] = session_state.momentum_period_days params['momentum_period_years'] = session_state.momentum_period_years params[ 'momentum_period_custweights'] = session_state.momentum_period_custweights params[ 'momentum_percentile_days'] = session_state.momentum_percentile_days params[ 'momentum_percentile_years'] = session_state.momentum_percentile_years params[ 'momentum_percentile_custweights'] = session_state.momentum_percentile_custweights params['corrmethod_days'] = session_state.corrmethod_days params['corrmethod_years'] = session_state.corrmethod_years params['corrmethod_custweights'] = session_state.corrmethod_custweights params['riskfree'] = session_state.riskfree params['targetrate'] = session_state.targetrate params['alpha'] = session_state.alpha params['market_mu'] = session_state.market_mu params['market_sigma'] = session_state.market_sigma params['stddev_sample'] = session_state.stddev_sample params['annualize'] = session_state.annualize params['logreturns'] = session_state.logreturns #if input_df != 0: mainout = main(params) if mainout is not False: input_df = copy.deepcopy(mainout) # Portfolio value idx = 0 columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date'], value_vars=columns, var_name='strategy', value_name='price') fig = px.line(input_df_long, x="date", y="price", color="strategy") st.markdown("### Portfolio value") st.plotly_chart(fig, use_container_width=True) # Portfolio drawdowns idx = 5 # find a smarter way later columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date'], value_vars=columns, var_name='strategy', value_name='drawdown') fig = px.line(input_df_long, x="date", y="drawdown", color="strategy") st.markdown("### Portfolio drawdown") st.plotly_chart(fig, use_container_width=True) # Portfolio metrics st.markdown("### Portfolio metrics") st.dataframe(input_df[2]) # Portfolio weights st.markdown("### Portfolio weights") # col1, col2 = st.beta_columns(2) # # idx = 3 # columns=input_df[idx].columns # input_df[idx]['date'] = input_df[idx].index # input_df_long = pd.melt(input_df[idx], id_vars=['date','strategy'], value_vars=columns[0:-1],var_name='asset', value_name='weight') # # col1.subheader("Target weights") # # for strat in input_df_long['strategy'].unique(): # fig = px.bar(input_df_long[input_df_long['strategy']==strat], x="date", y="weight", color="asset", title=strat + ' weights') # col1.plotly_chart(fig, use_container_width=True) idx = 4 columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date', 'strategy'], value_vars=columns[0:-1], var_name='asset', value_name='weight') st.subheader("Effective weights") for strat in input_df_long['strategy'].unique(): fig = px.bar(input_df_long[input_df_long['strategy'] == strat], x="date", y="weight", color="asset", title=strat + ' weights') st.plotly_chart(fig, use_container_width=True) # Asset value idx = 6 columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date'], value_vars=columns, var_name='asset', value_name='price') fig = px.line(input_df_long, x="date", y="price", color="asset") st.markdown("### Assets value") st.plotly_chart(fig, use_container_width=True) # Assets drawdowns idx = 7 # find a smarter way later columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date'], value_vars=columns, var_name='asset', value_name='drawdown') fig = px.line(input_df_long, x="date", y="drawdown", color="asset") st.markdown("### Assets drawdown") st.plotly_chart(fig, use_container_width=True) # # Portfolio Returns idx = 1 # Determine the price frequency dates = [] for i in range(1, len(input_df[idx].index)): dates.append( datetime.strptime(str(input_df[idx].index[i]), '%Y-%m-%d')) datediff = stats.mode(np.diff(dates))[0][0] if datediff > timedelta(days=250): frequency = "Years" elif datediff < timedelta(days=2): frequency = "Days" rolling_ret_period = st.slider( "rolling returns period (in years)", min_value=1, max_value=30, value=1, step=1, format='%i', key='rolling_ret_period', help='period of rolling annual return (in years)') if frequency == "Days": # plot the rolling return (annualized) for column in input_df[idx]: if params['logreturns']: input_df[idx][column] = (input_df[idx][column]).rolling( window=params['DAYS_IN_YEAR'] * rolling_ret_period).sum() / rolling_ret_period else: input_df[idx][column] = ( 1 + input_df[idx][column]).rolling( window=params['DAYS_IN_YEAR'] * rolling_ret_period).apply( np.prod)**(1 / rolling_ret_period) - 1 elif frequency == "Years": # plot the rolling 5 years return for column in input_df[idx]: if params['logreturns']: input_df[idx][column] = (input_df[idx][column]).rolling( window=rolling_ret_period).mean() else: input_df[idx][column] = ( 1 + input_df[idx][column]).rolling( window=rolling_ret_period).apply(np.prod) - 1 columns = input_df[idx].columns input_df[idx]['date'] = input_df[idx].index input_df_long = pd.melt(input_df[idx], id_vars=['date'], value_vars=columns, var_name='strategy', value_name='rolling return') fig = px.line(input_df_long, x="date", y="rolling return", color="strategy") st.markdown("### Portfolio returns") st.plotly_chart(fig, use_container_width=True) st.markdown("### Downloads area") today_str = datetime.today().strftime('%Y-%m-%d') outputfilename = [ "Fund Prices", "Returns", "Performance Metrics", "Target Weights", "Effective Weights", "Portfolio Drawdown", "Asset Prices", "Assets drawdown" ] i = 0 for name in outputfilename: inputfilepath = name + "_" + today_str + '.csv' tmp_download_link = utils.download_link( input_df[i], inputfilepath, 'Click here to download ' + name) st.markdown(tmp_download_link, unsafe_allow_html=True) i = i + 1 inputfilepath = params['report_name'] + "_" + today_str + '.html' tmp_download_link = utils.download_link( input_df[8], inputfilepath, 'Click here to download the html report') st.markdown(tmp_download_link, unsafe_allow_html=True)