@d8x/perpetuals-sdk 2.1.11-alpha2 → 2.1.13-alpha2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/d8XMath.d.ts +4 -5
- package/dist/cjs/d8XMath.js +51 -21
- package/dist/cjs/d8XMath.js.map +1 -1
- package/dist/cjs/marketData.d.ts +1 -2
- package/dist/cjs/marketData.js +7 -6
- package/dist/cjs/marketData.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/d8XMath.d.ts +4 -5
- package/dist/esm/d8XMath.js +51 -21
- package/dist/esm/d8XMath.js.map +1 -1
- package/dist/esm/marketData.d.ts +1 -2
- package/dist/esm/marketData.js +7 -6
- package/dist/esm/marketData.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/doc/d8x-perpetuals-sdk.md +29 -14
- package/doc/marketData.md +2 -3
- package/package.json +1 -1
- package/src/d8XMath.ts +50 -60
- package/src/marketData.ts +4 -8
- package/src/version.ts +1 -1
package/src/d8XMath.ts
CHANGED
|
@@ -505,6 +505,7 @@ export function pmInitialMarginRate(posSign: number, sm: number, m: number | und
|
|
|
505
505
|
* @param totShort total short
|
|
506
506
|
* @param tradeAmt signed trade amount, can be zero
|
|
507
507
|
* @param tradeMgnRate margin rate of the trader
|
|
508
|
+
* @returns expected loss in dollars
|
|
508
509
|
*/
|
|
509
510
|
export function expectedLoss(
|
|
510
511
|
p: number,
|
|
@@ -532,28 +533,53 @@ export function expectedLoss(
|
|
|
532
533
|
return p * (1 - p) * Math.max(0, a + b);
|
|
533
534
|
}
|
|
534
535
|
|
|
536
|
+
/**
|
|
537
|
+
* Equivalent to
|
|
538
|
+
* const el0 = expectedLoss(prob, m, totLong, totShort, 0, 0);
|
|
539
|
+
* const el1 = expectedLoss(prob, m, totLong, totShort, tradeAmt, tradeMgnRate)
|
|
540
|
+
* const fee = (el1 - el0) / Math.abs(tradeAmt);
|
|
541
|
+
* @param p prob long probability
|
|
542
|
+
* @param m max maintenance margin rate (0.18)
|
|
543
|
+
* @param tradeAmt trade amount in base currency
|
|
544
|
+
* @param tradeMgnRate margin rate for this trade
|
|
545
|
+
* @returns dollar fee
|
|
546
|
+
*/
|
|
547
|
+
function expectedLossImpact(p: number, m: number, tradeAmt: number, tradeMgnRate: number) {
|
|
548
|
+
m = (0.4 - m) * entropy(p) + m;
|
|
549
|
+
let dlm = 0;
|
|
550
|
+
let dl = 0;
|
|
551
|
+
let dsm = 0;
|
|
552
|
+
let ds = 0;
|
|
553
|
+
if (tradeAmt > 0) {
|
|
554
|
+
dlm = p * tradeAmt * tradeMgnRate;
|
|
555
|
+
dl = tradeAmt;
|
|
556
|
+
} else if (tradeAmt < 0) {
|
|
557
|
+
dsm = (1 - p) * Math.abs(tradeAmt) * tradeMgnRate;
|
|
558
|
+
ds = Math.abs(tradeAmt);
|
|
559
|
+
}
|
|
560
|
+
//long: p * (1 - p) max(0, dl-dlm) = p * (1 - p) max(0, tradeAmt - p * tradeAmt * tradeMgnRate)
|
|
561
|
+
const a = dl - dsm;
|
|
562
|
+
const b = ds - dlm;
|
|
563
|
+
return p * (1 - p) * Math.max(0, a + b);
|
|
564
|
+
}
|
|
565
|
+
|
|
535
566
|
/**
|
|
536
567
|
* Exchange fee as a rate for prediction markets
|
|
537
568
|
* For opening trades only
|
|
538
569
|
* @param prob long probability
|
|
539
570
|
* @param m max maintenance margin rate (0.18)
|
|
540
|
-
* @param totShort
|
|
541
|
-
* @param totLong
|
|
542
571
|
* @param tradeAmt trade amount in base currency
|
|
543
572
|
* @param tradeMgnRate margin rate for this trade
|
|
544
|
-
* @returns fee relative to tradeAmt
|
|
573
|
+
* @returns dollar fee relative to tradeAmt
|
|
545
574
|
*/
|
|
546
|
-
export function pmExchangeFee(
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
)
|
|
554
|
-
const el0 = expectedLoss(prob, m, totLong, totShort, 0, 0);
|
|
555
|
-
const el1 = expectedLoss(prob, m, totLong, totShort, tradeAmt, tradeMgnRate);
|
|
556
|
-
const fee = (el1 - el0) / Math.abs(tradeAmt);
|
|
575
|
+
export function pmExchangeFee(prob: number, m: number, tradeAmt: number, tradeMgnRate: number): number {
|
|
576
|
+
/*
|
|
577
|
+
equivalent:
|
|
578
|
+
const el0 = expectedLoss(prob, m, totLong, totShort, 0, 0);
|
|
579
|
+
const el1 = expectedLoss(prob, m, totLong, totShort, tradeAmt, tradeMgnRate);
|
|
580
|
+
const fee = (el1 - el0) / Math.abs(tradeAmt);
|
|
581
|
+
*/
|
|
582
|
+
let fee = expectedLossImpact(prob, m, tradeAmt, tradeMgnRate) / Math.abs(tradeAmt);
|
|
557
583
|
return Math.max(fee, 0.001);
|
|
558
584
|
}
|
|
559
585
|
|
|
@@ -622,8 +648,6 @@ export function pmFindLiquidationPrice(
|
|
|
622
648
|
* @param limitPrice
|
|
623
649
|
* @param Sm
|
|
624
650
|
* @param S3
|
|
625
|
-
* @param totLong
|
|
626
|
-
* @param totShort
|
|
627
651
|
* @returns excess margin as defined above
|
|
628
652
|
*/
|
|
629
653
|
function excessMargin(
|
|
@@ -633,9 +657,7 @@ function excessMargin(
|
|
|
633
657
|
currentLockedInQC: number,
|
|
634
658
|
limitPrice: number,
|
|
635
659
|
Sm: number,
|
|
636
|
-
S3: number
|
|
637
|
-
totLong: number,
|
|
638
|
-
totShort: number
|
|
660
|
+
S3: number
|
|
639
661
|
): number {
|
|
640
662
|
const m = 0.18; //max maintenance margin rate
|
|
641
663
|
const m0 = 0.2; //max initial margin rate
|
|
@@ -650,7 +672,7 @@ function excessMargin(
|
|
|
650
672
|
const b0 = currentCashCC + Math.abs(currentPos) * Sm - currentLockedInQC + Math.max(0, tradeAmt * (Sm - limitPrice));
|
|
651
673
|
// b0 + margin - fee > threshold
|
|
652
674
|
// margin = threshold - b0 + fee
|
|
653
|
-
const fee_cc = pmExchangeFee(p, m,
|
|
675
|
+
const fee_cc = pmExchangeFee(p, m, tradeAmt, tau) / S3;
|
|
654
676
|
|
|
655
677
|
// missing: referral rebate
|
|
656
678
|
return b0 / S3 - thresh / S3 - fee_cc;
|
|
@@ -715,9 +737,7 @@ function pmExcessCashAtLvg(
|
|
|
715
737
|
slippage: number,
|
|
716
738
|
S2: number,
|
|
717
739
|
Sm: number,
|
|
718
|
-
S3: number
|
|
719
|
-
totLong: number,
|
|
720
|
-
totShort: number
|
|
740
|
+
S3: number
|
|
721
741
|
): number {
|
|
722
742
|
//determine deposit amount for given leverage
|
|
723
743
|
const limitPrice = S2 * (1 + Math.sign(tradeAmt) * slippage);
|
|
@@ -728,7 +748,7 @@ function pmExcessCashAtLvg(
|
|
|
728
748
|
if (tradeAmt < 0) {
|
|
729
749
|
p0 = 2 - Sm; //=1-(Sm-1)
|
|
730
750
|
}
|
|
731
|
-
const feeCc = pmExchangeFee(p0, m0,
|
|
751
|
+
const feeCc = pmExchangeFee(p0, m0, tradeAmt, 1 / lvg) / S3;
|
|
732
752
|
|
|
733
753
|
//excess cash
|
|
734
754
|
let exc = walletBalCC - depositFromWallet - feeCc;
|
|
@@ -811,9 +831,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
|
|
|
811
831
|
slippage,
|
|
812
832
|
S2,
|
|
813
833
|
Sm,
|
|
814
|
-
S3
|
|
815
|
-
totLong,
|
|
816
|
-
totShort
|
|
834
|
+
S3
|
|
817
835
|
);
|
|
818
836
|
if (f0 < lot) {
|
|
819
837
|
// no trade possible
|
|
@@ -837,9 +855,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
|
|
|
837
855
|
slippage,
|
|
838
856
|
S2,
|
|
839
857
|
Sm,
|
|
840
|
-
S3
|
|
841
|
-
totLong,
|
|
842
|
-
totShort
|
|
858
|
+
S3
|
|
843
859
|
) ** 2;
|
|
844
860
|
const f2 =
|
|
845
861
|
pmExcessCashAtLvg(
|
|
@@ -852,9 +868,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
|
|
|
852
868
|
slippage,
|
|
853
869
|
S2,
|
|
854
870
|
Sm,
|
|
855
|
-
S3
|
|
856
|
-
totLong,
|
|
857
|
-
totShort
|
|
871
|
+
S3
|
|
858
872
|
) ** 2;
|
|
859
873
|
let ds = (f2 - f) / deltaS;
|
|
860
874
|
if (ds == 0) {
|
|
@@ -912,8 +926,6 @@ export function pmFindMaxTradeSize(
|
|
|
912
926
|
limitPrice: number,
|
|
913
927
|
Sm: number,
|
|
914
928
|
S3: number,
|
|
915
|
-
totLong: number,
|
|
916
|
-
totShort: number,
|
|
917
929
|
maxShort: number,
|
|
918
930
|
maxLong: number
|
|
919
931
|
): number {
|
|
@@ -924,17 +936,7 @@ export function pmFindMaxTradeSize(
|
|
|
924
936
|
}
|
|
925
937
|
const lot = 10;
|
|
926
938
|
const deltaS = 1; //for derivative
|
|
927
|
-
const f0 = excessMargin(
|
|
928
|
-
dir * deltaS,
|
|
929
|
-
currentCashCC,
|
|
930
|
-
currentPosition,
|
|
931
|
-
currentLockedInValue,
|
|
932
|
-
limitPrice,
|
|
933
|
-
Sm,
|
|
934
|
-
S3,
|
|
935
|
-
totLong,
|
|
936
|
-
totShort
|
|
937
|
-
);
|
|
939
|
+
const f0 = excessMargin(dir * deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3);
|
|
938
940
|
if (f0 < lot) {
|
|
939
941
|
// no trade possible
|
|
940
942
|
return 0;
|
|
@@ -946,21 +948,9 @@ export function pmFindMaxTradeSize(
|
|
|
946
948
|
let count = 0;
|
|
947
949
|
while (Math.abs(sNew - s) > 1 && count < 100) {
|
|
948
950
|
s = sNew;
|
|
949
|
-
const f =
|
|
950
|
-
excessMargin(s, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3, totLong, totShort) **
|
|
951
|
-
2;
|
|
951
|
+
const f = excessMargin(s, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
|
|
952
952
|
const f2 =
|
|
953
|
-
excessMargin(
|
|
954
|
-
s + deltaS,
|
|
955
|
-
currentCashCC,
|
|
956
|
-
currentPosition,
|
|
957
|
-
currentLockedInValue,
|
|
958
|
-
limitPrice,
|
|
959
|
-
Sm,
|
|
960
|
-
S3,
|
|
961
|
-
totLong,
|
|
962
|
-
totShort
|
|
963
|
-
) ** 2;
|
|
953
|
+
excessMargin(s + deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
|
|
964
954
|
let ds = (f2 - f) / deltaS;
|
|
965
955
|
sNew = s - f / ds;
|
|
966
956
|
count += 1;
|
package/src/marketData.ts
CHANGED
|
@@ -705,7 +705,9 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
705
705
|
}
|
|
706
706
|
let newSide = newPositionBC > 0 ? BUY_SIDE : newPositionBC < 0 ? SELL_SIDE : CLOSED_SIDE;
|
|
707
707
|
|
|
708
|
-
let tradingFeeCC =
|
|
708
|
+
let tradingFeeCC = isPredMkt
|
|
709
|
+
? (Math.abs(tradeAmountBC) * tradingFeeTbps * 1e-5) / S3
|
|
710
|
+
: (Math.abs(tradeAmountBC) * tradingFeeTbps * 1e-5 * S2) / S3;
|
|
709
711
|
let referralFeeCC = this.symbolToPerpStaticInfo.get(account.symbol)!.referralRebate;
|
|
710
712
|
// Trade type:
|
|
711
713
|
let isClose = newPositionBC == 0 || newPositionBC * tradeAmountBC < 0;
|
|
@@ -802,7 +804,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
802
804
|
|
|
803
805
|
/**
|
|
804
806
|
* Fee is relative to base-currency amount (=trade amount)
|
|
805
|
-
* @param state current perpetual state (need longBC and shortBC)
|
|
806
807
|
* @param maxMaintMgnRate maintenance margin rate param for pred mkts
|
|
807
808
|
* @param Sm Mark price
|
|
808
809
|
* @param tradeAmtBC signed trade amount
|
|
@@ -810,7 +811,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
810
811
|
* @returns relative exchange fee in decimals
|
|
811
812
|
*/
|
|
812
813
|
public static exchangeFeePrdMkts(
|
|
813
|
-
state: PerpetualState,
|
|
814
814
|
maxMaintMgnRate: number,
|
|
815
815
|
Sm: number,
|
|
816
816
|
tradeAmtBC: number,
|
|
@@ -824,7 +824,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
824
824
|
// 0.1cents if is close
|
|
825
825
|
return 0.001;
|
|
826
826
|
}
|
|
827
|
-
return pmExchangeFee(Sm - 1, maxMaintMgnRate,
|
|
827
|
+
return pmExchangeFee(Sm - 1, maxMaintMgnRate, tradeAmtBC, tradeMgnRate);
|
|
828
828
|
}
|
|
829
829
|
|
|
830
830
|
/**
|
|
@@ -1277,8 +1277,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1277
1277
|
Sm,
|
|
1278
1278
|
Sm,
|
|
1279
1279
|
indexPriceInfo.s3 ?? 0,
|
|
1280
|
-
totLong,
|
|
1281
|
-
totShort,
|
|
1282
1280
|
maxShortPosPerp,
|
|
1283
1281
|
maxLongPosPerp
|
|
1284
1282
|
);
|
|
@@ -1290,8 +1288,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1290
1288
|
Sm,
|
|
1291
1289
|
Sm,
|
|
1292
1290
|
indexPriceInfo.s3 ?? 0,
|
|
1293
|
-
totLong,
|
|
1294
|
-
totShort,
|
|
1295
1291
|
maxShortPosPerp,
|
|
1296
1292
|
maxLongPosPerp
|
|
1297
1293
|
);
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const D8X_SDK_VERSION = "2.1.
|
|
1
|
+
export const D8X_SDK_VERSION = "2.1.13-alpha2";
|