Example #1
0
    def check_trigger(self, trigger_id):
        session = None

        while True:
            try:
                session = get_session()

                # Refresh trigger from new session
                trigger = session.query(Trigger).filter_by(id=trigger_id).first()

                log.debug('Trigger %d checking for stock %s > %s',
                        trigger.id, trigger.stock_symbol, trigger.trigger_value)

                if trigger.state == Trigger.State.CANCELLED:
                    log.debug('Trigger %d cancelled!', trigger.id)
                    return

                # Get a new quote for the stock
                quote_client = get_quote_client()
                quote = quote_client.get_quote(trigger.stock_symbol, 
                        trigger.username)
                log.debug('Trigger %d: %s => %s', 
                        trigger.id, trigger.stock_symbol, quote)

                # If quote is greater than trigger value, buy stock and remove trigger
                if quote > trigger.trigger_value:
                    # buy the stock and update reserve balance
                    log.debug("Trigger %d activated: %s > %s", 
                            trigger.id, quote, trigger.trigger_value)
                    return self.process_transaction(session, quote, trigger)

            except Exception, e:
                log.error('Trigger %d: %s : %s', trigger_id, type(e), e)
            finally:
Example #2
0
    def run(self, username, stock_symbol, amount):
        self.session = get_session()
        user = self.session.query(User).filter_by(username=username).first()
        if not user:
            raise UserNotFoundError(username)
            xml.log_error('BUY', 'username not found')
        # Check for existing uncommitted transaction
        if self.session.query(Transaction).filter_by(
                user=user, operation='BUY', committed=False).count() > 0:
            raise BuyTransactionActiveError()
            xml.log_error('BUY', 'Outstanding Buy Exists')

        # Getting stock quote
        quote_client = get_quote_client()
        quote = quote_client.get_quote(stock_symbol, username)

        # Work out quantity of stock to buy, fail if not enough for one stock
        amount = Money.from_string(amount)
        quantity = amount_to_quantity(quote, amount)
        if quantity == 0:
            raise InsufficientFundsError()

        price = quote * quantity
        if user.account_balance < price:
            raise InsufficientFundsError()

        transaction = Transaction(user=user, quantity=quantity, operation='BUY', 
                stock_symbol=stock_symbol, stock_value=quote, committed=False)

        self.session.add(transaction)
        self.session.commit()

        xml.log_transaction('BUY', transaction) 
        return xml.QuoteResponse(quantity=quantity, price=price)
Example #3
0
    def run(self, username, stock_symbol, money_amount):

        # see if user exists
        self.session = get_session()
        user = self.session.query(User).filter_by(username=username).first()
        if not user:
            raise UserNotFoundError(username)

        # Check for existing uncommitted transaction
        if self.session.query(Transaction).filter_by(
                user=user, operation='SELL', committed=False).count() > 0:
            raise SellTransactionActiveError()

        #set up client to get quote
        quote_client = get_quote_client()
        quoted_stock_value = quote_client.get_quote(stock_symbol, username) 

        # Work out quantity of stock to sell, fail if not enough for one stock
        money_amount = Money.from_string(money_amount)
        quantity_to_sell = amount_to_quantity(quoted_stock_value, money_amount)
        if quantity_to_sell == 0:
            raise InsufficientFundsError()


        # see if the user owns the requested stock and has enough for request
        records = self.session.query(StockPurchase).filter_by(
                username=user.username, stock_symbol=stock_symbol).all()

        if(len(records) > 1):
            raise UnknownCommandError('Multiple StockPurchase for user %s: %d', 
                    username, len(records))
        if len(records) != 1 or records[0].quantity < quantity_to_sell:
            raise InsufficientStockError()

        price = quoted_stock_value * quantity_to_sell

        # make transaction
        self.trans = Transaction(username=user.username, 
                stock_symbol=stock_symbol, operation='SELL', committed=False, 
                quantity=quantity_to_sell, stock_value=quoted_stock_value)

        # commit transaction after all actions for atomicity
        self.session.add(self.trans)
        self.session.commit()


        xml.log_transaction('SELL', self.trans, status_message='success')
        return xml.QuoteResponse(quantity=quantity_to_sell, price=price)
Example #4
0
    def run(self, username='', stock_symbol=''):

        if not stock_symbol:
            xml.log_error('QUOTE', 'No stock symbol given')
            raise InvalidInputError('No stock symbol given')

        if len(stock_symbol) > 4:
            xml.log_error('QUOTE', 'stock symbol too long')
            raise InvalidInputError('stock symbol too long: %d' % \
                    len(stock_symbol))

        quote_client = get_quote_client()
        quote = quote_client.get_quote(stock_symbol, username)

        #create log
        xml.log_event('QUOTE', username, stock_symbol)

        return xml.QuoteResponse(quantity=1, price=quote)