Пример #1
0
 def sell(self, client: Client, entityId, count, date: datetime.datetime):
     sold_amount = client.sell(entityId=entityId, count=count, date=date)
     if sold_amount != 0:
         self.commisions += (int)(sold_amount * constants.COMMISION())
         client.wallet -= (int)(sold_amount * constants.COMMISION())
         if client.debt > 0:
             draw_back = min((int)(client.debt), sold_amount, client.wallet)
             client.wallet -= draw_back
             client.debt -= draw_back
             self.investment -= draw_back
Пример #2
0
    def buy(self, client: Client, creditId):
        print("Buying %s" % creditId)
        creditCache = self.redis.get(creditId)
        if creditCache is not None:
            credit = json.loads(creditCache)
            stock = stockService.findStock(credit['stockId'])
            if client.id == credit['clientId']:
                purchase_date = datetime.datetime.fromtimestamp(credit['date'])
                price = stockService.exactPrice(stock.id, purchase_date.year,
                                                purchase_date.month,
                                                purchase_date.day)
                if price is not None:
                    investment = min(client.wallet, credit['investment'])
                    if investment > 0:
                        count = int(
                            ((investment + credit['credit']) / price.high) /
                            (1 + constants.COMMISION()))
                        if count > 0:
                            commission = price.high * count * constants.COMMISION(
                            )
                            transaction = Transaction(type=TransactionType.BUY,
                                                      stock=stock,
                                                      client=client,
                                                      amount=price.high *
                                                      count,
                                                      count=count,
                                                      date=purchase_date,
                                                      commission=commission)
                            transaction.save()
                            association = stockService.findStockAssociation(
                                client, stock)
                            print(association)
                            association.count += count
                            association.save()

                            if credit['credit'] > 0:
                                _credit = Credit(
                                    stock=stock,
                                    client=client,
                                    count=count,
                                    amount=price.high * count - investment,
                                    date=purchase_date,
                                    debt=price.high * count - investment)
                                _credit.save()
                            if _credit is not None:
                                client.wallet -= (investment + commission)
                            else:
                                client.wallet -= price.high * count + commission

                            self.commisions += commission
                            client.save()

                            return transaction
                        else:
                            print("Buying %s, Not Enough Money" % creditId)
Пример #3
0
 def _buy(self, entityId, count, unit_price):
     if self.wallet > (count * unit_price) * (1 + constants.COMMISION()):
         self.wallet -= (int)(count * unit_price *
                              (1 + constants.COMMISION()))
         if entityId not in self.stocks:
             self.stocks[entityId] = count
         else:
             self.stocks[entityId] = self.stocks[entityId] + count
         return count * unit_price
     else:
         return 0
Пример #4
0
    def sell(self,
             client: Client,
             stock: Stock,
             count,
             date: datetime.datetime = datetime.datetime.now()):
        print("%s Selling (%d) of %d" % (date, count, stock.id))
        price = stockService.exactPrice(stock.id, date.year, date.month,
                                        date.day)
        if price is not None:
            association = stockService.findStockAssociation(client, stock)
            print("%s Selling (%d) of %d, Having %d" %
                  (date, count, stock.id, association.count))
            if count < 0:
                count = association.count
            if count > 0 and association.count >= count:
                print("%s Really Selling (%d) of %d" % (date, count, stock.id))
                commission = count * price.low * constants.COMMISION()

                transaction = Transaction(type=TransactionType.SELL,
                                          stock=stock,
                                          client=client,
                                          amount=price.low * count,
                                          count=count,
                                          date=date,
                                          commission=commission)
                transaction.save()

                self.commisions += commission

                association.count -= count
                client.wallet += count * price.low - commission

                clientService.payDebt(client, stock,
                                      count * price.low - commission)

                association.save()
                client.save()

                return transaction
            else:
                self.logger.warn("%s Zero Count on (%d) of %d " %
                                 (date, count, stock.id))
        else:
            self.logger.warn("%s Price Not Found of %d" % (date, stock.id))
Пример #5
0
 def buy(self, creditId):
     _credit = self.redis.get(creditId)
     if _credit is not None:
         credit = json.loads(_credit)
         client = self.clients[credit['clientId']]
         purchase_date = datetime.datetime.fromtimestamp(credit['date'])
         client.debt += (int)(credit['credit'])
         client.wallet += (int)(credit['credit'])
         purchase_amount = client.buy_v(
             credit['entityId'], credit['investment'] + credit['credit'],
             purchase_date)
         if purchase_amount == 0:
             logging.warning("Reverting Credit")
             client.debt -= (int)(credit['credit'])
             client.wallet -= (int)(credit['credit'])
         else:
             self.investment += (int)(credit['credit'])
             self.commisions += (int)(purchase_amount *
                                      constants.COMMISION())
     else:
         logging.warning("Credit Not Found")