Beispiel #1
0
def updateDB(symb):


    bday=BDay()
    today_date = pd.Timestamp.now().strftime("%Y-%m-%d").split("-")
    is_business_day = bday.is_on_offset(datetime(int(today_date[0]),int(today_date[1]),int(today_date[2])))
    # if not is_business_day:
        # return

    td = TDAPI()
    # td.pullHistPriceForAll()
    # td.pullTodayPriceForAllBatch()
    # td.pullOptionDfForAll()
    # td.pullTodayPrice()

    symb = 'WMT'
    # plotKChart(datapath, figpath, symb, saving=False)
    # visOptionsDist(datapath, figpath, ['VXRT'])
    visOptionsDist(datapath, figpath, [symb])
Beispiel #2
0
def initial_fetch():
    # check and see if we dont already have the information
    # make a list with 4 of the initial fetches in there (I plan to add more)
    # append to the list if the system detects we have already gathered that information
    bdays = BDay()
    is_business_day = bdays.is_on_offset(dt.datetime.today())
    if is_business_day and market_closed_bool:
        last_business_day = dt.datetime.today()
    else:
        last_business_day = dt.datetime.today() - BDay(1)
    # reason for this confusingness is because the us treasury department puts out new rates at 3pm CST every day,
    # libor does not update todays rates until the next day
    tbond_business_day = dt.datetime.strftime(last_business_day, '%Y-%m-%d')
    libor_business_day = dt.datetime.strftime(dt.datetime.today() - BDay(1),
                                              '%Y-%m-%d')

    cwd = os.getcwd()
    fetched_information = []
    libor_yields = None

    libor_files = glob.glob(
        cwd +
        r"\Daily Stock Analysis\Bonds\LIBOR Yields (last updated *).xlsx")
    if len(libor_files) > 0:
        for file in libor_files:
            try:
                libor_df = pd.read_excel(
                    cwd + fr"\Daily Stock Analysis\Bonds\LIBOR Yields "
                    fr"(last updated {libor_business_day}).xlsx")
                libor_list = libor_df[libor_business_day].tolist()
                libor_yields = []
                for element in libor_list:
                    if element == '-':
                        continue
                    libor_yields.append(element[:-2])
                fetched_information.append('LIBOR')
            except FileNotFoundError:
                os.remove(file)

    bond_files = glob.glob(
        cwd +
        r"\Daily Stock Analysis\Bonds\US T-Bond Yields (last updated *).xlsx")
    if len(bond_files) > 0:
        for file in bond_files:
            try:
                bond_df = pd.read_excel(
                    cwd + f"\\Daily Stock Analysis\\Bonds\\US T-Bond Yields "
                    f"(last updated {tbond_business_day}).xlsx")

                bond_df = bond_df.set_index(['Unnamed: 0'])
                bond_list = bond_df.loc[
                    tbond_business_day,
                    ['1 Mo', '2 Mo', '3 Mo', '6 Mo', '1 Yr', '2 Yr']].tolist()
                fetched_information.append('T-Bond')
            except FileNotFoundError:
                os.remove(file)

    with sqlite3.connect(cwd + r'\Databases\quotes.db') as db:
        cursor = db.cursor()
        initial_quote_fetch = {}
        for stock in stock_tickers:
            # check if initial quotes exist
            cursor.execute(
                f"select name from sqlite_master where type = 'table' and name = 'initial_quote_{stock}'"
            )
            table_name = cursor.fetchall()
            if len(table_name) == 1:
                cursor.execute(f"select * from initial_quote_{stock}")
                initial_data = cursor.fetchall()[0]
                if len(initial_data) > 0:
                    quote = {
                        'time': initial_data[0],
                        'beta': initial_data[1],
                        'dividend': initial_data[2],
                        "day's range": initial_data[3],
                        '52 week range': initial_data[4],
                        'one year target': initial_data[5],
                        'previous close': initial_data[6],
                        'open': initial_data[7],
                        'P/E ratio': initial_data[8],
                        'average volume': initial_data[9]
                    }
                    initial_quote_fetch[stock] = [quote]

        if len(initial_quote_fetch) == len(stock_tickers):
            fetched_information.append(f'Initial Quote')

    logging.debug(f"information already fetched: {fetched_information}")
    # initial fetches
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        if 'Initial Quote' not in fetched_information:
            initial_quote_fetch = executor.submit(
                stock_data_bootstrap.initial_quote_data_fetch).result()
        else:
            logging.debug(
                f"Initial Quote information for {dt.date.today()} already exists"
            )

        stock_quotes = executor.submit(
            stock_data_bootstrap.quote_data_processor).result()

        if 'T-Bond' not in fetched_information:
            t_bond_yields = executor.submit(
                bond_bootstrap.treasury_bond_yields).result()
        else:
            logging.debug(
                f"T-bond information for {tbond_business_day} already exists")

        if 'LIBOR' not in fetched_information:
            libor_yields = executor.submit(
                bond_bootstrap.LIBOR_yields).result()
        else:
            logging.debug(
                f"LIBOR information for the previous business day {libor_business_day} already exists"
            )

        ti_fetch = executor.submit(
            finnhub_tech_bootstrap.tech_indicator).result()

    try:
        if 'Initial Quote' not in fetched_information:
            db_bootstrap.initial_quote_insertion(
                file='quotes.db', initial_data=initial_quote_fetch)

        logging.debug("Options gathering started")
        options_bootstrapper = Options(stock_tickers=stock_tickers,
                                       initial_data=initial_quote_fetch,
                                       quote_data=stock_quotes,
                                       rate=libor_yields,
                                       bbond=bond_bootstrap)
        # function changed to a yield from in order to wait and make sure everything is done and priced
        options_bootstrapper.thread_marshaller()
        logging.debug("Options gathering completed")

        return options_bootstrapper, initial_quote_fetch, stock_quotes, libor_yields, ti_fetch
    except Exception as error:
        logging.error(f"Exception occurred {error}", exc_info=True)