Beispiel #1
0
    def calculate_total_commission_for_broker_order(self, broker_order: brokerOrder) -> brokerOrder:
        """
        This turns a broker_order with non-standard commission field (list of tuples) into a single figure
        in base currency

        :return: broker_order
        """
        if broker_order is missing_order:
            return broker_order

        if broker_order.commission is None:
            return broker_order

        currency_data = dataCurrency(self.data)
        if isinstance(broker_order.commission, float):
            base_values = [broker_order.commission]
        else:
            base_values = [
                currency_data.currency_value_in_base(ccy_value)
                for ccy_value in broker_order.commission
            ]

        commission = sum(base_values)
        broker_order.commission = commission

        return broker_order
def fx_prices(data):
    fx_code = get_valid_fx_code_from_user(data)
    diag_prices = dataCurrency(data)
    prices = diag_prices.get_fx_prices(fx_code)
    print(prices)

    return None
Beispiel #3
0
    def run_backtest(self):
        strategy_name = self.strategy_name
        data = self.data

        capital_data = dataCapital(data)
        notional_trading_capital = capital_data.get_capital_for_strategy(strategy_name)
        if notional_trading_capital is missing_data:
            # critical log will send email
            error_msg = (
                "Capital data is missing for %s: can't run backtest" %
                strategy_name)
            data.log.critical(error_msg)
            raise Exception(error_msg)

        currency_data = dataCurrency(data)
        base_currency = currency_data.get_base_currency()

        system = self.system_method(
            notional_trading_capital=notional_trading_capital, base_currency=base_currency
        )

        updated_buffered_positions(data, strategy_name, system)

        store_backtest_state(data, system, strategy_name=strategy_name)

        return success
Beispiel #4
0
def get_fx_series_for_instrument(data, instrument_code):
    diag_instruments = diagInstruments(data)
    currency = diag_instruments.get_currency(instrument_code)
    currency_data = dataCurrency(data)
    fx_series = currency_data.get_fx_prices_to_base(currency)

    return fx_series
Beispiel #5
0
def last_currency_fx(data: dataBlob, instrument_code: str) -> float:
    data_currency = dataCurrency(data)
    diag_instruments = diagInstruments(data)
    currency = diag_instruments.get_currency(instrument_code)
    fx_rate = data_currency.get_last_fx_rate_to_base(currency)

    return fx_rate
def get_last_fx_price_update_for_code(data, fx_code):
    data_fx = dataCurrency(data)
    px = data_fx.get_fx_prices(fx_code)
    last_timestamp = px.index[-1]

    update = genericUpdate(fx_code, last_timestamp)

    return update
Beispiel #7
0
    def get_point_size_base_currency(self, instrument_code: str) -> float:
        point_size_instrument_currency = self.get_point_size(instrument_code)
        instrument_currency = self.get_currency(instrument_code)

        currency_data = dataCurrency(self.data)
        point_size_currency_value = currencyValue(
            instrument_currency, point_size_instrument_currency)
        value = currency_data.currency_value_in_base(point_size_currency_value)

        return value
Beispiel #8
0
    def get_total_capital_value_in_base_currency(self) -> float:
        currency_data = dataCurrency(self.data)
        values_across_accounts = self.data.broker_capital.get_account_value_across_currency_across_accounts(
        )

        # This assumes that each account only reports either in one currency or
        # for each currency, i.e. no double counting
        total_account_value_in_base_currency = (
            currency_data.total_of_list_of_currency_values_in_base(
                values_across_accounts))

        return total_account_value_in_base_currency
Beispiel #9
0
def update_fx_prices_for_code(fx_code: str, data: dataBlob):
    broker_fx_data = dataBroker(data)
    db_fx_data = dataCurrency(data)

    new_fx_prices = broker_fx_data.get_fx_prices(fx_code)  # returns fxPrices object
    rows_added = db_fx_data.update_fx_prices_and_return_rows_added(
        fx_code, new_fx_prices, check_for_spike=True
    )

    if rows_added is spike_in_data:
        report_fx_data_spike(data, fx_code)
        return failure

    return success
Beispiel #10
0
    def get_total_excess_liquidity_in_base_currency(self) -> float:
        currency_data = dataCurrency(self.data)
        account_id = self.get_broker_account()
        values_across_accounts = (
            self.broker_capital_data.
            get_excess_liquidity_value_across_currency(account_id))

        # This assumes that each account only reports either in one currency or
        # for each currency, i.e. no double counting
        total_account_value_in_base_currency = (
            currency_data.total_of_list_of_currency_values_in_base(
                values_across_accounts))

        return total_account_value_in_base_currency
def update_fx_prices_for_code(fx_code: str, data: dataBlob):
    broker_fx_source = dataBroker(data)
    db_fx_data = dataCurrency(data)

    new_fx_prices = broker_fx_source.get_fx_prices(
        fx_code)  # returns fxPrices object
    rows_added = db_fx_data.update_fx_prices(
        fx_code, new_fx_prices, check_for_spike=True
    )

    if rows_added is data_error:
        report_fx_data_spike(data, fx_code)

    return success
    def _get_currency_and_capital(self):
        data = self.data
        strategy_name = self.strategy_name

        capital_data = dataCapital(data)
        notional_trading_capital = capital_data.get_capital_for_strategy(
            strategy_name)
        if notional_trading_capital is missing_data:
            # critical log will send email
            error_msg = ("Capital data is missing for %s: can't run backtest" %
                         strategy_name)
            data.log.critical(error_msg)
            raise Exception(error_msg)

        currency_data = dataCurrency(data)
        base_currency = currency_data.get_base_currency()

        return base_currency, notional_trading_capital
def update_manual_check_fx_prices_for_code(fx_code: str, data: dataBlob):
    db_currency_data = dataCurrency(data)
    data_broker = dataBroker(data)

    new_fx_prices = data_broker.get_fx_prices(
        fx_code)  # returns fxPrices object
    if len(new_fx_prices) == 0:
        data.log.warn("No FX prices found for %s" % fx_code)

    old_fx_prices = db_currency_data.get_fx_prices(fx_code)

    # Will break manual price checking code if not equal
    old_fx_prices.name = new_fx_prices.name = ""

    print("\n\n Manually checking prices for %s \n\n" % fx_code)
    new_prices_checked = manual_price_checker(
        old_fx_prices, new_fx_prices, type_new_data=fxPrices.from_data_frame)

    db_currency_data.update_fx_prices_and_return_rows_added(
        fx_code, new_prices_checked, check_for_spike=False)

    return success