|
4 | 4 | from pyinjective.async_client_v2 import AsyncClient |
5 | 5 | from pyinjective.core.network import Network |
6 | 6 |
|
7 | | -def adjusted_margin(quantity: Decimal, margin: Decimal, is_long: bool, cumulative_funding_entry: Decimal, cumulative_funding: Decimal) -> Decimal: |
| 7 | + |
| 8 | +def adjusted_margin( |
| 9 | + quantity: Decimal, margin: Decimal, is_long: bool, cumulative_funding_entry: Decimal, cumulative_funding: Decimal |
| 10 | +) -> Decimal: |
8 | 11 | unrealized_funding_payment = (cumulative_funding - cumulative_funding_entry) * quantity * (-1 if is_long else 1) |
9 | 12 | return margin + unrealized_funding_payment |
10 | 13 |
|
| 14 | + |
11 | 15 | async def main() -> None: |
12 | 16 | # select network: local, testnet, mainnet |
13 | 17 | network = Network.mainnet() |
@@ -38,23 +42,35 @@ async def main() -> None: |
38 | 42 | quantity = client_market._from_extended_chain_format(Decimal(position["position"]["quantity"])) |
39 | 43 | entry_price = client_market._from_extended_chain_format(Decimal(position["position"]["entryPrice"])) |
40 | 44 | margin = client_market._from_extended_chain_format(Decimal(position["position"]["margin"])) |
41 | | - cumulative_funding_entry = client_market._from_extended_chain_format(Decimal(position["position"]["cumulativeFundingEntry"])) |
42 | | - market_cumulative_funding = client_market._from_extended_chain_format(Decimal(market["perpetualInfo"]["fundingInfo"]["cumulativeFunding"])) |
| 45 | + cumulative_funding_entry = client_market._from_extended_chain_format( |
| 46 | + Decimal(position["position"]["cumulativeFundingEntry"]) |
| 47 | + ) |
| 48 | + market_cumulative_funding = client_market._from_extended_chain_format( |
| 49 | + Decimal(market["perpetualInfo"]["fundingInfo"]["cumulativeFunding"]) |
| 50 | + ) |
43 | 51 |
|
44 | 52 | adj_margin = adjusted_margin(quantity, margin, is_long, cumulative_funding_entry, market_cumulative_funding) |
45 | 53 | adjusted_unit_margin = (adj_margin / quantity) * (-1 if is_long else 1) |
46 | 54 | maintenance_margin_ratio = client_market.maintenance_margin_ratio * (-1 if is_long else 1) |
47 | 55 |
|
48 | 56 | liquidation_price = (entry_price + adjusted_unit_margin) / (Decimal(1) + maintenance_margin_ratio) |
49 | 57 |
|
50 | | - should_be_liquidated = (is_long and market_mark_price <= liquidation_price) or (not is_long and market_mark_price >= liquidation_price) |
| 58 | + should_be_liquidated = (is_long and market_mark_price <= liquidation_price) or ( |
| 59 | + not is_long and market_mark_price >= liquidation_price |
| 60 | + ) |
51 | 61 |
|
52 | 62 | if should_be_liquidated: |
53 | | - print(f"{'Long' if is_long else 'Short'} position for market {client_market.id} and subaccount {position['subaccountId']} should be liquidated (liquidation price: {liquidation_price.normalize()} / mark price: {market_mark_price.normalize()})") |
| 63 | + position_side = "Long" if is_long else "Short" |
| 64 | + print( |
| 65 | + f"{position_side} position for market {client_market.id} and subaccount " |
| 66 | + f"{position['subaccountId']} should be liquidated (liquidation price: " |
| 67 | + f"{liquidation_price.normalize()} / mark price: {market_mark_price.normalize()})" |
| 68 | + ) |
54 | 69 | liquidable_positions.append(position) |
55 | 70 |
|
56 | 71 | # print(f"\n\n\n") |
57 | 72 | # print(json.dumps(liquidable_positions, indent=4)) |
58 | 73 |
|
| 74 | + |
59 | 75 | if __name__ == "__main__": |
60 | 76 | asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments