Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_order_2(self):
"""
Test that orders are prioritized by price then chronologically.
"""
user = User.objects.get(pk=8)
wallet = Wallet.objects.get(user=user)
# Want to buy future "Jesse Day".
# There is a sell order of 8 for 600, a buy order of 4 for 100,
# a sell order of 4 for 325, a buy order of 4 for 275 in the fixture.
future = Future.objects.get(pk=4)
response = process_order(4, 600, wallet, 'buy', future)
# Get my order
order = Order.objects.all().order_by('-pk')[0]
self.assertEqual(order.filled, True)
self.assertEqual(order.unit_price, 600) # Unchanged
# Get the matching orders.
order_1 = Order.objects.get(pk=9) # The 325 S
order_2 = Order.objects.get(pk=3) # The 650 S
self.assertEqual(order_1.filled, True)
self.assertEqual(order_2.filled, False)
self.assertEqual(order_1.filled_by, user)
def execute_transfer(transfer):
"""
Executes a single transfer. This needs to be an atomic operation.
"""
# Lock the entire wallet. This is expensive but I can't think of a
# better way.
lonelock(Wallet, 0)
buyer_wallet = Wallet.objects.get(user__pk=transfer['buyer'])
seller_wallet = Wallet.objects.get(user__pk=transfer['seller'])
points = transfer['points']
shares = transfer['share_quantity']
future = Future.objects.get(pk=transfer['share_type'])
buyer_owned_shares = json.loads(buyer_wallet.shares_owned)
seller_owned_shares = json.loads(seller_wallet.shares_owned)
future_key = '%s' % future.pk
old_buyer_owned = buyer_owned_shares.get(future_key, 0)
buyer_owned_shares[future_key] = (old_buyer_owned + shares)
seller_owned_shares[future_key] = (
seller_owned_shares.get(future_key, 0) - shares)
# Transfer some points.
buyer_wallet.points -= points
seller_wallet.points += points
# Unfreeze points.
# Buyer points -- unfreeze 'points' since they were already transferred
# right above this - the order went through.
buyer_wallet.frozen -= points
def update_bid_ask(future_id):
"""
Update the bid and ask for a future based on open orders.
"""
future = Future.objects.get(pk=future_id)
open_orders = Order.objects.filter(filled=False, future=future)
bids = open_orders.filter(order_type=Order.ORDER_TYPE_BUY).order_by(
'-unit_price')
asks = open_orders.filter(order_type=Order.ORDER_TYPE_SELL).order_by(
'unit_price')
if bids.count() > 0:
future.bid = bids[0].unit_price
else:
future.bid = None
if asks.count() > 0:
future.ask = asks[0].unit_price
else:
future.ask = None
future.save()
def futures(request):
"""
Loads futures for category with id.
"""
id = request.GET.get('category')
try:
category = FutureCategory.objects.get(id=id)
except FutureCategory.DoesNotExist:
return response({}, status=400)
futures = Future.objects.filter(category=category)
futures_resp = [{'name': f.name,
'description': f.description,
'id': f.id,
'is_open': f.is_open,
'last_buy': f.last_buy,
'bid': f.bid,
'ask': f.ask,
'volume': f.volume} for f in futures]
return response(futures_resp)