def showMarket(request, idMarket): from django.db import connection xhr = request.GET.has_key('xhr') cursor = connection.cursor() form2 = LoginForm() titre="Market" deleted='' market=Market.objects.get(id=idMarket) event=market.event msgNb=0 err=False settled=False if event.status==1: settled=True mwin='Lost' if market.win==True: mwin='Won' trader=Trader() if request.user.is_authenticated(): trader=Trader.objects.get(user=request.user) idUser=trader.user.id if request.method == 'POST' and trader.active==True and event.status==0 and event.globalEvent.dateClose>timezone.now(): oform = OrderForm(request.POST) if oform.is_valid(): if 'buyOrder' in request.POST: volume=oform.cleaned_data['bvolume'] price=oform.cleaned_data['bprice'] side=1 else: volume=oform.cleaned_data['svolume'] price=oform.cleaned_data['sprice'] side=-1 if volume==None or price==None: oform.non_field_errors="Empty Fields !" else: if Trader.objects.availableBalanceIf(trader=trader, newIdMarket=market.id, newSide=side, newPrice=price, newVolume=volume)>=0: try: deleted=execute(market, trader, side, price, volume) if deleted==True: oform.non_field_errors="Not enough balance !" #else: # return redirect(reverse(showMarket, kwargs={'idMarket':idMarket, 'sd':0, 'isPost':1})) except: oform.non_field_errors="Order not submitted" else: oform.non_field_errors="Not enough balance !" else: oform = OrderForm() deposit=float(Decimal(Trader.objects.deposit(trader=trader)).quantize(Decimal('.01'), rounding=ROUND_DOWN)) available=float(Decimal(Trader.objects.availableBalance(trader=trader)).quantize(Decimal('.01'), rounding=ROUND_DOWN)) risk=-float(Decimal(Trader.objects.riskEvent(trader=trader, event=market.event)).quantize(Decimal('.01'), rounding=ROUND_DOWN)) myBuyVolume=Trade.objects.filter(Q(market=market) & ((Q(trader1=trader) & Q(side=1)) | (Q(trader2=trader) & Q(side=-1))) & Q(nullTrade=False)).aggregate(Sum('volume'))['volume__sum'] if myBuyVolume==None: myBuyVolume=0 myBuyVolume=float(Decimal(myBuyVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN)) mySellVolume=Trade.objects.filter(Q(market=market) & ((Q(trader1=trader) & Q(side=-1)) | (Q(trader2=trader) & Q(side=1))) & Q(nullTrade=False)).aggregate(Sum('volume'))['volume__sum'] if mySellVolume==None: mySellVolume=0 mySellVolume=float(Decimal(mySellVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN)) avgPriceSell=float(Decimal(Trader.objects.avgPrice(trader=trader, market=market, side=-1)).quantize(Decimal('.01'), rounding=ROUND_DOWN)) avgPriceBuy=float(Decimal(Trader.objects.avgPrice(trader=trader, market=market, side=1)).quantize(Decimal('.01'), rounding=ROUND_DOWN)) myBuyLimits = Limit.objects.filter(market=market, trader=trader, side=1).order_by('-timestamp') mySellLimits = Limit.objects.filter(market=market, trader=trader, side=-1).order_by('-timestamp') else: oform = OrderForm() cursor.execute("SELECT price price, sum(volume) volume FROM markets_limit WHERE side=1 and market_id=%i GROUP BY price ORDER BY price DESC" % market.id) limitsBuy = dictfetchall(cursor) cursor.execute("SELECT price price, sum(volume) volume FROM markets_limit WHERE side=-1 and market_id=%i GROUP BY price ORDER BY price ASC" % market.id) limitsSell = dictfetchall(cursor) cursor.execute("SELECT price price, volume volume, side side, timestamp timestamp FROM markets_trade WHERE not nullTrade and market_id=%i ORDER BY timestamp DESC" % market.id) trades = dictfetchall(cursor) trades=trades[:20] graphData=[] i=0 for trade in trades: trades[i]['price']=float(trades[i]['price']) trades[i]['volume']=float(trades[i]['volume']) trades[i]['timestamp']=str(trades[i]['timestamp']) i=i+1 if i>0: price=trades[i-1]['price'] else: price=0 i=20 for trade in trades: i=i-1 graphData.append([i,float(trade['price'])])#[trade['volume'], trade['price']]) buyVolume=Limit.objects.filter(market=market, side=1).aggregate(Sum('volume'))['volume__sum'] if buyVolume==None: buyVolume=0 buyVolume=float(Decimal(buyVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN)) sellVolume=Limit.objects.filter(market=market, side=-1).aggregate(Sum('volume'))['volume__sum'] if sellVolume==None: sellVolume=0 sellVolume=float(Decimal(sellVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN)) tradedVolume=Market.objects.tradedVolume(market=market) if tradedVolume==None: tradedVolume=0 tradedVolume=float(Decimal(tradedVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN)) openInterest=Market.objects.openInterest(market=market) if openInterest==None: openInterest=0 openInterest=float(Decimal(openInterest).quantize(Decimal('.01'), rounding=ROUND_DOWN)) time = str(datetime.now()) return render(request, 'markets/market.html', locals())
def showMarket(request, idMarket): series = flot.Series(data=[(1,2),(2,5),(3,7),(4,9)]) graph = flot.Graph([series,]) from django.db import connection cursor = connection.cursor() form2 = LoginForm() titre="Market" deleted='' market=Market.objects.get(id=idMarket) event=market.event msgNb=0 err=False settled=False if event.status==1: settled=True mwin='Lost' if market.win==True: mwin='Won' trader=Trader() if request.user.is_authenticated: trader=Trader.objects.get(user=request.user) idUser=trader.user.id if request.method == 'POST' and trader.active==True and event.status==0 and event.globalEvent.dateClose>timezone.now(): oform = OrderForm(request.POST) if oform.is_valid(): volume=oform.cleaned_data['volume'] price=oform.cleaned_data['price'] side=int(oform.cleaned_data['side']) if Trader.objects.availableBalanceIf(trader=trader, newIdMarket=market.id, newSide=side, newPrice=price, newVolume=volume)>=0: try: deleted=execute(market, trader, side, price, volume) except: msgNb=1 err=True else: msgNb=1 err=True else: oform = OrderForm() deposit=Decimal(Trader.objects.deposit(trader=trader)).quantize(Decimal('.01'), rounding=ROUND_DOWN) available=Decimal(Trader.objects.availableBalance(trader=trader)).quantize(Decimal('.01'), rounding=ROUND_DOWN) risk=-Decimal(Trader.objects.riskEvent(trader=trader, event=market.event)).quantize(Decimal('.01'), rounding=ROUND_DOWN) myBuyVolume=Trade.objects.filter(Q(market=market) & ((Q(trader1=trader) & Q(side=1)) | (Q(trader2=trader) & Q(side=-1))) & Q(nullTrade=False)).aggregate(Sum('volume'))['volume__sum'] if myBuyVolume==None: myBuyVolume=0 myBuyVolume=Decimal(myBuyVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN) mySellVolume=Trade.objects.filter(Q(market=market) & ((Q(trader1=trader) & Q(side=-1)) | (Q(trader2=trader) & Q(side=1))) & Q(nullTrade=False)).aggregate(Sum('volume'))['volume__sum'] if mySellVolume==None: mySellVolume=0 mySellVolume=Decimal(mySellVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN) avgPriceSell=Decimal(Trader.objects.avgPrice(trader=trader, market=market, side=-1)).quantize(Decimal('.01'), rounding=ROUND_DOWN) avgPriceBuy=Decimal(Trader.objects.avgPrice(trader=trader, market=market, side=1)).quantize(Decimal('.01'), rounding=ROUND_DOWN) myBuyLimits = Limit.objects.filter(market=market, trader=trader, side=1).order_by('-timestamp') mySellLimits = Limit.objects.filter(market=market, trader=trader, side=-1).order_by('-timestamp') else: oform = TradeForm() cursor.execute("SELECT price price, sum(volume) volume FROM markets_limit WHERE side=1 and market_id=%i GROUP BY price ORDER BY price DESC" % market.id) limitsBuy = dictfetchall(cursor) cursor.execute("SELECT price price, sum(volume) volume FROM markets_limit WHERE side=-1 and market_id=%i GROUP BY price ORDER BY price DESC" % market.id) limitsSell = dictfetchall(cursor) cursor.execute("SELECT price price, volume volume, side side, timestamp timestamp FROM markets_trade WHERE not nullTrade and market_id=%i ORDER BY timestamp DESC" % market.id) trades = dictfetchall(cursor) trades=trades[:20] graphData=[] i=20 for trade in trades: i=i-1 graphData.append([i,float(trade['price'])])#[trade['volume'], trade['price']]) buyVolume=Limit.objects.filter(market=market, side=1).aggregate(Sum('volume'))['volume__sum'] if buyVolume==None: buyVolume=0 buyVolume=Decimal(buyVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN) sellVolume=Limit.objects.filter(market=market, side=-1).aggregate(Sum('volume'))['volume__sum'] if sellVolume==None: sellVolume=0 sellVolume=Decimal(sellVolume).quantize(Decimal('.01'), rounding=ROUND_DOWN) tradedVolume=Decimal(Market.objects.tradedVolume(market=market)).quantize(Decimal('.01'), rounding=ROUND_DOWN) if tradedVolume==None: tradedVolume=0 openInterest=Decimal(Market.objects.openInterest(market=market)).quantize(Decimal('.01'), rounding=ROUND_DOWN) if msgNb==1: oform.non_field_errors="Not enough balance !" return render(request, 'markets/market.html', locals())