Skip to content

Commit

Permalink
upgrade diff calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
WhidRubeld committed Sep 12, 2021
1 parent 168d24c commit e20becb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 83 deletions.
60 changes: 9 additions & 51 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
import { ICandlestick, _candlestick } from '@interfaces'
import moment from 'moment-timezone'

export const convertKline = (candlestick: _candlestick): ICandlestick => {
const [
open_time,
open,
high,
low,
close,
volume,
close_time,
quote_asset_volume,
number_of_trades,
taker_by_asset_volume,
taker_buy_quote_asset_volume,
ignore,
] = candlestick

return {
open_time,
open,
high,
low,
close,
volume,
close_time,
quote_asset_volume,
number_of_trades,
taker_by_asset_volume,
taker_buy_quote_asset_volume,
ignore,
}
}

export const toLocalTime = (p: string) => {
return moment.tz(p, 'Etc/GMT').local()
}
Expand All @@ -41,24 +8,15 @@ export const unix2time = (time: number, format: string) => {
return moment.unix(time).tz('Etc/GMT').local().format(format)
}

export const avg = (arr: number[], idx: number, range: number) => {
return sum(arr.slice(idx - range, idx)) / range
}

export const sum = (arr: number[]) => {
let len = arr.length
let num = 0
while (len--) num += Number(arr[len])
return num
}
export const ma = (data: number[], period: number) => {
if (data.length < period) {
return undefined
}

export const sma = (arr: number[], range: number) => {
const num = range || arr.length
const res = []
const len = arr.length + 1
let idx = num - 1
while (++idx < len) {
res.push(avg(arr, idx, num))
let info = [...data]
if (info.length > period) {
info = info.slice(0, period)
}
return res

return info.reduce((a, b) => a + b, 0) / period
}
34 changes: 16 additions & 18 deletions src/hooks/useTractionForce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sma } from '@helpers'
import { ma } from '@helpers'
import { _interval } from '@interfaces'
import { useMemo } from 'react'

Expand All @@ -19,33 +19,31 @@ export default function useTractionForce({
return info.map((v) => {
const { pair, results } = v
let force = null
let diff = null
let diff = false

if (results.length >= second) {
const one = sma(
results.reverse().map((t) => t.close),
const one = ma(
results.map((t) => t.close),
first
).pop()
const two = sma(
results.reverse().map((t) => t.close),
)

const two = ma(
results.map((t) => t.close),
second
).pop()
)

if (one && two) {
force = (one / two - 1) * 100
}

const prevOne = sma(
results
.reverse()
.slice(1, first + 1)
.map((t) => t.close),
const prevOne = ma(
results.slice(1, first + 1).map((t) => t.close),
first
).pop()
)

if (prevOne && one) {
if (one > prevOne) diff = true
if (one < prevOne) diff = false
if (prevOne && one && force) {
if ((force > 0 && one < prevOne) || (force < 0 && one > prevOne)) {
diff = true
}
}
}

Expand Down
37 changes: 23 additions & 14 deletions src/screens/Stats/extra/PairTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ActivityIndicator, Card, DataTable, Text, Title } from '@components'
import { MaterialCommunityIcons } from '@expo/vector-icons'
import { useTheme, useTickSocket } from '@hooks'
import {
ActivityIndicator,
Card,
DataTable,
Text,
Title,
Colors,
} from '@components'
import { useColorScheme, useTheme, useTickSocket } from '@hooks'
import { IPair } from '@interfaces'
import React, { useState, useCallback, useMemo } from 'react'
import { View, StyleSheet } from 'react-native'
Expand All @@ -22,20 +28,23 @@ const forceColor = (force: number | null) => {
const PairLine = ({
info,
}: {
info: { pair: IPair; force: number | null; diff: boolean | null }
info: { pair: IPair; force: number | null; diff: boolean }
}) => {
const { colors } = useTheme()
const scheme = useColorScheme()
const isDark = scheme === 'dark'

return (
<DataTable.Row>
<DataTable.Row
style={
info.diff && {
backgroundColor: !isDark
? Colors.deepOrange200
: Colors.deepOrange300,
}
}
>
<DataTable.Cell>{info.pair.symbol}</DataTable.Cell>
<DataTable.Cell numeric>
{info.diff !== null && (
<MaterialCommunityIcons
name={info.diff ? 'arrow-up-thick' : 'arrow-down-thick'}
color={colors.disabled}
size={14}
/>
)}
<Text
style={{
color: forceColor(info.force),
Expand All @@ -52,7 +61,7 @@ const PairLine = ({
export default function PairsTable({
res,
}: {
res: { pair: IPair; force: number | null; diff: boolean | null }[]
res: { pair: IPair; force: number | null; diff: boolean }[]
}) {
const { colors } = useTheme()
const { loading } = useTickSocket()
Expand Down

0 comments on commit e20becb

Please sign in to comment.