Calculating spread and skew prices
This section describes the formulas used to spread and skew incoming prices. The spreader can be used in two different modes of operation, Top of Book or VWAP.
Top of Book
To calculate using Top of the Book mode, follow these steps:
1. Check that the bid and ask price are not 0, if yes then the price and the quantity for both the sides is set to 0.
2. To spread direction towards bid:
a. Check whether bid and ask quantities are greater than the spreadMinQtyLimit. If either is not, the price and quantity for both the sides are set to 0, if both are greater than the spreadMinQtyLimit, the prices are spread.
b. Spread the prices:
spread bid price = mathUtil.subtract(bid price,
((mathUtil.subtract(ask price,bid price)*(spread percentage/100))/2)
spread ask price = mathUtil.add(ask price,(mathUtil.subtract
(ask price,bid price)*(spread percentage/100))/2)
c. Check whether bid and ask quantities exceed spreadMaxQtyLimit. If yes, the exceeding quantity is set to the spreadMaxQuantityLimit parameter value.
3. To skew the prices towards bid:
skewed bid price = mathUtil.subtract(spread bid price,
mathUtil.subtract(spread ask price, spread bid price )
*skew percentage /100)
skewed ask price = mathUtil.substract(spread ask price,
mathUtil.subtract(spread ask price, spread bid price )
*skew percentage /100)
4. To spread direction towards ask:
a. Check whether bid and ask quantities are greater than the spreadMinQtyLimit. If either of them is not, the price and quantity for both the sides are set to 0, if both are greater than the spreadMinQtyLimit, the prices are spread.
b. Spread the prices:
spread bid price =
mathUtil.subtract(bid price,(mathUtil.subtract(ask price,
bid price)*spread percentage /100)/2
spread ask price =
mathUtil.add(ask price,(mathUtil.subtract(ask price,
bid price)*spread percentage/100)/2
c. Check that the bid and ask quantities do not exceed spreadMaxQtyLimit. If yes, the exceeding quantity is set to the spreadMaxQtyLimit parameter value.
5. To skew the prices towards bid:
skewed bid price = mathUtil.add(spread bid price,
mathUtil.subtract(spread ask price,spread bid price)*skew percentage/ 100)
skewed ask price = mathUtil.add (spread ask price,
mathUtil.subtract(spread ask price,spread bid price)*skew percentage/ 100)
6. Skew direction is set off:
a. Check whether bid and ask quantities are greater than the spreadMinQtyLimit. If either of them is not, the price and quantity for both the sides are set to 0, if both the quantities are greater than the spreadMinQtyLimit then the prices are spread.
b. Spread the price as following :
spreaded bid price =
mathUtil.subtract(bid price, (mathUtil.subtract(ask price,
bid price)*spread percentage /100)/2)
spreaded ask price =
mathUtil.add(ask price, (mathUtil.subtract(ask price,
bid price)*spread percentage/100)/2)
c. Check that the bid and ask quantities do not exceed spreadMaxQtyLimit. If they do, the exceeding quantity is set to the spreadMaxQtyLimit parameter value.
7. If calculated spreaded bid and ask prices are greater than 0 adjust the prices using the spreadAdjuster and the skewAdjuster parameters:
Bid price = spreaded Bid price -
(0.00001 * 5.0 * spreadAdjuster.toFloat())
Ask price = spreaded Ask price +
(0.00001 * 5.0 * spreadAdjuster.toFloat())
Bid price = spreaded Bid price +
(0.00001 * 5.0 * skewAdjuster.toFloat())
Ask price =
spreaded Ask price + (0.00001 * 5.0 * skewAdjuster.toFloat())
8. If the value of the parameter minBBASpread is not 0 then check that the applied spread to the bid and ask prices is appropriate.
spread = Ask price - Bid price
minSpread = minBBASpread / base.pow(decimalPlaces.toFloat()
Now if the minSpread and spread is greater than 0 and spread is less than the minSpread then the bid and ask prices are calculated as following:
Bid price = (Bid Price + Ask price)/2 - (minSpread /2)
Ask price = (Bid Price + Ask price)/2 + (minSpread /2)
9. Finally, adjust the bid and ask prices:
Bid price = round(10.0.pow(-1.0 * decimalPlaces.toFloat()), true, Bid price)
Ask price = round(10.0.pow(-1.0 * decimalPlaces.toFloat()), false, Ask price)
Reverting Top of the Book
Reverting Prices from the Spreaded/Skewed Prices
Spread and/or skewed prices can be reverted back to original values using the parameters provided. The only mode which supports reverting the prices is Top of the Book and the prices cannot be reverted if either the bid or the ask price is 0.
The calculations are as follows:
1. Skew direction is towards bid :
unskewed bid price = mathUtil.add(skewed bid price, mathUtil.subtract
( skewed ask price, skewed bid price*skew percentage/100)
unskewed ask price = mathUtil.add(skewed ask price, mathUtil.subtract
( skewed bid price, skewed ask price*skew percentage/100)
2. Skew direction is towards ask:
unskewed bid price = mathUtil.subtract(skewed bid price,
mathUtil.subtract(skewed ask price, skewed bid price)*
skew percentage/100)
unskewed ask price = mathUtil.subtract(skewed ask price,
mathUtil.subtract(skewed bid price,
skewed ask price*skew percentage/100)
3. Revert spread:
Original bid price = mathUtil.add(unskewed bid price,
mathUtil.subtract(unskewed ask price, unskewed bid price) /
mathUtil.add(1.0, spread percentage/100)*spread percentage/200.0)
Original ask price = mathUtil.subtract(unskewed ask price,
mathUtil.subtract (unskewed ask price, unskewed bid price) /
mathUtil.add(1.0 , spread percentage/100)* spread percentage/200.0)
VWAP
To spread or skew using VWAP mode, follow these steps:
1. Iterate over the bid side and keep a counter for the quantity and if the total quantity is less than the vwapVol then:
mathUtil.add (Total bid price, quantity.toFloat() * bid price)
When the total quantity becomes greater than the vwapVol, break the loop and the total bid price is calculated as:
mathUtil.add(Total bid price, mathUtil.subtract(bid quantity,
mathUtil.subtract(quantity counter, VwapVol)).toFloat()*
bid price)
2. If the available liquidity for the bid side is less than the specified VWAP volume, the VWAP price and quantity is set to 0. Otherwise, the VWAP bid price is calculated as:
VWAP bid price = Total bid price / vwapVol.toFloat()
3. Iterate over the ask side and keep a counter for the quantity and if the total quantity is less than the vwapVol, then:
Total ask price = quantity.toFloat() * ask price
When the total quantity becomes greater than the vwapVol, break the loop and the total ask price is calculated as:
mathUtil.add(Total ask price, (mathUtil.subtract(ask quantity,
(mathUtil.subtract(quantity counter, VwapVol)).toFloat()* ask price)
4. If the available liquidity for the ask side is less than the specified VWAP volume then the VWAP the price and quantity is set to 0 otherwise the VWAP ask price is calculated as:
VWAP ask price = Total ask price / vwapVol.toFloat()
5. Check that the bid and ask prices are not 0, if yes then the price and quantity for both the sides is set to 0.
6. Skew direction towards bid:
a. Check the bid and ask quantities are greater than the spreadMinQtyLimit. If either of them is not then the price and quantity for both the sides are set to 0, if both the quantities are greater than the spreadMinQtyLimit then the prices are spread.
b. Spread the prices as following :
spread bid price = mathUtil.subtract(bid price,
(mathUtil.subtract(ask price, bid price)
*spread percentage /100)/2)
spread ask price = mathUtil.add(ask price,
(mathUtil.subtract(ask price, bid price)*spread
percentage/100)/2)
c. Check the bid and ask quantities do not exceed spreadMaxQtyLimit. If yes, the exceeding quantity is set to the spreadMaxQtyLimit parameter value.
7. Skew the prices towards bid:
Skewed bid price = mathUtil.subtract(spread bid price,
(mathUtil.subtract(spread ask price, spread bid price )*
skew percentage / 100)
Skewed ask price = mathUtil.subtract(spread ask price,
(mathUtil.subtract(spread ask price, spread bid price )*
skew percentage / 100)
8. Skew direction towards ask:
a. Check the bid and ask quantities are greater than the spreadMinQtyLimit. If either of them is not then the price and quantity for both the sides are set to 0, if both the quantities are greater than the spreadMinQtyLimit then the prices are spread.
b. Spread the price as following:
spread bid price = mathUtil.subtract(
bid price, (mathUtil.subtract(ask price, bid price)
*spread percentage/100)/2)
spread ask price = mathUtil.add(ask price,(mathUtil.subtract(
ask price, bid price)*spread percentage/100)/2
c. Check that the bid and ask quantities do not exceed spreadMaxQtyLimit. If yes, the exceeding quantity is set to the spreadMaxQtyLimit parameter value.
9. Skew the prices toward ask:
skewed bid price = mathUtil.add(spread bid price, mathUtil.subtract(
spread ask price, spread bid price)*skew percentage/100)
b. skewed ask price =
mathUtil.add(spread ask price, mathUtil.subtract(
spread ask price, spread bid price)*skew percentage/100)
10. Skew direction is set off:
a. Check the bid and ask quantities are greater than the spreadMinQtyLimit. If either of them is not then the price and quantity for both the sides are set to 0, if both the quantities are greater than the spreadMinQtyLimit then the prices are spread.
b. Spread the price as following :
spread bid price = mathUtil.subtract(bid price,
(mathUtil.subtract(ask price, bid price)*spread
percentage/100)/2)
spread ask price = mathUtil.add(ask price,
(mathUtil.subtract(ask price, bid price)*spread
percentage/100)/2)
c. Check that the bid and ask quantities do not exceed spreadMaxQtyLimit. If they do then the exceeding quantity is set to the spreadMaxQtyLimit parameter value.