Example #1
0
    def test_close_position_with_more_share_raises_execption(self):
        self._buy_stock()
        # buy more to allow to sell 15
        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())
        buy_order = BuyOrder(account=self.account,
                             security=self.security,
                             stage=filled_stage,
                             price=Decimal(20),
                             share=10)
        self.session.add(buy_order)
        self.session.commit()

        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
        quote = SecurityQuote(date=tomorrow,
                              close_price=Decimal(100),
                              open_price=10.1,
                              high_price=14,
                              low_price=10.1,
                              volume=10000,
                              security=self.security)
        self.session.add(quote)
        self.session.commit()

        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())
        sell_order = SellOrder(account=self.account,
                               security=self.security,
                               stage=filled_stage,
                               price=Decimal(100),
                               share=15)

        self.session.add(sell_order)
        self.session.commit()

        with self.assertRaises(Exception):
            self.account.positions[0].close(sell_order)
Example #2
0
    def test_holding_value(self):
        share = 10
        price = 9.1
        cur_price = 10.1
        self.account.deposit(self.money)
        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())

        order = BuyOrder(account=self.account,
                         security=self.stock_one,
                         stage=filled_stage,
                         price=price,
                         share=share)
        self.session.add(order)
        self.session.commit()

        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
        quote = SecurityQuote(date=tomorrow,
                              close_price=cur_price,
                              open_price=10.1,
                              high_price=14,
                              low_price=10.1,
                              volume=10000,
                              security=self.stock_one)
        self.session.add(quote)
        self.session.commit()
        # account.setLastTickDict({'stock1': Quote(0, 0, 0, 0, curPrice, 0, 0)})

        holding_value = self.account.holdings_value
        currency = self.stock_one.exchange.currency
        self.assertAlmostEqual(Decimal(share * cur_price),
                               holding_value[currency])
Example #3
0
    def test_close_position_less_than_bought_opens_a_new_position(self):
        self._buy_stock()

        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
        quote = SecurityQuote(date=tomorrow,
                              close_price=Decimal(100),
                              open_price=10.1,
                              high_price=14,
                              low_price=10.1,
                              volume=10000,
                              security=self.security)
        self.session.add(quote)
        self.session.commit()

        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())
        sell_order = SellOrder(account=self.account,
                               security=self.security,
                               stage=filled_stage,
                               price=Decimal(100),
                               share=5)

        self.session.add(sell_order)
        self.session.commit()

        self.account.positions[0].close(sell_order)

        self.assertEquals(len(self.account.positions), 2)
        self.assertFalse(self.account.positions[0].is_open())
        self.assertTrue(self.account.positions[1].is_open())
Example #4
0
    def test_total_value(self):
        account = Account(owner=self.owner, broker=self.free_broker)
        account.deposit(self.money)

        share = 10
        price = 9.1
        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())
        order = BuyOrder(account=account,
                         security=self.stock_one,
                         stage=filled_stage,
                         price=price,
                         share=share)
        self.session.add(order)
        self.session.commit()
        # lets check that it was withdraw form the account
        # no fee is charged with the free broker
        self.assertEquals(account.cash[self.pesos], 1000 - 9.1 * share)

        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
        quote = SecurityQuote(date=tomorrow,
                              close_price=12,
                              open_price=10.1,
                              high_price=14,
                              low_price=10.1,
                              volume=10000,
                              security=self.stock_one)
        self.session.add(quote)
        self.session.commit()

        total = account.total
        self.assertAlmostEquals(909 + 10 * 12, total[self.pesos])
Example #5
0
    def test_holdings_cost(self):
        share = 10
        price = 9.1

        self.account.deposit(self.money)
        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())

        order = BuyOrder(account=self.account,
                         security=self.stock_one,
                         stage=filled_stage,
                         price=price,
                         share=share)
        self.session.add(order)
        self.session.commit()

        holding_cost = self.account.holdings_cost
        symbol = self.stock_one.symbol
        self.assertAlmostEquals(Decimal(share * price), holding_cost[symbol])
Example #6
0
    def _buy_stock(self):
        pesos = Currency(name='Pesos', code='ARG')
        broker = Broker(name='Cheap')
        self.account = Account(broker=broker)
        ten_thousand_pesos = Money(amount=Decimal(10000), currency=pesos)
        self.account.deposit(ten_thousand_pesos)
        exchange = Exchange(name='Merval', currency=pesos)
        self.security = Stock(symbol='PBR',
                              description='Petrobras BR',
                              ISIN='US71654V4086',
                              exchange=exchange)
        filled_stage = FillOrderStage(executed_on=datetime.datetime.now())
        price = Decimal(10)
        share = 10
        order = BuyOrder(account=self.account,
                         security=self.security,
                         stage=filled_stage,
                         price=price,
                         share=share)

        self.session.add(order)
        self.session.commit()
        return self.account
Example #7
0
    def test_update_order_stage_to_filled(self):
        order = self._buy_stock(self.stock_one)
        order.update_stage(FillOrderStage())

        self.assertTrue(order.current_stage.is_filled)