lodash#maxBy JavaScript Examples

The following examples show how to use lodash#maxBy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From gobench with Apache License 2.0 6 votes vote down vote up
getOfflineMetricData = async (metrics, timeRange = 3600, timestamp, isRealtime) => {
  const now = new Date().getTime()
  const fromTime = Math.round((now - timestamp) / 1000) < timeRange ? timestamp : (now - (timeRange * 1000))

  const metricsData = metrics.map(async m => {
    let mData
    if (isRealtime) {
      mData = await getMetricData(m.id, m.type, fromTime, now)
    } else {
      mData = await getMetricData(m.id, m.type)
    }
    if (mData.length === 0) {
      return {
        ...m,
        lastTimestamp: timestamp,
        chartData: {
          name: m.title,
          data: []
        }
      }
    }
    const lastTimestamp = get(maxBy(mData, m => m.time), 'time')
    return {
      ...m,
      lastTimestamp,
      chartData: {
        name: m.title,
        data: m.type === METRIC_TYPE.HISTOGRAM ? mData : getChartData(m.type, mData)
      }
    }
  })
  return await Promise.all(metricsData)
    .then(rs => rs)
    .catch(err => err)
}
Example #2
Source File: dataProvider.js    From acy-dex-interface with MIT License 4 votes vote down vote up
export function useTradersData({ from = FIRST_DATE_TS, to = NOW_TS, chainName = "arbitrum" } = {}) {
  const [closedPositionsData, loading, error] = useGraph(`{
    tradingStats(
      first: 1000
      orderBy: timestamp
      orderDirection: desc
      where: { period: "daily", timestamp_gte: ${from}, timestamp_lte: ${to} }
    ) {
      timestamp
      profit
      loss
      profitCumulative
      lossCumulative
      longOpenInterest
      shortOpenInterest
    }
  }`, { chainName })
  const [feesData] = useFeesData({ from, to, chainName })
  const marginFeesByTs = useMemo(() => {
    if (!feesData) {
      return {}
    }

    let feesCumulative = 0
    return feesData.reduce((memo, { timestamp, margin: fees}) => {
      feesCumulative += fees
      memo[timestamp] = {
        fees,
        feesCumulative
      }
      return memo
    }, {})
  }, [feesData])

  let ret = null
  const data = closedPositionsData ? sortBy(closedPositionsData.tradingStats, i => i.timestamp).map(dataItem => {
    const longOpenInterest = dataItem.longOpenInterest / 1e30
    const shortOpenInterest = dataItem.shortOpenInterest / 1e30
    const openInterest = longOpenInterest + shortOpenInterest

    const fees = (marginFeesByTs[dataItem.timestamp]?.fees || 0)
    const feesCumulative = (marginFeesByTs[dataItem.timestamp]?.feesCumulative || 0)

    const profit = dataItem.profit / 1e30
    const loss = dataItem.loss / 1e30
    const profitCumulative = dataItem.profitCumulative / 1e30
    const lossCumulative = dataItem.lossCumulative / 1e30
    const pnlCumulative = profitCumulative - lossCumulative
    const pnl = profit - loss
    return {
      longOpenInterest,
      shortOpenInterest,
      openInterest,
      profit,
      loss: -loss,
      profitCumulative,
      lossCumulative: -lossCumulative,
      pnl,
      pnlCumulative,
      timestamp: dataItem.timestamp
    }
  }) : null

  if (data) {
    const maxProfit = maxBy(data, item => item.profit)?.profit ?? 0
    const maxLoss = minBy(data, item => item.loss)?.loss ?? 0
    const maxProfitLoss = Math.max(maxProfit, -maxLoss)

    const maxPnl = maxBy(data, item => item.pnl)?.pnl ?? 0
    const minPnl = minBy(data, item => item.pnl)?.pnl ?? 0
    const maxCumulativePnl = maxBy(data, item => item.pnlCumulative)?.pnlCumulative ?? 0
    const minCumulativePnl = minBy(data, item => item.pnlCumulative)?.pnlCumulative ?? 0

    const profitCumulative = data[data.length - 1]?.profitCumulative ?? 0
    const lossCumulative = data[data.length - 1]?.lossCumulative ?? 0
    const stats = {
      maxProfit,
      maxLoss,
      maxProfitLoss,
      profitCumulative,
      lossCumulative,
      maxCumulativeProfitLoss: Math.max(profitCumulative, -lossCumulative),

      maxAbsOfPnlAndCumulativePnl: Math.max(
        Math.abs(maxPnl),
        Math.abs(maxCumulativePnl),
        Math.abs(minPnl),
        Math.abs(minCumulativePnl)
      ),
    }

    ret = {
      data,
      stats
    }
  }

  return [ret, loading]
}