Exemplo n.º 1
0
def get_factors( current_date, exchange_symbols ):
    global conversion_factor
    products = [ exchange_symbol[:-3] + '_1' for exchange_symbol in exchange_symbols ]
    (csidb,csidb_cursor) = db_connect()
    _format_strings = ','.join(['%s'] * len(products))
    csidb_cursor.execute("SELECT product,conversion_factor FROM products WHERE product IN (%s)" % _format_strings,tuple(products))
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row['product'][:-2]] = float(row['conversion_factor'])
def get_factors(current_date, exchange_symbols):
    global conversion_factor
    global price
    products = [exchange_symbol[:-3] + "_1" for exchange_symbol in exchange_symbols]
    (csidb, csidb_cursor) = db_connect()
    _format_strings = ",".join(["%s"] * len(products))
    csidb_cursor.execute(
        "SELECT product,conversion_factor FROM products WHERE product IN (%s)" % _format_strings, tuple(products)
    )
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row["product"][:-2]] = float(row["conversion_factor"])
    price = fetch_latest_prices_v1(exchange_symbols, current_date, "future")  # Uses exchange symbols
def get_factors(current_date, data_source, exchange_symbols, product_type):
    conversion_factor = {}
    products = [ exchange_symbol[:-3] + '_1' for exchange_symbol in exchange_symbols ]
    (csidb,csidb_cursor) = db_connect()
    _format_strings = ','.join(['%s'] * len(products))
    query = "SELECT product,conversion_factor FROM products WHERE product IN (%s)" % _format_strings
    csidb_cursor.execute(query, tuple(products))
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row['product'][:-2]] = float(row['conversion_factor'])
  
    # If data source is CSI get prices and factors from db
    if data_source == 'csi':
        open_price_2, close_price_1, close_price_2, is_valid = fetch_latest_prices(exchange_symbols, current_date, product_type) # Uses exchange symbols
    return open_price_2, close_price_1, close_price_2, conversion_factor, is_valid
def get_factors(current_date, data_source, exchange_symbols, product_type,
                wedbush_db_cursor):
    prices = {}
    conversion_factor = {}
    products = [
        exchange_symbol[:-3] + '_1' for exchange_symbol in exchange_symbols
    ]
    (csidb, csidb_cursor) = db_connect()
    _format_strings = ','.join(['%s'] * len(products))
    csidb_cursor.execute(
        "SELECT product,conversion_factor FROM products WHERE product IN (%s)"
        % _format_strings, tuple(products))
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row['product'][:-2]] = float(
            row['conversion_factor'])

    # If data source is CSI get prices and factors from db
    if data_source == 'csi':
        new_symbols = []
        for i in range(len(exchange_symbols)):
            basename = exchange_symbols[i][:-3]
            if basename == "ES":  # For ES we want to use SP prices
                new_symbols.append('SP' + exchange_symbols[i][-3:])
            else:
                new_symbols.append(exchange_symbols[i])
        #conversion_factor, currency_factor = get_latest_currency_and_conversion_factors_v1(basenames, curr_date_str, product_type)
        prices = fetch_latest_prices_v1(new_symbols, current_date,
                                        product_type)  # Uses exchange symbols
        for key in prices.keys():
            if key[:-3] == "SP":
                prices['ES' + key[-3:]] = prices[key]

    elif data_source == 'wedbush':
        _format_strings = ','.join(['%s'] * len(exchange_symbols))
        query = "SELECT product, broker_close_price FROM positions WHERE product IN (%s) and date = '%s'" % (
            _format_strings, current_date.strftime("%Y-%m-%d"))
        wedbush_db_cursor.execute(query, tuple(exchange_symbols))
        rows = wedbush_db_cursor.fetchall()
        for row in rows:
            prices[row['product']] = float(row['broker_close_price'])
    return prices, conversion_factor
Exemplo n.º 5
0
def get_factors(current_date, data_source, exchange_symbols, product_type):
    conversion_factor = {}
    products = [
        exchange_symbol[:-3] + '_1' for exchange_symbol in exchange_symbols
    ]
    (csidb, csidb_cursor) = db_connect()
    _format_strings = ','.join(['%s'] * len(products))
    query = "SELECT product,conversion_factor FROM products WHERE product IN (%s)" % _format_strings
    csidb_cursor.execute(query, tuple(products))
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row['product'][:-2]] = float(
            row['conversion_factor'])

    # If data source is CSI get prices and factors from db
    if data_source == 'csi':
        open_price_2, close_price_1, close_price_2, is_valid = fetch_latest_prices(
            exchange_symbols, current_date,
            product_type)  # Uses exchange symbols
    return open_price_2, close_price_1, close_price_2, conversion_factor, is_valid
def get_factors(current_date, data_source, exchange_symbols, product_type, wedbush_db_cursor):
    prices = {}
    conversion_factor = {}
    products = [exchange_symbol[:-3] + "_1" for exchange_symbol in exchange_symbols]
    (csidb, csidb_cursor) = db_connect()
    _format_strings = ",".join(["%s"] * len(products))
    csidb_cursor.execute(
        "SELECT product,conversion_factor FROM products WHERE product IN (%s)" % _format_strings, tuple(products)
    )
    rows = csidb_cursor.fetchall()
    for row in rows:
        conversion_factor[row["product"][:-2]] = float(row["conversion_factor"])

    # If data source is CSI get prices and factors from db
    if data_source == "csi":
        new_symbols = []
        for i in range(len(exchange_symbols)):
            basename = exchange_symbols[i][:-3]
            if basename == "ES":  # For ES we want to use SP prices
                new_symbols.append("SP" + exchange_symbols[i][-3:])
            else:
                new_symbols.append(exchange_symbols[i])
        # conversion_factor, currency_factor = get_latest_currency_and_conversion_factors_v1(basenames, curr_date_str, product_type)
        prices = fetch_latest_prices_v1(new_symbols, current_date, product_type)  # Uses exchange symbols
        for key in prices.keys():
            if key[:-3] == "SP":
                prices["ES" + key[-3:]] = prices[key]

    elif data_source == "wedbush":
        _format_strings = ",".join(["%s"] * len(exchange_symbols))
        query = "SELECT product, broker_close_price FROM positions WHERE product IN (%s) and date = '%s'" % (
            _format_strings,
            current_date.strftime("%Y-%m-%d"),
        )
        wedbush_db_cursor.execute(query, tuple(exchange_symbols))
        rows = wedbush_db_cursor.fetchall()
        for row in rows:
            prices[row["product"]] = float(row["broker_close_price"])
    return prices, conversion_factor
Exemplo n.º 7
0
def fetch_latest_prices(exchange_symbols, required_date, product_type):
    open_price_2 = {}
    close_price_1 = {}
    close_price_2 = {}
    tables = {}
    is_valid = dict.fromkeys(exchange_symbols, True)
    (db, db_cursor) = db_connect()
    for exchange_symbol in exchange_symbols:
        if product_type == 'future':
            shortcode = exchange_symbol[:-3] + '_1'
            query = "SELECT `table` FROM products WHERE product = '%s'" % shortcode
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
            query = "SELECT date, specific_ticker, open, close FROM %s WHERE specific_ticker = '%s' AND date <= '%s' ORDER BY date DESC LIMIT 2" % ( rows[0]['table'],\
                     exchange_symbol, required_date)
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
        else:
            query = "SELECT `table` FROM products WHERE product = '%s'" % exchange_symbol
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
            query = "SELECT date, product, open, close FROM %s WHERE product = '%s' AND date <= '%s' ORDER BY date DESC LIMIT 2" % (
                rows[0]['table'], exchange_symbol, required_date)
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
        print exchange_symbol, rows[0]['date'], required_date, rows[0][
            'date'] == required_date
        if rows[0]['date'] == required_date:
            close_price_2[exchange_symbol] = float(rows[0]['close'])
            close_price_1[exchange_symbol] = float(rows[1]['close'])
            open_price_2[exchange_symbol] = float(rows[0]['open'])
        else:
            is_valid[exchange_symbol] = False
            close_price_2[exchange_symbol] = float(rows[0]['close'])
            close_price_1[exchange_symbol] = float(rows[0]['close'])
            open_price_2[exchange_symbol] = float(rows[0]['close'])
    db_close(db)
    return open_price_2, close_price_1, close_price_2, is_valid
def fetch_latest_prices(exchange_symbols, required_date, product_type):
    open_price_2 = {}
    close_price_1 = {}
    close_price_2 = {}
    tables = {}
    is_valid = dict.fromkeys(exchange_symbols, True)
    (db,db_cursor) = db_connect()
    for exchange_symbol in exchange_symbols:
        if product_type == 'future':
            shortcode = exchange_symbol[:-3] + '_1'
            query = "SELECT `table` FROM products WHERE product = '%s'" % shortcode
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
            query = "SELECT date, specific_ticker, open, close FROM %s WHERE specific_ticker = '%s' AND date <= '%s' ORDER BY date DESC LIMIT 2" % ( rows[0]['table'],\
                     exchange_symbol, required_date)
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
        else:
            query = "SELECT `table` FROM products WHERE product = '%s'" % exchange_symbol
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
            query = "SELECT date, product, open, close FROM %s WHERE product = '%s' AND date <= '%s' ORDER BY date DESC LIMIT 2" % ( rows[0]['table'], exchange_symbol, required_date )
            db_cursor.execute(query)
            rows = db_cursor.fetchall()
        print exchange_symbol, rows[0]['date'], required_date, rows[0]['date'] == required_date
        if rows[0]['date'] == required_date:
            close_price_2[exchange_symbol] = float(rows[0]['close'])
            close_price_1[exchange_symbol] = float(rows[1]['close'])
            open_price_2[exchange_symbol] = float(rows[0]['open'])
        else:
            is_valid[exchange_symbol] = False
            close_price_2[exchange_symbol] = float(rows[0]['close'])
            close_price_1[exchange_symbol] = float(rows[0]['close'])
            open_price_2[exchange_symbol] = float(rows[0]['close'])
    db_close(db)
    return open_price_2, close_price_1, close_price_2, is_valid