HA looks good

This commit is contained in:
2023-06-29 09:48:11 +02:00
parent 4bfe5245a9
commit 4d4d609cea
13 changed files with 134 additions and 98 deletions

View File

@@ -1,59 +1,63 @@
from .base_aquire_agent import Base_Aquire_Agent
import random
class Price_Believe_Aquire_Agent(Base_Aquire_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={i: 1 for i in range(len(self.exchanges))}
self.open_orders={i: [] for i in range(len(self.exchanges))}
self.open_qty=0
self.lr = lr
self.max_price_adj_rate = max_price_adj_rate
self.price_believe = {i: 1 for i in range(len(self.exchanges))}
self.open_orders = {i: [] for i in range(len(self.exchanges))}
self.open_qty = 0
self.hp_threshold=0.25
self.lp_threshold=0.90
def tick(self,tick,episode):
def tick(self, tick, episode):
order_error=self.open_qty+self.target_error()
if order_error<0:
order_error = self.open_qty+self.target_error()
if order_error < 0:
# aquire based on current price believe
cx_id=self.select_best_cx()
order=self.order_resource(self.price_believe[cx_id],order_error*-1,cx_id)
if not order==None:
self.register_order(cx_id,order)
cx_id = self.select_best_cx()
order = self.order_resource(
self.price_believe[cx_id], order_error*-1, cx_id)
if not order == None:
self.register_order(cx_id, order)
else:
# order failed due to missing balance.. we need to adjust our price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_believe(cx_id,-1)
self.tick_open_orders()
self.update_believe(cx_id, -1)
self.tick_open_orders()
def select_best_cx(self):
best_id=-1
best=999999999
best_id = -1
best = 999999999
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
available=(cx.get_total_supply(self.resource)>0)
cx = self.exchanges[cx_id]
available = (cx.get_total_supply(self.resource) > 0)
if available:
potential=1*self.price_believe[cx_id]
potential = 1*self.price_believe[cx_id]
else:
continue
if potential<best:
best=potential
best_id=cx_id
if best_id==-1:
best_id=random.randint(0,len(self.exchanges)-1)
if potential < best:
best = potential
best_id = cx_id
if best_id == -1:
best_id = random.randint(0, len(self.exchanges)-1)
return best_id
def register_order(self,cx_id,order):
def register_order(self, cx_id, order):
self.open_orders[cx_id].append({
'id': order.order_id,
'lifetime': self.max_price_adj_rate,
'leaves': order.leaves_qty
})
if order.leaves_qty!=order.qty:
if order.leaves_qty != order.qty:
self.update_trades()
def tick_open_orders(self) -> int:
@@ -61,69 +65,81 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
Removes 1 from pending orders timeout timer and cancels timeed out orders.
Returns ids of orders that timeed out.
"""
self.open_qty=0
self.open_qty = 0
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
cx_orders=self.open_orders[cx_id]
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"]]
leaves=o.leaves_qty
if o.leaves_qty==0:
#order is done
self.open_orders[cx_id].remove(i) # remove order from open
self.update_believe(cx_id,-1) # update price believe
# Check for each order if it is fullfiled or if it is timed
o = self.orders[cx_id][i["id"]]
leaves = o.leaves_qty
if o.leaves_qty == 0:
# order is done
self.open_orders[cx_id].remove(i) # remove order from open
self.update_believe(cx_id, -1) # update price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
continue
if not (i["leaves"]==leaves):
#update in order
i["leaves"]=leaves
if not (i["leaves"] == leaves):
# update in order
i["leaves"] = leaves
self.update_trades()
#reset lifetime
i["lifetime"]=self.max_price_adj_rate
if i["lifetime"]>0:
self.open_qty+=o.leaves_qty
i["lifetime"]-=1 # subtract lifetime
# reset lifetime
i["lifetime"] = self.max_price_adj_rate
if i["lifetime"] > 0:
self.open_qty += o.leaves_qty
i["lifetime"] -= 1 # subtract lifetime
else:
# timeout
self.update_trades()
buy = o.qty-o.leaves_qty
buyp=buy/o.qty
sup = cx.total_supply[self.resource]+buy
if sup == 0:
sup = 1
coverage = buy/sup
# 50 % coverage limit
modifier = (max([coverage,buyp])*2)-1
modifier =0
success=self.calc_order_success(cx,o)
if success>=self.lp_threshold:
modifier=-1
elif success<=self.hp_threshold:
modifier=1
cx.cancel_order(i["id"])
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_believe(cx_id,1)
self.update_believe(cx_id, modifier)
self.open_orders[cx_id].remove(i)
def update_believe(self,cx_id,modifier):
def calc_order_success(self, cx, o):
"""
Calculate how we should adjust the price belive
"""
buy = o.qty-o.leaves_qty
buyperc = buy/o.qty
dem = cx.total_supply[self.resource]+buy
if dem == 0:
dem = 1
coverage = buy/dem
base_success = max([coverage, buyperc])
return base_success
def update_believe(self, cx_id, 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[cx_id]+=modifier*self.lr
if self.price_believe[cx_id]<0:
self.price_believe[cx_id]=0
self.price_believe[cx_id] += modifier*self.lr
if self.price_believe[cx_id] < 0:
self.price_believe[cx_id] = 0
def reset(self,episode):
def reset(self, episode):
# Clean shop for today
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
cx_orders=self.open_orders[cx_id]
cx = self.exchanges[cx_id]
cx_orders = self.open_orders[cx_id]
for i in cx_orders:
cx.cancel_order(i["id"])
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
# book keeping
self.update_trades()
self.open_orders={i: [] for i in range(len(self.exchanges))}
self.orders={i: {} for i in range(len(self.exchanges))}
return super().reset(episode)
self.open_orders = {i: [] for i in range(len(self.exchanges))}
self.orders = {i: {} for i in range(len(self.exchanges))}
return super().reset(episode)