Exemplo n.º 1
0
    def _send_orders(self, orders):
        """
        Creates *corresponding* orders to the list of orders received
        :param orders: list of favourable orders
        """

        if not self.is_session_active():
            return

        # REACTIVE ORDERS
        for order in orders:

            price_tick = order.market.price_tick

            new_order = Order.create_new()
            new_order.price = order.price - (order.price % price_tick)
            new_order.market = order.market
            new_order.units = 1

            if order.order_side == OrderSide.BUY:
                new_order.order_side = OrderSide.SELL
            else:
                new_order.order_side = OrderSide.BUY

                # don't send invalid orders, kill switch for edge cases
                if new_order.price > self._cash_available:
                    return

            new_order.order_type = OrderType.LIMIT
            new_order.ref = f"Order {self._order_id} - SM"
            self._order_id += 1
            self.send_order(new_order)
Exemplo n.º 2
0
    def _send_valid_mm_orders(self):
        """
        Executes list of valid market maker orders
        :return: List[Order] of valid orders
        """
        for key in self._mm_orders:

            order = Order.create_new()
            order.market = Market(self._market_ids[key[0]])
            price_tick = order.market.price_tick

            price = self._mm_orders[key] - \
                          (self._mm_orders[key] % price_tick)

            if price <= order.market.min_price:
                price = order.market.min_price
            elif price >= order.market.max_price:
                price = order.market.max_price

            order.price = price

            order.order_side = key[-1]
            order.order_type = OrderType.LIMIT
            order.units = 1

            if order.market.min_price <= order.price <= order.market.max_price:
                self._order_id += 1
                self._num_orders_sent += 1
                self._num_active_mm_orders += 1
                self.send_order(order)
        return
Exemplo n.º 3
0
    def _create_new_order(self,
                          price: int,
                          units: int,
                          order_side: OrderSide,
                          order_type: OrderType,
                          ref: str,
                          is_private: bool):
        """
        :param price: determined price
        :param units: # of units to be submitted, fixed to 1
        :param order_side: buy or sell
        :param order_type: limit or market, fixed to limit
        :param ref: customer string of format "Order: {self._sent_order_count} - SM"
        :return:
        """
        new_order = Order.create_new()

        if is_private:
            market = self._private_market_id
            new_order.owner_or_target = "M000"
        else:
            market = self._public_market_id

        new_order.market = Market(market)
        new_order.price = price
        new_order.units = units
        new_order.order_side = order_side
        new_order.order_type = order_type
        new_order.ref = ref

        self.send_order(new_order)
        self._waiting_for_server = True
Exemplo n.º 4
0
    def _get_order(self, order_side):
        """
        Takes a request for a either a buy or sell order, the type of which is
        specified by order_side. Returns an order.

        :param order_side: either OrderSide.BUY or OrderSide.SELL
        """
        action = self.BUY
        if order_side == OrderSide.SELL:
            action = self.SELL
        self.Vexperimentation(action)
        self.updateW(action)
        self.replicate(action)
        self.strat_selection(action)
        if order_side == OrderSide.SELL and (
                self.curr_strat[self.SELL] < self.sl
                or self.curr_strat[self.SELL] > self.su):
            raise Exception(
                'Price should not be less than the minimum or greater than the maximum when selling.'
            )
        if order_side == OrderSide.BUY and (
                self.curr_strat[self.BUY] < self.sl
                or self.curr_strat[self.BUY] > self.su):
            raise Exception(
                'Price should not be less than the minimum or greater than the maximum when selling.'
            )
        price = max(self.sl, self.curr_strat[action])
        order = Order.create_new(self.markets[self._market_id])
        order.price = price
        order.units = self._units
        order.order_type = OrderType.LIMIT
        order.order_side = order_side
        return order
Exemplo n.º 5
0
    def received_orders(self, orders: List[Order]):
        orders_avail = orders
        for o in orders_avail:
            # inform method just logs the messages
            self.inform(o)
            if o.mine:
                self.inform(o.owner, o.owner_or_target)

            # cancelling an order, make sure to check all orders sent are accepted
            # if o.mine:
            #     cancel_order = copy.copy(o)
            #     cancel_order.order_type = OrderType.CANCEL
            #     self.send_order(cancel_order)

        # order has attribute mine (bool) to know if our order or not

        # order.all() fetches all previous orders, and we don't need to physically track all changes,
        # we can query this order.all() data to find the status of the order
        # all_orders = Order.all()

        if not self._order_sent:
            order = Order.create_new()
            order.market = Market(1573)
            order.order_side = OrderSide.BUY
            order.order_type = OrderType.LIMIT
            order.price = 100
            order.ref = "rip"
            order.units = 1

            # we will know this owner/target from the private orders we receive
            # Manager is always M000

            order.owner_or_target = "T002"  # sending private orders, the market code should be changed to 1573
            self.send_order(order)
            self._order_sent = True
Exemplo n.º 6
0
 def _make_order(self, market_id, oside, price, otype = OrderType.LIMIT, \
     units = 1):
     """
     Makes and returns Order object with the defined parameters
     """
     new_order = Order.create_new()
     new_order.mine = True
     new_order.market = Market(market_id)
     new_order.order_side = oside
     new_order.order_type = otype
     new_order.price = price
     new_order.units = units
     new_order.ref = f'{otype} Order in {new_order.market.fm_id} \
          for {new_order.units}@{new_order.price}'
     return new_order
Exemplo n.º 7
0
    def  Go_create_new_order(self,
                          price: int,
                          units: int,
                          order_side: OrderSide,
                          order_type: OrderType,
                          ref: str,
                          is_private: bool):
        """
        Creates a new order given the parameters
        :param price: determined price
        :param units: # of units to be submitted, fixed to 1
        :param order_side: buy or sell
        :param order_type: limit or market, fixed to limit
        :param ref: customer string of format "Order: {self._sent_order_count} - SM"
        :return:
        """

        new_order = Order.create_new()

        # derive order attributes
        if is_private:
            market = self._private_market_id
            new_order.owner_or_target = MANAGER_ID
        else:
            market = self._public_market_id

            # make sure price is rounded in the public market
            price -= price % self._price_tick

            # in case price goes less than the tick, or more than ten
            # set prices to the bounds
            if price < self._price_tick:
                price = self._price_tick
            elif price > MAX_PRICE:
                price = MAX_PRICE

        new_order.market = Market(market)
        new_order.price = price
        new_order.units = units
        new_order.order_side = order_side
        new_order.order_type = order_type
        new_order.ref = ref

        # send order through
        self._waiting_for_server = True
        self.send_order(new_order)

        self._tradeID += 1
Exemplo n.º 8
0
    def _market_making_strategy(self):
        """
        MARKET MAKER STRATEGY
        =====================
        Simulate acquiring/disposing off one unit of each security to calculate
        the change in performance because of this balancing.

        This change in performance is determined to be the price the security
        should by bought/sold for (accounts for changing variance and
        changing expected return)

        That price is then adjusted by a PROFIT MARGIN, and submitted to the
        market
        """
        # do nothing if sending through orders
        if self._waiting or (len(self._asset_units.keys()) == 0):
            return

        self._mm_orders = {}

        # acquire list of all securities
        securities = list(self._payoffs.keys())

        # for each security, determine the prices of valid buys and sells that
        # can be created in the market
        order = Order.create_new()
        order.price = 0

        for security in securities:

            order.market = Market(self._market_ids[security])

            # create quasi buy order
            order.order_side = OrderSide.BUY
            self.is_portfolio_optimal([order])

            # create quasi sell order
            order.order_side = OrderSide.SELL
            self.is_portfolio_optimal([order])

        # filter out orders from potential orders that can't be executed
        # due to not enough cash / units
        self._remove_invalid_mm_orders()

        # # send valid market maker orders in the market
        if not self._waiting:
            self._waiting = True
            self._send_valid_mm_orders()
Exemplo n.º 9
0
    def make_order(self, market_id, oside, price, otype = OrderType.LIMIT, \
        units = 1):
        """
        Makes and returns Order object with the defined parameters
        """
        new_order = Order.create_new()
        new_order.market = Market(market_id)
        new_order.order_side = oside
        new_order.order_type = otype
        new_order.price = price
        new_order.units = units
        new_order.ref = f'{otype} Order in {new_order.market} \
             for {new_order.units}@{new_order.price}'

        if market_id == self._private_market_id:
            new_order.owner_or_target = "M000"
        return new_order
Exemplo n.º 10
0
    def _sell_notes(self):
        """
        Determines if current cash level is below arbitrary threshold,
        if it yes, sells notes at a small loss
        :return:
        """
        # stop if initialising OR if have enough cash
        if len(self._asset_units.keys()) == 0 or \
                self._cash_available > MIN_CASH_THRESHOLD:
            return

        # find the note key
        note_key = ""
        for asset in self._asset_units:
            if asset.lower() == "note":
                note_key = asset

        # check if have notes available to short
        if self._asset_units[note_key] > self._short_units_allowed[note_key]:
            self.inform(f"Cash avail: {self._cash_available}")
            self.inform(f"Selling notes+++++++++++++++++++++++++++")

            # create note sell orders for some price to obtain quick cash
            market = Market(self._market_ids[note_key])
            price_tick = market.price_tick
            new_order = Order.create_new()
            new_order.price = NOTE_SELLING_PRICE - \
                              (NOTE_SELLING_PRICE % price_tick)
            new_order.market = market
            new_order.units = 1
            new_order.order_side = OrderSide.SELL
            new_order.order_type = OrderType.LIMIT

            new_order.ref = "Order need_cash - SM"

            self._waiting = True
            self._order_id += 1
            self._num_orders_sent += 1
            self.send_order(new_order)
Exemplo n.º 11
0
    def _create_new_order(self, market: int, price: int, units: int,
                          order_side: OrderSide, order_type: OrderType,
                          ref: str):
        """
        :param market: market ID for the order
        :param price: determined price
        :param units: # of units to be submitted, fixed to 1
        :param order_side: buy or sell
        :param order_type: limit or market, fixed to limit
        :param ref: customer string of format "Order: {self._sent_order_count} - SM"
        :return:
        """
        new_order = Order.create_new()
        new_order.market = Market(market)
        new_order.price = price
        new_order.units = units
        new_order.order_side = order_side
        new_order.order_type = order_type
        new_order.ref = ref

        self.send_order(new_order)
        self._waiting_for_server = True
        self._sent_order_count += 1