Loan#

API documentation for tradeexecutor.state.loan.Loan Python class in Trading Strategy framework.

class Loan[source]#

Bases: object

Borrowed out assets.

See also

__init__(pair, collateral, collateral_interest, borrowed=None, borrowed_interest=None)#
Parameters:
Return type:

None

Methods

__init__(pair, collateral, collateral_interest)

calculate_collateral_for_target_leverage(...)

Calculate the collateral amount we need to hit a target leverage.

calculate_collateral_for_target_ltv(...)

Calculate the collateral amount we need to hit a target LTV.

calculate_size_adjust(collater_adjust[, ...])

Calculate the collateral amount we need to hit a target leverage.

check_health([desired_health_factor])

Check if this loan is healthy.

claim_interest([quantity])

Claim intrest from this position.

clone()

Clone this data structure for mutating.

from_dict(kvs, *[, infer_missing])

from_json(s, *[, parse_float, parse_int, ...])

get_borrow_interest()

How much interest we have paid on borrows.

get_borrow_value([include_interest])

Get the outstanding debt amount.

get_borrowed_principal_and_interest_quantity()

Get how much borrow there is left to repay.

get_borrowed_quantity()

Get abs number of vtokens we have.

get_collateral_interest()

How much interest we have received on collateral.

get_collateral_quantity()

Get abs number of atokens we have.

get_collateral_value([include_interest])

How much value the collateral for this loan has.

get_free_margin()

get_health_factor()

Get loan health factor.

get_leverage()

How leveraged this loan is.

get_loan_to_value()

Get LTV of this loan.

get_max_size()

get_net_asset_value([include_interest])

What's the withdrawable amount of the position is closed.

get_net_interest()

How many dollars of interest we have accumulated.

get_tracked_asset(asset)

Get one side of the loan.

repay_interest([quantity])

Repay interest for this position.

schema(*[, infer_missing, only, exclude, ...])

to_dict([encode_json])

to_json(*[, skipkeys, ensure_ascii, ...])

Attributes

borrowed

What collateral we used for this loan

borrowed_interest

Tracker for borrowed asset interest events

pair

Our trading pair data for this position

collateral

What collateral we used for this loan

collateral_interest

Tracker for collateral interest events

pair: TradingPairIdentifier#

Our trading pair data for this position

collateral: AssetWithTrackedValue#

What collateral we used for this loan

This is aToken for Aave

collateral_interest: Interest#

Tracker for collateral interest events

borrowed: tradeexecutor.state.identifier.AssetWithTrackedValue | None = None#

What collateral we used for this loan

This is vToken for Aave.

Not set if the loan is only for credit supply position.

borrowed_interest: tradeexecutor.state.interest.Interest | None = None#

Tracker for borrowed asset interest events

clone()[source]#

Clone this data structure for mutating.

Used when increasing/reducing shorts, as we copy the existing on-chain data and then apply our delta in tradeexecutor.state.position.TradingPosition.open_trade() on the top of this.

Return type:

Loan

get_tracked_asset(asset)[source]#

Get one side of the loan.

Parameters:

asset (AssetIdentifier) – Asset this loan is tracking

Returns:

Colleteral tracker, borrowed tracked or None if this loan does not track an asset.

Return type:

Tuple[tradeexecutor.state.loan.LoanSide | None, tradeexecutor.state.identifier.AssetWithTrackedValue | None]

get_collateral_interest()[source]#

How much interest we have received on collateral.

Return type:

float

get_collateral_value(include_interest=True)[source]#

How much value the collateral for this loan has.

Warning

TODO: Does not account for repaid interest at this point. Please use TradingPosition functions to get amounts with repaid interest.

Return type:

float

get_collateral_quantity()[source]#

Get abs number of atokens we have.

Return type:

Decimal

get_borrowed_quantity()[source]#

Get abs number of vtokens we have.

Return type:

Decimal

get_borrow_value(include_interest=True)[source]#

Get the outstanding debt amount.

Parameters:

include_interest – With interest.

Return type:

float

get_borrow_interest()[source]#

How much interest we have paid on borrows.

Returns:

Always positive

Return type:

float

get_borrowed_principal_and_interest_quantity()[source]#

Get how much borrow there is left to repay.

  • Round to zero

Return type:

Decimal

get_net_interest()[source]#

How many dollars of interest we have accumulated.

  • We gain money on collateral

  • We lost money by maintaining borrow

Return type:

float

get_net_asset_value(include_interest=True)[source]#

What’s the withdrawable amount of the position is closed.

Warning

For closed position the behavior here is a bit weird.

The value reflects any reminder of interest that was paid off when the position was closed. It is negative if borrowing costed more than collateral interest gained.

This is later fixed in TradingPosition.get_claimed_interest() and the position accounts the difference in the final trade that pays principal + interest back.

Returns:

The current net asset value or remaining interest that was paid off when the position was closed.

Return type:

float

get_leverage()[source]#

How leveraged this loan is.

Using formula (collateral / (collateral - borrow)).

Returns:

Zero if the loan has zero net asset value.

Return type:

float

get_health_factor()[source]#

Get loan health factor.

Safety of your deposited collateral against the borrowed assets and its underlying value.

If the health factor goes below 1, the liquidation of your collateral might be triggered.

Return type:

float

get_loan_to_value()[source]#

Get LTV of this loan.

LTV should stay below the liquidation threshold. For Aave ETH the liquidation threshold is 80%.

claim_interest(quantity=None)[source]#

Claim intrest from this position.

Interest should be moved to reserves.

Parameters:

quantity (decimal.Decimal | None) –

How many reserve tokens worth to claim.

If not given claim all accrued interest.

Returns:

Claimed interest in reserve tokens

Return type:

Decimal

repay_interest(quantity=None)[source]#

Repay interest for this position.

Pay any open interest on vToken position.

Parameters:

quantity (decimal.Decimal | None) –

How many vTokens worth of interest we pay.

If not given assume any remaining open interest.

Returns:

Repaid interest.

Return type:

Decimal

calculate_collateral_for_target_ltv(target_ltv, borrowed_quantity)[source]#

Calculate the collateral amount we need to hit a target LTV.

Assuming our debt stays the same, how much collateral we need to hit the target LTV.

Note

Watch out for rounding/epsilon errors.

Parameters:
Returns:

US dollars worth of collateral needed

Return type:

Decimal

calculate_collateral_for_target_leverage(leverage, borrowed_quantity)[source]#

Calculate the collateral amount we need to hit a target leverage.

Assuming our debt stays the same, how much collateral we need to hit the target LTV.

nav = col - borrow
leverage = borrow / nav
leverage = col / nav - 1

borrow = nav * leverage
col = borrow + nav
col = borrow + borrow / leverage
col = borrow * (1 + 1 / leverage)

See also calculate_leverage_for_target_size()

Parameters:
Returns:

US dollars worth of collateral needed

Return type:

Decimal

calculate_size_adjust(collater_adjust, borrowed_asset_price=None, leverage=None)[source]#

Calculate the collateral amount we need to hit a target leverage.

Assume collateral_adjust amount of collateral is deposited/withdrawn. Calculate the amount of borrowed token we need to trade to

Parameters:
  • collateral_adjust – Are we adding or removing collateral.

  • borrowed_asset_price (float) –

    The currentprice of the borrowed asset.

    If not given use the cached value.

  • leverage (float) –

    The target leverage level.

    If not given use the existing leverage.

  • collater_adjust (Decimal) –

Returns:

Positive to buy more borrowed token, negative to sell more.

Return type:

Decimal

check_health(desired_health_factor=1)[source]#

Check if this loan is healthy.

Health factor must stay above 1 or you get liquidated.

Raises:

LiquidationRisked – If the loan would be instantly liquidated

__init__(pair, collateral, collateral_interest, borrowed=None, borrowed_interest=None)#
Parameters:
Return type:

None