@@ -133,7 +133,6 @@ func TestEstimatePosition(t *testing.T) {
133
133
rfLong := num .DecimalFromFloat (0.1 )
134
134
rfShort := num .DecimalFromFloat (0.2 )
135
135
136
- markPrice := 123.456 * math .Pow10 (marketDecimals )
137
136
auctionEnd := int64 (0 )
138
137
fundingPayment := 1234.56789
139
138
@@ -197,7 +196,7 @@ func TestEstimatePosition(t *testing.T) {
197
196
expectedLiquidationBestVolumeOnly string
198
197
}{
199
198
{
200
- markPrice : markPrice ,
199
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
201
200
openVolume : 0 ,
202
201
avgEntryPrice : 0 ,
203
202
orders : []* v2.OrderInfo {
@@ -214,7 +213,7 @@ func TestEstimatePosition(t *testing.T) {
214
213
marginMode : vega .MarginMode_MARGIN_MODE_CROSS_MARGIN ,
215
214
},
216
215
{
217
- markPrice : markPrice ,
216
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
218
217
openVolume : 0 ,
219
218
avgEntryPrice : 0 ,
220
219
orders : []* v2.OrderInfo {
@@ -232,7 +231,7 @@ func TestEstimatePosition(t *testing.T) {
232
231
marginFactor : 0.1 ,
233
232
},
234
233
{
235
- markPrice : markPrice ,
234
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
236
235
openVolume : int64 (10 * math .Pow10 (positionDecimalPlaces )),
237
236
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
238
237
orders : []* v2.OrderInfo {
@@ -249,7 +248,7 @@ func TestEstimatePosition(t *testing.T) {
249
248
marginMode : vega .MarginMode_MARGIN_MODE_CROSS_MARGIN ,
250
249
},
251
250
{
252
- markPrice : markPrice ,
251
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
253
252
openVolume : int64 (- 10 * math .Pow10 (positionDecimalPlaces )),
254
253
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
255
254
orders : []* v2.OrderInfo {
@@ -267,7 +266,7 @@ func TestEstimatePosition(t *testing.T) {
267
266
marginFactor : 0.5 ,
268
267
},
269
268
{
270
- markPrice : markPrice ,
269
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
271
270
openVolume : int64 (- 10 * math .Pow10 (positionDecimalPlaces )),
272
271
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
273
272
orders : []* v2.OrderInfo {
@@ -290,7 +289,7 @@ func TestEstimatePosition(t *testing.T) {
290
289
marginMode : vega .MarginMode_MARGIN_MODE_CROSS_MARGIN ,
291
290
},
292
291
{
293
- markPrice : markPrice ,
292
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
294
293
openVolume : int64 (- 10 * math .Pow10 (positionDecimalPlaces )),
295
294
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
296
295
orders : []* v2.OrderInfo {
@@ -314,7 +313,7 @@ func TestEstimatePosition(t *testing.T) {
314
313
marginFactor : 0.3 ,
315
314
},
316
315
{
317
- markPrice : markPrice ,
316
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
318
317
openVolume : int64 (10 * math .Pow10 (positionDecimalPlaces )),
319
318
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
320
319
orders : []* v2.OrderInfo {
@@ -361,7 +360,7 @@ func TestEstimatePosition(t *testing.T) {
361
360
marginMode : vega .MarginMode_MARGIN_MODE_CROSS_MARGIN ,
362
361
},
363
362
{
364
- markPrice : markPrice ,
363
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
365
364
openVolume : - int64 (10 * math .Pow10 (positionDecimalPlaces )),
366
365
avgEntryPrice : 111.1 * math .Pow10 (marketDecimals ),
367
366
orders : []* v2.OrderInfo {
@@ -409,13 +408,13 @@ func TestEstimatePosition(t *testing.T) {
409
408
marginFactor : 0.1 ,
410
409
},
411
410
{
412
- markPrice : markPrice ,
411
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
413
412
openVolume : 0 ,
414
413
avgEntryPrice : 0 ,
415
414
orders : []* v2.OrderInfo {
416
415
{
417
416
Side : entities .SideBuy ,
418
- Price : fmt . Sprintf ( "%f" , markPrice ),
417
+ Price : floatToStringWithDp ( 123.456 , marketDecimals ),
419
418
Remaining : uint64 (1 * math .Pow10 (positionDecimalPlaces )),
420
419
IsMarketOrder : false ,
421
420
},
@@ -428,7 +427,7 @@ func TestEstimatePosition(t *testing.T) {
428
427
expectedCollIncBest : "3703680000" ,
429
428
},
430
429
{
431
- markPrice : markPrice ,
430
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
432
431
openVolume : 0 ,
433
432
avgEntryPrice : 0 ,
434
433
orders : []* v2.OrderInfo {
@@ -447,9 +446,9 @@ func TestEstimatePosition(t *testing.T) {
447
446
expectedCollIncBest : "3703680000" ,
448
447
},
449
448
{
450
- markPrice : markPrice ,
449
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
451
450
openVolume : int64 (1 * math .Pow10 (positionDecimalPlaces )),
452
- avgEntryPrice : markPrice ,
451
+ avgEntryPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
453
452
orders : []* v2.OrderInfo {},
454
453
marginAccountBalance : 0 ,
455
454
generalAccountBalance : 0 ,
@@ -459,13 +458,13 @@ func TestEstimatePosition(t *testing.T) {
459
458
expectedCollIncBest : "3703680000" ,
460
459
},
461
460
{
462
- markPrice : markPrice ,
461
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
463
462
openVolume : 0 ,
464
463
avgEntryPrice : 0 ,
465
464
orders : []* v2.OrderInfo {
466
465
{
467
466
Side : entities .SideSell ,
468
- Price : fmt . Sprintf ( "%f" , markPrice ),
467
+ Price : floatToStringWithDp ( 123.456 , marketDecimals ),
469
468
Remaining : uint64 (1 * math .Pow10 (positionDecimalPlaces )),
470
469
IsMarketOrder : false ,
471
470
},
@@ -478,7 +477,7 @@ func TestEstimatePosition(t *testing.T) {
478
477
expectedCollIncBest : "3703680000" ,
479
478
},
480
479
{
481
- markPrice : markPrice ,
480
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
482
481
openVolume : 0 ,
483
482
avgEntryPrice : 0 ,
484
483
orders : []* v2.OrderInfo {
@@ -497,9 +496,9 @@ func TestEstimatePosition(t *testing.T) {
497
496
expectedCollIncBest : "3703680000" ,
498
497
},
499
498
{
500
- markPrice : markPrice ,
499
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
501
500
openVolume : - int64 (1 * math .Pow10 (positionDecimalPlaces )),
502
- avgEntryPrice : markPrice ,
501
+ avgEntryPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
503
502
orders : []* v2.OrderInfo {},
504
503
marginAccountBalance : 0 ,
505
504
generalAccountBalance : 0 ,
@@ -509,9 +508,9 @@ func TestEstimatePosition(t *testing.T) {
509
508
expectedCollIncBest : "3703680000" ,
510
509
},
511
510
{
512
- markPrice : markPrice ,
511
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
513
512
openVolume : - int64 (1 * math .Pow10 (positionDecimalPlaces )),
514
- avgEntryPrice : markPrice ,
513
+ avgEntryPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
515
514
orders : []* v2.OrderInfo {
516
515
{
517
516
Side : entities .SideBuy ,
@@ -528,9 +527,9 @@ func TestEstimatePosition(t *testing.T) {
528
527
expectedCollIncBest : "0" ,
529
528
},
530
529
{
531
- markPrice : markPrice ,
530
+ markPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
532
531
openVolume : int64 (1 * math .Pow10 (positionDecimalPlaces )),
533
- avgEntryPrice : markPrice ,
532
+ avgEntryPrice : 123.456 * math . Pow10 ( marketDecimals ) ,
534
533
orders : []* v2.OrderInfo {
535
534
{
536
535
Side : entities .SideSell ,
@@ -558,6 +557,50 @@ func TestEstimatePosition(t *testing.T) {
558
557
marginFactor : 0.01277 ,
559
558
expectedLiquidationBestVolumeOnly : "6781300000" ,
560
559
},
560
+ {
561
+ markPrice : 3225 * math .Pow10 (marketDecimals ),
562
+ openVolume : 0 ,
563
+ avgEntryPrice : 0 ,
564
+ orders : []* v2.OrderInfo {
565
+ {
566
+ Side : entities .SideSell ,
567
+ Price : floatToStringWithDp (5000 , marketDecimals ),
568
+ Remaining : uint64 (1 * math .Pow10 (positionDecimalPlaces )),
569
+ IsMarketOrder : false ,
570
+ },
571
+ },
572
+ marginAccountBalance : 0 ,
573
+ generalAccountBalance : 0 ,
574
+ orderMarginAccountBalance : 0 ,
575
+ marginMode : vega .MarginMode_MARGIN_MODE_ISOLATED_MARGIN ,
576
+ marginFactor : 0.1 ,
577
+ expectedCollIncBest : "50000000000" ,
578
+ },
579
+ {
580
+ markPrice : 3225 * math .Pow10 (marketDecimals ),
581
+ openVolume : 0 ,
582
+ avgEntryPrice : 0 ,
583
+ orders : []* v2.OrderInfo {
584
+ {
585
+ Side : entities .SideSell ,
586
+ Price : floatToStringWithDp (5000 , marketDecimals ),
587
+ Remaining : uint64 (1 * math .Pow10 (positionDecimalPlaces )),
588
+ IsMarketOrder : false ,
589
+ },
590
+ {
591
+ Side : entities .SideBuy ,
592
+ Price : floatToStringWithDp (2500 , marketDecimals ),
593
+ Remaining : uint64 (2 * math .Pow10 (positionDecimalPlaces )),
594
+ IsMarketOrder : false ,
595
+ },
596
+ },
597
+ marginAccountBalance : 0 ,
598
+ generalAccountBalance : 0 ,
599
+ orderMarginAccountBalance : 50000000000 ,
600
+ marginMode : vega .MarginMode_MARGIN_MODE_ISOLATED_MARGIN ,
601
+ marginFactor : 0.1 ,
602
+ expectedCollIncBest : "0" ,
603
+ },
561
604
}
562
605
for i , tc := range testCases {
563
606
mktData := entities.MarketData {
@@ -676,8 +719,8 @@ func TestEstimatePosition(t *testing.T) {
676
719
adjNotional := (tc .avgEntryPrice * priceFactor * float64 (tc .openVolume )/ math .Pow10 (positionDecimalPlaces ) + marketOrderNotional )
677
720
678
721
requiredPositionMargin := math .Abs (adjNotional ) * tc .marginFactor
679
- requiredOrderMargin := getLimitOrderNotional (t , tc .orders , priceFactor , positionDecimalPlaces ) * tc .marginFactor
680
- expectedCollIncBest = requiredPositionMargin + requiredOrderMargin - tc .marginAccountBalance - tc .orderMarginAccountBalance
722
+ requiredBuyOrderMargin , requireSellOrderMargin := getLimitOrderNotionalScaledByMarginFactor (t , tc .orders , priceFactor , positionDecimalPlaces , tc .marginFactor )
723
+ expectedCollIncBest = requiredPositionMargin + max ( requiredBuyOrderMargin , requireSellOrderMargin ) - tc .marginAccountBalance - tc .orderMarginAccountBalance
681
724
expectedCollIncWorst = expectedCollIncBest
682
725
683
726
expectedPosMarginIncrease = max (0 , requiredPositionMargin - tc .marginAccountBalance )
@@ -804,18 +847,23 @@ func (s *mockStream) Context() context.Context { return context.Backgroun
804
847
func (s * mockStream ) SendMsg (m interface {}) error { return nil }
805
848
func (s * mockStream ) RecvMsg (m interface {}) error { return nil }
806
849
807
- func getLimitOrderNotional (t * testing.T , orders []* v2.OrderInfo , priceFactor float64 , positionDecimals int ) float64 {
850
+ func getLimitOrderNotionalScaledByMarginFactor (t * testing.T , orders []* v2.OrderInfo , priceFactor float64 , positionDecimals int , marginFactor float64 ) ( float64 , float64 ) {
808
851
t .Helper ()
809
- notional := 0.0
852
+ buyNotional , sellNotional := 0.0 , 0.0
810
853
for _ , o := range orders {
811
854
if o .IsMarketOrder {
812
855
continue
813
856
}
814
857
price , err := strconv .ParseFloat (o .Price , 64 )
815
858
require .NoError (t , err )
816
- notional += price * priceFactor * float64 (o .Remaining ) / math .Pow10 (positionDecimals )
859
+ if o .Side == entities .SideBuy {
860
+ buyNotional += price * priceFactor * float64 (o .Remaining ) / math .Pow10 (positionDecimals )
861
+ }
862
+ if o .Side == entities .SideSell {
863
+ sellNotional += price * priceFactor * float64 (o .Remaining ) / math .Pow10 (positionDecimals )
864
+ }
817
865
}
818
- return notional
866
+ return buyNotional * marginFactor , sellNotional * marginFactor
819
867
}
820
868
821
869
func getMarketOrderNotional (marketObservable float64 , orders []* v2.OrderInfo , priceFactor float64 , positionDecimals int ) float64 {
0 commit comments