Exemplo n.º 1
0
    def build_time_series(series_id: str,
                          series_cls: str = None,
                          desc: str = None,
                          keys: List[str] = None,
                          inputs: List = None,
                          input_keys: Dict[str, List[str]] = None,
                          default_output_key: str = 'value',
                          missing_value_replace: float = 0.0,
                          **kwargs) -> TimeSeries:
        time_series = TimeSeries()
        time_series.series_id = series_id
        if series_cls:
            time_series.series_cls = series_cls
        if desc:
            time_series.desc = desc
        add_to_list(time_series.keys, keys)
        input_keys = convert_input_keys(inputs, input_keys)
        if inputs:
            for input_name in convert_input(inputs):
                input_key = input_keys.get(input_name, None)

                ModelFactory.add_time_series_input(time_series, input_name,
                                                   input_key)
        time_series.default_output_key = default_output_key
        time_series.missing_value_replace = missing_value_replace

        for key, value in kwargs.items():
            time_series.configs[key] = str(value)
        return time_series
Exemplo n.º 2
0
 def add_time_series_input(time_series: TimeSeries,
                           source: str,
                           keys: List[str] = None) -> TimeSeries.Input:
     time_series_input = time_series.inputs.add()
     time_series_input.source = source
     if keys:
         add_to_list(time_series_input.keys, keys)
     return time_series_input
Exemplo n.º 3
0
    def add_order(self, inst_id: str, cl_id: str, cl_ord_id: str,
                  ordered_qty: float) -> None:
        add_to_list(self.state.cl_ord_ids, [cl_ord_id])

        position = self.get_position(inst_id)
        order_position = self.__get_or_add_order(position=position,
                                                 cl_id=cl_id,
                                                 cl_ord_id=cl_ord_id)
        order_position.ordered_qty = ordered_qty
        position.ordered_qty += ordered_qty
Exemplo n.º 4
0
    def add(self,
            timestamp: int = None,
            data: Dict[str, float] = None,
            init: bool = False) -> None:
        timestamp = timestamp if timestamp is not None else data.get(
            DataSeries.TIMESTAMP)

        if not self.time_series.keys:
            add_to_list(self.time_series.keys, list(data.keys()))

        if not self.time_series.start_time:
            self.time_series.start_time = timestamp

        enhanced_data = {}
        if not self.time_series.end_time \
                or timestamp > self.time_series.end_time \
                or len(self.data_list) == 0:

            self.time_list.append(timestamp)

            for key in list(self.time_series.keys):
                value = data.get(key, self.time_series.missing_value_replace)
                if key not in self.data_time_dict:
                    self.data_time_dict[key] = {}
                self.data_time_dict[key][timestamp] = value
                enhanced_data[key] = value

            if not init:
                self.last_item = ModelFactory.add_time_series_item(
                    self.time_series, timestamp, enhanced_data)

        elif timestamp == self.time_series.end_time:

            enhanced_data = self.data_list.pop()
            for key in list(self.time_series.keys):
                if key in data:
                    value = data.get(key)
                    if key not in self.data_time_dict:
                        self.data_time_dict[key] = {}
                    self.data_time_dict[key][timestamp] = value
                    enhanced_data[key] = value
            if not init:
                ModelFactory.update_time_series_item(self.last_item, timestamp,
                                                     enhanced_data)
        else:
            raise AssertionError(
                "Time for new Item %s cannot be earlier then previous item %s"
                % (timestamp, self.current_time()))

        self.time_series.end_time = timestamp
        self.data_list.append(enhanced_data)
        self.subject.on_next(
            ModelFactory.build_time_series_update_event(source=self.name,
                                                        timestamp=timestamp,
                                                        data=data))
Exemplo n.º 5
0
    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