Skip to content

Commit

Permalink
chore: update unrealized pnl percent to be based on leverage (#577)
Browse files Browse the repository at this point in the history
Co-authored-by: mobile-build-bot-git <[email protected]>
  • Loading branch information
aforaleka and mobile-build-bot authored Aug 19, 2024
1 parent d9cedc2 commit e8ca374
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.8.91"
version = "1.8.92"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet
import kotlin.math.max

internal enum class CalculationPeriod(val rawValue: String) {
current("current"),
Expand Down Expand Up @@ -158,13 +159,15 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
set(maxLeverage, modified, "maxLeverage", period)

if (entryPrice != null) {
val leverage = parser.asDouble(value(position, "leverage", period))
val scaledLeverage = max(leverage?.abs() ?: 1.0, 1.0)
val entryValue = size * entryPrice
val currentValue = size * oraclePrice
val unrealizedPnl = currentValue - entryValue
val unrealizedPnlPercent =
if (entryValue != Numeric.double.ZERO) unrealizedPnl / entryValue.abs() else null
val scaledUnrealizedPnlPercent =
if (entryValue != Numeric.double.ZERO) unrealizedPnl / entryValue.abs() * scaledLeverage else null
set(unrealizedPnl, modified, "unrealizedPnl", period)
set(unrealizedPnlPercent, modified, "unrealizedPnlPercent", period)
set(scaledUnrealizedPnlPercent, modified, "unrealizedPnlPercent", period)
}

val marginMode = parser.asString(parser.value(position, "marginMode"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import exchange.dydx.abacus.state.internalstate.InternalSubaccountCalculated
import exchange.dydx.abacus.state.internalstate.InternalSubaccountState
import exchange.dydx.abacus.utils.Numeric
import indexer.codegen.IndexerPerpetualPositionStatus
import kotlin.math.max

internal class SubaccountCalculatorV2(
val parser: ParserProtocol
Expand Down Expand Up @@ -395,13 +396,15 @@ internal class SubaccountCalculatorV2(
calculated.maxLeverage = maxLeverage

if (entryPrice != null) {
val leverage = position.calculated[period]?.leverage
val scaledLeverage = max(leverage?.abs() ?: 1.0, 1.0)
val entryValue = size * entryPrice
val currentValue = size * oraclePrice
val unrealizedPnl = currentValue - entryValue
val unrealizedPnlPercent =
if (entryValue != Numeric.double.ZERO) unrealizedPnl / entryValue.abs() else null
val scaledUnrealizedPnlPercent =
if (entryValue != Numeric.double.ZERO) unrealizedPnl / entryValue.abs() * scaledLeverage else null
calculated.unrealizedPnl = unrealizedPnl
calculated.unrealizedPnlPercent = unrealizedPnlPercent
calculated.unrealizedPnlPercent = scaledUnrealizedPnlPercent
}

val marginMode = position.marginMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ class V4AccountTests : V4BaseTests() {
"resources": {
},
"unrealizedPnlPercent": {
"current": 0.4066
"current": 0.7995
},
"valueTotal": {
"current": -43112.854562506596
Expand Down Expand Up @@ -670,7 +670,7 @@ class V4AccountTests : V4BaseTests() {
"current": -7.640711804814775E-4
},
"unrealizedPnlPercent": {
"current": -0.3848
"current": -0.587
},
"valueTotal": {
"current": -186164.40898202002
Expand Down
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.8.91'
spec.version = '1.8.92'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit e8ca374

Please sign in to comment.