Skip to content

Commit

Permalink
update fcn naming
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNineteen committed Nov 15, 2022
1 parent fec4426 commit a3d1684
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 64 deletions.
36 changes: 34 additions & 2 deletions examples/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,49 @@ async def main(
chu = ClearingHouseUser(ch, authority=authority, subaccount_id=subaccount, use_cache=True)
await chu.set_cache()

user = await chu.get_user()
print('subaccount name:', bytes(user.name))

from driftpy.constants.numeric_constants import QUOTE_PRECISION
spot_collateral = await chu.get_spot_market_asset_value(
None,
include_open_orders=True,
)
print('spot collat:', spot_collateral/QUOTE_PRECISION)

pnl = await chu.get_unrealized_pnl(False)
print('pnl:', pnl/QUOTE_PRECISION)

total_collateral = await chu.get_total_collateral()
print('total collateral:', total_collateral)
print('leverage:', await chu.get_leverage())

perp_liability = await chu.get_total_perp_positon(
None, 0, True
)
spot_liability = await chu.get_spot_market_liability(
None, None, 0, True
)
print(
'perp_liability', perp_liability,
'spot_liability', spot_liability
)

total_liability = await chu.get_margin_requirement(None)
total_asset_value = await chu.get_total_collateral()
print(
'total_liab', total_liability,
'total_asset', total_asset_value
)
print('leverage:', (await chu.get_leverage()) / 10_000)


user = await chu.get_user()
print('perp positions:')
for position in user.perp_positions:
if not is_available(position):
print('>', position)

print(time.time() - s)
print('time taken:', time.time() - s)
print('done! :)')

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/clearing_house_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async def get_unrealized_pnl(
market = await self.get_perp_market(position.market_index)

oracle_data = await self.get_perp_oracle_data(market)
position_unrealized_pnl = calculate_position_pnl(
position_unrealized_pnl = calculate_position_pnl_with_oracle(
market, position, oracle_data, with_funding
)

Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/math/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import copy
from solana.publickey import PublicKey
import numpy as np
from driftpy.math.positions import calculate_base_asset_value, calculate_position_pnl
# from driftpy.math.positions import calculate_base_asset_value, calculate_position_pnl
from driftpy.constants.numeric_constants import (
AMM_RESERVE_PRECISION,
AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO,
Expand Down
120 changes: 60 additions & 60 deletions src/driftpy/math/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def calculate_position_funding_pnl(market: PerpMarket, perp_position: PerpPositi
return funding_rate_pnl


def calculate_position_pnl(
def calculate_position_pnl_with_oracle(
market: PerpMarket,
perp_position: PerpPosition,
oracle_data,
Expand Down Expand Up @@ -103,83 +103,83 @@ def is_available(position: PerpPosition):
)


# def calculate_base_asset_value(market: PerpMarket, user_position: PerpPosition) -> int:
# if user_position.base_asset_amount == 0:
# return 0
def calculate_base_asset_value(market: PerpMarket, user_position: PerpPosition) -> int:
if user_position.base_asset_amount == 0:
return 0

# direction_to_close = (
# PositionDirection.SHORT
# if user_position.base_asset_amount > 0
# else PositionDirection.LONG
# )
direction_to_close = (
PositionDirection.SHORT
if user_position.base_asset_amount > 0
else PositionDirection.LONG
)

# new_quote_asset_reserve, _ = calculate_amm_reserves_after_swap(
# market.amm,
# AssetType.BASE,
# abs(user_position.base_asset_amount),
# get_swap_direction(AssetType.BASE, direction_to_close),
# )
new_quote_asset_reserve, _ = calculate_amm_reserves_after_swap(
market.amm,
AssetType.BASE,
abs(user_position.base_asset_amount),
get_swap_direction(AssetType.BASE, direction_to_close),
)

# result = None
# if direction_to_close == PositionDirection.SHORT:
# result = (
# (market.amm.quote_asset_reserve - new_quote_asset_reserve)
# * market.amm.peg_multiplier
# ) / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO
# else:
# # PositionDirection.LONG:
# result = (
# (
# (new_quote_asset_reserve - market.amm.quote_asset_reserve)
# * market.amm.peg_multiplier
# )
# / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO
# ) + 1.0
result = None
if direction_to_close == PositionDirection.SHORT:
result = (
(market.amm.quote_asset_reserve - new_quote_asset_reserve)
* market.amm.peg_multiplier
) / AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO
else:
# PositionDirection.LONG:
result = (
(
(new_quote_asset_reserve - market.amm.quote_asset_reserve)
* market.amm.peg_multiplier
)
/ AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO
) + 1.0

# return result
return result


# def calculate_position_pnl(
# market: PerpMarket, market_position: PerpPosition, with_funding=False
# ):
# pnl = 0.0
def calculate_position_pnl(
market: PerpMarket, market_position: PerpPosition, with_funding=False
):
pnl = 0.0

# if market_position.base_asset_amount == 0:
# return pnl
if market_position.base_asset_amount == 0:
return pnl

# base_asset_value = calculate_base_asset_value(market, market_position)
base_asset_value = calculate_base_asset_value(market, market_position)

# if market_position.base_asset_amount > 0:
# pnl = base_asset_value - market_position.quote_asset_amount
# else:
# pnl = market_position.quote_asset_amount - base_asset_value
if market_position.base_asset_amount > 0:
pnl = base_asset_value - market_position.quote_asset_amount
else:
pnl = market_position.quote_asset_amount - base_asset_value

# if with_funding:
# funding_rate_pnl = 0.0
# pnl += funding_rate_pnl / float(PRICE_TO_QUOTE_PRECISION_RATIO)
if with_funding:
funding_rate_pnl = 0.0
pnl += funding_rate_pnl / float(PRICE_TO_QUOTE_PRECISION_RATIO)

# return pnl
return pnl


# def calculate_position_funding_pnl(market: PerpMarket, market_position: PerpPosition):
# funding_pnl = 0.0
def calculate_position_funding_pnl(market: PerpMarket, market_position: PerpPosition):
funding_pnl = 0.0

# if market_position.base_asset_amount == 0:
# return funding_pnl
if market_position.base_asset_amount == 0:
return funding_pnl

# amm_cum_funding_rate = (
# market.amm.cumulative_funding_rate_long
# if market_position.base_asset_amount > 0
# else market.amm.cumulative_funding_rate_short
# )
amm_cum_funding_rate = (
market.amm.cumulative_funding_rate_long
if market_position.base_asset_amount > 0
else market.amm.cumulative_funding_rate_short
)

# funding_pnl = (
# market_position.last_cumulative_funding_rate - amm_cum_funding_rate
# ) * market_position.base_asset_amount
funding_pnl = (
market_position.last_cumulative_funding_rate - amm_cum_funding_rate
) * market_position.base_asset_amount

# funding_pnl /= float(AMM_RESERVE_PRECISION * FUNDING_RATE_BUFFER)
funding_pnl /= float(AMM_RESERVE_PRECISION * FUNDING_RATE_BUFFER)

# return funding_pnl
return funding_pnl


def calculate_entry_price(market_position: PerpPosition):
Expand Down

0 comments on commit a3d1684

Please sign in to comment.