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, realised_interest=Decimal('0'))#
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.

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_collateral_interest()

How much interest we have received on collateral.

get_collateral_value([include_interest])

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.

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

realised_interest

How much accrued interest we have moved to the cast reserves from this position.

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

realised_interest: Decimal = Decimal('0')#

How much accrued interest we have moved to the cast reserves from this position.

clone()[source]#

Clone this data structure for mutating.

Return type:

Loan

get_collateral_interest()[source]#

How much interest we have received on collateral.

Return type:

float

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_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.

Return type:

float

get_leverage()[source]#

How leveraged this loan is.

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

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 (Optional[Decimal]) –

How many reserve tokens worth to claim.

If not given claim all accrued interest.

Returns:

Claimed interest in reserve tokens

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.

col / (col - borrow) = leverage
col = (col - borrow) * leverage
col = col * leverage - borrow * leverage
col - col * leverage = - borrow * levereage
col(1 - leverage) = - borrow * leverage
col = -(borrow * leverage) / (1 - leverage)

See also calculate_leverage_for_target_size()

Parameters:
Returns:

US dollars worth of collateral needed

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, realised_interest=Decimal('0'))#
Parameters:
Return type:

None