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

Add weighted averages #306

Merged
merged 8 commits into from
Jul 4, 2023
Merged
2 changes: 1 addition & 1 deletion test/1-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ describe('API', () => {
this.timeout(120000)

const paramsArr = [
{ end, start },
{ end, start, symbol: 'tBTCUSD' },
{
end,
start: end - (10 * 60 * 60 * 1000),
Expand Down
1 change: 1 addition & 0 deletions test/4-queue-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ describe('Queue', () => {
auth,
method: 'getWeightedAveragesReportCsv',
params: {
symbol: 'tBTCUSD',
end,
start: _start,
email
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/helpers.mock-rest-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ const getMockDataOpts = () => ({
get_settings: null,
set_settings: null,
generate_token: null,
movement_info: null
movement_info: null,
weighted_averages: null
})

const createMockRESTv2SrvWithDate = (
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/mock-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,5 +758,19 @@ module.exports = new Map([
merchantName: 'Testing'
}
]
],
[
'weighted_averages',
[
321,
684407.06727697,
32.30565315,
null,
-596599.40936202,
-33.16213227,
null,
21185.36542503,
17990.3814539
]
]
])
11 changes: 10 additions & 1 deletion workers/loc.api/helpers/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,16 @@ const paramsSchemaForWeightedAveragesReportApi = {
type: 'integer'
},
symbol: {
type: ['string', 'array']
type: ['string', 'array'],
if: {
type: 'array'
},
then: {
maxItems: 1,
items: {
type: 'string'
}
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion workers/loc.api/service.report.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ class ReportService extends Api {
return Array.isArray(res) ? res : []
}

_getWeightedAveragesReportFromApi (args) {
const { auth, params } = args ?? {}

const rest = this._getREST(auth)

return rest.getWeightedAverages(params)
}

getPositionsSnapshot (space, args, cb) {
return this._responder(() => {
return this._prepareApiResponse(
Expand Down Expand Up @@ -621,7 +629,7 @@ class ReportService extends Api {

getWeightedAveragesReport (space, args, cb) {
return this._responder(async () => {
checkParams(args, 'paramsSchemaForWeightedAveragesReportApi')
checkParams(args, 'paramsSchemaForWeightedAveragesReportApi', ['symbol'])

return this._weightedAveragesReport
.getWeightedAveragesReport(args)
Expand Down
78 changes: 77 additions & 1 deletion workers/loc.api/weighted.averages.report/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ class WeightedAveragesReport {
) {
this.rService = rService
this.getDataFromApi = getDataFromApi

// Used to switch data fetching from DB for framework mode
this._isNotCalcTakenFromBfxApi = false
}

async getWeightedAveragesReport (args = {}) {
async getWeightedAveragesReport (args = {}, opts = {}) {
const {
auth = {},
params = {}
} = args ?? {}
const {
isNotCalcTakenFromBfxApi = this._isNotCalcTakenFromBfxApi
} = opts ?? {}

const {
start = 0,
Expand All @@ -33,6 +39,13 @@ class WeightedAveragesReport {
s && typeof s === 'string'
))

if (!isNotCalcTakenFromBfxApi) {
return await this._getWeightedAveragesReportFromApi({
auth,
params: { start, end, symbol }
})
}

const {
res: trades,
nextPage
Expand All @@ -50,6 +63,69 @@ class WeightedAveragesReport {
}
}

async _getWeightedAveragesReportFromApi (args) {
const limit = 100_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the lodash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS shipped a new feature called numeric separator to improve readability on numbers. The underscore _ was introduced in ES2021 as a separator in numbers and we can use it for decimal, binary, hexadecimal, and BigInt numbers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting

const symbols = args?.params?.symbol ?? []

const weightedAverages = []

// If `nextPage === true` it means that data is not consistent and need to change timeframe
let nextPage = false

/*
* The loop here is to keep previous implementation,
* amount of symbols is limited in params schema
*/
for (const symbol of symbols) {
const res = await this.getDataFromApi({
getData: (space, _args) => this.rService._getWeightedAveragesReportFromApi(_args),
args: {
auth: args?.auth ?? {},
params: { ...args?.params, limit, symbol },
notThrowError: true
},
callerName: 'WEIGHTED_AVERAGES',
eNetErrorAttemptsTimeframeMin: 10 / 60,
eNetErrorAttemptsTimeoutMs: 1000,
shouldNotInterrupt: true
})

const {
tradeCount,
sumBuyingSpent = 0,
sumBuyingAmount = 0,
sumSellingSpent = 0,
sumSellingAmount = 0,
buyingWeightedPrice = 0,
sellingWeightedPrice = 0
} = res ?? {}

if (tradeCount >= limit) {
nextPage = true
}

const cumulativeAmount = sumBuyingAmount + sumSellingAmount
const cumulativeWeightedPrice = cumulativeAmount === 0
? 0
: (sumBuyingSpent + sumSellingSpent) / cumulativeAmount

weightedAverages.push({
symbol,
buyingWeightedPrice,
buyingAmount: sumBuyingAmount,
sellingWeightedPrice,
sellingAmount: sumSellingAmount,
cumulativeWeightedPrice,
cumulativeAmount
})
}

return {
nextPage,
res: weightedAverages
}
}

async _getTrades (args) {
const limit = 2000
const start = args?.start ?? 0
Expand Down