ok dokey, performance ist ein wenig besser
This commit is contained in:
@@ -6,6 +6,7 @@ from influxdb_client import InfluxDBClient, Point
|
||||
import time
|
||||
import pandas as pd
|
||||
import uuid
|
||||
import threading
|
||||
|
||||
software_start=int(time.time())
|
||||
class Exchange():
|
||||
@@ -30,16 +31,18 @@ class Exchange():
|
||||
self.best_ask={}
|
||||
self.best_bid={}
|
||||
self.total_demand={}
|
||||
self.demand={}
|
||||
#self.demand={}
|
||||
self.total_supply={}
|
||||
self.supply={}
|
||||
#self.supply={}
|
||||
self.traded_commoditys={}
|
||||
self.lock=threading.Lock()
|
||||
|
||||
def add_to_account(self,account_id,resource,amount):
|
||||
"""
|
||||
Adds resources to account escrow
|
||||
"""
|
||||
#check if account exists
|
||||
self.lock.acquire()
|
||||
if account_id not in self.account:
|
||||
self.account[account_id]={_bal: 0}
|
||||
#check if ressource exists
|
||||
@@ -47,12 +50,14 @@ class Exchange():
|
||||
self.account[account_id][resource]=0
|
||||
|
||||
self.account[account_id][resource]+=amount
|
||||
self.lock.release()
|
||||
|
||||
def remove_from_account(self,account_id,resource,amount) -> int:
|
||||
"""
|
||||
Remove resources from account. Returns amount retrieved
|
||||
"""
|
||||
#check if account exists
|
||||
self.lock.acquire()
|
||||
if account_id not in self.account:
|
||||
self.account[account_id]={_bal: 0}
|
||||
#check if ressource exists
|
||||
@@ -65,6 +70,7 @@ class Exchange():
|
||||
self.account[account_id][resource]=0
|
||||
else:
|
||||
self.account[account_id][resource]-=amount
|
||||
self.lock.release()
|
||||
return ret
|
||||
|
||||
def _move_to_escrow(self,account_id,resource,amount):
|
||||
@@ -133,13 +139,14 @@ class Exchange():
|
||||
# no sufficient resources
|
||||
return None
|
||||
# create order and execude any trades
|
||||
|
||||
self.lock.acquire()
|
||||
order,trades=self.lme.add_order(resource,price,amount,side)
|
||||
|
||||
self.orders[order.order_id]=order
|
||||
self.order_account_map[order.order_id]=account_id
|
||||
self._execute_trades(trades)
|
||||
self.calculate_resource_metrics(resource)
|
||||
self.lock.release()
|
||||
return order
|
||||
|
||||
def cancel_order(self,order_id):
|
||||
@@ -152,6 +159,7 @@ class Exchange():
|
||||
order=self.orders[order_id]
|
||||
if self._is_order_complete(order):
|
||||
return False
|
||||
self.lock.acquire()
|
||||
order=self.lme.cancel_order(order_id,order.instmt)
|
||||
|
||||
if order_id in self.order_trades_map: # has order any kind of trades
|
||||
@@ -171,6 +179,7 @@ class Exchange():
|
||||
qty_traded+=trade.trade_qty
|
||||
qty_diff=qty_submitted-qty_traded
|
||||
self._move_to_account(self.order_account_map[order_id],order.instmt,qty_diff)
|
||||
self.lock.release()
|
||||
return True
|
||||
|
||||
|
||||
@@ -222,21 +231,29 @@ class Exchange():
|
||||
|
||||
def calculate_resource_metrics(self, resource):
|
||||
order_book = self.lme.order_books.setdefault(resource, OrderBook())
|
||||
best_bid = max(order_book.bids.keys()) if len(order_book.bids) > 0 else None
|
||||
best_ask = max(order_book.asks.keys()) if len(order_book.asks) > 0 else None
|
||||
best_bid = None
|
||||
best_ask = None
|
||||
self.total_demand[resource]=0
|
||||
self.demand[resource]={}
|
||||
#self.demand[resource]={}
|
||||
for k,v in order_book.bids.items():
|
||||
self.demand[resource][k]=0
|
||||
# self.demand[resource][k]=0
|
||||
if best_bid==None:
|
||||
best_bid=k
|
||||
if best_bid<=k:
|
||||
best_bid=k
|
||||
for o in v:
|
||||
self.demand[resource][k]+=o.leaves_qty
|
||||
# self.demand[resource][k]+=o.leaves_qty
|
||||
self.total_demand[resource]+=o.leaves_qty
|
||||
self.supply[resource]={}
|
||||
#self.supply[resource]={}
|
||||
self.total_supply[resource]=0
|
||||
for k,v in order_book.asks.items():
|
||||
self.supply[resource][k]=0
|
||||
# self.supply[resource][k]=0
|
||||
if best_ask==None:
|
||||
best_ask=k
|
||||
if best_ask<=k:
|
||||
best_ask=k
|
||||
for o in v:
|
||||
self.supply[resource][k]+=o.leaves_qty
|
||||
# self.supply[resource][k]+=o.leaves_qty
|
||||
self.total_supply[resource]+=o.leaves_qty
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user