def buy_stock(self, data): price = data['stocksToBuy'] * data['data']['price'] self.stock_add_owner(data['data'], price, data['stocksToBuy'], data['origin']) helper.print_green("Agent %s bought %d shares of:%s for %d$" % (data['origin'], data['stocksToBuy'], data['data']['name'], price))
def _setup(self): helper.print_green("\nVAS Stock exchange\t%s\tis up" % self.getAID().getAddresses()) template = ACLTemplate() template.setOntology('stock') behaviour = spade.Behaviour.MessageTemplate(template) self.addBehaviour(self.OpenStockExchange(), behaviour)
def declare_win(self): helper.print_green("\nAgent %s WON! Total sum: %d$" % (self.name, self.get_bank_account())) msg_stock_win = json.dumps({ 'uuid': str(uuid.uuid4()), 'request_type': 'stock_win', 'data': None, 'origin': self.ip }) self.send_message_to_stock(msg_stock_win)
def remove_from_my_stocks(self, data): clean = [] for stock in self.myStocks: if stock['id'] != data['id']: clean.append(stock) self.myStocks = clean helper.print_green("Agent:%s\t+%d$ to budget" % (self.name, float(data['price']))) self.budget += float(data['price']) self.print_money_status()
def _process(self): self.msg = self._receive(True) if self.msg: request = json.loads(self.msg.content) # Registering brokers to start stock exchange if request['request_type'] == 'stock_sign_in': self.brokers += 1 print "Broker %s signed in %d/%d" % ( request['origin'], self.brokers, self.brokers_total) # All brokers are registrated if self.brokers == self.brokers_total: self.initialize() helper.print_green("Opening stock exchange...") self.open_stock_exchange() # Collect round status from agents if request['request_type'] == 'evaluation_done': self.evaluation += 1 print "Broker %s done with move round %d status: %d/%d" % ( request['origin'], self.round + 1, self.evaluation, self.brokers_total) if self.evaluation == self.brokers_total: # Round is over, send new report self.round += 1 self.evaluation = 0 self.stock_speculate() # Get stock report if request['request_type'] == 'stock_report': self.send_stock_exchange_report(request['origin']) # Buy stock if request['request_type'] == 'stock_buy': self.buy_stock(request) # Sell stock if request['request_type'] == 'stock_sell': self.sell_stock(request) # Declare winner and close the stock exchange if request['request_type'] == 'stock_win': helper.print_green( "Broker %s got rich. Closing stock exchange..." % request['origin']) self.send_close_stock_exchange()