Beispiel #1
0
def match_orders(sec, buysell):
    """Match orders in cross"""
    # Get buy and sell lists
    b = Order.query(Order.security == sec, Order.buysell == 'Buy', Order.active == True, ancestor=sec.key).order(-Order.price, Order.timestamp)
    s = Order.query(Order.security == sec, Order.buysell == 'Sell', Order.active == True, ancestor=sec.key).order(Order.price, Order.timestamp)
    b = list(b)
    s = list(s)
    
    # Match orders until market uncrosses
    bn = 0
    sn = 0
    while(1):
        if bn + 1 > len(b):
            break
        if sn + 1 > len(s):
            break
        if b[bn].price >= s[sn].price:
            t = Trade()
            t.timestamp = datetime.utcnow()
            t.buy_user = b[bn].user
            t.sell_user = s[sn].user
            t.security = b[bn].security
            if buysell == "Buy":
                t.price = s[sn].price
            else:
                t.price = b[bn].price
            b[bn] = b[bn].key.get()
            s[sn] = s[sn].key.get()
            b_ptf = Portfolio.query(Portfolio.user == b[bn].user).get()
            s_ptf = Portfolio.query(Portfolio.user == s[sn].user).get()
            if b[bn].volume > s[sn].volume:
                t.volume = s[sn].volume
                b[bn].volume += -s[sn].volume
                s[sn].active = False
                b_ptf.points += s[sn].volume
                s_ptf.points += s[sn].volume
                b[bn].put()
                s[sn].put()
                sn += 1
            elif b[bn].volume < s[sn].volume:
                t.volume = b[bn].volume
                s[sn].volume += -b[bn].volume
                b[bn].active = False
                b_ptf.points += b[bn].volume
                s_ptf.points += b[bn].volume
                b[bn].put()
                s[sn].put()
                bn += 1
            elif b[bn].volume == s[sn].volume:
                t.volume = b[bn].volume
                b[bn].active = False
                s[sn].active = False
                b_ptf.points += b[bn].volume
                s_ptf.points += b[bn].volume
                b[bn].put()
                s[sn].put()
                bn += 1
                sn += 1
            b_ptf.put()
            s_ptf.put()
            t.put()
            flash(u'Trade %s successfully completed.' % t.key.id(), 'success')
            continue
        break
Beispiel #2
0
def trade_add(request):
    goods_top = Trade.objects.values('goods__id','goods__title').annotate(goods_count=Count('goods')).order_by('-goods_count')[:10]

    price1 = 0
    results = []
    if request.method == 'POST': # If the form has been submitted...
        form = TradeForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            #return HttpResponseRedirect('/thanks/') # Redirect after POST

            #results = GClass.objects.filter(title__icontains=form.cleaned_data['goodstitle'])

            isOldTrade = False
            if int(form.cleaned_data["trade_pk"]) > 0:
                isOldTrade = True

            shop1 = None
            if int(form.cleaned_data["shop_pk"]) > 0:
                shop1 = Shop.objects.get(pk=form.cleaned_data["shop_pk"])
            else:
                shop1 = Shop(title=form.cleaned_data["shop"], type='mag')
                shop1.save()

            #gclass1 = None
            #if int(form.cleaned_data["gclass_pk"]) > 0:
            #    gclass1 = GClass.objects.get(pk=form.cleaned_data["gclass_pk"])
            #else:
            #    gclass1 = GClass(title=form.cleaned_data["gclass"], section=GSection.objects.get(pk=1)) #TODO GSection
            #    gclass1.save()

            goods1 = None
            if int(form.cleaned_data["gtitle_pk"]) > 0:
                goods1 = Goods.objects.get(pk=form.cleaned_data["gtitle_pk"])
                goods1.title = form.cleaned_data["gtitle"]
                goods1.ed = form.cleaned_data["ed"]
                #goods1.gclass = gclass1
                goods1.save()
            else:
                goods1 = Goods(title=form.cleaned_data["gtitle"], ed=form.cleaned_data["ed"])
                goods1.save()

            price1 = "%.2f" % ( float(form.cleaned_data['cost']) / float(form.cleaned_data['amount']) )

            if isOldTrade:
                trade1 = Trade.objects.get(pk=form.cleaned_data["trade_pk"])
            else:
                trade1 = Trade()

            trade1.user = request.user
            trade1.shop = shop1
            trade1.goods = goods1
            trade1.time = form.cleaned_data["time"]
            trade1.amount = form.cleaned_data["amount"]
            trade1.price = price1
            trade1.cost = form.cleaned_data["cost"]
            trade1.currency = form.cleaned_data["currency"]
            trade1.spytrade = form.cleaned_data["spytrade"]

            trade1.save()

            return HttpResponseRedirect("/")

    else:
        data = {'time': datetime.datetime.now, 'trade_pk': '0', 'shop_pk': '0', 'gclass_pk': '0', 'gtitle_pk': '0' }
        form = TradeForm(initial=data) # An unbound form

    return render_to_response('trade_add.html',
        {'price': price1, 'results': results, 'form': form, 'goods_top': goods_top},
        context_instance=RequestContext(request))