Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transaction = SuccessfulTransaction(future=order.future,
quantity=open_order.quantity,
unit_price=open_order.unit_price)
if order.order_type == Order.ORDER_TYPE_BUY:
# Transfer points from order.creator to open_order.creator
transfer = {'buyer': order.creator.pk,
'buyer_order': order.pk,
'seller': open_order.creator.pk,
'seller_order': open_order.pk,
'price': open_order.unit_price,
'points': quantity * open_order.unit_price,
'share_quantity': quantity,
'share_type': order.future.pk}
transaction.buyer = order.creator
transaction.seller = open_order.creator
elif order.order_type == Order.ORDER_TYPE_SELL:
# Transfer points from open_order.creator to order.creator
transfer = {'buyer': open_order.creator.pk,
'buyer_order': open_order.pk,
'seller': order.creator.pk,
'seller_order': order.pk,
'price': open_order.unit_price,
'points': quantity * open_order.unit_price,
'share_quantity': quantity,
'share_type': order.future.pk}
transaction.buyer = open_order.creator
transaction.seller = order.creator
history_entry = FutureHistory(future=order.future,
price=open_order.unit_price)
if filled_q > 0:
# We want to buy or sell more than this open order's quantity.
error = validate_order_params(num_shares, price, wallet, order_type,
future)
if error:
return response(error, status=400)
open_orders = Order.objects.filter(filled=False, future=future).exclude(
creator=wallet.user)
# Create an order.
order = Order(future=future, quantity=int(num_shares),
unit_price=int(price), creator=wallet.user)
if order_type == 'buy':
order.order_type = Order.ORDER_TYPE_BUY
open_orders = open_orders.filter(order_type=Order.ORDER_TYPE_SELL)
elif order_type == 'sell':
order.order_type = Order.ORDER_TYPE_SELL
open_orders = open_orders.filter(order_type=Order.ORDER_TYPE_BUY)
# Save the order, then we will see if we have a match.
order.save()
freeze_assets(order, wallet)
logger.debug('*' * 20)
logger.debug(order)
# Try to execute order now, if possible.
transfers = execute_order(order, open_orders)
# Try to execute all transfers.
execute_transfers(transfers)
logger.debug('*' * 20)
# Update bid/ask
update_bid_ask(future.pk)
return response('Success') # Front-end should just refresh the page for
def compute_frozen(order, wallet):
"""
Compute how many points should be frozen by this order.
"""
shares = json.loads(wallet.shares_owned)
key = '%s' % order.future.pk
num_shares = shares.get(key, 0)
if order.order_type == Order.ORDER_TYPE_BUY:
return order.quantity * order.unit_price
elif order.order_type == Order.ORDER_TYPE_SELL and num_shares <= 0:
return SETTLEMENT_PRICE * order.quantity
return 0
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()