Example #1
0
def sync_stock_history(positions):
    iexcloud_api_key = Iexcloud.get_api_key()

    dates = get_dates_list()
    history_db = get_history_db_cursor()

    for position in positions:
        print("Processing database entries for %s" % position.symbol)
        missing_dates = get_missing_dates(history_db, position.symbol, dates)
        for date in missing_dates:
            if Config.get_yahoo("use_yahoo") == "False":
                symbol = position.symbol
                api_request = Iexcloud.get_api_request(symbol, date, api_key = iexcloud_api_key)
                response = Api.make_api_request(api_request)
            elif Config.get_yahoo("use_yahoo") == "True":
                response = Yahoo.request(position.symbol, date)
            if len(response) > 1:
                Io.error("Expected 1 result from request %s, got %s" % (api_request, str(len(response))))
            elif len(response) == 0:
                #we set the close to -9001 so we don't keep making api calls for dates before the symbol existed
                print("No data for %s on %s" % (position.symbol, date))
                adj_close = -9001
            else:
                adj_close = (response[0])["close"]
            print("Adding record for %s on %s: %s" % (position.symbol, date, adj_close))
            history_db.execute("INSERT INTO '%s' VALUES ('%s','%s')" % (position.symbol, date, adj_close))
    def set_symbol(self, symbol):
        if Config.get_yahoo("use_yahoo") == "True":
            if symbol == "BRKB" or symbol == "BRK.B":
                self.symbol = "BRK-B"

        elif Config.get_yahoo("use_yahoo") == "False":
            if symbol == "BRKB" or symbol == "BRK-B":
                self.symbol = "BRK.B"

        if self.symbol == None:
            self.symbol = symbol
Example #3
0
def get_filepath(symbol, prepend=None):
    destination_dir = Config.get_yahoo("download_dir")
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)
    if prepend == None:
        prepend = str(datetime.now().strftime("%Y-%m-%d"))
    else:
        prepend = prepend
    destination_file = prepend + "-" + symbol + ".csv"
    destination_path = os.path.join(destination_dir, destination_file)
    return (destination_path)
Example #4
0
def sync_history(positions, positions_type):
    #sync benchmark
    if Config.get_yahoo("use_yahoo") == "False":
        benchmark_symbol = Config.get_beta("benchmark")
    elif Config.get_yahoo("use_yahoo") == "True":
        benchmark_symbol = Config.get_yahoo("benchmark")
    sync_stock_history([Instruments.StockPosition(benchmark_symbol, 0)])

    if positions_type == "stock_shares":
        sync_stock_history(positions)

    elif positions_type == "crypto":
        if Config.get_yahoo("use_yahoo") == "True":
            sync_stock_history(positions)

    elif positions_type == "metals":
        sync_metals_history(positions)

    if Config.get_yahoo("use_yahoo") == "True":
        Yahoo.clean()
    return(0)
    def __init__(self, currency, addresses):
        self.currency = currency
        if currency.upper() == "BTC":
            self.description = "Bitcoin"
        elif currency.upper() == "LTC":
            self.description = "Litecoin"
        elif currency.upper() == "ETH":
            self.description = "Ethereum"
        else:
            self.description = currency
        self.addresses = addresses
        self.balance = float(0)

        if Config.get_yahoo("use_yahoo") == "True":
            stock_symbol = self.currency.upper() + "-USD"

        self.emulated_stock = StockPosition(stock_symbol, self.balance, emulated = True)
Example #6
0
def get_dates_list():
    dates = []
    now = datetime.now()
    #don't use this month's data if it's the first
    if now.day == 1:
        dates_end = now - relativedelta(months = 1)
    else:
        dates_end = now

    dates_end = dates_end.replace(day = 1)
    total_dates_count = (Config.get_beta("max_years") * 12) + 1
    #build the dates list
    date_to_add = dates_end
    while len(dates) < total_dates_count:
        formatted_date = date_to_add.strftime("%Y-%m-%d")
        dates.append(formatted_date)
        date_to_add = date_to_add - relativedelta(months = 1)

    adjusted_dates = []
    for date in dates:
        if Config.get_yahoo("use_yahoo") == "False":
            date = Calendar.get_trading_date(date)
        adjusted_dates.append(date)
    return(adjusted_dates[::-1])
Example #7
0
def clean():
    print("Cleaning download dir")
    download_dir = Config.get_yahoo("download_dir")
    for i in os.listdir(download_dir):
        os.remove(os.path.join(download_dir, i))
Example #8
0
def get_benchmark():
    if Config.get_yahoo("use_yahoo") == "False":
        return (Config.get_beta("benchmark"))
    elif Config.get_yahoo("use_yahoo") == "True":
        return (Config.get_yahoo("benchmark"))