def _buy_stock(self, stock): self.account.deposit(self.money) # buy it order = BuyOrder(account=self.account, security=stock, price=100, share=9) self.session.add(order) self.session.commit() return order
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])
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])
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)
def test_buy_validate_enough_money_ok_path(self): self.account.deposit(self.money) # buy it order = BuyOrder(account=self.account, security=self.stock_one, price=100, share=9) self.session.add(order) self.session.commit()
def test_buy_validate_comission_fee_plus_cost_not_enough_money(self): self.account.deposit(self.money) self.free_broker.commission = lambda target: 10000 # can't buy because of commission fee with self.assertRaises(Exception): order = BuyOrder(account=self.account, security=self.stock_one, price=100, share=10) self.session.add(order) self.session.commit()
def test_buy_validate_price_too_high(self): self.account.deposit(self.money) # can't buy because price too high with self.assertRaises(Exception): order = BuyOrder(account=self.account, security=self.stock_one, price=10000, share=100000) self.session.add(order) self.session.commit()
def test_is_order_met(self): now = datetime.datetime.now() broker = Broker(name='Broker1') account = Account(broker=broker) pesos = Currency(name='Pesos', code='ARG') account.deposit(Money(amount=10000, currency=pesos)) exchange = Exchange(name='Merval', code='MERV', currency=pesos) stock=Stock(symbol='symbol', description='a stock', ISIN='US123456789', exchange=exchange) tick1=Tick(trade_date=now, price=13.20, amount=1000, volume=1000, security=stock) order1=BuyOrder(account=account, security=stock, price=13.25, share=10, is_market=True) order2=BuyOrder(account=account, security=stock, price=13.15, share=10) order3=SellOrder(account=account, security=stock, price=13.25, share=10) order4=SellOrder(account=account, security=stock, price=13.15, share=10, is_market=True) self.session.add(order1) self.session.add(order2) self.session.add(order3) self.session.add(order4) self.session.commit() self.assertTrue(order1.is_order_met(tick1)) self.assertFalse(order2.is_order_met(tick1)) self.assertFalse(order3.is_order_met(tick1)) self.assertTrue(order4.is_order_met(tick1))
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])
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
def test_is_order_met(self): now = datetime.datetime.now() broker = Broker(name='Broker1') account = Account(broker=broker) pesos = Currency(name='Pesos', code='ARG') account.deposit(Money(amount=10000, currency=pesos)) exchange = Exchange(name='Merval', code='MERV', currency=pesos) stock = Stock(symbol='symbol', description='a stock', ISIN='US123456789', exchange=exchange) tick1 = Tick(trade_date=now, price=13.20, amount=1000, volume=1000, security=stock) order1 = BuyOrder(account=account, security=stock, price=13.25, share=10, is_market=True) order2 = BuyOrder(account=account, security=stock, price=13.15, share=10) order3 = SellOrder(account=account, security=stock, price=13.25, share=10) order4 = SellOrder(account=account, security=stock, price=13.15, share=10, is_market=True) self.session.add(order1) self.session.add(order2) self.session.add(order3) self.session.add(order4) self.session.commit() self.assertTrue(order1.is_order_met(tick1)) self.assertFalse(order2.is_order_met(tick1)) self.assertFalse(order3.is_order_met(tick1)) self.assertTrue(order4.is_order_met(tick1))
def test_order_str(self): security = Security(symbol='YPF') order = BuyOrder(security=security, share=10, price=Decimal(10)) self.assertEquals('pystock_buy_order Total 100', str(order))