Example #1
0
    def testDelete(self):
        trade = Trade(pk=2)
        trade.delete()

        self.assertEqual(trade.quantity, None)
        self.assertEqual(trade.type, None)
        self.assertEqual(trade.type, None)
Example #2
0
def seed(dbpath=DBPATH):
    ORM.dbpath = dbpath

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

    buy_trade = Trade(accounts_pk=mike_bloom.pk,
                      ticker='appl',
                      volume=10,
                      price=100.0)
    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()
Example #3
0
 def testFromId(self):
     # First trade instance to initialize data
     trade = Trade(ticker="GS",
                   volume=1234.0,
                   unit_price=77.88,
                   account_id="7654321")
     trade.save()
     trade2 = Trade.from_id(trade.id)
     self.assertEqual(
         trade.id, trade2.id,
         "fromId() should return identical object from instance")
def seed(dbpath=DBPATH):
    ORM.dbpath = dbpath
    print("DATABASE: ", dbpath)
    mike_bloom = Account(username='******', balance=10000.00)
    mike_bloom.set_password("password")
    print(mike_bloom.dbpath)
    mike_bloom.save()

    tsla_position = Position(ticker='tsla', shares=5, account_pk=mike_bloom.pk)
    tsla_position.save()

    fake_trade = Trade(ticker='tsla', quantity=2, type=1)
    fake_trade.save()
 def testTrade(self):
     trade = Trade(ticker_symbol="NYSE:T",
                   quantity=10,
                   type=0,
                   date="10/9/2010",
                   price=2500,
                   account_pk=1)
     trade.save()
     self.assertEqual(trade.ticker_symbol, "NYSE:T")
     self.assertEqual(trade.quantity, "10")
     self.assertEqual(trade.type, 0)
     self.assertEqual(trade.date, "10/9/2010")
     self.assertEqual(trade.account_pk, 1)
Example #6
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,
                             accounts_pk=mike_bloom.pk)
    tsla_position.save()

    ms_position = Position(ticker='ms', shares=10, accounts_pk=mike_bloom.pk)
    ms_position.save()

    tsla_trade = Trade(
        ticker='tsla',
        volume=5,
        price=95.20,
        time=time.time(),
        accounts_pk=mike_bloom.pk,
    )
    tsla_trade.save()

    ms_trade = Trade(
        ticker='ms',
        volume=10,
        price=25.50,
        time=time.time(),
        accounts_pk=mike_bloom.pk,
    )
    ms_trade.save()
Example #7
0
    def testDelete(self):
        # First trade instance to initialize data
        trade = Trade(ticker="GS",
                      volume=1100.0,
                      unit_price=77.88,
                      account_id="7654321")
        trade.delete()

        with sqlite3.connect(Trade.dbpath) as connection:
            cursor = connection.cursor()
            SELECTSQL = "SELECT * FROM trades WHERE id='1';"
            cursor.execute(SELECTSQL)
            rows = cursor.fetchall()
            self.assertEqual(len(rows), 0, "delete() should delete DB rows")
Example #8
0
 def testSaveInsert(self):
     # First trade instance to initialize data on row 1
     trade = Trade(ticker="IBM",
                   volume=100.0,
                   unit_price=11.22,
                   account_id="1234567")
     trade.save()
     self.assertIsNotNone(trade.id, "save should set an ID value")
     with sqlite3.connect(Trade.dbpath) as connection:
         cursor = connection.cursor()
         SQL = "SELECT * FROM trades WHERE ticker='IBM' AND account_id='1234567';"
         cursor.execute(SQL)
         rows = cursor.fetchall()
         self.assertEqual(len(rows), 1,
                          "save should create a row in the database")
 def testTrade(self):
     trade = Trade()
     self.assertEqual(trade.tablename, "trades")
     self.assertEqual(
         trade.fields,
         ['ticker', 'quantity', 'type', 'price', 'date', 'account_pk'])
     self.assertIsInstance(trade, Trade)
     trade1 = Trade(ticker="ibm",
                    quantity=5,
                    type=1,
                    price=400,
                    date=5000,
                    account_pk=20)
     self.assertEqual(trade1.ticker, "ibm")
     self.assertEqual(trade1.quantity, 5)
     self.assertEqual(trade1.type, 1)
     self.assertEqual(trade1.price, 400)
     self.assertEqual(trade1.date, 5000)
     self.assertEqual(trade1.account_pk, 20)
Example #10
0
    def testSaveUpdate(self):
        # First trade instance to initialize data on row 2
        trade = Trade(ticker="GS",
                      volume=100.0,
                      unit_price=33.44,
                      account_id="7654321")
        trade.save()
        self.assertIsNotNone(trade.id, "save() should set an ID value")

        # Second trade instance with same id to update data
        trade2 = Trade.from_id(trade.id)
        trade2.ticker = "AAPL"
        trade2.volume = 200.0
        trade2.unit_price = 55.66
        trade2.account_id = "7654321"
        trade2.save()
        self.assertEqual(trade2.id, trade.id,
                         "save() doesn't create new row for existing account")

        # Third instance which should be all updated to compare second one with
        trade3 = Trade.from_id(trade.id)
        self.assertEqual(trade3.ticker, "AAPL", "ticker should be updated")
        self.assertEqual(trade3.unit_price, 55.66,
                         "unit_price should be updated")
        self.assertEqual(trade3.volume, 200.00, "volume should be updated")
        self.assertNotEqual(trade3.time, trade.time, "time should be updated")
Example #11
0
def buy(api_key):
    data = request.get_json()
    account = Account.api_authenticate(api_key)
    if account:
        ticker = request.get_json()['ticker']
        amount = request.get_json()['amount']
        Deposit = request.get_json()['type']
        current_price = get_price_of_ticker(ticker) * int(amount)
        if account.balance < current_price:
            return jsonify({"Insufficient_funds": "Insufficient_funds"})
        account.balance -= current_price
        account.equity += current_price
        current_position = Position.from_account(account.pk, ticker)
        current_position.equity += current_price
        current_position.number_shares += int(amount)
        current_position.save()
        time = data.get('unix_time')
        new_trade = Trade(account.pk, ticker, amount, current_position.equity,
                          Deposit, time)
        new_trade.equity = current_price
        new_trade.insert()
        account.save()
        return jsonify({"ticker": ticker, "amount": amount})
    return jsonify({'error': 'invalid key'})
Example #12
0
def sell(api_key):
    data = request.get_json()
    account = Account.api_authenticate(api_key)
    if account:
        ticker = request.get_json()['ticker']
        amount = request.get_json()['amount']
        Withdrawal = request.get_json()['type']
        current_position = Position.from_account(account.pk, ticker)
        if current_position.num_shares < int(amount):
            return jsonify({"Insufficient_funds": "Insufficient_funds"})
        transaction_price = get_price_of_ticker(ticker) * int(amount)
        account.balance += (transaction_price)
        account.equity -= transaction_price
        current_position.equity -= (transaction_price)
        current_position.num_shares -= int(amount)
        current_position.save()
        time = data.get('unix_time')
        new_trade = Trade(account.pk, ticker, amount, current_position.equity,
                          Withdrawal, time)
        new_trade.equity = transaction_price
        new_trade.insert()
        account.save()
        return jsonify({"ticker": ticker, "amount": amount})
    return jsonify({'error': 'invalid key'})
Example #13
0
 def get_trades_for(self, ticker):
     """ return all Trades where account_pk == self.pk. 
     returns a list of Trade objects """
     return Trade.all_from_where_clause("WHERE account_pk=? AND ticker=?", (self.pk, ticker))
Example #14
0
 def testAllFromWhereClause(self):
     trade = Trade().all_from_where_clause()
     self.assertEqual(trade[0].ticker, "tsla")
     self.assertEqual(trade[0].quantity, 2)
     self.assertEqual(trade[0].type, 1)
Example #15
0
 def testOneFromPk(self):
     trade = Trade().one_from_pk(pk=1)
     self.assertEqual(trade.ticker, 'tsla')
     self.assertEqual(trade.quantity, 2)
     self.assertEqual(trade.type, 1)
Example #16
0
    def testAll_from_account_id_and_ticker(self):
        # Three trade instances to initialize data
        trade0 = Trade(ticker="IBM",
                       volume=100.0,
                       unit_price=11.22,
                       account_id="1111111")
        trade0.save()
        trade1 = Trade(ticker="IBM",
                       volume=200.0,
                       unit_price=33.44,
                       account_id="1111111")
        trade1.save()
        trade2 = Trade(ticker="AAPL",
                       volume=300.0,
                       unit_price=55.66,
                       account_id="1111111")
        trade2.save()

        all_data = Trade.all_from_account_id_and_ticker(
            trade0.account_id, trade0.ticker)
        self.assertEqual(
            len(all_data), 2,
            "all_from_account_id_and_ticker() should only return rows WHERE 2 conditions are met"
        )
        self.assertEqual(
            all_data[0].account_id, all_data[1].account_id,
            "all_from_account_id_and_ticker() should only return same account_id data"
        )
        self.assertEqual(
            all_data[0].ticker, all_data[1].ticker,
            "all_from_account_id_and_ticker() should only return same ticker data"
        )
    def testDelete_all(self):
        # Three trade instances to initialize data
        trade0 = Trade(ticker="IBM",
                       volume=100.0,
                       unit_price=11.22,
                       account_id="1111111")
        trade0.save()
        trade1 = Trade(ticker="GS",
                       volume=200.0,
                       unit_price=33.44,
                       account_id="2222222")
        trade1.save()
        trade2 = Trade(ticker="AAPL",
                       volume=300.0,
                       unit_price=55.66,
                       account_id="3333333")
        trade2.save()

        Trade.delete_all()
        with sqlite3.connect(DBPATH) as connection:
            cursor = connection.cursor()
            SQL = "SELECT * FROM positions;"
            cursor.execute(SQL)
            rows = cursor.fetchall()
            self.assertEqual(
                len(rows), 0,
                "delete_all should delete all rows in the database")
Example #18
0
    def testDelete_all(self):
        # Three trade instances to initialize data
        trade0 = Trade(ticker="IBM",
                       volume=100.0,
                       unit_price=11.22,
                       account_id="1111111")
        trade0.save()
        trade1 = Trade(ticker="GS",
                       volume=200.0,
                       unit_price=33.44,
                       account_id="2222222")
        trade1.save()
        trade2 = Trade(ticker="AAPL",
                       volume=300.0,
                       unit_price=55.66,
                       account_id="3333333")
        trade2.save()

        all_data = Trade.delete_all()
        self.assertEqual(
            all_data, [],
            "delete() should return empty list [] from Trades table")
Example #19
0
    def testAll_from_account_id(self):
        # Three trade instances to initialize data
        trade0 = Trade(ticker="IBM",
                       volume=100.0,
                       unit_price=11.22,
                       account_id="1111111")
        trade0.save()
        trade1 = Trade(ticker="GS",
                       volume=200.0,
                       unit_price=33.44,
                       account_id="2222222")
        trade1.save()
        trade2 = Trade(ticker="AAPL",
                       volume=300.0,
                       unit_price=55.66,
                       account_id="1111111")
        trade2.save()

        all_data = Trade.all_from_account_id(trade0.account_id)
        self.assertEqual(
            all_data[0].account_id, all_data[1].account_id,
            "all_from_account_id() should only return same account_id data")
Example #20
0
    def testAll(self):
        # Three trade instances to initialize data
        trade0 = Trade(ticker="IBM",
                       volume=100.0,
                       unit_price=11.22,
                       account_id="1111111")
        trade0.save()
        trade1 = Trade(ticker="GS",
                       volume=200.0,
                       unit_price=33.44,
                       account_id="2222222")
        trade1.save()
        trade2 = Trade(ticker="AAPL",
                       volume=300.0,
                       unit_price=55.66,
                       account_id="3333333")
        trade2.save()

        all_data = Trade.all()
        self.assertEqual(len(all_data), 3,
                         "all() should return all rows from Trades table")
        self.assertEqual(all_data[0].ticker, "IBM",
                         "all() should return correct ticker data")
Example #21
0
 def testOneTrade(self):
     trade = Trade(ticker="aapl", quantity=2, type=1)
     self.assertEqual(trade.ticker, "aapl")
     self.assertEqual(trade.quantity, 2)
     self.assertEqual(trade.type, 1)
     self.assertIsInstance(trade, Trade)
 def testGet_account(self):
     trade = Trade(account_id="9999999")
     accountTest = trade.get_account()
Example #23
0
 def get_trades(self):
     """ return all Trades where account_pk == self.pk. 
     returns a list of Trade objects """
     return Trade.all_from_where_clause("WHERE account_pk=?", (self.pk,))
 def testGet_position(self):
     trade = Trade(account_id="9999999")
     positionTest = trade.get_position()
 def testTrade(self):
     trade = Trade(ticker='aaaaaaaaa', quantity=12, type=0)
     trade.save()
     test = Trade.one_from_where_clause("WHERE ticker=?", ('aaaaaaaaa', ))
     self.assertIsInstance(test, Trade)
     self.assertEqual(test.quantity, 12)
Example #26
0
 def testSave(self):
     trade = Trade(ticker="EA", quatity=10, type=0)
     trade.save()
     self.assertEqual(trade.pk, 2)
     trade = Trade(pk=2, ticker="EA", quantity=100, type=1)
     self.assertEqual(trade.quantity, 100)