def get_and_check_prices_for_frequency(data,
                                       log,
                                       contract_object,
                                       frequency="D"):
    broker_data = dataBroker(data)
    price_data = diagPrices(data)
    price_updater = updatePrices(data)

    try:
        old_prices = price_data.get_prices_for_contract_object(contract_object)
        ib_prices = broker_data.get_prices_at_frequency_for_contract_object(
            contract_object, frequency)
        if len(ib_prices) == 0:
            raise Exception("No IB prices found for %s nothing to check" %
                            str(contract_object))

        print("\n\n Manually checking prices for %s \n\n" %
              str(contract_object))
        new_prices_checked = manual_price_checker(
            old_prices,
            ib_prices,
            column_to_check='FINAL',
            delta_columns=['OPEN', 'HIGH', 'LOW'],
            type_new_data=futuresContractPrices)
        result = price_updater.update_prices_for_contract(
            contract_object, new_prices_checked, check_for_spike=False)
        return result

    except Exception as e:
        log.warn(
            "Exception %s when getting or checking data at frequency %s for %s"
            % (e, frequency, str(contract_object)))
        return failure
Ejemplo n.º 2
0
    def update_prices(self) -> updatePrices:
        update_prices = getattr(self, "_update_prices", None)
        if update_prices is None:
            update_prices = updatePrices(self.data)
            self._update_prices = update_prices

        return update_prices
Ejemplo n.º 3
0
def get_and_check_prices_for_frequency(data: dataBlob,
                                       contract_object: futuresContract,
                                       frequency="D"):

    broker_data = dataBroker(data)
    price_data = diagPrices(data)
    price_updater = updatePrices(data)

    old_prices = price_data.get_prices_for_contract_object(contract_object)

    broker_prices = broker_data.get_prices_at_frequency_for_contract_object(
        contract_object, frequency)
    if len(broker_prices) == 0:
        print("No broker prices found for %s nothing to check" %
              str(contract_object))
        return failure

    print("\n\n Manually checking prices for %s \n\n" % str(contract_object))
    new_prices_checked = manual_price_checker(
        old_prices,
        broker_prices,
        column_to_check="FINAL",
        delta_columns=["OPEN", "HIGH", "LOW"],
        type_new_data=futuresContractPrices,
    )
    price_updater.update_prices_for_contract(contract_object,
                                             new_prices_checked,
                                             check_for_spike=False)
def get_and_add_prices_for_frequency(data,
                                     log,
                                     contract_object,
                                     frequency="D"):
    broker_data_source = dataBroker(data)
    db_futures_prices = updatePrices(data)

    try:
        ib_prices = broker_data_source.get_prices_at_frequency_for_contract_object(
            contract_object, frequency)
        rows_added = db_futures_prices.update_prices_for_contract(
            contract_object, ib_prices, check_for_spike=True)
        if rows_added is data_error:
            ## SPIKE
            ## Need to email user about this as will need manually checking
            msg = "Spike found in prices for %s: need to manually check by running update_manual_check_historical_prices" % str(
                contract_object)
            log.warn(msg)
            try:
                send_mail_msg(msg, "Price Spike")
            except:
                log.warn("Couldn't send email about price spike for %s" %
                         str(contract_object))

            return failure

        log.msg("Added %d rows at frequency %s for %s" %
                (rows_added, frequency, str(contract_object)))
        return success

    except Exception as e:
        log.warn("Exception %s when getting data at frequency %s for %s" %
                 (e, frequency, str(contract_object)))
        return failure
def get_and_add_prices_for_frequency(
    data: dataBlob,
    contract_object: futuresContract,
    frequency: Frequency = DAILY_PRICE_FREQ,
):
    broker_data_source = dataBroker(data)
    db_futures_prices = updatePrices(data)

    broker_prices = broker_data_source.get_prices_at_frequency_for_contract_object(
        contract_object, frequency)

    if len(broker_prices) == 0:
        data.log.msg("No prices from broker for %s" % str(contract_object))
        return failure

    error_or_rows_added = db_futures_prices.update_prices_for_contract(
        contract_object, broker_prices, check_for_spike=True)

    if error_or_rows_added is spike_in_data:
        report_price_spike(data, contract_object)
        return failure

    data.log.msg("Added %d rows at frequency %s for %s" %
                 (error_or_rows_added, frequency, str(contract_object)))
    return success
def get_and_add_prices_for_frequency(data,
                                     log,
                                     contract_object,
                                     frequency="D"):
    broker_data_source = dataBroker(data)
    db_futures_prices = updatePrices(data)

    ib_prices = broker_data_source.get_prices_at_frequency_for_contract_object(
        contract_object, frequency)
    if len(ib_prices) == 0:
        log.msg("No prices from broker for %s" % str(contract_object))
        return failure

    rows_added = db_futures_prices.update_prices_for_contract(
        contract_object, ib_prices, check_for_spike=True)
    if rows_added is data_error:
        # SPIKE
        # Need to email user about this as will need manually checking
        msg = (
            "Spike found in prices for %s: need to manually check by running interactive_manual_check_historical_prices"
            % str(contract_object))
        log.warn(msg)
        try:
            send_production_mail_msg(
                data, msg, "Price Spike %s" % contract_object.instrument_code)
        except BaseException:
            log.warn("Couldn't send email about price spike for %s" %
                     str(contract_object))

        return failure

    log.msg("Added %d rows at frequency %s for %s" %
            (rows_added, frequency, str(contract_object)))
    return success
Ejemplo n.º 7
0
 def write_new_rolled_data(self):
     # Apparently good let's try and write rolled data
     price_updater = updatePrices(self.data)
     price_updater.add_multiple_prices(self.instrument_code,
                                       self.updated_multiple_prices,
                                       ignore_duplication=True)
     price_updater.add_adjusted_prices(self.instrument_code,
                                       self.new_adjusted_prices,
                                       ignore_duplication=True)
def _roll_adjusted_and_multiple_prices(data, instrument_code):
    """
    Roll multiple and adjusted prices

    THE POSITION MUST BE ZERO IN THE PRICED CONTRACT! WE DON'T CHECK THIS HERE

    :param data: dataBlob
    :param instrument_code: str
    :return:
    """
    print(landing_strip(80))
    print("")
    print("Rolling adjusted prices!")
    print("")

    diag_prices = diagPrices(data)

    current_multiple_prices = diag_prices.get_multiple_prices(instrument_code)

    # Only required for potential rollback
    current_adjusted_prices = diag_prices.get_adjusted_prices(instrument_code)

    try:
        updated_multiple_prices = update_multiple_prices_on_roll(data, current_multiple_prices, instrument_code)
        new_adj_prices = futuresAdjustedPrices.stich_multiple_prices(updated_multiple_prices)
    except Exception as e:
        data.log.warn("%s : went wrong when rolling: No roll has happened" % e)
        return failure

    # We want user input before we do anything
    compare_old_and_new_prices([current_multiple_prices, updated_multiple_prices,
                                current_adjusted_prices, new_adj_prices],
                               ["Current multiple prices", "New multiple prices",
                               "Current adjusted prices", "New adjusted prices"])
    print("")
    confirm_roll = input("Confirm roll adjusted prices for %s are you sure y/n:" % instrument_code)
    if confirm_roll!="y":
        print("\nUSER DID NOT WANT TO ROLL: Setting roll status back to previous state")
        return failure

    try:
        # Apparently good let's try and write rolled data
        price_updater = updatePrices(data)
        price_updater.add_adjusted_prices(instrument_code, new_adj_prices,
                                                                ignore_duplication=True)
        price_updater.add_multiple_prices(instrument_code, updated_multiple_prices,
                                                                ignore_duplication=True)

    except Exception as e:
        data.log.warn("%s went wrong when rolling: Going to roll-back to original multiple/adjusted prices" % e)
        rollback_adjustment(data, instrument_code, current_adjusted_prices, current_multiple_prices)
        return failure

    return success
Ejemplo n.º 9
0
def update_with_new_prices(data, instrument_code: str,
                           updated_multiple_prices: futuresMultiplePrices,
                           updated_adjusted_prices: futuresAdjustedPrices):

    update_prices = updatePrices(data)

    update_prices.add_multiple_prices(instrument_code,
                                      updated_multiple_prices,
                                      ignore_duplication=True)
    update_prices.add_adjusted_prices(instrument_code,
                                      updated_adjusted_prices,
                                      ignore_duplication=True)
def rollback_adjustment(data, instrument_code, current_adjusted_prices, current_multiple_prices):
    price_updater = updatePrices(data)

    try:
        price_updater.add_adjusted_prices(instrument_code, current_adjusted_prices,
                                                                ignore_duplication=True)
        price_updater.add_multiple_prices(instrument_code, current_multiple_prices,
                                                                ignore_duplication=True)
    except Exception as e:
        data.log.warn("***** ROLLBACK FAILED! %s!You may need to rebuild your data! Check before trading!! *****" % e)
        return failure

    return success
Ejemplo n.º 11
0
def update_multiple_adjusted_prices_for_instrument(instrument_code, data):
    """
    Update for multiple and adjusted prices for a given instrument

    :param instrument_code:
    :param data: dataBlob
    :param log: logger
    :return: None
    """

    log = data.log.setup(instrument_code=instrument_code)
    diag_prices = diagPrices(data)
    update_prices = updatePrices(data)
    # update multiple prices with new prices
    # (method in multiple prices object and possible in data socket)
    existing_adjusted_prices = diag_prices.get_adjusted_prices(instrument_code)
    existing_multiple_prices = diag_prices.get_multiple_prices(instrument_code)

    relevant_contracts = existing_multiple_prices.current_contract_dict()

    new_prices_dict = get_dict_of_new_prices_and_contractid(
        instrument_code, relevant_contracts, data
    )
    updated_multiple_prices = existing_multiple_prices.update_multiple_prices_with_dict(
        new_prices_dict)

    updated_adjusted_prices = (
        existing_adjusted_prices.update_with_multiple_prices_no_roll(
            updated_multiple_prices
        )
    )

    if updated_adjusted_prices is no_update_roll_has_occured:
        log.critical(
            "Can't update adjusted prices for %s as roll has occured but not registered properly" %
            instrument_code)
        raise Exception()

    update_prices.add_multiple_prices(
        instrument_code, updated_multiple_prices, ignore_duplication=True
    )
    update_prices.add_adjusted_prices(
        instrument_code, updated_adjusted_prices, ignore_duplication=True
    )

    return success
Ejemplo n.º 12
0
def price_updating_or_errors(
        data: dataBlob,
        contract_object: futuresContract,
        new_prices_checked: futuresContractPrices,
        check_for_spike: bool = True,
        cleaning_config: priceFilterConfig = arg_not_supplied):

    price_updater = updatePrices(data)

    error_or_rows_added = price_updater.update_prices_for_contract(
        contract_object,
        new_prices_checked,
        check_for_spike=check_for_spike,
        max_price_spike=cleaning_config.max_price_spike)

    if error_or_rows_added is spike_in_data:
        report_price_spike(data, contract_object)
        return failure

    if error_or_rows_added is failure:
        data.log.warn("Something went wrong when adding rows")
        return failure

    return error_or_rows_added