示例#1
0
def enter_manual_instrument_order(data):
    strategy_name = get_valid_strategy_name_from_user(data=data,
                                                      source="positions")
    instrument_code = get_valid_instrument_code_from_user(data)
    qty = get_and_convert("Quantity (-ve for sell, +ve for buy?)",
                          type_expected=int,
                          allow_default=False)
    possible_order_types = market_order_type.allowed_types()
    order_type = input("Order type (one of %s)?" % str(possible_order_types))
    limit_price = get_and_convert(
        "Limit price? (if you put None you can still add one to the contract order)",
        type_expected=float,
        default_value=None,
        default_str="None",
    )
    if limit_price is None:
        limit_contract = None
    else:
        print("Enter contractid that limit price is referenced to")
        _, contract_date = get_valid_instrument_code_and_contractid_from_user(
            data, instrument_code=instrument_code)
        limit_contract = contract_date

    instrument_order = instrumentOrder(
        strategy_name,
        instrument_code,
        qty,
        order_type=instrumentOrderType(order_type),
        limit_price=limit_price,
        limit_contract=limit_contract,
        manual_trade=True,
        roll_order=False,
    )

    return instrument_order
def create_instrument_balance_trade(data):
    data_broker = dataBroker(data)
    default_account = data_broker.get_broker_account()

    print("Use to fix breaks between instrument strategy and contract level positions")
    strategy_name = get_valid_strategy_name_from_user(data=data, source="positions")
    instrument_code = get_valid_instrument_code_from_user(data)
    fill_qty = get_and_convert("Quantity ", type_expected=int, allow_default=False)
    filled_price = get_and_convert(
        "Filled price", type_expected=float, allow_default=False
    )
    fill_datetime = get_datetime_input("Fill datetime", allow_default=True)

    instrument_order = instrumentOrder(
        strategy_name,
        instrument_code,
        fill_qty,
        fill=fill_qty,
        order_type=instrument_balance_order_type,
        filled_price=filled_price,
        fill_datetime=fill_datetime,
    )

    print(instrument_order)
    ans = input("Are you sure? (Y/other)")
    if ans != "Y":
        return None

    stack_handler = stackHandlerCreateBalanceTrades(data)

    stack_handler.create_balance_instrument_trade(instrument_order)
    def does_strategy_and_instrument_already_have_order_on_stack(
            self, strategy_name: str, instrument_code: str) -> bool:
        pseudo_order = instrumentOrder(strategy_name, instrument_code, 0)
        existing_orders = self._get_list_of_orderids_with_same_tradeable_object_on_stack(
            pseudo_order)
        if existing_orders is missing_order:
            return False

        return True
示例#4
0
def trade_given_optimal_and_actual_positions(
    data: dataBlob,
        strategy_name: str,
        instrument_code: str,
        optimal_positions: optimalPositions,
        actual_positions: dict
) -> instrumentOrder:


    upper_for_instrument = optimal_positions.upper_positions[instrument_code]
    lower_for_instrument = optimal_positions.lower_positions[instrument_code]
    actual_for_instrument = actual_positions.get(instrument_code, 0.0)

    if actual_for_instrument < lower_for_instrument:
        required_position = round(lower_for_instrument)
    elif actual_for_instrument > upper_for_instrument:
        required_position = round(upper_for_instrument)
    else:
        required_position = actual_for_instrument

    # Might seem weird to have a zero order, but since orders can be updated
    # it makes sense

    trade_required = required_position - actual_for_instrument

    reference_contract = optimal_positions.reference_contracts[instrument_code]
    reference_price = optimal_positions.reference_prices[instrument_code]


    ref_date = optimal_positions.ref_dates[instrument_code]

    # No limit orders, just best execution
    order_required = instrumentOrder(
        strategy_name,
        instrument_code,
        trade_required,
        order_type=best_order_type,
        reference_price=reference_price,
        reference_contract=reference_contract,
        reference_datetime=ref_date,
    )

    log = order_required.log_with_attributes(data.log)
    log.msg(
        "Upper %.2f Lower %.2f Current %d Required position %d Required trade %d Reference price %f  for contract %s" %
        (upper_for_instrument,
         lower_for_instrument,
         actual_for_instrument,
         required_position,
         trade_required,
         reference_price,
         reference_contract,
         ))

    return order_required
示例#5
0
def create_balance_instrument_order_from_contract_order(contract_order):
    instrument_order = instrumentOrder(
        contract_order.strategy_name,
        contract_order.instrument_code,
        contract_order.trade[0],
        fill=contract_order.fill[0],
        filled_price=contract_order.filled_price,
        fill_datetime=contract_order.fill_datetime,
        manual_trade=True,
        active=False,
        order_type=balance_order_type_for_instrument_orders)
    return instrument_order
示例#6
0
def balance_trade(data, strategy_name: str, instrument_code: str,
                  fill_qty: int, filled_price: float):
    instrument_order = instrumentOrder(
        strategy_name,
        instrument_code,
        fill_qty,
        fill=fill_qty,
        order_type=transfer_order_type,
        filled_price=filled_price,
    )

    stack_handler = stackHandlerCreateBalanceTrades(data)

    stack_handler.create_balance_instrument_trade(instrument_order)
示例#7
0
def create_instrument_roll_order(roll_spread_info: rollSpreadInformation ,
                                 instrument_code: str) -> instrumentOrder:
    strategy = ROLL_PSEUDO_STRATEGY
    trade = 0
    instrument_order = instrumentOrder(
        strategy,
        instrument_code,
        trade,
        roll_order=True,
        order_type=zero_roll_order_type,
        reference_price=roll_spread_info.reference_price_spread,
    reference_contract=ROLL_PSEUDO_STRATEGY,
    reference_datetime=roll_spread_info.reference_date)

    return instrument_order
示例#8
0
def trade_given_optimal_and_actual_positions(
    data: dataBlob,
    strategy_name: str,
    instrument_code: str,
    optimised_position_entry: optimalPositionWithDynamicCalculations,
    current_position: int,
) -> instrumentOrder:

    optimised_position = optimised_position_entry.optimised_position

    trade_required = optimised_position - current_position

    reference_contract = optimised_position_entry.reference_contract
    reference_price = optimised_position_entry.reference_price
    reference_date = optimised_position_entry.reference_date

    # No limit orders, just best execution
    order_required = instrumentOrder(
        strategy_name,
        instrument_code,
        trade_required,
        order_type=best_order_type,
        reference_price=reference_price,
        reference_contract=reference_contract,
        reference_datetime=reference_date,
    )

    log = order_required.log_with_attributes(data.log)
    log.msg(
        "Current %d Required position %d Required trade %d Reference price %f  for contract %s"
        % (
            current_position,
            optimised_position,
            trade_required,
            reference_price,
            reference_contract,
        ))

    return order_required