Policies

The agent follows "policies" to make decisions.

The policy defines the mapping from the environment-observations to the actions that are being taken.

Within Dojo, the policy can be referred to as the agent's DeFi strategy. This is where you can get creative by implementing your own strategy!


Purpose

The policy predict(obs) method is run to generate a sequence of actions to be run in the block. You can think of the policy as the brain of the agent, where it decides what to do based on conditions defined by your trading strategy.


Policy Implementation

To test out your own DeFi policy, all you need to do is implement the predict(obs) method.
It takes the observation object from the environment as input and returns a list of actions as output.

In this example, we consider a basic policy for trading on UniswapV3, where we define an upper and lower spot-price and once the price reaches the upper or lower limit, all tokens are converted to the currently lower value token:

policy.py
class PriceWindowPolicy(UniswapV3Policy):
  """Policy for Moving Average Price Window strategy."""
 
  def __init__(
      self, agent: UniswapV3Agent, lower_limit: float, upper_limit: float
  ) -> None:  # noqa: D107
      super().__init__(agent=agent)
      self.upper_limit = upper_limit
      self.lower_limit = lower_limit
 
  # derive actions from observations
  def predict(self, obs: UniswapV3Observation) -> list[BaseUniswapV3Action]:
      """Derive actions from observations."""
      pool = obs.pools[0]
      x_token, y_token = obs.pool_tokens(pool)
      spot_price = obs.price(token=x_token, unit=y_token, pool=pool)
 
      x_quantity, y_quantity = self.agent.quantity(x_token), self.agent.quantity(
          y_token
      )
 
      if spot_price > self.upper_limit and y_quantity > Decimal("0"):
          action = UniswapV3Trade(
              agent=self.agent,
              pool=pool,
              quantities=(Decimal(0), y_quantity),
          )
          return [action]
 
      if spot_price < self.lower_limit and x_quantity > Decimal("0"):
          action = UniswapV3Trade(
              agent=self.agent,
              pool=pool,
              quantities=(x_quantity, Decimal(0)),
          )
          return [action]
 
      return []
 

These are just example policies to get you started. You can get a lot more creative and sophisticated than this.