Exemplo n.º 1
0
    def __init__(self,
                 *args,
                 fill=None,
                 locked=False,
                 order_id=no_order_id,
                 modification_status=None,
                 modification_quantity=None,
                 parent=no_parent,
                 children=no_children,
                 active=True,
                 order_type="best",
                 limit_price=None,
                 limit_contract=None,
                 reference_datetime=None,
                 reference_price=None,
                 reference_contract=None,
                 generated_datetime=None,
                 filled_price=None,
                 fill_datetime=None,
                 manual_trade=False,
                 roll_order=False):
        """

        :param args: Either a single argument 'strategy/instrument' str, or strategy, instrument; followed by trade
        i.e. instrumentOrder(strategy, instrument, trade,  **kwargs) or 'strategy/instrument', trade, type, **kwargs)

        :param fill: fill done so far, int
        :param locked: if locked an order can't be modified, bool
        :param order_id: ID given to orders once in the stack, do not use when creating order
        :param modification_status: NOT USED
        :param modification_quantity: NOT USED
        :param parent: int, order ID of parent order in upward stack
        :param children: list of int, order IDs of child orders in downward stack
        :param active: bool, inactive orders have been filled or cancelled
        :param order_type: str, type of execution required
        :param limit_price: float, limit orders only
        :param limit_contract: YYYYMM string, contract that limit price references
        :param reference_price: float, used for execution calculations
        :param reference_contract: YYYYMM string, contract that relates to reference price
        :param filled_price: float, used for execution calculations and p&l
        :param reference_datetime: datetime, when reference price captured
        :param fill_datetime: datetime used for p&l
        :param generated_datetime: when order generated
        :param manual_trade: bool, was trade iniated manually
        :param roll_order: bool, is this a roll order

        """
        """

        :param trade: float
        :param args: Either 2: strategy, instrument; or 1: instrumentTradeableObject
        :param type: str
        """
        if len(args) == 2:
            self._tradeable_object = instrumentTradeableObject.from_key(
                args[0])
            trade = args[1]
        else:
            strategy = args[0]
            instrument = args[1]
            trade = args[2]
            self._tradeable_object = instrumentTradeableObject(
                strategy, instrument)

        if generated_datetime is None:
            generated_datetime = datetime.datetime.now()

        (
            resolved_trade,
            resolved_fill,
            resolved_filled_price,
        ) = resolve_trade_fill_fillprice(trade, fill, filled_price)

        self._trade = resolved_trade
        self._fill = resolved_fill
        self._filled_price = resolved_filled_price
        self._fill_datetime = fill_datetime
        self._locked = locked
        self._order_id = order_id
        self._modification_status = modification_status
        self._modification_quantity = modification_quantity
        self._parent = parent
        self._children = children
        self._active = active

        assert order_type in possible_order_types
        self._order_info = dict(
            order_type=order_type,
            limit_contract=limit_contract,
            limit_price=limit_price,
            reference_contract=reference_contract,
            reference_price=reference_price,
            manual_trade=manual_trade,
            roll_order=roll_order,
            reference_datetime=reference_datetime,
            generated_datetime=generated_datetime,
        )
Exemplo n.º 2
0
    def __init__(self, *args, fill=None,
                 locked=False, order_id=no_order_id,
                 modification_status = None,
                 modification_quantity = None, parent=no_parent,
                 children=no_children, active=True,
                 algo_to_use="", reference_price=None,
                 limit_price = None, filled_price = None,
                 fill_datetime = None,
                 generated_datetime = None,
                 manual_fill = False, manual_trade = False,
                 roll_order = False,
                 inter_spread_order = False,
                 calendar_spread_order = None,
                 reference_of_controlling_algo = None,
                 split_order = False,
                 sibling_id_for_split_order = None
                 ):

        """
        :param args: Either a single argument 'strategy/instrument/contract_order_id' str, or strategy, instrument, contract_order_id; followed by trade
        i.e. contractOrder(strategy, instrument, contractid, trade,  **kwargs) or 'strategy/instrument/contract_order_id', trade, type, **kwargs)

        Contract_id can either be a single str or a list of str for spread orders, all YYYYMM
        If expressed inside a longer string, separate contract str by '_'

        i.e. contractOrder('a strategy', 'an instrument', '201003', 6,  **kwargs)
         same as contractOrder('a strategy/an instrument/201003', 6,  **kwargs)
        contractOrder('a strategy', 'an instrument', ['201003', '201406'], [6,-6],  **kwargs)
          same as contractOrder('a strategy/an instrument/201003_201406', [6,-6],  **kwargs)

        :param fill: fill done so far, list of int
        :param locked: if locked an order can't be modified, bool
        :param order_id: ID given to orders once in the stack, do not use when creating order
        :param modification_status: NOT USED
        :param modification_quantity: NOT USED
        :param parent: int, order ID of parent order in upward stack
        :param children: list of int, order IDs of child orders in downward stack
        :param active: bool, inactive orders have been filled or cancelled
        :param algo_to_use: str, full pathname of method to use to execute order.
        :param reference_of_controlling_algo: str, the key of the controlling algo. If None not currently controlled.
        :param limit_price: float, limit orders only
        :param reference_price: float, used to benchmark order (usually price from previous days close)
        :param filled_price: float, used for execution calculations and p&l
        :param fill_datetime: datetime used for p&l
        :param generated_datetime: datetime order generated
        :param manual_fill: bool, fill entered manually
        :param manual_trade: bool, trade entered manually
        :param roll_order: bool, part of a (or if a spread an entire) roll order. Passive rolls will be False
        :param calendar_spread_order: bool, a calendar spread (intra-market) order
        :param inter_spread_order: bool, part of an instrument order that is a spread across multiple markets
        """

        tradeable_object, trade = self._resolve_args(args)
        self._tradeable_object = tradeable_object


        if generated_datetime is None:
            generated_datetime = datetime.datetime.now()

        resolved_trade, resolved_fill, resolved_filled_price = resolve_trade_fill_fillprice(trade, fill, filled_price)

        # ensure contracts and lists all match
        resolved_trade, resolved_fill, resolved_filled_price, tradeable_object = \
            sort_by_cid(resolved_trade, resolved_fill, resolved_filled_price, tradeable_object)

        if len(resolved_trade.qty)==1:
            calendar_spread_order = False
        else:
            calendar_spread_order = True

        self._trade = resolved_trade
        self._fill = resolved_fill
        self._filled_price = resolved_filled_price

        self._fill_datetime = fill_datetime
        self._locked = locked
        self._order_id = order_id
        self._modification_status = modification_status
        self._modification_quantity = modification_quantity
        self._parent = parent
        self._children = children
        self._active = active
        self._order_info = dict(algo_to_use=algo_to_use, reference_price=reference_price,
                 limit_price = limit_price,
                                manual_trade = manual_trade, manual_fill = manual_fill,
                                roll_order = roll_order, calendar_spread_order = calendar_spread_order,
                                inter_spread_order = inter_spread_order, generated_datetime = generated_datetime,
                                reference_of_controlling_algo = reference_of_controlling_algo, split_order = split_order,
                                sibling_id_for_split_order = sibling_id_for_split_order)
Exemplo n.º 3
0
    def __init__(self,
                 *args,
                 fill=None,
                 locked=False,
                 order_id=no_order_id,
                 modification_status=None,
                 modification_quantity=None,
                 parent=no_parent,
                 children=no_children,
                 active=True,
                 algo_used="",
                 order_type="market",
                 limit_price=None,
                 filled_price=None,
                 submit_datetime=None,
                 fill_datetime=None,
                 side_price=None,
                 mid_price=None,
                 algo_comment="",
                 broker="",
                 broker_account="",
                 broker_clientid="",
                 commission=0.0,
                 broker_permid="",
                 broker_tempid="",
                 manual_fill=False,
                 calendar_spread_order=None,
                 split_order=False,
                 sibling_id_for_split_order=None,
                 roll_order=False,
                 broker_objects=dict()):
        """

        :param args: Either a single argument 'strategy/instrument/contract_id' str, or strategy, instrument, contract_id; followed by trade
        i.e. brokerOrder(strategy, instrument, contractid, trade,  **kwargs) or 'strategy/instrument/contract_id', trade, type, **kwargs)

        Contract_id can either be a single str or a list of str for spread orders, all YYYYMM
        If expressed inside a longer string, separate contract str by '_'

        i.e. brokerOrder('a strategy', 'an instrument', '201003', 6,  **kwargs)
         same as brokerOrder('a strategy/an instrument/201003', 6,  **kwargs)
        brokerOrder('a strategy', 'an instrument', ['201003', '201406'], [6,-6],  **kwargs)
          same as brokerOrder('a strategy/an instrument/201003_201406', [6,-6],  **kwargs)
        :param fill:  fill done so far, list of int
        :param locked: bool, is order locked
        :param order_id: int, my ref number
        :param modification_status: NOT USED
        :param modification_quantity: NOT USED
        :param parent: int or not supplied, parent order
        :param children: list of int or not supplied, child order ids (FUNCTIONALITY NOT USED HERE)
        :param active: bool, is order active or has been filled/cancelled
        :param algo_used: Name of the algo I used to generate the order
        :param order_type: market or limit order (other types may be supported in future)
        :param limit_price: if relevant, float
        :param filled_price: float
        :param submit_datetime: datetime
        :param fill_datetime: datetime
        :param side_price: Price on the 'side' we are submitting eg offer if buying, when order submitted
        :param mid_price: Average of bid and offer when we are submitting
        :param algo_comment: Any comment made by the algo, eg 'Aggressive', 'Passive'...
        :param broker: str, name of broker
        :param broker_account: str, brokerage account
        :param broker_clientid: int, client ID used to generate order
        :param commission: float
        :param broker_permid: Brokers permanent ref number
        :param broker_tempid: Brokers temporary ref number
        :param broker_objects: store brokers representation of objects
        :param manual_fill: bool, was fill entered manually rather than being picked up from IB

        """

        tradeable_object, trade = super()._resolve_args(args)
        self._tradeable_object = tradeable_object

        resolved_trade, resolved_fill, resolved_filled_price = resolve_trade_fill_fillprice(
            trade, fill, filled_price)

        if len(resolved_trade.qty) == 1:
            calendar_spread_order = False
        else:
            calendar_spread_order = True

        self._trade = resolved_trade
        self._fill = resolved_fill
        self._filled_price = resolved_filled_price
        self._fill_datetime = fill_datetime
        self._locked = locked
        self._order_id = order_id
        self._modification_status = modification_status
        self._modification_quantity = modification_quantity
        self._parent = parent
        self._children = children
        self._active = active

        self._order_info = dict(
            algo_used=algo_used,
            order_type=order_type,
            submit_datetime=submit_datetime,
            limit_price=limit_price,
            manual_fill=manual_fill,
            calendar_spread_order=calendar_spread_order,
            side_price=side_price,
            mid_price=mid_price,
            algo_comment=algo_comment,
            broker=broker,
            broker_account=broker_account,
            broker_permid=broker_permid,
            broker_tempid=broker_tempid,
            broker_clientid=broker_clientid,
            commission=commission,
            split_order=split_order,
            sibling_id_for_split_order=sibling_id_for_split_order,
            roll_order=roll_order)

        # NOTE: we do not include these in the normal order info dict
        # This means they will NOT be saved when we do .as_dict(), i.e. they won't be saved in the mongo record
        # This is deliberate since these objects can't be saved accordingly
        # Instead we store them only in a single session to make it easier to match and modify orders

        self._broker_objects = broker_objects