i think it is working
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user