Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Trade type member variables to better indicate type #773

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions elfpy/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def get_trades(self, market: BaseMarket) -> list[Trade]:
"""
actions: list[Trade] = self.action(market) # get the action list from the policy
for action in actions: # edit each action in place
if action.market == MarketType.HYPERDRIVE and action.trade.mint_time is None:
action.trade.mint_time = market.latest_checkpoint_time
if action.trade.trade_amount <= 0:
if action.market_type == MarketType.HYPERDRIVE and action.market_action.mint_time is None:
action.market_action.mint_time = market.latest_checkpoint_time
if action.market_action.trade_amount <= 0:
raise ValueError("Trade amount cannot be zero or negative.")
return actions

Expand All @@ -113,8 +113,8 @@ def get_liquidation_trades(self, market: HyperdriveMarket) -> list[Trade]:
# TODO: Find a way to avoid converting type back and forth for dict keys
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_LONG,
trade_amount=long.balance,
wallet=self.wallet,
Expand All @@ -127,8 +127,8 @@ def get_liquidation_trades(self, market: HyperdriveMarket) -> list[Trade]:
if short.balance > FixedPoint(0):
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_SHORT,
trade_amount=short.balance,
wallet=self.wallet,
Expand All @@ -142,8 +142,8 @@ def get_liquidation_trades(self, market: HyperdriveMarket) -> list[Trade]:
)
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.REMOVE_LIQUIDITY,
trade_amount=self.wallet.lp_tokens,
wallet=self.wallet,
Expand Down
4 changes: 2 additions & 2 deletions elfpy/agents/policies/init_lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def action(self, market: BaseMarket, wallet: Wallet) -> list[Trade]:
return []
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.ADD_LIQUIDITY,
trade_amount=self.budget,
slippage_tolerance=self.slippage_tolerance,
Expand Down
8 changes: 4 additions & 4 deletions elfpy/agents/policies/lp_and_withdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if not has_lp and can_lp:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.ADD_LIQUIDITY,
trade_amount=self.amount_to_lp,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -67,8 +67,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if enough_time_has_passed:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.REMOVE_LIQUIDITY,
trade_amount=wallet.lp_tokens,
slippage_tolerance=self.slippage_tolerance,
Expand Down
28 changes: 14 additions & 14 deletions elfpy/agents/policies/random_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def open_short_with_random_amount(self, market: HyperdriveMarket, wallet: Wallet

return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_SHORT,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -95,8 +95,8 @@ def close_random_short(self, wallet: Wallet) -> list[Trade[HyperdriveMarketActio
trade_amount = wallet.shorts[short_time].balance # close the full trade
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_SHORT,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -122,8 +122,8 @@ def open_long_with_random_amount(
# return a trade using a specification that is parsable by the rest of the sim framework
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_LONG,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -139,8 +139,8 @@ def close_random_long(self, wallet: Wallet) -> list[Trade[HyperdriveMarketAction
trade_amount = wallet.longs[long_time].balance # close the full trade
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_LONG,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -161,8 +161,8 @@ def add_liquidity_with_random_amount(self, wallet: Wallet) -> list[Trade[Hyperdr
# return a trade using a specification that is parsable by the rest of the sim framework
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.ADD_LIQUIDITY,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -182,8 +182,8 @@ def remove_liquidity_with_random_amount(self, wallet: Wallet) -> list[Trade[Hype
# return a trade using a specification that is parsable by the rest of the sim framework
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.REMOVE_LIQUIDITY,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -210,8 +210,8 @@ def redeem_withdraw_shares_with_random_amount(
# return a trade using a specification that is parsable by the rest of the sim framework
return [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.REDEEM_WITHDRAW_SHARE,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand Down
8 changes: 4 additions & 4 deletions elfpy/agents/policies/single_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if enough_time_has_passed:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_LONG,
trade_amount=longs[-1].balance,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -48,8 +48,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
trade_amount = market.get_max_long_for_account(wallet.balance.amount)
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_LONG,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand Down
4 changes: 2 additions & 2 deletions elfpy/agents/policies/single_lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def action(self, market: BaseMarket, wallet: Wallet) -> list[Trade]:
if can_lp and not has_lp:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.ADD_LIQUIDITY,
trade_amount=self.amount_to_lp,
slippage_tolerance=self.slippage_tolerance,
Expand Down
4 changes: 2 additions & 2 deletions elfpy/agents/policies/single_short.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if can_open_short and not has_opened_short:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_SHORT,
trade_amount=self.amount_to_trade,
slippage_tolerance=self.slippage_tolerance,
Expand Down
8 changes: 4 additions & 4 deletions elfpy/agents/policies/smart_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
trade_amount = wallet.longs[long_time].balance # close the whole thing
action_list += [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_LONG,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand Down Expand Up @@ -116,8 +116,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if trade_amount > WEI and wallet.balance.amount > WEI:
action_list += [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_LONG,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand Down
8 changes: 4 additions & 4 deletions elfpy/agents/policies/smart_short.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
trade_amount = wallet.shorts[short_time].balance # close the whole thing
action_list += [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_SHORT,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand All @@ -97,8 +97,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if trade_amount > WEI and wallet.balance.amount > WEI:
action_list += [
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_SHORT,
trade_amount=trade_amount,
slippage_tolerance=self.slippage_tolerance,
Expand Down
4 changes: 2 additions & 2 deletions elfpy/simulators/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def execute_trades(self, agent_actions: list[tuple[int, types.Trade]]) -> None:
# and market action before sending the info off to the
# correct market. This way, for example, a trade can happen
# on the borrow market OR the hyperdrive market.
action_details = (trade[0], trade[1].trade)
action_details = (trade[0], trade[1].market_action)
market_state_before_trade = self.market.market_state.copy()
try:
agent_id, agent_deltas, market_deltas = self.market.perform_action(action_details)
Expand All @@ -230,7 +230,7 @@ def execute_trades(self, agent_actions: list[tuple[int, types.Trade]]) -> None:
self.trade_number,
float(self.market.fixed_apr),
float(self.market.spot_price),
trade[1].trade,
trade[1].market_action,
market_deltas,
agent_id,
agent_deltas,
Expand Down
8 changes: 4 additions & 4 deletions elfpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ def __neg__(self):
return Quantity(amount=-self.amount, unit=self.unit)


T = TypeVar("T")
MarketAction = TypeVar("MarketAction")


@dataclass
class Trade(Generic[T]):
class Trade(Generic[MarketAction]):
r"""A trade for a market"""

market: MarketType
trade: T # TODO: How to specify the type as a generic market action?
market_type: MarketType
market_action: MarketAction
2 changes: 1 addition & 1 deletion elfpy/utils/sim_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_simulator(config: SimulationConfig, agents: list[Agent] | None = None) -
trade_number=0,
fixed_apr=float(simulator.market.fixed_apr),
spot_price=float(simulator.market.spot_price),
trade_action=init_agent_action.trade, # type: ignore # pylint: disable=unexpected-keyword-arg
trade_action=init_agent_action.market_action, # type: ignore # pylint: disable=unexpected-keyword-arg
market_deltas=market_deltas,
agent_address=0,
agent_deltas=init_agent_deltas,
Expand Down
24 changes: 12 additions & 12 deletions examples/eth_bots/custom_policies/example_custom_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if wallet.lp_tokens > 0: # agent has liquidity
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.REMOVE_LIQUIDITY,
trade_amount=wallet.lp_tokens,
wallet=wallet,
Expand All @@ -74,8 +74,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
else: # remove all of the agent's liquidity
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.ADD_LIQUIDITY,
trade_amount=self.trade_amount,
wallet=wallet,
Expand All @@ -91,8 +91,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if market.block_time.time - mint_time >= market.position_duration.years:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_LONG,
trade_amount=longs[0].balance,
wallet=wallet,
Expand All @@ -103,8 +103,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
else:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_LONG,
trade_amount=self.trade_amount,
wallet=wallet,
Expand All @@ -120,8 +120,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
if market.block_time.time - mint_time >= market.position_duration.years:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.CLOSE_SHORT,
trade_amount=shorts[0].balance,
wallet=wallet,
Expand All @@ -132,8 +132,8 @@ def action(self, market: HyperdriveMarket, wallet: Wallet) -> list[Trade]:
else:
action_list.append(
Trade(
market=MarketType.HYPERDRIVE,
trade=HyperdriveMarketAction(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
action_type=MarketActionType.OPEN_SHORT,
trade_amount=self.trade_amount,
wallet=wallet,
Expand Down
Loading