i think it is working

This commit is contained in:
2023-01-23 11:28:12 +01:00
parent 3a421ceb34
commit 89dc8ed54a
22 changed files with 540 additions and 28 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,12 +1,12 @@
from base_agent import BaseAgent
from .base_agent import BaseAgent
class AutoProductionAgent(BaseAgent):
"""
Automaticaly produces commodity if in business inventory
"""
def __init__(self,business) -> None:
super().__init__()
def __init__(self,sim,business) -> None:
super().__init__(sim)
self.business=business
self.prod=business.production
@@ -14,7 +14,7 @@ class AutoProductionAgent(BaseAgent):
def can_produce(self):
# If can produce item
for k,cost in self.prod.items():
for k,cost in self.prod["craft"].items():
if cost > self.business.inventory[k]:
return False
return True
@@ -23,7 +23,7 @@ class AutoProductionAgent(BaseAgent):
if not self.can_produce():
return
# remove cost from inventory
for k,cost in self.prod.items():
for k,cost in self.prod["craft"].items():
self.business.inventory[k]-=cost
# add commodity
self.business.inventory[self.prod['name']]+=self.prod["amount"]

View File

@@ -1,5 +1,17 @@
class BaseAgent():
def tick():
import uuid
from abc import ABC
class BaseAgent(ABC):
_bal="balance"
def __init__(self,simulation) -> None:
"""
Base class of an agent.
Simulation: to register tick method at.
"""
self.id=uuid.uuid4()
self.simulation=simulation
simulation.register_tick(self.id,self.tick)
pass
def tick(self):
"""
Tick method for simulation to call to execute selected action
"""

View File

@@ -0,0 +1,98 @@
from .base_agent import BaseAgent
from ..exchange import Exchange
from lightmatchingengine.lightmatchingengine import Order,Side
from abc import ABC
class Base_Aquire_Agent(BaseAgent,ABC):
def __init__(self,simulation,business,resource,exchanges: list) -> None:
"""
Agent for aquire of resources.
business: Business to aquire resources for.
resource: Resource to aquire.
exchanges: list of exchanges to aquire resources from
"""
self.business=business
self.resource=resource
self.exchanges=exchanges
self.orders={i: {} for i in range(len(self.exchanges))}
self.target=0
self.max_price=-1
super().__init__(simulation)
def set_target(self,target: int):
"""
Sets the amount of resources the agent should aquire.
Checks against current inventory of business.
"""
self.target=target
def target_error(self):
"""
Returns the difference between target and current business inventory.
If err >0 the business is in surplus
If err < 0 then agent needs to aquire resources
"""
err=self.business.inventory[self.resource]-self.target
return err
def set_price_max(self,price: int):
"""
Sets the min price the agent should distribute the resources for.
Set to -1 to disable minimum
"""
self.max_price=price
def order_resource(self,price_per, amount,cx_id):
"""
Aquire resource from selected exchange.
Return order if order has been placed else None.
"""
# Get exchange
cx=self.exchanges[cx_id]
# Calculate order price
total_price=price_per*amount
if not self.business.balance>=total_price:
return None # we dont have enough balance
self.business.balance-=total_price
cx.add_to_account(self.id,"balance",total_price) # prepaid charge account for cx
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.BUY)
if order==None: # Order failed
return False
self.orders[cx_id][order.order_id]=order
return order
def collect_balance_from_cxs(self):
"""
Collects resouces from account inventory in cxs.
"""
for cx in self.exchanges:
amount=cx.get_account_resource_amount(self.id,"balance")
cx.remove_from_account(self.id,"balance",amount)
self.business.balance+=amount
def collect_resource_from_cxs(self,resource):
"""
Collects resouces from account inventory in cxs.
"""
for cx in self.exchanges:
amount=cx.get_account_resource_amount(self.id,resource)
cx.remove_from_account(self.id,resource,amount)
self.business.inventory[resource]+=amount
def update_trades(self) -> list:
"""
Returns a list of all trades performed by this agent
"""
trades=[]
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
orders=self.orders[cx_id]
if len(orders)>0:
for o in orders:
trades.append(cx.order_trades_map[o.order_id])
self.trades=trades
return trades

View File

@@ -0,0 +1,97 @@
from .base_agent import BaseAgent
from ..exchange import Exchange
from lightmatchingengine.lightmatchingengine import Order,Side
from abc import ABC
class Base_Distribution_Agent(BaseAgent,ABC):
def __init__(self,simulation,business,resource,exchanges: list) -> None:
"""
Agent for distribute of resources.
business: Business to distribute resources for.
resource: Resource to distribute.
exchanges: list of exchanges to distribute resources to.
"""
self.business=business
self.resource=resource
self.exchanges=exchanges
self.orders={i: {} for i in range(len(self.exchanges))}
self.target=0
self.min_price=-1
super().__init__(simulation)
def set_target(self,target: int):
"""
Sets the amount of resources the agent should keep in business inventory
"""
self.target=target
def set_price_min(self,price: int):
"""
Sets the min price the agent should distribute the resources for.
Set to -1 to disable minimum.
"""
self.min_price=price
def target_error(self):
"""
Returns the difference between target and current business inventory.
If err >0 the business is in surplus
If err < 0 then agent needs to distribute resources
"""
err=self.business.inventory[self.resource]-self.target
return err
def distribute_resource(self,price_per,amount,cx_id):
"""
distribute resource from selected exchange.
Return true if order has been placed.
"""
# Get exchange
cx=self.exchanges[cx_id]
if not self.business.inventory[self.resource]>=amount:
return False # we dont have enough balance
self.business.inventory[self.resource]-=amount
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)
if order==None: # Order failed
return False
self.orders[cx_id][order.order_id]=order
return order
def collect_balance_from_cxs(self):
"""
Collects resouces from account inventory in cxs.
"""
for cx in self.exchanges:
amount=cx.get_account_resource_amount(self.id,"balance")
cx.remove_from_account(self.id,"balance",amount)
self.business.balance+=amount
def collect_resource_from_cxs(self,resource):
"""
Collects resouces from account inventory in cxs.
"""
for cx in self.exchanges:
amount=cx.get_account_resource_amount(self.id,resource)
cx.remove_from_account(self.id,resource,amount)
self.business.inventory[resource]+=amount
def update_trades(self) -> list:
"""
Returns a list of all trades performed by this agent
"""
trades=[]
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
orders=self.orders[cx_id]
if len(orders)>0:
for o in orders:
trades.append(cx.order_trades_map[o.order_id])
self.trades=trades
return trades

View File

@@ -0,0 +1,79 @@
from .base_aquire_agent import Base_Aquire_Agent
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=-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.max_price
order_error=self.open_qty+self.target_error()
if order_error<0:
# aquire based on current price belive
order=self.order_resource(self.price_believe,order_error*-1,0)
if not order==None:
self.register_order(0,order)
else:
# order failed .. we need to adjust our price believe
self.update_believe(-1)
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.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

View File

@@ -0,0 +1,75 @@
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