I have a mesa agent:
import mesa
from typing import Type, Dict, Any
from abc import ABC, abstractmethod
class Position: pass # Stub for future implementation
class Response: pass # Stub for margin responses
class _TraderSpec:
def init(self, trader_cls: Type[mesa.Agent], count: int=1, *args: Any, **kwargs: Any):
self.cls = trader_cls
self.count = count
self.args = args
self.kwargs = kwargs
class Trader(mesa.Agent, ABC):
def init(self, model: Type[mesa.Model], wealth: int, *args: Any, **kwargs: Any):
super().init(model, *args, **kwargs)
self.wealth = wealth
self.margin_accounts: Dict[int, float] = {}
self.contracts = set()
@classmethod
def add_spec(cls, count: int=1, *args: Any, **kwargs: Any):
return _TraderSpec(cls, count, *args, **kwargs)
@abstractmethod
def trade_position(self, *args, **kwargs) -> Position:
pass
@abstractmethod
def handle_margin_update(self, *args, **kwargs) -> Response:
pass
The exchange:
import mesa
from .agents.infrastructure import ClearingHouse, ClearingMember, Broker
from typing import Sequence, Iterable
from .agents.traders.base import _TraderSpec
class FuturesExchange(mesa.Model):
"""
A class representing a simulated futures exchange.
Attributes:
tick (int): The current tick of the simulation.
agents (list): A list of agents participating in the exchange.
contracts (list): A list of contracts available in the exchange.
"""
def __init__(self, traders_specs: Iterable[_TraderSpec], contracts: Sequence,
n_clearing_members: int=1, n_brokers: int=1):
"""Initializes the FuturesExchange with a given name.
Args:
agents (Dict[Type[mesa.Agent], int]): _description_
contracts (Sequence): _description_
"""
super().__init__()
ClearingHouse.create_agents(model=self, n=1)
ClearingMember.create_agents(model=self, n=n_clearing_members)
Broker.create_agents(model=self, n=n_brokers)
for trader in traders_specs:
trader.cls.create_agents(model=self, n=trader.count, *trader.args, **trader.kwargs)
self.contracts = contracts
The code:
from futures_sim.agents import Trader
from futures_sim import FuturesExchange
class TrendFollower(Trader):
def trade_position(self, prices):
if len(prices) < 2:
return 0
pos = 2*(prices[-1] > prices[-2]) - 1 # indicator of price growth
return min(pos, self.wealth)
def handle_margin_update(self):
return 0
exchange = FuturesExchange([TrendFollower.add_spec(10, wealth=0)], 0)
exchange._agents
The output:
{<futures_sim.agents.infrastructure.clearing_house.ClearingHouse at 0x134dd2890>: None,
<futures_sim.agents.infrastructure.clearing_member.ClearingMember at 0x116c8e950>: None,
<futures_sim.agents.infrastructure.broker.Broker at 0x135aeead0>: None,
<main.TrendFollower at 0x1042d3f50>: None,
<main.TrendFollower at 0x127a62290>: None,
<main.TrendFollower at 0x1042d2f10>: None,
<main.TrendFollower at 0x1042d31d0>: None,
<main.TrendFollower at 0x1042d3dd0>: None,
<main.TrendFollower at 0x1042d32d0>: None,
<main.TrendFollower at 0x1042d2c90>: None,
<main.TrendFollower at 0x1042d9810>: None,
<main.TrendFollower at 0x1042d8d50>: None,
<main.TrendFollower at 0x1042d2710>: None}
Why are there None's in the dict? Is there supposed to be an index or something?