예제 #1
0
    def generateNaiveOrder(self, signal):
        order = None

        symbol = signal.symbol
        direction = signal.signalType

        mktQuantity = signal.quantity
        curQuantity = self.currentPosition[symbol]
        orderType = 'MKT'

        if direction == 'LONG' and curQuantity == 0:
            order = OrderEvent(symbol, orderType, mktQuantity, 1)
        if direction == 'SHORT' and curQuantity == 0:
            order = OrderEvent(symbol, orderType, mktQuantity, -1)

        if direction == 'EXIT' and curQuantity > 0:
            order = OrderEvent(symbol, orderType, abs(curQuantity), -1)
        if direction == 'EXIT' and curQuantity < 0:
            order = OrderEvent(symbol, orderType, abs(curQuantity), 1)

        return order
예제 #2
0
    def _processOrders(self):

        signals = []

        currDTTime = self.current_datetime
        currDT = dt.datetime(currDTTime.year, currDTTime.month, currDTTime.day)

        cashAmount = self._port.currentHoldings['cash']
        for order in self._orderRecords:
            cashAmount = max(cashAmount, 1.e-5)
            symbol = order['symbol']
            quantity = order['quantity']
            direction = order['direction']
            currValue = self.bars.getLatestBarValue(symbol, 'close')

            multiplier = self._port.assets[symbol].multiplier
            settle = self._port.assets[symbol].settle
            margin = self._port.assets[symbol].margin

            # amount available for buy back or sell
            if direction == OrderDirection.BUY:
                fill_cost = quantity * currValue * multiplier * settle
                margin_cost = 0.
            elif direction == OrderDirection.BUY_BACK:
                fill_cost = 0.
                margin_cost = 0.
            elif direction == OrderDirection.SELL:
                fill_cost = 0.
                margin_cost = 0.
            elif direction == OrderDirection.SELL_SHORT:
                fill_cost = 0.
                margin_cost = quantity * currValue * multiplier * margin

            maximumCashCost = max(fill_cost, margin_cost)

            if maximumCashCost <= cashAmount:
                signal = OrderEvent(currDTTime, symbol, "MKT", quantity,
                                    direction)
                self._posBook.updatePositionsByOrder(symbol, currDT, quantity,
                                                     direction)
                signals.append(signal)
                cashAmount -= maximumCashCost
            elif maximumCashCost > cashAmount:
                if direction == OrderDirection.BUY:
                    self.logger.warning(
                        "{0}: ${1} cash needed to buy the quantity {2} of {3} "
                        "is less than available cash ${4}".format(
                            currDTTime, maximumCashCost, quantity, symbol,
                            cashAmount))
                elif direction == OrderDirection.SELL_SHORT:
                    self.logger.warning(
                        "{0}: ${1} cash needed to sell short the quantity {2} of {3} "
                        "is less than available cash ${4}".format(
                            currDTTime, maximumCashCost, quantity, symbol,
                            cashAmount))

        # log the signal informations
        for signal in signals:
            self.logger.info(
                "{0}: {1} Order ID: {2} is sent with quantity {3} and direction {4} on symbol {5}"
                .format(signal.timeIndex, signal.orderType, signal.orderID,
                        signal.quantity, signal.direction, signal.symbol))
            self.events.put(signal)
예제 #3
0
    def _processOrders(self):

        signals = []

        currDTTime = self.current_datetime
        if isinstance(currDTTime, dt.datetime):
            currDT = currDTTime.date()
        else:
            currDT = currDTTime

        cashAmount = max(self._port.currentHoldings['cash'], 1e-5)
        for order in self._orderRecords:
            symbol = order['symbol']
            quantity = order['quantity']
            direction = order['direction']
            currValue = self.bars.getLatestBarValue(symbol, 'close')

            multiplier = self._port.assets[symbol].multiplier
            settle = self._port.assets[symbol].settle
            margin = self._port.assets[symbol].margin

            # amount available for buy back or sell
            if direction == 1:
                amount = self._posBook.avaliableForTrade(symbol, currDT)[1]
            elif direction == -1:
                amount = self._posBook.avaliableForTrade(symbol, currDT)[0]

            fill_cost = quantity * currValue * multiplier * settle * direction

            margin_cost = max(quantity - amount,
                              0) * currValue * multiplier * margin
            maximumCashCost = max(fill_cost, margin_cost)

            if maximumCashCost <= cashAmount and (direction == 1
                                                  or quantity <= amount):
                signal = OrderEvent(currDTTime, symbol, "MKT", quantity,
                                    direction)
                self._posBook.updatePositionsByOrder(symbol, currDT, quantity,
                                                     direction)
                signals.append(signal)
                cashAmount -= maximumCashCost
            elif maximumCashCost > cashAmount:
                if direction == 1:
                    self.logger.warning(
                        "{0}: ${1} cash needed to buy the quantity {2} of {3} "
                        "is less than available cash ${4}".format(
                            currDTTime, maximumCashCost, quantity, symbol,
                            cashAmount))
                else:
                    self.logger.warning(
                        "{0}: ${1} cash needed to sell the quantity {2} of {3} "
                        "is less than available cash ${4}".format(
                            currDTTime, maximumCashCost, quantity, symbol,
                            cashAmount))
            else:
                self.logger.warning(
                    "{0}: short disabled {1} quantity need to be sold {2} "
                    "is less then the available for sell amount {3}".format(
                        currDTTime, symbol, quantity, amount))

        # log the signal informations
        for signal in signals:
            self.logger.info(
                "{0}: {1} Order ID: {2} is sent with quantity {3} and direction {4} on symbol {5}"
                .format(signal.timeIndex, signal.orderType, signal.orderID,
                        signal.quantity, signal.direction, signal.symbol))
            self.events.put(signal)