-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds `examples/short_examples.ipynb` that runs through quick examples on using agent0 to: - Analyze positions - Close all mature positions - Write a quick policy This involves the following changes: - Agents now can be passed in a public address instead of a private address for analysis functions. - Adding a `get_hyperdrive_pools_from_registry` that returns a list of `Hyperdrive` objects given a registry address. - `agent.get_positions` now allows a list of `pool_filters`.
- Loading branch information
Sheng Lundquist
authored
May 31, 2024
1 parent
8f11205
commit a3cbc79
Showing
8 changed files
with
347 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from dotenv import load_dotenv\n", | ||
"\n", | ||
"load_dotenv(\".env\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Query all your positions\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from agent0 import Chain, Hyperdrive\n", | ||
"import os\n", | ||
"\n", | ||
"# PUBLIC_ADDRESS = \"0xPUBLIC_ADDRESS\"\n", | ||
"# RPC_URI = \"https://sepolia.rpc.url\"\n", | ||
"PUBLIC_ADDRESS = os.getenv(\"PUBLIC_ADDRESS\")\n", | ||
"RPC_URI = os.getenv(\"RPC_URI\")\n", | ||
"\n", | ||
"# Address of Hyperdrive Sepolia registry\n", | ||
"REGISTRY_ADDRESS = \"0x4ba58147e50e57e71177cfedb1fac0303f216104\"\n", | ||
"\n", | ||
"## View open and closed positions in all pools\n", | ||
"chain = Chain(RPC_URI)\n", | ||
"agent = chain.init_agent(public_address=PUBLIC_ADDRESS)\n", | ||
"registered_pools = Hyperdrive.get_hyperdrive_pools_from_registry(\n", | ||
" chain,\n", | ||
" registry_address=REGISTRY_ADDRESS,\n", | ||
")\n", | ||
"agent.get_positions(pool_filter=registered_pools, show_closed_positions=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Automate withdrawing funds from matured positions\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# PRIVATE_KEY = \"0xPRIVATE_KEY\"\n", | ||
"PRIVATE_KEY = os.getenv(\"PRIVATE_KEY\")\n", | ||
"\n", | ||
"# Initialize agent with private key for transactions\n", | ||
"agent = chain.init_agent(private_key=PRIVATE_KEY)\n", | ||
"for pool in registered_pools:\n", | ||
" # Close all mature longs\n", | ||
" for long in agent.get_longs(pool=pool):\n", | ||
" if long.maturity_time <= chain.block_time():\n", | ||
" print(f\"Close long-{long.maturity_time} on {pool.name}\")\n", | ||
" agent.close_long(maturity_time=long.maturity_time, bonds=long.balance, pool=pool)\n", | ||
" # Close all mature shorts\n", | ||
" for short in agent.get_shorts(pool=pool):\n", | ||
" if short.maturity_time <= chain.block_time():\n", | ||
" print(f\"Close short-{short.maturity_time} on {pool.name}\")\n", | ||
" agent.close_short(maturity_time=short.maturity_time, bonds=short.balance, pool=pool)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Write policies in Python\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from agent0 import HyperdriveBasePolicy, open_long_trade\n", | ||
"from fixedpointmath import FixedPoint\n", | ||
"from dataclasses import dataclass" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class OpenLongPolicy(HyperdriveBasePolicy):\n", | ||
" @dataclass(kw_only=True)\n", | ||
" class Config(HyperdriveBasePolicy.Config):\n", | ||
" fixed_rate_threshold: FixedPoint\n", | ||
" open_long_amount: FixedPoint\n", | ||
"\n", | ||
" def action(self, interface, wallet):\n", | ||
" \"\"\"Get agent actions for teh current block\n", | ||
"\n", | ||
" Action fn returns the trades to be executed\n", | ||
" at a given moment in time.\n", | ||
" \"\"\"\n", | ||
" done_trading = False # Never done trading\n", | ||
"\n", | ||
" # If no longs in wallet, we check our fixed rate\n", | ||
" # threshold and open the long if threshold reached.\n", | ||
" if len(wallet.longs) == 0:\n", | ||
" if interface.calc_spot_rate() > self.config.fixed_rate_threshold:\n", | ||
" return [open_long_trade(self.config.open_long_amount)], done_trading\n", | ||
"\n", | ||
" # We don't do any trades otherwise\n", | ||
" return [], done_trading" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"agent.set_active(\n", | ||
" policy=OpenLongPolicy,\n", | ||
" policy_config=OpenLongPolicy.Config(\n", | ||
" fixed_rate_threshold=FixedPoint(0.06),\n", | ||
" open_long_amount=FixedPoint(\"100_000\"),\n", | ||
" ),\n", | ||
")\n", | ||
"agent.execute_policy_action(pool=registered_pools[0])" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.11" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.