it just look good
This commit is contained in:
@@ -63,6 +63,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
|
||||
self.business.balance-=total_price
|
||||
cx.add_to_account(self.id,"balance",total_price) # prepaid charge account for cx
|
||||
cx.register_account(self.id,self.business.id)
|
||||
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.BUY)
|
||||
if order==None: # Order failed
|
||||
return None
|
||||
|
||||
@@ -69,6 +69,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
self.business.inventory[self.resource]-=amount
|
||||
|
||||
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
|
||||
cx.register_account(self.id,self.business.id)
|
||||
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)
|
||||
if order==None: # Order failed
|
||||
return False
|
||||
|
||||
@@ -10,18 +10,21 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
def __init__(self, simulation, business, resource, exchanges: list, lr,at_sales, max_price_adj_rate) -> None:
|
||||
super().__init__(simulation, business, resource, exchanges)
|
||||
self.lr = lr
|
||||
|
||||
self.lr_at_sales=at_sales
|
||||
self.max_price_adj_rate = max_price_adj_rate
|
||||
self.price_believe = {i: 1 for i in range(len(self.exchanges))}
|
||||
self.lr_moment={i: 0 for i in range(len(self.exchanges))}
|
||||
self.open_orders = {i: [] for i in range(len(self.exchanges))}
|
||||
self.open_qty = 0
|
||||
self.beta=0.6
|
||||
self.lp_threshold=0.05
|
||||
self.hp_threshold=0.80
|
||||
|
||||
def tick(self, step, episode):
|
||||
|
||||
order_error = self.target_error()
|
||||
if order_error > 0:
|
||||
if (order_error > 0) & (self.open_qty==0):
|
||||
# aquire based on current price belive
|
||||
cx_id = self.select_best_cx()
|
||||
|
||||
@@ -77,16 +80,17 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
if o.leaves_qty == 0:
|
||||
# order is done
|
||||
self.open_orders[cx_id].remove(i) # remove order from open
|
||||
self.update_trades()
|
||||
succsess=self.calc_order_success(cx,o)
|
||||
modifier=0
|
||||
if succsess>=self.hp_threshold:
|
||||
modifier=1
|
||||
elif succsess<=self.lp_threshold:
|
||||
modifier=-1
|
||||
self.update_believe(cx_id,o.price, succsess) # update price believe
|
||||
self.update_believe(cx_id,o.price, succsess,o.qty) # update price believe
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_trades()
|
||||
|
||||
continue
|
||||
|
||||
if not (i["leaves"] == leaves):
|
||||
@@ -107,7 +111,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_believe(cx_id,o.price, succsess)
|
||||
self.update_believe(cx_id,o.price, succsess,o.qty)
|
||||
self.open_orders[cx_id].remove(i)
|
||||
def calc_order_success(self,cx, o):
|
||||
"""
|
||||
@@ -121,15 +125,19 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
score=sold-nsold
|
||||
return score
|
||||
|
||||
def update_believe(self, cx_id, used_price, modifier):
|
||||
def update_believe(self, cx_id, used_price, modifier,qty):
|
||||
"""
|
||||
Updates the believe based on the modifier.
|
||||
If positive will add lr to believe
|
||||
If negative will sub lr to believe
|
||||
"""
|
||||
clipMod=max(-self.lr_at_sales,min(modifier,self.lr_at_sales))
|
||||
clipMod=max(-qty,min(modifier,qty))
|
||||
moment=clipMod/qty
|
||||
#clip mod so that price believe more smooth
|
||||
self.price_believe[cx_id]+=(clipMod/self.lr_at_sales)*self.lr
|
||||
|
||||
self.lr_moment[cx_id]=self.lr_moment[cx_id]*self.beta + (1-self.beta)*moment
|
||||
update=self.lr_moment[cx_id]*self.lr
|
||||
self.price_believe[cx_id]+=update
|
||||
|
||||
self.price_believe[cx_id] = round(self.price_believe[cx_id], 2)
|
||||
if self.price_believe[cx_id] < 1:
|
||||
|
||||
Reference in New Issue
Block a user