def test_order_matching(self): sell_order = SellOrder(hookup = self.hookup, volume = 10, price = 100, owner = self.user) buy_order = BuyOrder(hookup = self.hookup, volume = 1, price = 100, owner = self.user) sell_order.save() buy_order.save() helpers.match_orders(buy_order, sell_order) self.assertEqual(sell_order.volume, 9)
def place_buy_order(request, volume, price, hookup_pk): volume = int(volume) price = int(price) hookup = Hookup.objects.get(pk=hookup_pk) try: order = BuyOrder(owner=request.user, volume=volume, price=price, hookup=hookup) order.save() serializer=JSONSerializer() sellers = BuyOrder.objects.filter(hookup=hookup) for sell_order in sellers: match_orders(order, sell_order) #if the order is empty it will delete itself and return nothing if order is None: return None order = BuyOrder.objects.filter(id=order.id) #return the order to the user return serializer.serialize(order) except ValidationError: #User doesn't seem to own have enough points... error = AjaxError("You do not have enough points to place this order.") return error.to_json()
def place_sell_order(request, volume, price, hookup_pk): volume = int(volume) price = int(price) hookup = Hookup.objects.get(pk=hookup_pk) try: ShareGroup.objects.get(owner=request.user) order = SellOrder(owner=request.user, volume=volume, price=price, hookup=hookup) order.save() serializer=JSONSerializer() buyers = BuyOrder.objects.filter(hookup=hookup) for buy_order in buyers: match_orders(buy_order, order) #if the order is empty it will delete itself and return nothing if order is None: return None order = SellOrder.objects.filter(id=order.id) #return the order to the user return serializer.serialize(order) except ShareGroup.DoesNotExist, ValidationError: #User doesn't seem to own enough of these... error = AjaxError("You cannot sell shares that you do not own.") return error.to_json()