75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
from .base_distribution_agent import Base_Distribution_Agent
|
|
|
|
class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
|
"""
|
|
Aquire agent with internal price believe system.
|
|
"""
|
|
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
|
super().__init__(simulation, business, resource, exchanges)
|
|
self.lr=lr
|
|
self.max_price_adj_rate=max_price_adj_rate
|
|
self.price_believe=-1
|
|
self.open_orders={i: [] for i in range(len(self.exchanges))}
|
|
self.open_qty=0
|
|
|
|
def tick(self):
|
|
if self.price_believe==-1:
|
|
self.price_believe=self.min_price
|
|
|
|
order_error=self.target_error()
|
|
if order_error>0:
|
|
# aquire based on current price belive
|
|
order=self.distribute_resource(self.price_believe,order_error,0)
|
|
self.register_order(0,order)
|
|
self.tick_open_orders()
|
|
|
|
|
|
|
|
def register_order(self,cx_id,order):
|
|
self.open_orders[cx_id].append({
|
|
'id': order.order_id,
|
|
'lifetime': self.max_price_adj_rate,
|
|
})
|
|
|
|
def tick_open_orders(self) -> int:
|
|
"""
|
|
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
|
Returns ids of orders that timeed out.
|
|
"""
|
|
self.open_qty=0
|
|
for cx_id in range(len(self.exchanges)):
|
|
cx=self.exchanges[cx_id]
|
|
cx_orders=self.open_orders[cx_id]
|
|
for i in cx_orders:
|
|
# Check for each order if it is fullfiled or if it is timed
|
|
o=self.orders[cx_id][i["id"]]
|
|
if o.leaves_qty==0:
|
|
#order is done
|
|
self.open_orders[cx_id].remove(i) # remove order from open
|
|
self.update_believe(1) # update price believe
|
|
self.collect_balance_from_cxs()
|
|
self.collect_resource_from_cxs(self.resource)
|
|
continue
|
|
if i["lifetime"]>0:
|
|
self.open_qty+=o.leaves_qty
|
|
i["lifetime"]-=1 # subtract lifetime
|
|
else:
|
|
# timeout
|
|
cx.cancel_order(i["id"])
|
|
self.collect_balance_from_cxs()
|
|
self.collect_resource_from_cxs(self.resource)
|
|
self.update_believe(-1)
|
|
self.open_orders[cx_id].remove(i)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_believe(self,modifier):
|
|
"""
|
|
Updates the believe based on the modifier.
|
|
If positive will add lr to believe
|
|
If negative will sub lr to believe
|
|
"""
|
|
self.price_believe+=modifier*self.lr |