def state_change_to_roll_adjusted_prices(
    data: dataBlob,
    instrument_code: str,
    original_roll_state: RollState,
    confirm_adjusted_price_change: bool = True,
):
    # Going to roll adjusted prices
    update_positions = updatePositions(data)

    roll_result = roll_adjusted_and_multiple_prices(
        data=data,
        instrument_code=instrument_code,
        confirm_adjusted_price_change=confirm_adjusted_price_change,
    )

    if roll_result is success:
        # Return the state back to default (no roll) state
        data.log.msg(
            "Successful roll! Returning roll state of %s to %s"
            % (instrument_code, default_state)
        )

        update_positions.set_roll_state(instrument_code, default_state)
    else:
        data.log.msg(
            "Something has gone wrong with rolling adjusted of %s! Returning roll state to previous state of %s"
            % (instrument_code, original_roll_state)
        )
        update_positions.set_roll_state(instrument_code, original_roll_state)
def modify_roll_state(
    data: dataBlob,
    instrument_code: str,
    original_roll_state: RollState,
    roll_state_required: RollState,
    confirm_adjusted_price_change: bool = True,
):

    if roll_state_required is no_change_required:
        return

    if roll_state_required is original_roll_state:
        return

    update_positions = updatePositions(data)

    if original_roll_state is roll_close_state:
        roll_state_was_closed_now_something_else(data, instrument_code)

    update_positions.set_roll_state(instrument_code, roll_state_required)
    if roll_state_required is roll_adj_state:
        state_change_to_roll_adjusted_prices(
            data=data,
            instrument_code=instrument_code,
            original_roll_state=original_roll_state,
            confirm_adjusted_price_change=confirm_adjusted_price_change,
        )

    if roll_state_required is roll_close_state:
        roll_state_is_now_closing(data, instrument_code)
Пример #3
0
def updated_buffered_positions(data, strategy_name, system):
    log = data.log

    update_positions = updatePositions(data)

    list_of_instruments = system.get_instrument_list()
    for instrument_code in list_of_instruments:
        try:
            lower_buffer, upper_buffer = get_position_buffers_from_system(
                system, instrument_code)
            position_entry = construct_position_entry(data, system,
                                                      instrument_code,
                                                      lower_buffer,
                                                      upper_buffer)
            update_positions.update_optimal_position_for_strategy_and_instrument(
                strategy_name, instrument_code, position_entry)
            log.msg(
                "New buffered positions %.3f %.3f" %
                (position_entry.lower_position, position_entry.upper_position),
                instrument_code=instrument_code)
        except Exception as e:
            log.critical("Couldn't get or update buffered positions error %s" %
                         e,
                         instrument_code=instrument_code)

    return success
Пример #4
0
    def handle_completed_instrument_order(self, instrument_order_id):
        ## Check children all done
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(instrument_order_id)

        list_of_broker_order_id, list_of_contract_order_id = self.\
            get_all_children_and_grandchildren_for_instrument_order_id(instrument_order_id)

        completely_filled = self.\
            confirm_all_children_and_grandchildren_are_filled(list_of_broker_order_id, list_of_contract_order_id)

        if not completely_filled:
            return success

        # If we have got this far then all our children are filled, and the parent is filled
        contract_order_list = self.contract_stack.get_list_of_orders_from_order_id_list(list_of_contract_order_id)
        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(list_of_broker_order_id)

        # update positions
        position_updater = updatePositions(self.data)
        position_updater.update_positions_with_instrument_and_contract_orders(instrument_order, contract_order_list)

        # Update historic order database
        order_data = dataOrders(self.data)
        order_data.add_historic_orders_to_data(instrument_order, contract_order_list, broker_order_list)

        # Make orders inactive
        # A subsequent process will delete them
        self.deactivate_family_of_orders(instrument_order_id, list_of_contract_order_id, list_of_broker_order_id)

        return success
Пример #5
0
    def handle_completed_instrument_order(self,
                                          instrument_order_id,
                                          allow_partial_completions=False,
                                          allow_zero_completions=False):
        ## Check children all done

        list_of_broker_order_id, list_of_contract_order_id = self.\
            get_all_children_and_grandchildren_for_instrument_order_id(instrument_order_id)

        completely_filled = self.\
            confirm_all_children_and_grandchildren_are_filled(list_of_broker_order_id, list_of_contract_order_id,
                                                              allow_partial_completions=allow_partial_completions,
                                                              allow_zero_completions = allow_zero_completions)

        if not completely_filled:
            return success

        # If we have got this far then all our children are filled, and the parent is filled

        # We need to seperate out broker and contract spread orders into their individual components
        # When we do this the original orders are deactivated
        # the instrument order has it's children removed and replaced by the components of each spread order
        # the contract order
        self.split_up_spread_orders(instrument_order_id)

        # we need to do this again in case of any splits
        list_of_broker_order_id, list_of_contract_order_id = self.\
            get_all_children_and_grandchildren_for_instrument_order_id(instrument_order_id)

        contract_order_list = self.contract_stack.get_list_of_orders_from_order_id_list(
            list_of_contract_order_id)
        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(
            list_of_broker_order_id)
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(
            instrument_order_id)

        # Make orders inactive
        # A subsequent process will delete them
        self.deactivate_family_of_orders(instrument_order_id,
                                         list_of_contract_order_id,
                                         list_of_broker_order_id)

        # update positions
        position_updater = updatePositions(self.data)
        position_updater.update_positions_with_instrument_and_contract_orders(
            instrument_order, contract_order_list)

        # Update historic order database
        order_data = dataOrders(self.data)
        order_data.add_historic_orders_to_data(instrument_order,
                                               contract_order_list,
                                               broker_order_list)

        return success
Пример #6
0
    def apply_position_change_to_instrument(self, instrument_order, total_filled_qty, apply_entire_trade= False):
        current_fill = instrument_order.fill

        if apply_entire_trade:
            new_fill = current_fill
        else:
            if total_filled_qty==current_fill:
                ## no change needed here
                return success

            new_fill = total_filled_qty - current_fill

        position_updater = updatePositions(self.data)
        result = position_updater.update_strategy_position_table_with_instrument_order(instrument_order, new_fill)

        return result
def interactive_update_roll_status():
    """
    Update the roll state for a particular instrument
    This includes the option, where possible, to switch the adjusted price series on to a new contract

    :param instrument_code: str
    :return: None
    """

    with dataBlob(log_name="Interactive_Update-Roll-Status") as data:
        instrument_code = get_valid_instrument_code_from_user(data=data)
        # First get the roll info
        # This will also update to console
        config = roll_report_config.new_config_with_modified_output("console")
        config.modify_kwargs(instrument_code=instrument_code)
        report_results = run_report_with_data_blob(config, data)
        if report_results is failure:
            print("Can't run roll report, so can't change status")
            return failure

        current_roll_status, roll_state_required = get_required_roll_state(
            data, instrument_code)
        if roll_state_required is no_state_available:
            return failure

        update_positions = updatePositions(data)
        update_positions.set_roll_state(instrument_code, roll_state_required)

        if roll_state_required is roll_adj_state:
            # Going to roll adjusted prices
            roll_result = _roll_adjusted_and_multiple_prices(
                data, instrument_code)
            if roll_result is success:
                # Return the state back to default (no roll) state
                data.log.msg(
                    "Successful roll! Returning roll state of %s to %s" %
                    (instrument_code, default_state))
                update_positions.set_roll_state(instrument_code, default_state)
            else:
                data.log.msg(
                    "Something has gone wrong with rolling adjusted of %s! Returning roll state to previous state of %s"
                    % (instrument_code, current_roll_status))
                update_positions.set_roll_state(instrument_code,
                                                current_roll_status)

        return success
    def handle_completed_instrument_order(self, instrument_order_id):
        ## Check children all done
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(
            instrument_order_id)
        list_of_contract_order_id = instrument_order.children
        if list_of_contract_order_id is no_children:
            list_of_contract_order_id = []

        for contract_order_id in list_of_contract_order_id:
            completely_filled = self.contract_stack.is_completed(
                contract_order_id)
            if not completely_filled:
                ## OK We can't do this unless all our children are filled
                return success

        # If we have got this far then all our children are filled, and the parent is filled

        list_of_contract_orders = []
        for contract_order_id in list_of_contract_order_id:
            list_of_contract_orders.append(
                self.contract_stack.get_order_with_id_from_stack(
                    contract_order_id))

        position_updater = updatePositions(self.data)
        # Update strategy position table
        position_updater.update_strategy_position_table_with_instrument_order(
            instrument_order)

        # Update contract position table
        for contract_order in list_of_contract_orders:
            position_updater.update_contract_position_table_with_contract_order(
                contract_order)

        order_data = dataOrders(self.data)
        # Update historic order database
        order_data.add_historic_instrument_order_to_data(instrument_order)
        for contract_order in list_of_contract_orders:
            order_data.add_historic_contract_order_to_data(contract_order)

        # Make orders inactive
        # A subsequent process will delete them
        self.instrument_stack.deactivate_order(instrument_order_id)
        for contract_order_id in list_of_contract_order_id:
            self.contract_stack.deactivate_order(contract_order_id)

        return success
Пример #9
0
    def apply_position_change_to_instrument(
        self, original_instrument_order: instrumentOrder,
            total_filled_qty: tradeQuantity,
            apply_entire_trade: bool=False
    ):
        current_fill = original_instrument_order.fill

        if apply_entire_trade:
            new_fill = current_fill
        else:
            new_fill = total_filled_qty - current_fill

        if new_fill.equals_zero():
            return None

        position_updater = updatePositions(self.data)
        position_updater.update_strategy_position_table_with_instrument_order(
            original_instrument_order, new_fill)
Пример #10
0
    def apply_position_change_to_contracts(
        self, contract_order, total_filled_qty, apply_entire_trade=False
    ):
        current_fills = contract_order.fill

        if apply_entire_trade:
            new_fills = current_fills
        else:
            if total_filled_qty == current_fills:
                # no change needed here
                return success

            new_fills = total_filled_qty - current_fills

        position_updater = updatePositions(self.data)
        result = position_updater.update_contract_position_table_with_contract_order(
            contract_order, new_fills)

        return result
Пример #11
0
    def apply_position_change_to_stored_contract_positions(
        self, contract_order_before_fill: contractOrder,
            total_filled_qty: tradeQuantity,
            apply_entire_trade: bool=False
    ):
        current_fills = contract_order_before_fill.fill

        if apply_entire_trade:
            # used for balance trades
            new_fills = current_fills
        else:
            new_fills = total_filled_qty - current_fills

        if new_fills.equals_zero():
            # nothing to do
            return None

        position_updater = updatePositions(self.data)
        position_updater.update_contract_position_table_with_contract_order(
            contract_order_before_fill, new_fills)
def modify_roll_state(data: dataBlob, instrument_code: str,
                      roll_state_required: RollState):
    update_positions = updatePositions(data)
    update_positions.set_roll_state(instrument_code, roll_state_required)