some things work

This commit is contained in:
2023-01-26 14:58:41 +01:00
parent be83cbd988
commit 966db6e5ad
24 changed files with 242 additions and 68 deletions
+14 -11
View File
@@ -1,5 +1,5 @@
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.
@@ -8,14 +8,13 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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.price_believe={i: 0 for i in range(len(self.exchanges))}
self.open_orders={i: [] for i in range(len(self.exchanges))}
self.open_qty=0
def tick(self,tick,episode):
if self.price_believe==-1:
self.price_believe=self.max_price
order_error=self.open_qty+self.target_error()
if order_error<0:
@@ -23,7 +22,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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(0,order)
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()
@@ -32,14 +31,16 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
self.tick_open_orders()
def select_best_cx(self):
best_id=0
best_id=-1
best=0
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
potential=cx.total_supply[self.resource]*self.price_believe[cx_id]
potential=cx.get_total_supply(self.resource)*self.price_believe[cx_id]
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):
@@ -63,7 +64,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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.update_believe(cx_id,-1) # update price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
continue
@@ -75,7 +76,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
cx.cancel_order(i["id"])
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_believe(1)
self.update_believe(cx_id,1)
self.open_orders[cx_id].remove(i)
def update_believe(self,cx_id,modifier):
@@ -85,8 +86,10 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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
def reset(self):
def reset(self,episode):
# Clean shop for today
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
@@ -98,4 +101,4 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
# book keeping
self.update_trades()
return super().reset()
return super().reset(episode)