Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed spread calculation #108

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bancor/carbon-sdk",
"type": "module",
"source": "src/index.ts",
"version": "0.0.86-DEV",
"version": "0.0.87-DEV",
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
54 changes: 25 additions & 29 deletions src/strategy-management/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,34 +323,28 @@ export function calculateOverlappingPriceRanges(
buyPriceMarginal: Decimal;
sellPriceLow: Decimal;
sellPriceMarginal: Decimal;
spread: Decimal;
} {
const totalPriceRange = sellPriceHigh.minus(buyPriceLow);

const spread = totalPriceRange.mul(spreadPercentage).div(100);

const buyPriceHigh = sellPriceHigh.minus(spread);

const sellPriceLow = buyPriceLow.plus(spread);
const spreadFactor = spreadPercentage.plus(1);
const buyPriceHigh = sellPriceHigh.div(spreadFactor);
const sellPriceLow = buyPriceLow.mul(spreadFactor);

// buy marginal price is the market price minus 0.5 spread. But it can never be lower than buyPriceLow
const buyPriceMarginal = Decimal.max(
buyPriceLow,
marketPrice.minus(spread.div(2))
marketPrice.div(spreadFactor.sqrt())
);

// sell marginal price is the market price plus 0.5 spread. But ir can never be higher than sellPriceHigh
const sellPriceMarginal = Decimal.min(
sellPriceHigh,
marketPrice.plus(spread.div(2))
marketPrice.mul(spreadFactor.sqrt())
);

return {
buyPriceHigh,
buyPriceMarginal,
sellPriceLow,
sellPriceMarginal,
spread,
};
}

Expand All @@ -364,25 +358,26 @@ export function calculateOverlappingSellBudget(
// zero buy budget means zero sell budget
if (buyBudget.isZero()) return new Decimal(0);

const { buyPriceHigh, spread } = calculateOverlappingPriceRanges(
buyPriceLow,
sellPriceHigh,
marketPrice,
spreadPercentage
);
const { buyPriceHigh, sellPriceMarginal, buyPriceMarginal } =
calculateOverlappingPriceRanges(
buyPriceLow,
sellPriceHigh,
marketPrice,
spreadPercentage
);

// if buy range takes the entire range then there's zero sell budget
if (marketPrice.minus(spread.div(2)).gte(buyPriceHigh)) return new Decimal(0);
if (sellPriceMarginal.gte(sellPriceHigh)) return new Decimal(0);

// if buy range is zero there's no point to this call
if (marketPrice.minus(spread.div(2)).lte(buyPriceLow)) {
if (buyPriceMarginal.lte(buyPriceLow)) {
throw new Error(
'calculateOverlappingSellBudget called with zero buy range and non zero buy budget'
);
}

const buyPriceRange = buyPriceHigh.minus(buyPriceLow);
const buyLowRange = marketPrice.minus(buyPriceLow).minus(spread.div(2));
const buyLowRange = buyPriceMarginal.minus(buyPriceLow);
const buyLowBudgetRatio = buyLowRange.div(buyPriceRange);
const buyOrderYint = new Decimal(buyBudget).div(buyLowBudgetRatio);
const geoMeanOfBuyRange = buyPriceLow.mul(buyPriceHigh).sqrt();
Expand All @@ -403,25 +398,26 @@ export function calculateOverlappingBuyBudget(
// zero sell budget means zero buy budget
if (sellBudget.isZero()) return new Decimal(0);

const { sellPriceLow, spread } = calculateOverlappingPriceRanges(
buyPriceLow,
sellPriceHigh,
marketPrice,
spreadPercentage
);
const { sellPriceLow, sellPriceMarginal, buyPriceMarginal } =
calculateOverlappingPriceRanges(
buyPriceLow,
sellPriceHigh,
marketPrice,
spreadPercentage
);

// if sell range takes the entire range then there's zero buy budget
if (marketPrice.plus(spread.div(2)).lte(sellPriceLow)) return new Decimal(0);
if (buyPriceMarginal.lte(buyPriceLow)) return new Decimal(0);

// if sell range is zero there's no point to this call
if (marketPrice.plus(spread.div(2)).gte(sellPriceHigh)) {
if (sellPriceMarginal.gte(sellPriceHigh)) {
throw new Error(
'calculateOverlappingBuyBudget called with zero sell range and non zero sell budget'
);
}

const sellPriceRange = sellPriceHigh.minus(sellPriceLow);
const sellHighRange = sellPriceHigh.minus(marketPrice).minus(spread.div(2));
const sellHighRange = sellPriceHigh.minus(sellPriceMarginal);
const sellHighBudgetRatio = sellHighRange.div(sellPriceRange);
const sellOrderYint = new Decimal(sellBudget).div(sellHighBudgetRatio);
const geoMeanOfSellRange = sellPriceHigh.mul(sellPriceLow).sqrt();
Expand Down
26 changes: 13 additions & 13 deletions tests/toolkit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ describe('Toolkit', () => {
'quoteToken',
'1500',
'2000',
'1600',
'0.1'
'1845',
'0.01'
);
expect(result).to.deep.equal({
buyPriceHigh: '1999.5',
buyPriceMarginal: '1599.75',
sellPriceLow: '1500.5',
sellPriceMarginal: '1600.25',
buyPriceHigh: '1980.198019801980198019',
buyPriceMarginal: '1835.843615937429955302',
sellPriceLow: '1515',
sellPriceMarginal: '1854.202052096804254855',
});
});
it('should calculate strategy sell budget', async () => {
Expand All @@ -121,23 +121,23 @@ describe('Toolkit', () => {
'baseToken',
'1500',
'2000',
'1600',
'0.1',
'1845',
'0.01',
'100'
);
expect(result).to.equal('0.231403132822275197');
expect(result).to.equal('0.024939801923642185');
});
it('should calculate strategy buy budget', async () => {
const toolkit = new Toolkit(apiMock, cacheMock, () => 6);
const result = await toolkit.calculateOverlappingStrategyBuyBudget(
'quoteToken',
'1500',
'2000',
'1600',
'0.1',
'0.231403132822275197'
'1845',
'0.01',
'0.024939801923642185'
);
expect(result).to.equal('100.029169');
expect(result).to.equal('100.999999');
});
});

Expand Down
38 changes: 13 additions & 25 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,20 @@ describe('utils', () => {
quoteTokenDecimals: 6,
buyPriceLow: new Decimal('1500'),
sellPriceHigh: new Decimal('2000'),
marketPrice: new Decimal('1600'),
spreadPercentage: new Decimal('0.1'),
spread: new Decimal('0.5'),
marketPrice: new Decimal('1845'),
spreadPercentage: new Decimal('0.01'),
buyBudget: new Decimal('100'),
buyPriceHigh: new Decimal('1999.5'),
sellPriceLow: new Decimal('1500.5'),
buyPriceMarginal: new Decimal('1599.75'),
sellPriceMarginal: new Decimal('1600.25'),
sellBudget: new Decimal('0.231403132822275197'),
},
{
baseTokenDecimals: 18,
quoteTokenDecimals: 6,
buyPriceLow: new Decimal('0.005'),
sellPriceHigh: new Decimal('0.03'),
marketPrice: new Decimal('0.007241'),
spreadPercentage: new Decimal('0.1'),
spread: new Decimal('0.000025'),
buyBudget: new Decimal('3090.190579'),
buyPriceHigh: new Decimal('0.029975'),
sellPriceLow: new Decimal('0.005025'),
buyPriceMarginal: new Decimal('0.0072285'),
sellPriceMarginal: new Decimal('0.0072535'),
sellBudget: new Decimal('2576455.281630354877824211'),
buyPriceHigh: new Decimal(
'1980.19801980198019801980198019801980198019801980198019801980198019801980198019801980198019801980198'
),
sellPriceLow: new Decimal('1515'),
buyPriceMarginal: new Decimal(
'1835.843615937429955302430075647665154941937455663234267436323585007036269526077757760470083423519019'
),
sellPriceMarginal: new Decimal(
'1854.202052096804254855454376404141806491356830219866610110686820857106632221338535338074784257754209'
),
sellBudget: new Decimal('0.024939801923642185'),
},
];

Expand All @@ -60,7 +50,6 @@ describe('utils', () => {
sellPriceHigh,
marketPrice,
spreadPercentage,
spread,
buyBudget,
buyPriceHigh,
sellPriceLow,
Expand Down Expand Up @@ -92,7 +81,6 @@ describe('utils', () => {
expect(prices.sellPriceMarginal.toString()).to.equal(
sellPriceMarginal.toString()
);
expect(prices.spread.toString()).to.equal(spread.toString());

const sellRes = calculateOverlappingSellBudget(
buyPriceLow,
Expand Down
Loading