Beispiel #1
0
    def sell(self, ticker, amount):
        #Get account and positions details
        account = self.get_account()
        position = self.get_position_for(ticker)
        amount = int(amount)
        #Check stock exists and if so retrieve current price
        quote_price = get_price(ticker)
        if not quote_price:
            #    raise KeyError
            msg = "Input Ticker doesn't exist"
        else:
            #Check sufficient shares
            if position.shares == 0 or amount > position.shares:
                #    raise ValueError
                msg = "Insufficient shares"
            else:
                #Insert Trade row
                new_trade = Trade(accounts_pk=self.pk, ticker=ticker, \
                                  volume=amount*-1, price=quote_price)
                new_trade.save()

                #Update Position row
                new_position = Position(pk=position.pk, accounts_pk=self.pk, ticker=ticker, \
                                        shares=position.shares - amount)
                new_position.save()

                #Update balance on Account row
                new_balance = Account(pk=self.pk, username=account.username, \
                                    password_hash=account.password_hash, \
                                    balance=account.balance + (amount*quote_price), \
                                    api_key=account.api_key)
                new_balance.save()
                msg = "Sell transaction completed successfully"
        return msg
 def test_save_insert(self):
     tester = Position(account_pk = jimid, total_quantity = 10, ticker = "pton")
     tester.save()
     result = Position.all()
     self.assertEqual(len(result),2, "len =2 b/c seeded 1 pos and added another")
     self.assertEqual(result[0].ticker,"aapl","all func populates attributes, checking first for row[0] / pk1" )
     self.assertEqual(result[1].ticker,"pton","all func populates attributes, checking email for row[1] / pk2" )
Beispiel #3
0
def seed(dbpath=DBPATH):
    ORM.dbpath = dbpath

    mike_bloom = Account(username='******', balance=10000.00)
    mike_bloom.set_password('password')
    mike_bloom.save()

    # trade for a purchase of 10 shares yesterday
    # trade for a sell of 5 shares today

    tsla_position = Position(ticker='tsla',
                             shares=5,
                             account_pk=mike_bloom.values['pk'])
    tsla_position.save()
Beispiel #4
0
def seed(dbpath=DBPATH):
    ORM.dbpath = dbpath
    
    mike_bloom = Account(username='******', balance=10000.00)
    mike_bloom.set_password('password')
    mike_bloom.save()

    # trade for a purchase of 10 shares yesterday
    # trade for a sell of 5 shares today

    buy_trade = Trade(accounts_pk = mike_bloom.pk, ticker='tsla', volume=10, price=100.0)
    sell_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=5, price=200.0)
    buy_trade.save()
    sell_trade.save()

    tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk)
    tsla_position.save()
Beispiel #5
0
    def buy(self, ticker, amount):
        #Get account details
        account = self.get_account()
        #Check stock exists and if so retrieve current price
        quote_price = get_price(ticker)
        if not quote_price:
            #    raise KeyError
            msg = "Input Ticker doesn't exist"
        else:
            #Check sufficient funds
            quote_price = float(quote_price)
            amount = float(amount)
            if not account.balance >= amount * quote_price:
                #raise ValueError
                msg = "Insufficient funds"
            else:
                #Insert Trade row
                new_trade = Trade(accounts_pk=self.pk, ticker=ticker, \
                                  volume=amount, price=quote_price)
                new_trade.save()

                #Update or Insert Position row
                position = self.get_position_for(ticker)
                if position.shares == 0:
                    #Insert
                    new_position = Position(accounts_pk=self.pk,
                                            ticker=ticker,
                                            shares=amount)
                else:
                    #Update
                    new_position = Position(pk=position.pk, accounts_pk=self.pk, ticker=ticker, \
                                            shares=position.shares + amount)
                new_position.save()

                #Update balance on Account row
                new_balance = Account(pk=self.pk, username=account.username, \
                                    password_hash=account.password_hash, \
                                    balance=account.balance - (amount*quote_price), \
                                    api_key=account.api_key)
                new_balance.save()
                msg = "Buy transaction completed successfully"
        return msg
Beispiel #6
0
    def sell(self, ticker, quantity):
        """
            SELL stock. checks if a stock exists in the user's positions and
            has sufficient shares. creates a new Trade and modifies the Position
            as well as adding to the user's balance. returns nothing
        """
        trade = Trade()
        current_price = get_price(ticker)
        mv = current_price * int(quantity)

        trade.account_id = self.account_id
        trade.volume = quantity
        trade.ticker = ticker
        trade.price = current_price
        trade.market_value = mv * -1

        self.balance += mv
        self.update_balance()

        position = Position()
        position.account_id = self.account_id
        position.ticker = trade.ticker
        stored_position = Position.select_one_where(
            'WHERE account_id=? and ticker=?',
            (position.account_id, position.ticker))
        if stored_position:
            position.num_shares = stored_position.num_shares
            position.position_id = stored_position.position_id
            if stored_position.num_shares < trade.volume or stored_position.num_shares == 0:
                transaction_error('Not enough shares')
            else:
                trade.save()
                if stored_position:
                    position.num_shares = trade.volume
                else:
                    position.num_shares -= trade.volume
                position.save()
        else:
            transaction_error('Ticker {} is invalid'.format(trade.ticker))
Beispiel #7
0
    def buy(self, ticker, quantity):
        """
            BUY stock. checks if a stock exists in the user's positions and
            has sufficient shares. creates a new Trade and modifies the Position
            as well as adding to the user's balance. returns nothing
        """
        trade = Trade()
        current_price = get_price(ticker)
        mv = current_price * int(quantity)

        if self.balance < mv:
            transaction_error('Insufficient funds')
        else:
            trade.account_id = self.account_id
            trade.volume = quantity
            trade.ticker = ticker
            trade.price = current_price
            trade.market_value = mv

            self.balance -= mv
            self.update_balance()

            position = Position()
            position.account_id = self.account_id
            position.ticker = trade.ticker
            stored_position = Position.select_one_where(
                'WHERE account_id=? and ticker=?',
                (position.account_id, position.ticker))

            if stored_position:
                position.position_id = stored_position.position_id
                position.num_shares = stored_position.num_shares
                position.num_shares += trade.volume
            else:
                position.num_shares = trade.volume
            trade.save()
            position.save()