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

[Lease][Close] Stop Loss and Take Profit Triggers #381

Open
5 tasks
gmanev7 opened this issue Aug 21, 2024 · 1 comment
Open
5 tasks

[Lease][Close] Stop Loss and Take Profit Triggers #381

gmanev7 opened this issue Aug 21, 2024 · 1 comment
Assignees

Comments

@gmanev7
Copy link
Member

gmanev7 commented Aug 21, 2024

The lease owner can set Stop Loss, SL, or/and TakeProfit, TP, triggers after the lease has been fully opened. They may be set up and removed individually, or together until the lease is fully paid, fully closed, or fully liquidated.

Partial closes are defined in percent of the total position. It is not an absolute value, because it might change over time, for example past partial close or liquidations, partial TPs, etc.

The trigger of SL and TP is defined as LTV percent such as Liquidation% > SL > current LTV > TP. Note that the Liquidation% is set on the Lease Open time and remains intact over the Lease Lifetime.

Correspondingly, Partial or Full Close occurs if

  • Partial or Full SL is set and SL% <= lease LTV%
  • Partial or Full TP is set and lease LTV% <= TP% .

If a liquidation occurs despite having a SL set, then the higher amount of liquidation and the stop-loss should be closed to guarantee the position's healthiness.

Once a trigger is fired it is consumed and no longer valid.

It's worth noting that:

  • if TP > healthy LTV and a partial liquidation occurs a TP would be triggered, and
  • the TP, if set, might be triggered right after payment due to lowered LTV
    Both would be communicated with the customers, the former on setting TP, and the latter on payment.

Design
Define struct ClosePolicy and keep an instance in lease::position::spec::Spec next to liability.
Introduce enum Close<Asset> with two variants Full and Partial, similar to lease::position::Liquidation<Asset>, and fn Position::trigger(&Due, Price<Asset>) -> Option<Close<Asset>> .
Two options to continue, either
(a) Rename appropriately, and amend fn Lease::check_debt to accommodate the trigger result handling, optionally starting a position close. Add a new variant DebtStatus::FireClose(Close<asset>) (how are the alarms scheduled?) , or
(b) add a sibling method to fn Lease::check_debt that checks for a position close. Merge the liability and trigger zones to proceed with setting up alarms.

Tasks

  • implement the core logic and unit tests as per the design described above
  • add a Lease execute message to setup triggers
  • consider "smart" or no migration of the existing leases to add the new properties or ClosePolicy to the Position Spec, e.g. "#serde[default]"
  • add events that leave enough trace in the chain for audibility
  • add integration tests
@gmanev7
Copy link
Member Author

gmanev7 commented Aug 21, 2024

@kostovster selected Medium size though my confidence is not higher than 50%, so would say it would take somewhat in between Medium and Large Tasks, i.e. 3-4w

@gmanev7 gmanev7 self-assigned this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant