@d8x/perpetuals-sdk 2.1.12-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.
@@ -149,10 +149,11 @@ on-chain</p></dd>
149
149
  * [~pmMarginThresh(pos, s2, s3, m)](#module_d8xMath..pmMarginThresh) ⇒
150
150
  * [~pmMaintenanceMarginRate(posSign, sm, m)](#module_d8xMath..pmMaintenanceMarginRate) ⇒
151
151
  * [~pmInitialMarginRate(posSign, sm, m)](#module_d8xMath..pmInitialMarginRate) ⇒
152
- * [~expectedLoss(p, m, totLong, totShort, tradeAmt, tradeMgnRate)](#module_d8xMath..expectedLoss)
153
- * [~pmExchangeFee(prob, m, totShort, totLong, tradeAmt, tradeMgnRate)](#module_d8xMath..pmExchangeFee) ⇒
152
+ * [~expectedLoss(p, m, totLong, totShort, tradeAmt, tradeMgnRate)](#module_d8xMath..expectedLoss)
153
+ * [~expectedLossImpact(p, m, tradeAmt, tradeMgnRate)](#module_d8xMath..expectedLossImpact) ⇒
154
+ * [~pmExchangeFee(prob, m, tradeAmt, tradeMgnRate)](#module_d8xMath..pmExchangeFee) ⇒
154
155
  * [~pmMarginBalance(pos, s2, s3, ell, mc)](#module_d8xMath..pmMarginBalance) ⇒
155
- * [~excessMargin(tradeAmt, currentCashCC, currentPos, currentLockedInQC, limitPrice, Sm, S3, totLong, totShort)](#module_d8xMath..excessMargin) ⇒
156
+ * [~excessMargin(tradeAmt, currentCashCC, currentPos, currentLockedInQC, limitPrice, Sm, S3)](#module_d8xMath..excessMargin) ⇒
156
157
  * [~pmGetDepositAmtForLvgTrade(tradeAmt, targetLvg, price, S3, S2Mark)](#module_d8xMath..pmGetDepositAmtForLvgTrade) ⇒
157
158
  * [~pmExcessCashAtLvg(tradeAmt, lvg, walletBalCC, currentCashCC, currentPosition, currentLockedInValue, slippage, S2, Sm, S3, totLong, totShort)](#module_d8xMath..pmExcessCashAtLvg) ⇒
158
159
  * [~pmFindMaxPersonalTradeSizeAtLeverage(dir, lvg, walletBalCC, slippage, currentPosition, currentCashCC, currentLockedInValue, S2, Sm, S3, totLong, totShort, maxShort, maxLong)](#module_d8xMath..pmFindMaxPersonalTradeSizeAtLeverage) ⇒
@@ -464,11 +465,12 @@ Smart contract equivalent: calcMarginForTargetLeverage(..., _ignorePosBalance =
464
465
 
465
466
  <a name="module_d8xMath..expectedLoss"></a>
466
467
 
467
- ### d8xMath~expectedLoss(p, m, totLong, totShort, tradeAmt, tradeMgnRate)
468
+ ### d8xMath~expectedLoss(p, m, totLong, totShort, tradeAmt, tradeMgnRate)
468
469
  <p>Calculate the expected loss for a prediction market trade used for
469
470
  prediction market fees</p>
470
471
 
471
472
  **Kind**: inner method of [<code>d8xMath</code>](#module_d8xMath)
473
+ **Returns**: <p>expected loss in dollars</p>
472
474
 
473
475
  | Param | Description |
474
476
  | --- | --- |
@@ -479,21 +481,37 @@ prediction market fees</p>
479
481
  | tradeAmt | <p>signed trade amount, can be zero</p> |
480
482
  | tradeMgnRate | <p>margin rate of the trader</p> |
481
483
 
484
+ <a name="module_d8xMath..expectedLossImpact"></a>
485
+
486
+ ### d8xMath~expectedLossImpact(p, m, tradeAmt, tradeMgnRate) ⇒
487
+ <p>Equivalent to
488
+ const el0 = expectedLoss(prob, m, totLong, totShort, 0, 0);
489
+ const el1 = expectedLoss(prob, m, totLong, totShort, tradeAmt, tradeMgnRate)
490
+ const fee = (el1 - el0) / Math.abs(tradeAmt);</p>
491
+
492
+ **Kind**: inner method of [<code>d8xMath</code>](#module_d8xMath)
493
+ **Returns**: <p>dollar fee</p>
494
+
495
+ | Param | Description |
496
+ | --- | --- |
497
+ | p | <p>prob long probability</p> |
498
+ | m | <p>max maintenance margin rate (0.18)</p> |
499
+ | tradeAmt | <p>trade amount in base currency</p> |
500
+ | tradeMgnRate | <p>margin rate for this trade</p> |
501
+
482
502
  <a name="module_d8xMath..pmExchangeFee"></a>
483
503
 
484
- ### d8xMath~pmExchangeFee(prob, m, totShort, totLong, tradeAmt, tradeMgnRate) ⇒
504
+ ### d8xMath~pmExchangeFee(prob, m, tradeAmt, tradeMgnRate) ⇒
485
505
  <p>Exchange fee as a rate for prediction markets
486
506
  For opening trades only</p>
487
507
 
488
508
  **Kind**: inner method of [<code>d8xMath</code>](#module_d8xMath)
489
- **Returns**: <p>fee relative to tradeAmt</p>
509
+ **Returns**: <p>dollar fee relative to tradeAmt</p>
490
510
 
491
511
  | Param | Description |
492
512
  | --- | --- |
493
513
  | prob | <p>long probability</p> |
494
514
  | m | <p>max maintenance margin rate (0.18)</p> |
495
- | totShort | |
496
- | totLong | |
497
515
  | tradeAmt | <p>trade amount in base currency</p> |
498
516
  | tradeMgnRate | <p>margin rate for this trade</p> |
499
517
 
@@ -515,7 +533,7 @@ For opening trades only</p>
515
533
 
516
534
  <a name="module_d8xMath..excessMargin"></a>
517
535
 
518
- ### d8xMath~excessMargin(tradeAmt, currentCashCC, currentPos, currentLockedInQC, limitPrice, Sm, S3, totLong, totShort) ⇒
536
+ ### d8xMath~excessMargin(tradeAmt, currentCashCC, currentPos, currentLockedInQC, limitPrice, Sm, S3) ⇒
519
537
  <p>Calculate the excess margin defined as
520
538
  excess := margin balance - trading fee - initial margin threshold
521
539
  for the given trade and position</p>
@@ -532,8 +550,6 @@ for the given trade and position</p>
532
550
  | limitPrice |
533
551
  | Sm |
534
552
  | S3 |
535
- | totLong |
536
- | totShort |
537
553
 
538
554
  <a name="module_d8xMath..pmGetDepositAmtForLvgTrade"></a>
539
555
 
@@ -3773,7 +3789,7 @@ No gas required for the queries here.</p>
3773
3789
  * [.getABI(contract)](#PerpetualDataHandler+getABI) ⇒
3774
3790
  * [.isPredictionMarket(symbol)](#PerpetualDataHandler+isPredictionMarket) ⇒
3775
3791
  * _static_
3776
- * [.exchangeFeePrdMkts(state, maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate)](#MarketData.exchangeFeePrdMkts) ⇒
3792
+ * [.exchangeFeePrdMkts(maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate)](#MarketData.exchangeFeePrdMkts) ⇒
3777
3793
 
3778
3794
  <a name="new_MarketData_new"></a>
3779
3795
 
@@ -4942,7 +4958,7 @@ main();
4942
4958
 
4943
4959
  <a name="MarketData.exchangeFeePrdMkts"></a>
4944
4960
 
4945
- ### MarketData.exchangeFeePrdMkts(state, maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate) ⇒
4961
+ ### MarketData.exchangeFeePrdMkts(maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate) ⇒
4946
4962
  <p>Fee is relative to base-currency amount (=trade amount)</p>
4947
4963
 
4948
4964
  **Kind**: static method of [<code>MarketData</code>](#MarketData)
@@ -4950,7 +4966,6 @@ main();
4950
4966
 
4951
4967
  | Param | Description |
4952
4968
  | --- | --- |
4953
- | state | <p>current perpetual state (need longBC and shortBC)</p> |
4954
4969
  | maxMaintMgnRate | <p>maintenance margin rate param for pred mkts</p> |
4955
4970
  | Sm | <p>Mark price</p> |
4956
4971
  | tradeAmtBC | <p>signed trade amount</p> |
package/doc/marketData.md CHANGED
@@ -44,7 +44,7 @@ No gas required for the queries here.</p>
44
44
  * [.fetchPricesForPerpetual(symbol)](#MarketData+fetchPricesForPerpetual) ⇒
45
45
  * [.fetchPrdMktMetaData(symbol)](#MarketData+fetchPrdMktMetaData) ⇒
46
46
  * _static_
47
- * [.exchangeFeePrdMkts(state, maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate)](#MarketData.exchangeFeePrdMkts) ⇒
47
+ * [.exchangeFeePrdMkts(maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate)](#MarketData.exchangeFeePrdMkts) ⇒
48
48
 
49
49
  <a name="new_MarketData_new"></a>
50
50
 
@@ -810,7 +810,7 @@ main();
810
810
 
811
811
  <a name="MarketData.exchangeFeePrdMkts"></a>
812
812
 
813
- ### MarketData.exchangeFeePrdMkts(state, maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate) ⇒
813
+ ### MarketData.exchangeFeePrdMkts(maxMaintMgnRate, Sm, tradeAmtBC, tradeMgnRate) ⇒
814
814
  <p>Fee is relative to base-currency amount (=trade amount)</p>
815
815
 
816
816
  **Kind**: static method of [<code>MarketData</code>](#MarketData)
@@ -818,7 +818,6 @@ main();
818
818
 
819
819
  | Param | Description |
820
820
  | --- | --- |
821
- | state | <p>current perpetual state (need longBC and shortBC)</p> |
822
821
  | maxMaintMgnRate | <p>maintenance margin rate param for pred mkts</p> |
823
822
  | Sm | <p>Mark price</p> |
824
823
  | tradeAmtBC | <p>signed trade amount</p> |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d8x/perpetuals-sdk",
3
- "version": "2.1.12-alpha2",
3
+ "version": "2.1.13-alpha2",
4
4
  "description": "Node TypeScript SDK for D8X Perpetual Futures",
5
5
  "author": "D8X",
6
6
  "homepage": "https://github.com/D8-X/d8x-futures-node-sdk#readme",
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,7 +533,18 @@ export function expectedLoss(
532
533
  return p * (1 - p) * Math.max(0, a + b);
533
534
  }
534
535
 
535
- function newFee(p: number, m: number, tradeAmt: number, tradeMgnRate: number) {
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) {
536
548
  m = (0.4 - m) * entropy(p) + m;
537
549
  let dlm = 0;
538
550
  let dl = 0;
@@ -556,26 +568,18 @@ function newFee(p: number, m: number, tradeAmt: number, tradeMgnRate: number) {
556
568
  * For opening trades only
557
569
  * @param prob long probability
558
570
  * @param m max maintenance margin rate (0.18)
559
- * @param totShort
560
- * @param totLong
561
571
  * @param tradeAmt trade amount in base currency
562
572
  * @param tradeMgnRate margin rate for this trade
563
- * @returns fee relative to tradeAmt
573
+ * @returns dollar fee relative to tradeAmt
564
574
  */
565
- export function pmExchangeFee(
566
- prob: number,
567
- m: number,
568
- totShort: number,
569
- totLong: number,
570
- tradeAmt: number,
571
- tradeMgnRate: number
572
- ): number {
573
- const el0 = expectedLoss(prob, m, totLong, totShort, 0, 0);
574
- const el1 = expectedLoss(prob, m, totLong, totShort, tradeAmt, tradeMgnRate);
575
- console.log("el0=", el0);
576
- console.log("el1=", el1);
577
- const fee = (el1 - el0) / Math.abs(tradeAmt);
578
- console.log("diff=", el1 - el0);
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);
579
583
  return Math.max(fee, 0.001);
580
584
  }
581
585
 
@@ -644,8 +648,6 @@ export function pmFindLiquidationPrice(
644
648
  * @param limitPrice
645
649
  * @param Sm
646
650
  * @param S3
647
- * @param totLong
648
- * @param totShort
649
651
  * @returns excess margin as defined above
650
652
  */
651
653
  function excessMargin(
@@ -655,9 +657,7 @@ function excessMargin(
655
657
  currentLockedInQC: number,
656
658
  limitPrice: number,
657
659
  Sm: number,
658
- S3: number,
659
- totLong: number,
660
- totShort: number
660
+ S3: number
661
661
  ): number {
662
662
  const m = 0.18; //max maintenance margin rate
663
663
  const m0 = 0.2; //max initial margin rate
@@ -672,7 +672,7 @@ function excessMargin(
672
672
  const b0 = currentCashCC + Math.abs(currentPos) * Sm - currentLockedInQC + Math.max(0, tradeAmt * (Sm - limitPrice));
673
673
  // b0 + margin - fee > threshold
674
674
  // margin = threshold - b0 + fee
675
- const fee_cc = pmExchangeFee(p, m, totShort, totLong, tradeAmt, tau) / S3;
675
+ const fee_cc = pmExchangeFee(p, m, tradeAmt, tau) / S3;
676
676
 
677
677
  // missing: referral rebate
678
678
  return b0 / S3 - thresh / S3 - fee_cc;
@@ -737,9 +737,7 @@ function pmExcessCashAtLvg(
737
737
  slippage: number,
738
738
  S2: number,
739
739
  Sm: number,
740
- S3: number,
741
- totLong: number,
742
- totShort: number
740
+ S3: number
743
741
  ): number {
744
742
  //determine deposit amount for given leverage
745
743
  const limitPrice = S2 * (1 + Math.sign(tradeAmt) * slippage);
@@ -750,7 +748,7 @@ function pmExcessCashAtLvg(
750
748
  if (tradeAmt < 0) {
751
749
  p0 = 2 - Sm; //=1-(Sm-1)
752
750
  }
753
- const feeCc = pmExchangeFee(p0, m0, totShort, totLong, tradeAmt, 1 / lvg) / S3;
751
+ const feeCc = pmExchangeFee(p0, m0, tradeAmt, 1 / lvg) / S3;
754
752
 
755
753
  //excess cash
756
754
  let exc = walletBalCC - depositFromWallet - feeCc;
@@ -833,9 +831,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
833
831
  slippage,
834
832
  S2,
835
833
  Sm,
836
- S3,
837
- totLong,
838
- totShort
834
+ S3
839
835
  );
840
836
  if (f0 < lot) {
841
837
  // no trade possible
@@ -859,9 +855,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
859
855
  slippage,
860
856
  S2,
861
857
  Sm,
862
- S3,
863
- totLong,
864
- totShort
858
+ S3
865
859
  ) ** 2;
866
860
  const f2 =
867
861
  pmExcessCashAtLvg(
@@ -874,9 +868,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
874
868
  slippage,
875
869
  S2,
876
870
  Sm,
877
- S3,
878
- totLong,
879
- totShort
871
+ S3
880
872
  ) ** 2;
881
873
  let ds = (f2 - f) / deltaS;
882
874
  if (ds == 0) {
@@ -934,8 +926,6 @@ export function pmFindMaxTradeSize(
934
926
  limitPrice: number,
935
927
  Sm: number,
936
928
  S3: number,
937
- totLong: number,
938
- totShort: number,
939
929
  maxShort: number,
940
930
  maxLong: number
941
931
  ): number {
@@ -946,17 +936,7 @@ export function pmFindMaxTradeSize(
946
936
  }
947
937
  const lot = 10;
948
938
  const deltaS = 1; //for derivative
949
- const f0 = excessMargin(
950
- dir * deltaS,
951
- currentCashCC,
952
- currentPosition,
953
- currentLockedInValue,
954
- limitPrice,
955
- Sm,
956
- S3,
957
- totLong,
958
- totShort
959
- );
939
+ const f0 = excessMargin(dir * deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3);
960
940
  if (f0 < lot) {
961
941
  // no trade possible
962
942
  return 0;
@@ -968,21 +948,9 @@ export function pmFindMaxTradeSize(
968
948
  let count = 0;
969
949
  while (Math.abs(sNew - s) > 1 && count < 100) {
970
950
  s = sNew;
971
- const f =
972
- excessMargin(s, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3, totLong, totShort) **
973
- 2;
951
+ const f = excessMargin(s, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
974
952
  const f2 =
975
- excessMargin(
976
- s + deltaS,
977
- currentCashCC,
978
- currentPosition,
979
- currentLockedInValue,
980
- limitPrice,
981
- Sm,
982
- S3,
983
- totLong,
984
- totShort
985
- ) ** 2;
953
+ excessMargin(s + deltaS, currentCashCC, currentPosition, currentLockedInValue, limitPrice, Sm, S3) ** 2;
986
954
  let ds = (f2 - f) / deltaS;
987
955
  sNew = s - f / ds;
988
956
  count += 1;
package/src/marketData.ts CHANGED
@@ -804,7 +804,6 @@ export default class MarketData extends PerpetualDataHandler {
804
804
 
805
805
  /**
806
806
  * Fee is relative to base-currency amount (=trade amount)
807
- * @param state current perpetual state (need longBC and shortBC)
808
807
  * @param maxMaintMgnRate maintenance margin rate param for pred mkts
809
808
  * @param Sm Mark price
810
809
  * @param tradeAmtBC signed trade amount
@@ -812,7 +811,6 @@ export default class MarketData extends PerpetualDataHandler {
812
811
  * @returns relative exchange fee in decimals
813
812
  */
814
813
  public static exchangeFeePrdMkts(
815
- state: PerpetualState,
816
814
  maxMaintMgnRate: number,
817
815
  Sm: number,
818
816
  tradeAmtBC: number,
@@ -826,7 +824,7 @@ export default class MarketData extends PerpetualDataHandler {
826
824
  // 0.1cents if is close
827
825
  return 0.001;
828
826
  }
829
- return pmExchangeFee(Sm - 1, maxMaintMgnRate, state.shortBC, state.longBC, tradeAmtBC, tradeMgnRate);
827
+ return pmExchangeFee(Sm - 1, maxMaintMgnRate, tradeAmtBC, tradeMgnRate);
830
828
  }
831
829
 
832
830
  /**
@@ -1279,8 +1277,6 @@ export default class MarketData extends PerpetualDataHandler {
1279
1277
  Sm,
1280
1278
  Sm,
1281
1279
  indexPriceInfo.s3 ?? 0,
1282
- totLong,
1283
- totShort,
1284
1280
  maxShortPosPerp,
1285
1281
  maxLongPosPerp
1286
1282
  );
@@ -1292,8 +1288,6 @@ export default class MarketData extends PerpetualDataHandler {
1292
1288
  Sm,
1293
1289
  Sm,
1294
1290
  indexPriceInfo.s3 ?? 0,
1295
- totLong,
1296
- totShort,
1297
1291
  maxShortPosPerp,
1298
1292
  maxLongPosPerp
1299
1293
  );
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const D8X_SDK_VERSION = "2.1.12-alpha2";
1
+ export const D8X_SDK_VERSION = "2.1.13-alpha2";