def build_order_replace_request(
            timestamp: int,
            cl_id: str,
            cl_ord_id: str,
            cl_orig_req_id: str,
            type: OrderType = None,
            qty: float = None,
            limit_price: float = None,
            stop_price: float = None,
            tif: TIF = DAY,
            oca_tag: str = None,
            params: Dict[str, str] = None) -> OrderReplaceRequest:
        req = OrderReplaceRequest()

        req.timestamp = timestamp
        req.cl_id = str(cl_id)
        req.cl_ord_id = str(cl_ord_id)
        req.cl_orig_req_id = str(cl_orig_req_id)
        if type:
            req.type = type
        if qty:
            req.qty = qty
        if limit_price:
            req.limit_price = limit_price
        if stop_price:
            req.stop_price = stop_price
        if tif:
            req.tif = tif
        if oca_tag:
            req.oca_tag = oca_tag
        add_to_dict(req.params, params)

        return req
 def update_time_series_item(
         item: TimeSeriesItem,
         timestamp: int = None,
         data: Dict[str, float] = None) -> TimeSeriesItem:
     item.timestamp = timestamp
     add_to_dict(item.data, data)
     return item
    def build_order_cancel_request(
            timestamp: int,
            cl_id: str,
            cl_ord_id: str,
            cl_orig_req_id: str,
            params: Dict[str, str] = None) -> OrderCancelRequest:
        req = OrderCancelRequest()
        req.timestamp = timestamp
        req.cl_id = str(cl_id)
        req.cl_ord_id = str(cl_ord_id)
        req.cl_orig_req_id = str(cl_orig_req_id)
        add_to_dict(req.params, params)

        return req
 def build_exchange(exch_id: str,
                    name: str = None,
                    country_id: str = None,
                    trading_hours_id: str = None,
                    holidays_id: str = None,
                    alt_ids: Dict[str, str] = None) -> Exchange:
     exchange = Exchange()
     exchange.exch_id = str(exch_id)
     if name:
         exchange.name = name
     if country_id:
         exchange.country_id = str(country_id)
     if trading_hours_id:
         exchange.trading_hours_id = str(trading_hours_id)
     if holidays_id:
         exchange.holidays_id = str(holidays_id)
     add_to_dict(exchange.alt_ids, alt_ids)
     return exchange
    def build_new_order_request(
            timestamp: int,
            cl_id: str,
            cl_ord_id: str,
            portf_id: str = None,
            broker_id: str = None,
            inst_id: str = None,
            action: OrderAction = None,
            type: OrderType = None,
            qty: float = None,
            limit_price: float = None,
            stop_price: float = None,
            tif: TIF = DAY,
            oca_tag: str = None,
            params: Dict[str, str] = None) -> NewOrderRequest:
        req = NewOrderRequest()

        req.timestamp = timestamp
        req.cl_id = str(cl_id)
        req.cl_ord_id = str(cl_ord_id)
        if portf_id:
            req.portf_id = str(portf_id)
        if broker_id:
            req.broker_id = str(broker_id)
        if inst_id:
            req.inst_id = str(inst_id)
        if action:
            req.action = action
        if type:
            req.type = type
        if qty:
            req.qty = qty
        if limit_price:
            req.limit_price = limit_price
        if stop_price:
            req.stop_price = stop_price
        if tif:
            req.tif = tif
        if oca_tag:
            req.oca_tag = oca_tag
        add_to_dict(req.params, params)

        return req
    def build_order_state(
            cl_id: str,
            cl_ord_id: str,
            portf_id: str,
            broker_id: str,
            inst_id: str,
            creation_timestamp: int = None,
            action: OrderAction = None,
            type: OrderType = None,
            qty: float = None,
            limit_price: float = None,
            stop_price: float = None,
            tif: TIF = DAY,
            oca_tag: str = None,
            params: Dict[str, str] = None,
            broker_ord_id: str = None,
            update_timestamp: int = None,
            status: OrderStatus = New,
            filled_qty: float = 0,
            avg_price: float = 0,
            last_qty: float = 0,
            last_price: float = 0,
            stop_limit_ready: bool = False,
            trailing_stop_exec_price: float = None) -> OrderState:
        order = OrderState()
        order.cl_id = str(cl_id)
        order.cl_ord_id = str(cl_ord_id)
        order.portf_id = str(portf_id)
        order.broker_id = str(broker_id)
        if broker_ord_id:
            order.broker_ord_id = str(broker_ord_id)
        order.inst_id = str(inst_id)

        if creation_timestamp:
            order.creation_timestamp = creation_timestamp
        if update_timestamp:
            order.update_timestamp = update_timestamp
        if action:
            order.action = action
        if type:
            order.type = type
        if qty:
            order.qty = qty
        if limit_price:
            order.limit_price = limit_price
        if stop_price:
            order.stop_price = stop_price
        if tif:
            order.tif = tif
        if oca_tag:
            order.oca_tag = oca_tag
        add_to_dict(order.params, params)

        if status:
            order.status = status
        if filled_qty:
            order.filled_qty = filled_qty
        if avg_price:
            order.avg_price = avg_price
        if last_qty:
            order.last_qty = last_qty
        if last_price:
            order.last_price = last_price
        if stop_limit_ready:
            order.stop_limit_ready = stop_limit_ready
        if trailing_stop_exec_price:
            order.trailing_stop_exec_price = trailing_stop_exec_price

        return order
 def update_account_value(value: AccountValue, key: str,
                          ccy_values: Dict[str, float]) -> AccountValue:
     value.key = key
     add_to_dict(value.ccy_values, ccy_values)
     return value
    def build_instrument(symbol: str,
                         type: Instrument.InstType,
                         primary_exch_id: str,
                         ccy_id: str,
                         name: str = None,
                         exch_ids: List[str] = None,
                         sector: str = None,
                         industry: str = None,
                         margin: float = None,
                         tick_size: float = None,
                         alt_symbols: Dict[str, str] = None,
                         alt_ids: Dict[str, str] = None,
                         alt_sectors: Dict[str, str] = None,
                         alt_industries: Dict[str, str] = None,
                         underlying_type: Underlying.UnderlyingType = None,
                         underlying_ids: List[str] = None,
                         underlying_weights: List[float] = None,
                         option_type: Instrument.OptionType = None,
                         option_style: Instrument.OptionStyle = None,
                         strike: float = None,
                         exp_date: int = None,
                         multiplier: float = None) -> Instrument:
        inst = Instrument()
        inst.inst_id = symbol + '@' + primary_exch_id
        inst.symbol = symbol
        if isinstance(type, int):
            inst.type = type
        else:
            inst.type = Instrument.InstType.DESCRIPTOR.values_by_name[
                type].number
        inst.primary_exch_id = str(primary_exch_id)
        inst.ccy_id = str(ccy_id)

        if name:
            inst.name = name

        add_to_list(inst.exch_ids, exch_ids)

        if sector:
            inst.sector = sector

        if industry:
            inst.industry = industry

        if margin:
            inst.margin = margin

        if tick_size:
            inst.tick_size = tick_size

        add_to_dict(inst.alt_symbols, alt_symbols)
        add_to_dict(inst.alt_ids, alt_ids)
        add_to_dict(inst.alt_sectors, alt_sectors)
        add_to_dict(inst.alt_industries, alt_industries)

        if underlying_type and underlying_ids:
            ModelFactory.build_underlying(inst, underlying_type,
                                          underlying_ids, underlying_weights)

            if option_type:
                inst.option_type = option_type
            if option_style:
                inst.option_style = option_style
            if strike:
                inst.strike = strike
            if exp_date:
                inst.exp_date = exp_date
            if multiplier:
                inst.multiplier = multiplier

        return inst