需要帮助在javascript中计算rsi

问题描述 投票:0回答:1

我正在尝试在 Node.js API 中计算 RSI(相对强度指数)。 我试图复制我在 Tradingview 中获得的 RSI 值,但是,在使用不同的库(技术指标和 ta.js)和函数进行多次尝试后,我无法获得相同的(或与 ± 类似的值) 2分)。我正在从 ByBit API v5 检索 chandles 数据。

我实际使用的功能是这个:

function calculateRsi(values, period) {
  let changes = values.map((value, index, array) => {
    return index != 0 ? value - array[index - 1] : 0;
  });

  let gains = changes.map(value => Math.max(value, 0));
  let losses = changes.map(value => Math.max(-value, 0));

  let smaGains = gains.slice(0, period).reduce((a, b) => a + b, 0) / period;
  let smaLosses = losses.slice(0, period).reduce((a, b) => a + b, 0) / period;

  let rmaGains = [smaGains];
  let rmaLosses = [smaLosses];

  let alpha = 1 / period;
  for (let i = period; i < values.length; i++) {
    rmaGains[i] = alpha * gains[i] + (1 - alpha) * rmaGains[i - 1];
    rmaLosses[i] = alpha * losses[i] + (1 - alpha) * rmaLosses[i - 1];
  }

  return rmaGains.map((value, index) => 100 - (100 / (1 + value / rmaLosses[index])));
}

这是完整的 API 代码:

const express = require('express');
const cors = require('cors');
const { BollingerBands } = require('technicalindicators');
const ta = require('ta.js');
const axios = require('axios');

const app = express();
const port = 3000;

app.use(cors());

app.get('/', async (req, res) => {
  const totalCloses = await fetchCloses('https://api.bybit.com/v5/market/kline', Date.now(), 5);

  let input = {
    period: 30,
    stdDev: 2,
    values: totalCloses
  };

  let bbResult = BollingerBands.calculate(input);

  let roundedBBResult = bbResult.map(item => ({
    ...item,
    upper: parseFloat(item.upper.toFixed(2)),
    middle: parseFloat(item.middle.toFixed(2)),
    lower: parseFloat(item.lower.toFixed(2))
  }));

  let actualPrice = totalCloses[0];

  let action = "WAITING FOR SIGNAL";

  let rsi = calculateRsi(totalCloses, 13); 

  if (actualPrice > roundedBBResult[0].upper && rsi[0] > 75) {
    action = "GO SHORT!";
  } else if (actualPrice < roundedBBResult[0].lower && rsi[0] < 25) {
    action = "GO LONG!";
  }

  let responseObj = {
    Symbol: 'ETHUSDT',
    price: actualPrice,
    rsi: parseFloat(rsi).toFixed(2),
    action: action,
    closes: totalCloses,
    ///bb: roundedBBResult
  }

  res.send(JSON.stringify(responseObj));
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`)
});

async function fetchCloses(url, startDate, iterations, totalCloses = []) {
  if (iterations <= 0) {
    return totalCloses;
  }

  let endDate = startDate - 716400000;
  let config = {
    method: 'get',
    url: url + `?category=linear&symbol=ETHUSDT&interval=60&start=${endDate}&end=${startDate}`,
    headers: {}
  };

  try {
    const response = await axios(config);
    const list = response.data.result.list;
    const closes = list.map(item => parseFloat(item[4]));
    const roundedCloses = closes.map(item => parseFloat(item.toFixed(2)));
    totalCloses = totalCloses.concat(roundedCloses);
    return await fetchCloses(url, endDate, iterations - 1, totalCloses);
  } catch (error) {
    console.log(error);
    return totalCloses;
  }
}

function calculateRsi(values, period) {
  let changes = values.map((value, index, array) => {
    return index != 0 ? value - array[index - 1] : 0;
  });

  let gains = changes.map(value => Math.max(value, 0));
  let losses = changes.map(value => Math.max(-value, 0));

  let smaGains = gains.slice(0, period).reduce((a, b) => a + b, 0) / period;
  let smaLosses = losses.slice(0, period).reduce((a, b) => a + b, 0) / period;

  let rmaGains = [smaGains];
  let rmaLosses = [smaLosses];

  let alpha = 1 / period;
  for (let i = period; i < values.length; i++) {
    rmaGains[i] = alpha * gains[i] + (1 - alpha) * rmaGains[i - 1];
    rmaLosses[i] = alpha * losses[i] + (1 - alpha) * rmaLosses[i - 1];
  }

  return rmaGains.map((value, index) => 100 - (100 / (1 + value / rmaLosses[index])));
}

这是我从 API 检索数据的 HTML 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>

</head>
<body>

    <div id="info">

    </div>

</body>

<script>
    setInterval(function() {
    fetch('http://localhost:3000')
        .then(response => response.json())
        .then(data => {
            document.getElementById('info').innerText = JSON.stringify(data);

            

        })
        .catch(error => console.error('Errore:', error));
}, 1000);

</script>

</html>

因此,以示例为例,如果这些是收盘价:

1912.19,1905.91,1901.44,1897.86,1901.82,1897.28,1897.13,1900.02,1898.66,1896.93,1892.75,1891.42,1891.92,1889.78,1892.04,1890.49,1890.64,1890.69,1881.47,1888.63,1893.72,1899.23,1897.52,1902.73,1901.43,1904.12,1898.85,1899.5,1903.37,1902.84,1899.73,1902.89,1908.98,1915.99,1913.44,1907.93,1906.06,1867.41,1852.29,1847.91,1850.63,1846.5,1843.15,1842.99,1838.74,1842.69,1839.75,1842.18,1844.8,1843.96,1844.69,1846.65,1844.63,1846.19,1847.75,1849.42,1845.19,1847.55,1848.44,1829.2,1828.81,1826.42,1826.27,1824.98,1820.64,1823.05,1820.62,1822.46,1821.72,1818.91,1827.42,1825.84,1825.57,1826.23,1827.43,1828.98,1831.77,1830.81,1833.06,1832.86,1832.31,1828.61,1825.48,1826.54,1825.13,1830.06,1833.51,1834.5,1830.71,1823.45,1827.48,1829.67,1829.3,1822.15,1813.59,1811.42,1812.73,1812.65,1813.3,1816,1810.82,1801.99,1807.2,1806.51,1800.89,1800.73,1804.56,1805,1806.39,1806.94,1808.36,1807.82,1804.49,1804.12,1795.98,1790.19,1798.16,1798.7,1800.95,1788.9,1785,1786.47,1784.15,1777.67,1783.42,1783.45,1778.35,1780.15,1778.71,1775.87,1791.63,1799,1798.46,1799.62,1803.84,1791.67,1786.92,1791.94,1788.45,1792.54,1786.4,1802.02,1815.49,1815.17,1814.78,1816.2,1817.65,1813.01,1820.72,1825.29,1825.73,1821.11,1844.99,1844.21,1849,1853.79,1853.57,1848.42,1853.7,1850.76,1844.32,1848.89,1848.72,1856.28,1855.2,1846.43,1850.99,1854,1856.27,1854.31,1855.43,1853.28,1855.17,1856.87,1856.23,1864.59,1841.3,1833.64,1815.44,1816.11,1817.3,1819.89,1817.39,1815.68,1818.64,1815.99,1816.89,1815.55,1818.38,1821.58,1809.41,1813.46,1816.64,1816.89,1811,1810.44,1808.23,1803.06,1801.69,1799.89,1800.8,1793.25,1798.13,1804.44,1804.27,1802.53,1804.66,1809.38,1805.83,1805.64,1806.34,1806.29,1811.15,1811.49,1812.7,1808.17,1809.01,1815.95,1814.52,1815.9,1814.28,1814.13,1815.98,1817.01,1817.85,1816.77,1825.88,1818.3,1817.37,1816.62,1814.99,1820.49,1819.02,1825.94,1814.84,1816.38,1818.1,1812.94,1812.81,1812,1812.45,1812.19,1814.01,1810.68,1809.35,1810.09,1812.8,1812.4,1807.66,1808.96,1807.76,1811.48,1811.2,1813.56,1811.6,1810.68,1816.17,1817.97,1811.04,1814.46,1810.47,1813.51,1813.92,1806.65,1806.49,1806.99,1807.59,1804.79,1806.6,1804.47,1803.19,1800.21,1799.39,1798.71,1803.35,1799.9,1808.01,1813.57,1795.64,1794.4,1784.89,1777.2,1808.72,1806.73,1816.15,1817.81,1817.56,1824.69,1825.5,1825.8,1825.53,1826.7,1816.69,1818.28,1822.04,1825.51,1825.01,1819,1819.65,1821.13,1818.91,1822.33,1825.83,1827.08,1817.15,1814.76,1803.5,1796.61,1796.87,1789.37,1793.34,1789.11,1805.24,1806.17,1805.17,1806.74,1817.51,1817.81,1823.48,1823.3,1822.94,1830.52,1824.81,1823.4,1822.5,1825.51,1820.91,1815.92,1824.9,1827.18,1823.14,1819.47,1815.32,1812.73,1817.8,1815.49,1812,1816.1,1819.21,1821.81,1811.31,1809.99,1811.61,1812.62,1808.8,1805.52,1811.71,1815.02,1821.8,1824.11,1824.19,1827.89,1828.47,1832.79,1833.11,1826.71,1823.42,1822.45,1825.23,1823.09,1828.04,1827.58,1830.3,1827.37,1831.14,1824.15,1826.97,1825.1,1822.52,1823.22,1790.06,1798.32,1798.99,1798.6,1797.69,1795.21,1800.44,1805.24,1807,1817.8,1808.22,1809.86,1807.44,1801.15,1801.66,1803.01,1801.99,1804.54,1805.03,1801.79,1802.11,1803.05,1798.15,1793.73,1799.39,1794.14,1801.04,1799.22,1802.38,1792,1787.86,1792.41,1793.41,1795.27,1792.21,1800.25,1802.99,1802.58,1805.01,1806.7,1803.16,1801.98,1796.9,1796.99,1800.66,1804.67,1804.44,1808.22,1807.64,1806.32,1801.61,1804.22,1787.08,1789.41,1769.41,1770.02,1768.57,1764.73,1765.45,1772.92,1771.35,1764.14,1764.38,1765.62,1759.67,1758.72,1754.59,1747.03,1768.22,1761.96,1768.47,1787.79,1797.57,1793.61,1788.75,1792.71,1794.77,1783.76,1788.67,1782.01,1803.7,1807.45,1797.55,1806.46,1818.09,1819.79,1822.01,1819.44,1817.99,1828.47,1830.37,1831.51,1828.55,1826.63,1831.8,1833.99,1830.86,1839.98,1841.68,1837.12,1856.14,1848.2,1833.88,1829.58,1871.83,1871.41,1870.01,1874.01,1876.09,1849.56,1841.87,1838.76,1837,1837.28,1841.01,1842.61,1844.17,1842.56,1843.44,1847.72,1850.37,1845.73,1842.99,1842.13,1848.04,1851.67,1842,1846.39,1852.55,1838.2,1842,1841.86,1846.8,1845.75,1840.98,1839.27,1839.85,1847.77,1837.74,1839.93,1839.41,1842.6,1845.61,1849.26,1841.98,1846.21,1846.01,1839.53,1839.61,1828.26,1830,1822.92,1858.41,1863.8,1867.52,1857.87,1854.24,1861.69,1855.99,1856.1,1841.41,1850.2,1861.36,1866.82,1858.01,1864.38,1858.6,1852.02,1880.46,1869.24,1906.3,1906.37,1917.57,1912.03,1908.89,1911.22,1914.1,1916.26,1926.82,1912.65,1910.66,1901.33,1904.32,1903.15,1902.01,1904.53,1900.1,1897.98,1894.08,1891.93,1891.19,1898.98,1904.46,1895.5,1894.36,1894.8,1893.39,1896.48,1899.66,1880.8,1881,1876.4,1874.82,1899.61,1911,1933.79,1927,1934.75,1942.7,1957.62,1962.66,1959.56,1965.03,1969.89,1978.73,1982.52,2002.44,1992.84,1987.77,1984.62,1987.23,1991.17,1979.5,1990.75,1990.73,1952.84,1945.75,1931.53,1904.03,1910.56,1904.02,1899.05,1894.93,1894.16,1897.99,1895.89,1897.08,1896.41,1897.59,1898.39,1878.26,1875.86,1870.45,1875.9,1876.56,1875.58,1872.02,1881.5,1885.29,1885.39,1885.76,1884.43,1897.48,1903.54,1910.67,1902.38,1900.81,1897.58,1899.75,1902.28,1900.01,1902.63,1901.59,1901.48,1901.2,1904.4,1906.04,1888.61,1872.8,1867.21,1884.19,1874.16,1863.99,1852.48,1847.45,1856.09,1849.26,1858.51,1863.82,1867.46,1867.53,1864.19,1865.38,1859.69,1862.05,1861.81,1860.53,1863.01,1859.02,1868.74,1872.83,1870.85,1868.6,1867.67,1869.85,1859.87,1854.41,1857.91,1851.07,1828.08,1831.14,1832.45,1828.41,1827.48,1831.69,1835.05,1828.06,1828.3,1824.5,1829.53,1828.1,1825.23,1831.5,1829.77,1824.99,1823.68,1806.83,1812.2,1831.32,1838.79,1838.99,1832.76,1842.16,1840,1848.49,1844.4,1844.66,1846.5,1847.01,1848.54,1848.52,1845.73,1846.62,1845.4,1843,1835.08,1884.1,1868.2,1891.2,1892.79,1890.6,1903.82,1917.38,1920.72,1923.67,1919.28,1907.92,1903.99,1899.4,1899.82,1903,1903.21,1902.71,1901.75,1902.36,1902.78,1896.73,1895.39,1897.74,1893.31,1897.9,1907.07,1902.81,1903.09,1901.5,1901.92,1896.71,1893.19,1905.51,1905.1,1909.81,1911.52,1896,1896.89,1895.66,1899.97,1899.09,1901.15,1897.87,1898.32,1900.8,1895.57,1896.28,1890.22,1887.6,1890.01,1893.25,1893.24,1895.05,1891.54,1892.68,1889.91,1892.05,1885.66,1887.13,1887.48,1897.65,1901.76,1910.1,1905.8,1908.45,1917.85,1914.98,1913.74,1912.67,1904.3,1905.53,1909.31,1912.94,1907.7,1903.88,1914.53,1918.77,1921.43,1926.43,1903.26,1898.86,1898.74,1884.13,1883.34,1874.99,1880,1883.54,1880.75,1882.01,1884,1881.16,1906.53,1909.64,1897.81,1908.5,1890.93,1919.22,1865.21,1863.89,1877.63,1865.28,1827.42,1945.63,1946.92,1953.49,1952.75,1945.21,1953.19,1960.09,1912.89,1914.71,1895.5,1876.19,1860.55,1865.03,1868.01,1864.04,1861.92,1867.7,1868.54,1866.35,1865.1,1866.59,1869.49,1859.17,1836.81,1832.62,1838.86,1823.33,1814.06,1811,1820.87,1824,1824.65,1814.35,1813.96,1823.51,1814.28,1821.94,1829.65,1831.38,1829.5,1834.31,1834.36,1840.04,1841,1837.73,1832.01,1839.23,1830.56,1830.26,1833.35,1812.7,1833,1854.52,1864.33,1847.01,1854.87,1844.31,1838.31,1834.71,1844.4,1846.3,1855.36,1860.89,1867.29,1867.03,1870.99,1875.97,1861.52,1861.4,1853.53,1847.66,1845.35,1845.92,1858,1864.39,1858.7,1864.92,1872.62,1871.96,1874.07,1866.32,1870.62,1873.28,1873.37,1867.58,1866.12,1863.63,1861.22,1858.21,1854.58,1855.99,1872.74,1876.3,1870.85,1868.52,1871.2,1873.87,1880.48,1873.8,1865.85,1865.01,1849.51,1855.3,1851.26,1848.24,1849.08,1848.83,1850.56,1855.6,1864.2,1859.92,1858.36,1853.99,1855.43,1846.32,1848.03,1844.21,1839.64,1836.94,1841.71,1879.65,1880.69,1899.18,1903.08,1906.94,1906.82,1915.52,1908.18,1912,1920.4,1920.5,1927.48,1907.32,1927.33,1938.22,1945.57,1944.64,1941.01,1946.93,1941.83,1942.45,1942.64,1936.67,1926.75,1932.77,1934.82,1937,1945.05,1967.44,1971.53,1970.8,1939.18,1952.85,1955.17,1949.38,1947.84,1953.06,1952.51,1951.38,1948.3,1937.38,1949.99,1945.83,1933.74,1964.95,1974.47,1979.35,1980.06,1978.08,1980.78,1983.24,1983.09,1994.71,1984.11,1977.69,1981.56,1975.58,1980.77,1987.2,2073.51,2069.14,2088.82,2086,2091.14,2092,2095.01,2097.95

TradingView 给我这个 RSI 结果:69.17
但是,我的脚本,给我这个:21.02
48点差距!

我实际上做错了什么?如何获得接近 TradingView RSI 指标值的值?

感谢您的帮助!

附注
如果需要测试,这是我的 package.json 文件

{
  "name": "new",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bybit-api": "^3.6.0",
    "cors": "^2.8.5",
    "lodash": "^4.17.21",
    "ta.js": "^1.16.3",
    "tulind": "^0.8.20"
  }
}

javascript node.js algorithmic-trading technical-indicator
1个回答
0
投票

我注意到你和我有同样的问题,但你没有得到答案! :o

您同时找到解决方案了吗?如果是这样,你能在这一点上帮助我吗,因为我真的找不到任何东西:/.

提前致谢;)

© www.soinside.com 2019 - 2024. All rights reserved.