def order_placed(self, ord): "Handles creation of orders. " assert (not ord.is_processed()) acc = ord.account order_ev_pos = ord.group_by_event() # get the cost for completing the order cost = sum([self.eval_cost(ev, ps) for (ev,ps) in order_ev_pos]) logger.debug("Order '%s' total sum: %f" % (ord, cost)) # see if the order can be completed ord.is_successful = (acc.funds >= cost) if ord.is_successful: # update the market maker amounts MarketBalance.accept_order(ord) # update the account amounts AccountBalance.accept_order(ord) # deduct the total transaction cost from the account acc.funds -= cost # update buy/sell prices for (ev, ps) in order_ev_pos: self.update_prices(ev) ord.set_processed() acc.save() ord.save() logger.debug("Order '%s' processed!" % (ord,))
def end_challenge(self, datum): """ Finalises a challenge given the current datum. Treats the amounts in AccountBalance and MarketBalance as the number of shares owned for each Outcome. Rewards all players holding shares for winning outcomes (i.e. Results) with 1 credit, or substracts money in the case they have negative. Iterates through all events in this market, gets the Result of the event, and rewards all players with a non-neutral position in the outcome with 1 credit for each share they possess. Finally, resets the market state. """ mkt = datum.data_set.market for ev in mkt.events.all(): # get the event result try: result = Result.objects.get(datum=datum, outcome__event=ev) except: # if it does not exist, print a warning logger.warn("Datum %s from DatSet %s does not define a result for event %s. " % (datum, datum.dataset, ev)) else: # if it was found, reward the winners (and penalise the losers) for accShares in result.outcome.accountbalance_set.all(): if accShares.amount != 0: acc = accShares.account acc.funds += accShares.amount acc.save() logger.debug("Rewarded user %s with %d credits. " % (acc.user, accShares.amount)) # reset all account and market amounts for this event self.reset_event(ev)
def price_quote(self, ord): """Gets the estimated price for the given order. """ ps = ord.group_by_event() cost = sum([self.eval_cost(ev, ps) for (ev,ps) in order_ev_pos]) logger.debug("Order '%s' quote: %f" % (ord, cost)) return cost