def participate_market(request, rs_id,market_id): player = Player.objects.get(id=request.user.player.id) market = get_object_or_404(Market, pk=market_id) new_market_number = random.randint(1,market.product.market_number) new_market = Market.objects.get(product = market.product,market_number = new_market_number) securities = Security.objects.filter(market = new_market) rs = RetailStore.objects.get(id = rs_id) try: pr = PlayerResult.objects.get(player =player,market = market,estimate = True,checked=False) pr.checked = True pr.save() try: PlayerResult.objects.get(player =player,market__product = market.product,estimate = False,checked=False) except PlayerResult.DoesNotExist: new_pr = PlayerResult(player =player,market = new_market,estimate_time = timezone.now()) new_pr.save() except PlayerResult.DoesNotExist: try: PlayerResult.objects.get(player =player,market__product = market.product,estimate = False,checked=False) except PlayerResult.DoesNotExist: new_pr = PlayerResult(player =player,market = new_market,estimate_time = timezone.now()) new_pr.save() try: participation = Participation.objects.get(market = market, player = player) participation.reward_checked = True participation.market = new_market participation.save() except Participation.DoesNotExist: participation = Participation(market = new_market, player = player) participation.save() for security in securities: try: PlayerSecurity.objects.get(player = player,security = security) except PlayerSecurity.DoesNotExist: ps = PlayerSecurity(player = player,security = security,amount = 0) ps.save() designers = Designer.objects.filter(player = player,newIdea__accept = True,newIdea__product = market.product,checked = False) if designers.count()>0: point = 10000 * designers.count() designers.update(checked=True) return render_to_response('Market/accept_idea.html', {'market': new_market,'rs':rs,'designers':designers,'point':point}, context_instance=RequestContext(request)) else: return render_to_response('Market/participate_market.html', {'market': new_market,'rs':rs},context_instance=RequestContext(request))
def results(request, market_id): m = get_object_or_404(Market, pk=market_id) lmsr = get_object_or_404(LMSR, product = m.product) player = Player.objects.get(id=request.user.player.id) security = SecurityPrice.objects.get(market = m) try: pr = PlayerResult.objects.get(player = player, market = m,estimate = False) except MultipleObjectsReturned: pr_list = PlayerResult.objects.filter(player = player, market = m,estimate = False) pr_list.delete() pr = PlayerResult(player = player, market = m,estimate = False,estimate_time = timezone.now()) pr.save() except PlayerResult.DoesNotExist: pr = PlayerResult.objects.get(player = player, market = m,estimate = False) pr.save() pt = PlayerTransaction.objects.get(player=player) trade_security = Security.objects.filter(market = m).get(number = pt.security_number) trade_security_price = SecurityPrice.objects.get(market = m) try: ps = PlayerSecurity.objects.get(player = player,security = trade_security) except PlayerSecurity.DoesNotExist: ps = PlayerSecurity(player = player,security = trade_security,amount = 0) if pt.action == "buy": if player.point - pt.cost >= 0 : ps.amount += pt.amount player.point -= pt.cost pr.investment += pt.cost trade_security_price.amount[pt.security_number] += pt.amount else: return render_to_response('Market/buy_transaction.html', { 'market': m,'price':security.price,'share':security.sale_rate,'P0':lmsr.P0,'error_message': "You don't have enough price", }, context_instance=RequestContext(request)) elif pt.action == "sell": if ps.amount >= pt.amount: ps.amount -= pt.amount player.point -= pt.cost pr.returned -= pt.cost trade_security_price.amount[pt.security_number] -= pt.amount else : return render_to_response('Market/sell_transaction.html', { 'market': m,'price':security.price,'share':security.sale_rate,'P0':lmsr.P0,'error_message': "You don't have enough products", }, context_instance=RequestContext(request)) security = m.security_set.all() cost_sum = sum([math.exp(s/lmsr.b) for s in trade_security_price.amount]) trade_security_price.price = [round(lmsr.P0*math.exp(s/lmsr.b)/cost_sum,2) for s in trade_security_price.amount] trade_security_price.sale_rate = [round(s/lmsr.P0*0.5+0.5,2) for s in trade_security_price.price] trade_security_price.save() if m.trade_amount > 10: trade_security.VWAP_amount += int(pt.amount) trade_security.VWAP_price += abs(pt.cost) - trade_security.gross_margin * pt.amount trade_security.save() m.trade_amount += 1 pt.transaction = False m.save() ps.save() pt.save() pr.save() player.save() return render_to_response('Market/results.html', {'market': m,'player':player,'sale_rate':trade_security_price.sale_rate}, context_instance=RequestContext(request))
def participate_market(request, rs_id, market_id): player = Player.objects.get(id=request.user.player.id) market = get_object_or_404(Market, pk=market_id) new_market_number = random.randint(1, market.product.market_number) new_market = Market.objects.get(product=market.product, market_number=new_market_number) securities = Security.objects.filter(market=new_market) rs = RetailStore.objects.get(id=rs_id) try: pr = PlayerResult.objects.get(player=player, market=market, estimate=True, checked=False) pr.checked = True pr.save() try: PlayerResult.objects.get(player=player, market__product=market.product, estimate=False, checked=False) except PlayerResult.DoesNotExist: new_pr = PlayerResult(player=player, market=new_market, estimate_time=timezone.now()) new_pr.save() except PlayerResult.DoesNotExist: try: PlayerResult.objects.get(player=player, market__product=market.product, estimate=False, checked=False) except PlayerResult.DoesNotExist: new_pr = PlayerResult(player=player, market=new_market, estimate_time=timezone.now()) new_pr.save() try: participation = Participation.objects.get(market=market, player=player) participation.reward_checked = True participation.market = new_market participation.save() except Participation.DoesNotExist: participation = Participation(market=new_market, player=player) participation.save() for security in securities: try: PlayerSecurity.objects.get(player=player, security=security) except PlayerSecurity.DoesNotExist: ps = PlayerSecurity(player=player, security=security, amount=0) ps.save() designers = Designer.objects.filter(player=player, newIdea__accept=True, newIdea__product=market.product, checked=False) if designers.count() > 0: point = 10000 * designers.count() designers.update(checked=True) return render_to_response('Market/accept_idea.html', { 'market': new_market, 'rs': rs, 'designers': designers, 'point': point }, context_instance=RequestContext(request)) else: return render_to_response('Market/participate_market.html', { 'market': new_market, 'rs': rs }, context_instance=RequestContext(request))
def results(request, market_id): m = get_object_or_404(Market, pk=market_id) lmsr = get_object_or_404(LMSR, product=m.product) player = Player.objects.get(id=request.user.player.id) security = SecurityPrice.objects.get(market=m) try: pr = PlayerResult.objects.get(player=player, market=m, estimate=False) except MultipleObjectsReturned: pr_list = PlayerResult.objects.filter(player=player, market=m, estimate=False) pr_list.delete() pr = PlayerResult(player=player, market=m, estimate=False, estimate_time=timezone.now()) pr.save() except PlayerResult.DoesNotExist: pr = PlayerResult.objects.get(player=player, market=m, estimate=False) pr.save() pt = PlayerTransaction.objects.get(player=player) trade_security = Security.objects.filter(market=m).get( number=pt.security_number) trade_security_price = SecurityPrice.objects.get(market=m) try: ps = PlayerSecurity.objects.get(player=player, security=trade_security) except PlayerSecurity.DoesNotExist: ps = PlayerSecurity(player=player, security=trade_security, amount=0) if pt.action == "buy": if player.point - pt.cost >= 0: ps.amount += pt.amount player.point -= pt.cost pr.investment += pt.cost trade_security_price.amount[pt.security_number] += pt.amount else: return render_to_response( 'Market/buy_transaction.html', { 'market': m, 'price': security.price, 'share': security.sale_rate, 'P0': lmsr.P0, 'error_message': "You don't have enough price", }, context_instance=RequestContext(request)) elif pt.action == "sell": if ps.amount >= pt.amount: ps.amount -= pt.amount player.point -= pt.cost pr.returned -= pt.cost trade_security_price.amount[pt.security_number] -= pt.amount else: return render_to_response( 'Market/sell_transaction.html', { 'market': m, 'price': security.price, 'share': security.sale_rate, 'P0': lmsr.P0, 'error_message': "You don't have enough products", }, context_instance=RequestContext(request)) security = m.security_set.all() cost_sum = sum([math.exp(s / lmsr.b) for s in trade_security_price.amount]) trade_security_price.price = [ round(lmsr.P0 * math.exp(s / lmsr.b) / cost_sum, 2) for s in trade_security_price.amount ] trade_security_price.sale_rate = [ round(s / lmsr.P0 * 0.5 + 0.5, 2) for s in trade_security_price.price ] trade_security_price.save() if m.trade_amount > 10: trade_security.VWAP_amount += int(pt.amount) trade_security.VWAP_price += abs( pt.cost) - trade_security.gross_margin * pt.amount trade_security.save() m.trade_amount += 1 pt.transaction = False m.save() ps.save() pt.save() pr.save() player.save() return render_to_response('Market/results.html', { 'market': m, 'player': player, 'sale_rate': trade_security_price.sale_rate }, context_instance=RequestContext(request))