Skip to content

Commit

Permalink
update 'squeeze price' to 'margin call order price'
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangxn committed Apr 26, 2022
1 parent 155c606 commit 4c0a13f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
8 changes: 5 additions & 3 deletions app/components/Exchange/ExchangeHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ export default class ExchangeHeader extends React.Component {
)}
ready={marketReady}
className="hide-order-6 is-call"
price={feedPrice.getSqueezePrice({
real: true
})}
price={feedPrice.getMarginCallOrderPrice(
{
real: true
}
)}
quote={quoteAsset}
base={baseAsset}
market={marketID}
Expand Down
70 changes: 53 additions & 17 deletions app/lib/common/MarketClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class Price {
}

class FeedPrice extends Price {
constructor({priceObject, assets, market_base, sqr, real = false}) {
constructor({priceObject, assets, market_base, sqr, mcfr, real = false}) {
if (
!priceObject ||
typeof priceObject !== "object" ||
Expand Down Expand Up @@ -404,6 +404,7 @@ class FeedPrice extends Price {

this.sqr = parseInt(sqr, 10) / 1000;
this.inverted = inverted;
this.mcfr = parseInt(mcfr, 10) / 1000;
}

getSqueezePrice({real = false} = {}) {
Expand All @@ -427,6 +428,30 @@ class FeedPrice extends Price {
}
return this._squeeze_price;
}

// see https://github.com/bitshares/bsips/blob/master/bsip-0074.md
getMarginCallOrderPrice({real = false} = {}) {
if (!this._margin_call_order_price) {
this._margin_call_order_price = this.clone();
if (this.inverted) {
this._margin_call_order_price.base.amount = Math.floor(
this._margin_call_order_price.base.amount *
(this.sqr - this.mcfr) *
1000
);
this._margin_call_order_price.quote.amount *= 1000;
} else if (!this.inverted) {
this._margin_call_order_price.quote.amount = Math.floor(
this._margin_call_order_price.quote.amount *
(this.sqr - this.mcfr) *
1000
);
this._margin_call_order_price.base.amount *= 1000;
}
}
if (real) return this._margin_call_order_price.toReal();
return this._margin_call_order_price;
}
}

class LimitOrderCreate {
Expand Down Expand Up @@ -696,7 +721,8 @@ class CallOrder {

getPrice(squeeze = true, p = this.call_price) {
if (squeeze) {
return this.getSqueezePrice();
// return this.getSqueezePrice();
return this.getMarginCallOrderPrice();
}
if (this._real_price) {
return this._real_price;
Expand All @@ -722,6 +748,15 @@ class CallOrder {
return (this._squeeze_price = f.getSqueezePrice().toReal());
}

getMarginCallOrderPrice(f = this.feed_price) {
if (this._margin_call_order_price) {
return this._margin_call_order_price;
}
return (this._margin_call_order_price = f
.getMarginCallOrderPrice()
.toReal());
}

isMarginCalled() {
if (this.is_prediction_market) return false;
return this.isBid()
Expand All @@ -740,8 +775,8 @@ class CallOrder {
sellPrice(squeeze = true) {
if (squeeze) {
return this.isBid()
? this.feed_price.getSqueezePrice()
: this.feed_price.getSqueezePrice().invert();
? this.feed_price.getMarginCallOrderPrice()
: this.feed_price.getMarginCallOrderPrice().invert();
}
return this.call_price;
}
Expand Down Expand Up @@ -797,8 +832,8 @@ class CallOrder {
_getMatchPrice() {
return (
(this.inverted
? this.getSqueezePrice()
: parseFloat((1 / this.getSqueezePrice()).toFixed(8))) *
? this.getMarginCallOrderPrice()
: parseFloat((1 / this.getMarginCallOrderPrice()).toFixed(8))) *
this.precisionsRatio
);
}
Expand Down Expand Up @@ -859,7 +894,8 @@ class CallOrder {
return (this._for_sale = this.max_collateral_to_sell);
}
return (this._for_sale = this.amountToReceive().times(
this.feed_price.getSqueezePrice(),
// this.feed_price.getSqueezePrice(),
this.feed_price.getMarginCallOrderPrice(),
isBid
));
}
Expand Down Expand Up @@ -899,25 +935,25 @@ class CallOrder {
let orderDebt = order.iSum
? order.debt
: orderUseCR
? order.max_debt_to_cover.getAmount()
: order.amountToReceive().getAmount();
? order.max_debt_to_cover.getAmount()
: order.amountToReceive().getAmount();
let newOrderDebt = newOrder.iSum
? newOrder.debt
: newOrderUseCR
? newOrder.max_debt_to_cover.getAmount()
: newOrder.amountToReceive().getAmount();
? newOrder.max_debt_to_cover.getAmount()
: newOrder.amountToReceive().getAmount();

/* Determine which collateral values to use */
let orderCollateral = order.iSum
? order.collateral
: orderUseCR
? order.max_collateral_to_sell.getAmount()
: order.amountForSale().getAmount();
? order.max_collateral_to_sell.getAmount()
: order.amountForSale().getAmount();
let newOrderCollateral = newOrder.iSum
? newOrder.collateral
: newOrderUseCR
? newOrder.max_collateral_to_sell.getAmount()
: newOrder.amountForSale().getAmount();
? newOrder.max_collateral_to_sell.getAmount()
: newOrder.amountForSale().getAmount();

newOrder.debt = newOrderDebt + orderDebt;
newOrder.collateral = newOrderCollateral + orderCollateral;
Expand Down Expand Up @@ -1232,8 +1268,8 @@ class FillOrder {
this.className = this.isCall
? "orderHistoryCall"
: this.isBid
? "orderHistoryBid"
: "orderHistoryAsk";
? "orderHistoryBid"
: "orderHistoryAsk";
this.time = fill.time && new Date(utils.makeISODateString(fill.time));
this.block = fill.block;
this.account = fill.op.account || fill.op.account_id;
Expand Down
11 changes: 10 additions & 1 deletion app/stores/MarketsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,14 @@ class MarketsStore {
"current_feed",
"maximum_short_squeeze_ratio"
]);
let mcfr = this[
this.invertedCalls ? "baseAsset" : "quoteAsset"
].getIn([
"bitasset",
"options",
"extensions",
"margin_call_fee_ratio"
]);

this.is_prediction_market = this[
this.invertedCalls ? "baseAsset" : "quoteAsset"
Expand Down Expand Up @@ -791,6 +799,7 @@ class MarketsStore {
priceObject: feedPriceRaw,
market_base: this.quoteAsset.get("id"),
sqr,
mcfr,
assets
});

Expand Down Expand Up @@ -1252,7 +1261,7 @@ class MarketsStore {
let callsAsBids = this.marketData.calls[0].isBid();
this.marketData.calls.forEach(order => {
calls.push([
order.getSqueezePrice(),
order.getMarginCallOrderPrice(),
order[
order.isBid() ? "amountToReceive" : "amountForSale"
]().getAmount({real: true})
Expand Down

0 comments on commit 4c0a13f

Please sign in to comment.