Exemplo n.º 1
0
def store_stock(symbol, period="1d", interval="5m"):

    print("Starting store_stock:", symbol)

    ### Start Timer
    logging.basicConfig(filename=LOG_FILENAME,
                        filemode="a",
                        level=logging.INFO)
    logger = logging.getLogger("rstocks.store_stock")
    start = data.start_logger(logger)

    ### Retrieve collection
    cluster = data.cluster_connect()
    db = cluster["rstocks"]
    coll = db[symbol]

    ### Update document if found, generate new doc if not found
    doc_list = serialize_stock(symbol, period=period, interval=interval)
    for doc in doc_list:
        coll.update_one({"_id": doc["_id"]}, {"$set": doc}, upsert=True)

    ### End Timer
    data.end_logger(start, logger)
    logger.debug('Ticker symbol stored: ' + symbol)

    print("Ending store_stock:", symbol)
Exemplo n.º 2
0
def serialize_stock(symbol, period="1d", interval="5m"):

    # Start Timer
    logging.basicConfig(filename=LOG_FILENAME,
                        filemode="a",
                        level=logging.INFO)
    logger = logging.getLogger("rstocks.serialize_stock")
    start = data.start_logger(logger)

    # Initialize and generate data
    try:
        ticker = yf.Ticker(symbol)
        df = ticker.history(period=period, interval=interval)
        date = df.index[0].strftime("%m/%d/%y")
        doc = create_doc(symbol, date)
        doc_list = []
    except IndexError:
        logger.error("rstocks.serialize_stock: Ticker symbol [" + symbol +
                     "] was not found!")

    for i in df.index:
        timestamp = i
        newdate = timestamp.strftime("%m/%d/%y")

        # new date finalizes current document and generates a new document
        if (newdate != date):
            date = newdate
            doc_list.append(doc)
            doc = create_doc(symbol, date)

        # append data to the document
        doc["Time"].append(timestamp.strftime("%H:%M:%S"))
        doc["Timestamp"].append(timestamp)
        doc["Open"].append(df["Open"][i])
        doc["Close"].append(df["Close"][i])
        doc["High"].append(df["High"][i])
        doc["Low"].append(df["Low"][i])
        doc["Volume"].append(int(df["Volume"][i]))
        doc["Dividends"].append(int(df["Dividends"][i]))
        doc["Stock Splits"].append(int(df["Dividends"][i]))

    doc_list.append(doc)

    # End Timer
    data.end_logger(start, logger)

    return doc_list
Exemplo n.º 3
0
def update_stocks(filename=TICKER_FILENAME, threads=16):

    ### Start Timer
    logging.basicConfig(filename=LOG_FILENAME,
                        filemode="a",
                        level=logging.INFO)
    logger = logging.getLogger("rstocks.update_stocks")
    start = data.start_logger(logger)

    ### Generate a list of tickers
    with open(filename) as inFile:
        symbols = inFile.read().splitlines()

    ### Use a Thread Pool to asynchronously make updates to the database
    with ThreadPool(threads) as pool:
        pool.map(store_stock, symbols)

    ### End Timer
    data.end_logger(start, logger)