Agent Classes
Simulator to the code section the Agent Classes section allows you
to write code for certain agent classes. The agent class itself can be anything
you would like for your simulation, most AMM will have 3 different agents (but
this is just an example, you can identify more and integrate them easily). These
3 agents are lp
, arb
and trader
which stand for Liquidity Provider,
Arbitrager and Trader respectively.
Agent classes have to be defined in this section where each of the agent class
will allow you to specify an agent specific initialization
, preStep
,
featureVector
, step
, postStep
, and teardown
. These code classes will
overwrite the default code block (to see how that works look at the
code section page.
Optionally, a models
list may be defined. This is a list of strings whose
values are equal to a model alias defined in the configuration. These models
will be run for each agent step for agents of the given class. Specific agent
instances may override this in their own configurations.
This has been made because you probably want a Trader to perform a trade during it's step but the Liquidity Provider to provide liquidity. These are different operations which you will have to code here. The code blocks are all optional.
To get to know more about how to write the code blocks please refer to the instructions for each of the codeblocks here:
Format
name | type |
---|---|
type | array of { class: string, models?: array of strings, preStep?: string, featureVector?: string, step?: string, postStep?: string } |
required | true |
Example
agentClasses:
- class: "lp"
models: ["bancor_lp_action", "bancor_lp_volume"]
agentInitialization:
code: |
def custom_agent_class_initialization(agent, environment, metric_collector):
agent.set_variable('withdrawal_request_ids_by_token', collections.defaultdict(list))
custom_agent_class_initialization(a, b, metric_collector)
agentPreStep:
code: |
def custom_agent_class_pre_step(agent, environment, metric_collector):
# A set of individual token names (e.g., {"ETH", "DAI", "LINK"})
current_tokens = set(agent.get_wallet().get_token_names())
# A list of TokenPair objects (e.g., [TokenPair("ETH", "USDT"), TokenPair("USDT", "DAI")])
simulated_tokens = list(environment.get_current_prices().keys())
step_list = []
for token in simulated_tokens:
if (token.get_a() in current_tokens or token.get_b() in current_tokens) and \
token.upper() != 'BNTUSDT':
step = AgentStep()
step.token = token
step_list.append(step)
agent.set_step_list(step_list)
custom_agent_class_pre_step(a, b, metric_collector)
agentFeatureVector:
code: |
def agent_feature_vector(agent, step, model_alias, environment, metric_collector):
token = step.token
non_usdt_token = token.get_a() if token.get_a() != 'USDT' else token.get_b()
coins_abr = {
'BNT': 0,
'LINK': 1,
'ETH': 2,
'DAI': 3,
'WBTC': 4
}
# same as for traders, calculate asset specific features
vol = environment.get_state().token_states[token].volatility
change = environment.get_state().token_states[token].price_change
# calculate asset share
share = agent.get_wallet().get_token_share(non_usdt_token)
return np.array([coins_abr[non_usdt_token], change, vol, share])
agentStep:
code: |
def custom_agent_class_step(agent, step, environment, metric_collector):
try:
model_output = step.get_model_output()
action = model_output['bancor_lp_action']['prediction'][0]
quantity = model_output['bancor_lp_volume']['prediction'][0][0]
token = step.token
non_usdt_token = token.get_a() if token.get_a() != 'USDT' else token.get_b()
token_address = get_token_address(environment, non_usdt_token)
quantity = int(quantity)
if action == 1:
agent.set_latest_action("deposit")
agent.get_client().bancor_network.deposit(token_address, quantity).transact(generate_tx_details(agent.get_address()))
agent.add_affected_token(token)
elif action == 2:
pending_withdrawals = agent.get_variable('withdrawal_request_ids_by_token')[non_usdt_token]
if len(pending_withdrawals) > 0:
agent.set_latest_action("withdrawal")
environment.get_client().bancor_network.withdraw(pending_withdrawals[0]).transact({'from': agent.get_address()})
else:
agent.set_latest_action("cooldown")
withdrawal_request_id = environment.get_client().bancor_network.initWithdrawal(
token_address, quantity).transact({'from': agent.get_address()})
agent.get_variable('withdrawal_request_ids_by_token')[non_usdt_token].append(withdrawal_request_id)
else:
agent.set_latest_action('nothing')
except Exception as e:
agent.set_latest_action('nothing')
custom_agent_class_step(a, b, c, metric_collector)
agentPostStep:
code: |
def custom_agent_class_post_step(agent, environment, metric_collector):
pass
custom_agent_class_post_step(a, b, metric_collector)