Loan#
API documentation for tradeexecutor.state.loan.Loan Python class in Trading Strategy framework.
- class Loan[source]#
Bases:
object
Borrowed out assets.
See also
tradeexcutor.state.position.TradingPosition
for tracking a long/shorttradeexcutor.state.interest.Interest
for position interest calculations
- __init__(pair, collateral, collateral_interest, borrowed=None, borrowed_interest=None)#
- Parameters:
pair (TradingPairIdentifier) –
collateral (AssetWithTrackedValue) –
collateral_interest (Interest) –
borrowed (tradeexecutor.state.identifier.AssetWithTrackedValue | None) –
borrowed_interest (tradeexecutor.state.interest.Interest | None) –
- Return type:
None
Methods
__init__
(pair, collateral, collateral_interest)Calculate the collateral amount we need to hit a target leverage.
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, ...])How much interest we have paid on borrows.
get_borrow_value
([include_interest])Get the outstanding debt amount.
Get how much borrow there is left to repay.
Get abs number of vtokens we have.
How much interest we have received on collateral.
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 loan health factor.
How leveraged this loan is.
Get LTV of this loan.
get_max_size
()get_net_asset_value
([include_interest])What's the withdrawable amount of the position is closed.
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
What collateral we used for this loan
Tracker for borrowed asset interest events
Our trading pair data for this position
What collateral we used for this loan
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
- 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:
- 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_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:
- get_borrow_value(include_interest=True)[source]#
Get the outstanding debt amount.
- Parameters:
include_interest – With interest.
- Return type:
- get_borrow_interest()[source]#
How much interest we have paid on borrows.
- Returns:
Always positive
- Return type:
- get_borrowed_principal_and_interest_quantity()[source]#
Get how much borrow there is left to repay.
Round to zero
- Return type:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
borrowed_quantity (decimal.Decimal | float) – What is expected outstanding loan amount
target_ltv (float) –
- Returns:
US dollars worth of collateral needed
- Return type:
- 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:
borrowed_quantity (decimal.Decimal | float) – What is expected outstanding loan amount
leverage (float) –
- Returns:
US dollars worth of collateral needed
- Return type:
- 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:
- Returns:
Positive to buy more borrowed token, negative to sell more.
- Return type:
- 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:
pair (TradingPairIdentifier) –
collateral (AssetWithTrackedValue) –
collateral_interest (Interest) –
borrowed (tradeexecutor.state.identifier.AssetWithTrackedValue | None) –
borrowed_interest (tradeexecutor.state.interest.Interest | None) –
- Return type:
None