Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7中计算在binance web界面中显示的正确MACD和RSI索引_Python_Finance_Moving Average_Trading_Timeserieschart - Fatal编程技术网

在Python2.7中计算在binance web界面中显示的正确MACD和RSI索引

在Python2.7中计算在binance web界面中显示的正确MACD和RSI索引,python,finance,moving-average,trading,timeserieschart,Python,Finance,Moving Average,Trading,Timeserieschart,我一直在试图计算和绘制Binance上加密硬币的价格、MACD和RSI指数(数据是通过Binance获得的),但我担心要么我的指数不准确,要么Binance使用了不同的算法。 我一直在使用Matplotlib教程中的MACD和RSI函数,它们产生的结果与我在别处找到的其他算法相同,因此该算法应该是准确的,但我得到了错误的结果(从比较图中可以看出)。 特别是,RSI似乎是正确的,但MACD(以及MACD和信号线)与binance网站交易视图显示的不同(参见图片) 我做错了什么 #!/usr/bin

我一直在试图计算和绘制Binance上加密硬币的价格、MACD和RSI指数(数据是通过Binance获得的),但我担心要么我的指数不准确,要么Binance使用了不同的算法。 我一直在使用Matplotlib教程中的MACD和RSI函数,它们产生的结果与我在别处找到的其他算法相同,因此该算法应该是准确的,但我得到了错误的结果(从比较图中可以看出)。 特别是,RSI似乎是正确的,但MACD(以及MACD和信号线)与binance网站交易视图显示的不同(参见图片)

我做错了什么

#!/usr/bin/python
# -*- coding: utf8 -*-


import numpy as np
import matplotlib.pyplot as plt



######   data


prices = np.array([ 0.00061422,  0.00061422,  0.00061593,  0.00061672,  0.0006161 ,
        0.00061233,  0.000615  ,  0.00061305,  0.00061346,  0.00061417,
        0.00061428,  0.00061418,  0.0006115 ,  0.00061203,  0.0006125 ,
        0.00061295,  0.00061296,  0.00061295,  0.00061242,  0.00061144,
        0.00060874,  0.00060661,  0.00060512,  0.00060931,  0.000611  ,
        0.0006129 ,  0.00061296,  0.000613  ,  0.00061138,  0.0006115 ,
        0.0006123 ,  0.0006123 ,  0.00061288,  0.00061494,  0.000615  ,
        0.0006146 ,  0.00061488,  0.00061399,  0.00061285,  0.0006129 ,
        0.0006129 ,  0.00061291,  0.0006134 ,  0.00061338,  0.00061355,
        0.0006139 ,  0.00061475,  0.0006167 ,  0.0006158 ,  0.000617  ,
        0.00061638,  0.00061452,  0.0006164 ,  0.00061641,  0.00061646,
        0.00061898,  0.0006198 ,  0.00061818,  0.00061922,  0.00061979,
        0.00061977,  0.00061924,  0.00061626,  0.00061488,  0.000616  ,
        0.000616  ,  0.00061693,  0.0006165 ,  0.0006165 ,  0.00061699,
        0.00061685,  0.00061687,  0.00061691,  0.000617  ,  0.00061784,
        0.00061899,  0.0006177 ,  0.000617  ,  0.00061732,  0.0006176 ,
        0.0006174 ,  0.00061739,  0.00061739,  0.00061794,  0.0006185 ,
        0.0006185 ,  0.00061785,  0.00061735,  0.00061743,  0.00061742,
        0.00061429,  0.0006152 ,  0.00061451,  0.00061514,  0.0006143 ,
        0.000614  ,  0.0006154 ,  0.0006148 ,  0.00061444,  0.00061572])


######   functions


def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type == 'simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()

    a = np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a


def relative_strength(prices, n=14):
    """
    compute the n period relative strength indicator
    http://stockcharts.com/school/doku.php?id=chart_school:glossary_r#relativestrengthindex
    http://www.investopedia.com/terms/r/rsi.asp
    """

    deltas = np.diff(prices)
    seed = deltas[:n+1]
    up = seed[seed >= 0].sum()/n
    down = -seed[seed < 0].sum()/n
    rs = up/down
    rsi = np.zeros_like(prices)
    rsi[:n] = 100. - 100./(1. + rs)

    for i in range(n, len(prices)):
        delta = deltas[i - 1]  # cause the diff is 1 shorter

        if delta > 0:
            upval = delta
            downval = 0.
        else:
            upval = 0.
            downval = -delta

        up = (up*(n - 1) + upval)/n
        down = (down*(n - 1) + downval)/n

        rs = up/down
        rsi[i] = 100. - 100./(1. + rs)

    return rsi


def moving_average_convergence(x, nslow=26, nfast=12):
    """
    compute the MACD (Moving Average Convergence/Divergence) using a fast and slow exponential moving avg'
    return value is emaslow, emafast, macd which are len(x) arrays
    """
    emaslow = moving_average(x, nslow, type='exponential')
    emafast = moving_average(x, nfast, type='exponential')
    return emaslow, emafast, emafast - emaslow


######   code


nslow = 26
nfast = 12
nema = 9
emaslow, emafast, macd = moving_average_convergence(prices, nslow=nslow, nfast=nfast)
ema9 = moving_average(macd, nema, type='exponential')
rsi = relative_strength(prices)

wins = 80


plt.figure(1)

### prices

plt.subplot2grid((8, 1), (0, 0), rowspan = 4)
plt.plot(prices[-wins:], 'k', lw = 1)


### rsi

plt.subplot2grid((8, 1), (5, 0))
plt.plot(rsi[-wins:], color='black', lw=1)
plt.axhline(y=30,     color='red',   linestyle='-')
plt.axhline(y=70,     color='blue',  linestyle='-')


## MACD

plt.subplot2grid((8, 1), (6, 0))

plt.plot(ema9[-wins:], 'red', lw=1)
plt.plot(macd[-wins:], 'blue', lw=1)


plt.subplot2grid((8, 1), (7, 0))

plt.plot(macd[-wins:]-ema9[-wins:], 'k', lw = 2)
plt.axhline(y=0, color='b', linestyle='-')

plt.show()
#/usr/bin/python
#-*-编码:utf8-*-
将numpy作为np导入
将matplotlib.pyplot作为plt导入
######资料
价格=np.数组([0.00061422,0.00061422,0.00061593,0.00061672,0.0006161,
0.00061233,  0.000615  ,  0.00061305,  0.00061346,  0.00061417,
0.00061428,  0.00061418,  0.0006115 ,  0.00061203,  0.0006125 ,
0.00061295,  0.00061296,  0.00061295,  0.00061242,  0.00061144,
0.00060874,  0.00060661,  0.00060512,  0.00060931,  0.000611  ,
0.0006129 ,  0.00061296,  0.000613  ,  0.00061138,  0.0006115 ,
0.0006123 ,  0.0006123 ,  0.00061288,  0.00061494,  0.000615  ,
0.0006146 ,  0.00061488,  0.00061399,  0.00061285,  0.0006129 ,
0.0006129 ,  0.00061291,  0.0006134 ,  0.00061338,  0.00061355,
0.0006139 ,  0.00061475,  0.0006167 ,  0.0006158 ,  0.000617  ,
0.00061638,  0.00061452,  0.0006164 ,  0.00061641,  0.00061646,
0.00061898,  0.0006198 ,  0.00061818,  0.00061922,  0.00061979,
0.00061977,  0.00061924,  0.00061626,  0.00061488,  0.000616  ,
0.000616  ,  0.00061693,  0.0006165 ,  0.0006165 ,  0.00061699,
0.00061685,  0.00061687,  0.00061691,  0.000617  ,  0.00061784,
0.00061899,  0.0006177 ,  0.000617  ,  0.00061732,  0.0006176 ,
0.0006174 ,  0.00061739,  0.00061739,  0.00061794,  0.0006185 ,
0.0006185 ,  0.00061785,  0.00061735,  0.00061743,  0.00061742,
0.00061429,  0.0006152 ,  0.00061451,  0.00061514,  0.0006143 ,
0.000614  ,  0.0006154 ,  0.0006148 ,  0.00061444,  0.00061572])
######功能
def移动_平均值(x,n,type='simple'):
"""
计算n周期移动平均值。
类型为“简单”|“指数”
"""
x=np.asarray(x)
如果类型==“简单”:
权重=np.个(n)
其他:
权重=np.exp(np.linspace(-1,0,n))
权重/=权重.sum()
a=np.卷积(x,权重,mode='full')[:len(x)]
a[:n]=a[n]
归还
def相对强度(价格,n=14):
"""
计算n周期相对强度指标
http://stockcharts.com/school/doku.php?id=chart_school:glossary_r#relativestrengthindex
http://www.investopedia.com/terms/r/rsi.asp
"""
增量=np.差异(价格)
种子=三角洲[:n+1]
up=seed[seed>=0].sum()/n
down=-seed[seed<0].sum()/n
rs=上升/下降
rsi=np.类零(价格)
rsi[:n]=100100./(1.+rs)
对于范围(n,len(价格))内的i:
delta=delta[i-1]#因为差值较短
如果增量>0:
upval=delta
downval=0。
其他:
upval=0。
downval=-delta
up=(up*(n-1)+upval)/n
向下=(向下*(n-1)+向下值)/n
rs=上升/下降
rsi[i]=100100./(1.+rs)
回归rsi
def移动平均收敛(x,nslow=26,nfast=12):
"""
使用快速和慢速指数移动平均值计算MACD(移动平均收敛/发散)
返回值是emaslow、emafast和macd,它们是len(x)数组
"""
emaslow=移动平均值(x,nslow,type='index')
emafast=移动平均值(x,nfast,type='index')
返回emaslow,emafast,emafast-emaslow
######代码
nslow=26
nfast=12
nema=9
emaslow、Emasfast、macd=移动平均收敛(价格,nslow=nslow,nfast=nfast)
ema9=移动平均值(macd,nema,type='index')
rsi=相对强度(价格)
wins=80
plt.图(1)
###价格
plt.subplot2grid((8,1)、(0,0),rowspan=4)
plt.绘图(价格[-wins:],'k',lw=1)
###rsi
plt.子图2grid((8,1)、(5,0))
plt.绘图(rsi[-wins:],color='black',lw=1)
plt.axhline(y=30,color='red',linestyle='-')
plt.axhline(y=70,color='blue',linestyle='-')
##MACD
plt.子图2grid((8,1)、(6,0))
plt.绘图(ema9[-wins:],“红色”,lw=1)
plt.绘图(macd[-wins:],“蓝色”,lw=1)
plt.子图2grid((8,1)、(7,0))
plt.plot(macd[-wins:]-ema9[-wins:],'k',lw=2)
plt.axhline(y=0,color='b',linestyle='-')
plt.show()

我尝试了talib,这就是我想要的:)(第二张照片) 我猜它们是计算MACD的两种不同方法(但我以前尝试过非指数平均,但无论如何都不起作用,所以我仍然不知道差异背后的原因)


我可以问一下您是如何将您的值与Binance的值对齐的吗?我正在努力完成同样的任务,我有一个库,它给了我与talib相同的结果(我检查了,因为我认为libs中确实存在差异),改变了输入参数的数量(您也使用了100个)。。。但对我来说,输出值仍然不同于我在Binance上看到的值。也许现在,3年后,他们使用的参数与你发布这篇文章时不同?@YetiCGN抱歉,我对这一切记得很少:这已经发生了。我已经验证了我的库与talib具有完全相同的值,现在这对我来说很好。
import talib
macd, macdsignal, macdhist = talib.MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9)