<jupyter_start><jupyter_text>FRAMEWORK: What should signal be when there is no 'nearest' model output?<jupyter_code>import pandas as pd
import numpy as np
from datetime import *
from pandas_datareader.data import DataReader
import seaborn as sns
yahoo_url = "https://finance.yahoo.com/quote/%5EDJI/components/"
djia_table = pd.read_html(yahoo_url, header=0, index_col=0)[0]
djia_table = djia_table.reset_index()
def get_data_for_multiple_stocks(tickers):
    '''
    Obtain stocks information (Date, OHLC, Volume and Adjusted Close). 
    Uses Pandas DataReader to make an API Call to Yahoo Finance and download the data directly.
    Computes other values - Log Return and Arithmetic Return.
    
    Input: List of Stock Tickers
    Output: A dictionary of dataframes for each stock
    '''
    stocks = dict()
    for ticker in tickers:
        s = DataReader(ticker, 'yahoo', start_date, end_date)
        s.insert(0, "Ticker", ticker)  #insert ticker column so you can reference better later
        s['Date'] = pd.to_datetime(s.index) #useful for transformation later
        s['Prev Close'] = s['Close'].shift(1)
        s['Log Return'] = np.log(s['Close']/s['Prev Close'])
        s['Return'] = (s['Close']/s['Prev Close']-1)
        s = s.reset_index(drop=True)
        
        cols = list(s.columns.values) # re-arrange columns
        cols.remove("Date")
        s = s[["Date"] + cols]
        
        stocks[ticker] = s
        
    return stocks
start_date = "2010-01-01"
end_date = "2019-12-31"
apple_appl = get_data_for_multiple_stocks(["AAPL"])
apple_prices = apple_appl["AAPL"]
apple_prices.head()
apple_prices["Return"].describe()
rtns = apple_prices["Return"]
rtns = rtns.dropna()
print("Total Long-Only Returns", round(list(np.cumprod(1+rtns))[-1]*100, 2), "%")
sns.distplot(rtns, rug=True, hist=False)
start_date = "2010-01-01"
end_date = "2019-12-31"

rtns_lst = []

for symbol in djia_table["Symbol"]:
    print(symbol)
    all_data = get_data_for_multiple_stocks([symbol])[symbol]
    rtns = all_data["Return"].dropna()
    tot_rtns = list(np.cumprod(1+rtns))[-1]*100
    
    print(rtns.describe())
    print("Total Long-Only Returns", round(tot_rtns, 2), "%")
    print("------------------------------------")
    
    rtns_lst.append(round(tot_rtns, 0))
### Make a dataframe based on returns list and symbols
symbols = djia_table["Symbol"]
rtns_lst = pd.Series(rtns_lst)

rtns_df = pd.DataFrame({"Tickers": symbols,
             "Rtns": rtns_lst})
rtns_df["Default Wts"] = (rtns_lst - min(rtns_lst)) / (max(rtns_lst) - min(rtns_lst))
rtns_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15, 'fraction_numerical': 0.02, 'mean_word_length': 3.619893428063943, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '52318', 'license': 'MIT', 'n_tokens_mistral': 952, 'n_tokens_neox': 908, 'n_words': 211, 'path': 'Analytics/Financial News/Signal Baseline.ipynb', 'repo_name': 'jasmineseah-17/BT4222-NLP-Project', 'size': '2600'}