Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to process a query from my database of model TradeOrder:

class TradeOrder(models.Model):
    #denoted in 2 three letter tickers with underscore such as anc_btc
    pair = models.CharField(max_length=50)
    #buy_order or sell_order
    order_type = models.CharField(max_length=50)
    #number e.g. .3455 or .190  
    trade_rate = models.PositiveIntegerField()
    trade_amount = models.PositiveIntegerField()
    #name of the account
    trade_order_account = models.CharField(max_length=50)

    def __str__(self):
        return '%s %s %s %s %s'  % (self.pair, self.order_type, self.trade_rate, self.trade_amount, self.trade_order_account)

when i execute under views.py for my app

buyorders=TradeOrder.objects.filter(pair="anc_btc", order_type="buy_order")

i get a list that looks like this:

[<TradeOrder: anc_btc buy_order 7987 7897 a>, <TradeOrder: anc_btc buy_order 7897 789 a>, <TradeOrder: anc_btc buy_order 7897 789 a>]

so want to process and refine that data, firstly to compare each item to a new order

something like:

            if new_order_type=="buy_order":
                #crosscheck against sell orders
                market_sell_orders = TradeOrder.objects.filter(pair="anc_btc", order_type="sell_order", trade_order_account=price)
                #now i need to sort the orders for trade rates above/greater than $new_order_price
                #how do i do this?

i now know that this can be accomplished by adding .order_by('trade_order_price') to the end of the query

                if potential_sell_orders is not None:
                #trade
                    do=1
                else: 
                    #no sell orders to fill, submit to order book
                    try:
                        tradeordersubmit=TradeOrder(pair=order_pair, order_type=order_type, trade_rate=price, trade_amount=quantity, trade_order_account=request.user.username) 
                        tradeordersubmit.save()
                        order_error=0
                    except:
                        order_error="1"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

now i need to sort the orders for trade rates above/greater than $new_order_price

You should refine your QuerySet appropriately using QuerySet methods:

 market_sell_orders = TradeOrder.objects.filter(pair="anc_btc", order_type="sell_order", trade_order_account=price, trade_rate__gt=<trade_rate>).order_by('-trade_rate')

You can find the tutorial for QuerySet here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...