Exemplo n.º 1
0
    def trade_shares_for(self, client):
        """
        requests all shares for client, than requests shares prices from market
        and based on market's recommendation trades
        :param client: dict {'idclients':1, 'name':'CL', 'balance':256.33}
        :return:
        """

        # TODO: optimization! Firstly, should be sold all shares to get additional money, then perform buy action

        actives = self.get_actives_for(client['idclients'])
        for active in actives:
            try:  # in case of bad answer from the market will be returned empty dict - will ignore it
                logging.info('Request data for {0}'.format(active))
                share_code = active['code']

                market_share = self.__market.get_share(
                    share_code)  # share_code is string!!!

                # will construct 'deal' abstraction,
                # could be buy/sell/neutral or active buy/sell
                # the abstraction will perform transaction
                info_for_share = market_share[share_code]
                # price = Shares().get_price_in_usd(int(share_code), info_for_share['price'])  # convert to USD
                deal = construct_deal_for(info_for_share['summary'])
                deal(client, active, info_for_share['price'])
                logging.info('Market info for {0}'.format(market_share))
            except KeyError:
                logging.error('Wrong answer for {0}'.format(active['code']))

        # TODO: updated client's balance and shares quantity should be stored as one transaction
        self.update_balance_for(client['idclients'], client['balance'])
        for active in actives:
            self.update_active_for(active['idactives'], active['quantity'])
Exemplo n.º 2
0
    def test_nothing(self):
        client = {'idclients': 1, 'name': 'CoolClient', 'balance': 1000.00}

        active = {'idactives': 1, 'code': 8899, 'quantity': 100}

        market_info = {'8899': {'price': 10, 'summary': 'neutral'}}

        share_code = str(active['code'])
        deal = construct_deal_for(market_info[share_code]['summary'])
        deal(client, active, market_info[share_code]['price'])

        self.assertEqual(1000.00, client['balance'])
        self.assertEqual(100, active['quantity'])
Exemplo n.º 3
0
    def test_deal_for_sell(self):
        client = {'idclients': 1, 'name': 'CoolClient', 'balance': 1000.00}

        active = {'idactives': 1, 'code': 8831, 'quantity': 300}

        market_info = {'8831': {'price': 10, 'summary': 'sell'}}

        share_code = str(active['code'])
        deal = construct_deal_for(market_info[share_code]['summary'])
        deal(client, active, market_info[share_code]['price'])

        # be sold 200 shares in total (200 * 10$) = $2000
        self.assertEqual(3000.00, client['balance'])
        self.assertEqual(100, active['quantity'])
Exemplo n.º 4
0
    def test_buy_not_enough_money(self):
        client = {'idclients': 1, 'name': 'CoolClient', 'balance': 10.00}

        active = {'idactives': 1, 'code': 8831, 'quantity': 100}

        market_info = {'8831': {'price': 10, 'summary': 'buy'}}

        share_code = str(active['code'])
        deal = construct_deal_for(market_info[share_code]['summary'])
        deal(client, active, market_info[share_code]['price'])

        self.assertEqual(
            10.00, client['balance'])  # it will cost 100$ but we don't have it
        self.assertEqual(100, active['quantity'])  # quantity won't change
Exemplo n.º 5
0
    def test_deal_for_buy(self):
        client = {'idclients': 1, 'name': 'CoolClient', 'balance': 10.50}

        active = {'idactives': 1, 'code': 3, 'quantity': 100}

        market_info = {'3': {'price': 200, 'summary': 'buy'}}

        share_code = str(active['code'])
        deal = construct_deal_for(market_info[share_code]['summary'])
        deal(client, active, market_info[share_code]['price'])

        self.assertEqual(8.00, client['balance'])  # it will cost 100$
        self.assertEqual(600,
                         active['quantity'])  # quantity will grow up to 110
Exemplo n.º 6
0
    def test_sell_less_then_minimum(self):
        client = {'idclients': 1, 'name': 'CoolClient', 'balance': 1000.00}

        active = {'idactives': 1, 'code': 2186, 'quantity': 700}

        market_info = {'2186': {'price': 70, 'summary': 'sell'}}

        share_code = str(active['code'])
        deal = construct_deal_for(market_info[share_code]['summary'])
        deal(client, active, market_info[share_code]['price'])

        # should be sold 1000 shares, but we have 700 only, therefore
        # will be sold 700 by price $(1/70) in total: $10
        self.assertEqual(1010.00, client['balance'])
        self.assertEqual(0, active['quantity'])  # quantity will be 0