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, realised_interest=Decimal('0'))#
- Parameters:
pair (TradingPairIdentifier) –
collateral (AssetWithTrackedValue) –
collateral_interest (Interest) –
borrowed (Optional[AssetWithTrackedValue]) –
realised_interest (Decimal) –
- 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.
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.
How much interest we have received on collateral.
get_collateral_value
([include_interest])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.
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
How much accrued interest we have moved to the cast reserves from this position.
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
- realised_interest: Decimal = Decimal('0')#
How much accrued interest we have moved to the cast reserves from this position.
- 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_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.
- Return type:
- get_leverage()[source]#
How leveraged this loan is.
Using formula
(collateral / (collateral - borrow))
.- 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.
- 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.
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:
borrowed_quantity (decimal.Decimal | float) – What is expected outstanding loan amount
leverage (float) –
- Returns:
US dollars worth of collateral needed
- 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, realised_interest=Decimal('0'))#
- Parameters:
pair (TradingPairIdentifier) –
collateral (AssetWithTrackedValue) –
collateral_interest (Interest) –
borrowed (Optional[AssetWithTrackedValue]) –
realised_interest (Decimal) –
- Return type:
None