微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在python中从头开始计算macd

如何解决在python中从头开始计算macd

我从一开始就尝试使用 anaconda 和 spyder 软件计算 macd 值。脚本如下。但是我的值没有给出直方图的正确值。有谁知道为什么?我使用这个公式来计算移动平均线: help entry

脚本:

-- 编码:utf-8 --

创建于 2020 年 11 月 4 日星期三 14:47:40

@author: tor-pc

import numpy as np
import datetime
import time
from binance.client import Client
api_key = 'censored'
secret_key = 'censored'
client = Client(api_key,secret_key) 

symbol= 'ETHUSDT'
quantity= '42'
x=0
t=0
y=0
buyprice=0
BTC=[]
BTC= client.get_historical_klines(symbol=symbol,interval='1d',start_str="1 sep,2020")
ma1=0
sma1=0
weightlast=(2/26+1)
weightrest=1-weightlast
sweightlast=(2/12+1)
sweightrest=1-sweightlast
sigweightlast=(2/9+1)
sigweightrest=1-sweightlast
for i in range(61,36,-1):
    
    ma1=ma1+float(BTC[-i][4])
    x=x+1
    
print(x)

    
ma1=(ma1/25)*weightrest+float(BTC[-35][4])*weightlast

ma2=(ma1*weightrest+float(BTC[-34][4])*weightlast)

ma3=(ma2*weightrest+float(BTC[-33][4])*weightlast)

ma4=(ma3*weightrest+float(BTC[-32][4])*weightlast)

ma5=(ma4*weightrest+float(BTC[-31][4])*weightlast)

ma6=(ma5*weightrest+float(BTC[-30][4])*weightlast)

ma7=(ma6*weightrest+float(BTC[-29][4])*weightlast)

ma8=(ma7*weightrest+float(BTC[-28][4])*weightlast)

ma9=(ma8*weightrest+float(BTC[-27][4])*weightlast)

ma10=(ma9*weightrest+float(BTC[-26][4])*weightlast)

ma11=(ma10*weightrest+float(BTC[-25][4])*weightlast)

ma12=(ma11*weightrest+float(BTC[-24][4])*weightlast)

ma13=(ma12*weightrest+float(BTC[-23][4])*weightlast)

ma14=(ma13*weightrest+float(BTC[-22][4])*weightlast)

ma15=(ma14*weightrest+float(BTC[-21][4])*weightlast)

ma16=(ma15*weightrest+float(BTC[-20][4])*weightlast)

ma17=(ma16*weightrest+float(BTC[-19][4])*weightlast)

ma18=(ma17*weightrest+float(BTC[-18][4])*weightlast)

ma19=(ma18*weightrest+float(BTC[-17][4])*weightlast)

ma20=(ma19*weightrest+float(BTC[-16][4])*weightlast)

ma21=(ma20*weightrest+float(BTC[-15][4])*weightlast)

ma22=(ma21*weightrest+float(BTC[-14][4])*weightlast)

ma23=(ma22*weightrest+float(BTC[-13][4])*weightlast)

ma24=(ma23*weightrest+float(BTC[-12][4])*weightlast)

ma25=(ma24*weightrest+float(BTC[-11][4])*weightlast)

ma26=(ma25*weightrest+float(BTC[-10][4])*weightlast)

ma27=(ma26*weightrest+float(BTC[-9][4])*weightlast)

ma28=(ma27*weightrest+float(BTC[-8][4])*weightlast)

ma29=(ma28*weightrest+float(BTC[-7][4])*weightlast)

ma30=(ma29*weightrest+float(BTC[-6][4])*weightlast)

ma31=(ma30*weightrest+float(BTC[-5][4])*weightlast)

ma32=(ma31*weightrest+float(BTC[-4][4])*weightlast)

ma33=(ma32*weightrest+float(BTC[-3][4])*weightlast)

ma34=(ma33*weightrest+float(BTC[-2][4])*weightlast)

ma35=(ma34*weightrest+float(BTC[-1][4])*weightlast)

for i in range(61,50,-1):
    
    sma1=sma1+float(BTC[-i][4])
    y=y+1
print(y)   

sma1=(sma1/11)*sweightrest+float(BTC[-49][4])*sweightlast 

sma2=sma1*sweightrest+float(BTC[-48][4])*sweightlast 

sma3=sma2*sweightrest+float(BTC[-47][4])*sweightlast

sma4=sma3*sweightrest+float(BTC[-46][4])*sweightlast

sma5=sma4*sweightrest+float(BTC[-45][4])*sweightlast

sma6=sma5*sweightrest+float(BTC[-44][4])*sweightlast

sma7=sma6*sweightrest+float(BTC[-43][4])*sweightlast

sma8=sma7*sweightrest+float(BTC[-42][4])*sweightlast

sma9=sma8*sweightrest+float(BTC[-41][4])*sweightlast

sma10=sma9*sweightrest+float(BTC[-40][4])*sweightlast

sma11=sma10*sweightrest+float(BTC[-39][4])*sweightlast

sma12=sma11*sweightrest+float(BTC[-38][4])*sweightlast

sma13=sma12*sweightrest+float(BTC[-37][4])*sweightlast

sma14=sma13*sweightrest+float(BTC[-36][4])*sweightlast

sma15=sma14*sweightrest+float(BTC[-35][4])*sweightlast

sma16=sma15*sweightrest+float(BTC[-34][4])*sweightlast

sma17=sma16*sweightrest+float(BTC[-33][4])*sweightlast

sma18=sma17*sweightrest+float(BTC[-32][4])*sweightlast

sma19=sma18*sweightrest+float(BTC[-31][4])*sweightlast

sma20=sma19*sweightrest+float(BTC[-30][4])*sweightlast

sma21=sma20*sweightrest+float(BTC[-29][4])*sweightlast

sma22=sma21*sweightrest+float(BTC[-28][4])*sweightlast

sma23=sma22*sweightrest+float(BTC[-27][4])*sweightlast

sma24=sma23*sweightrest+float(BTC[-26][4])*sweightlast

sma25=sma24*sweightrest+float(BTC[-25][4])*sweightlast

sma26=sma25*sweightrest+float(BTC[-24][4])*sweightlast

sma27=sma26*sweightrest+float(BTC[-23][4])*sweightlast

sma28=sma27*sweightrest+float(BTC[-22][4])*sweightlast

sma29=sma28*sweightrest+float(BTC[-21][4])*sweightlast

sma30=sma29*sweightrest+float(BTC[-20][4])*sweightlast

sma31=sma30*sweightrest+float(BTC[-19][4])*sweightlast

sma32=sma31*sweightrest+float(BTC[-18][4])*sweightlast

sma33=sma32*sweightrest+float(BTC[-17][4])*sweightlast

sma34=sma33*sweightrest+float(BTC[-16][4])*sweightlast

sma35=sma34*sweightrest+float(BTC[-15][4])*sweightlast

sma36=sma35*sweightrest+float(BTC[-14][4])*sweightlast

sma37=sma36*sweightrest+float(BTC[-13][4])*sweightlast

sma38=sma37*sweightrest+float(BTC[-12][4])*sweightlast

sma39=sma38*sweightrest+float(BTC[-11][4])*sweightlast

sma40=sma39*sweightrest+float(BTC[-10][4])*sweightlast

sma41=sma40*sweightrest+float(BTC[-9][4])*sweightlast

sma42=sma41*sweightrest+float(BTC[-8][4])*sweightlast

sma43=sma42*sweightrest+float(BTC[-7][4])*sweightlast

sma44=sma43*sweightrest+float(BTC[-6][4])*sweightlast

sma45=sma44*sweightrest+float(BTC[-5][4])*sweightlast

sma46=sma45*sweightrest+float(BTC[-4][4])*sweightlast

sma47=sma46*sweightrest+float(BTC[-3][4])*sweightlast

sma48=sma47*sweightrest+float(BTC[-2][4])*sweightlast

sma49=sma48*sweightrest+float(BTC[-1][4])*sweightlast


macd=sma49-ma35

sig91=(sma32 -ma18+sma33-ma19+sma34-ma20+sma35-ma21+sma36-ma22+sma37-ma23+sma38-ma24+sma39-ma25+sma40-ma26)/9

signal1=sig91*sigweightrest+(sma41-ma27)*sigweightlast

signal2=signal1*sigweightrest+(sma42-ma28)*sigweightlast

signal3=signal2*sigweightrest+(sma43-ma29)*sigweightlast

signal4=signal3*sigweightrest+(sma44-ma30)*sigweightlast

signal5=signal4*sigweightrest+(sma45-ma31)*sigweightlast

signal6=signal5*sigweightrest+(sma46-ma32)*sigweightlast

signal7=signal6*sigweightrest+(sma47-ma33)*sigweightlast

signal8=signal7*sigweightrest+(sma48-ma34)*sigweightlast

signal9=signal8*sigweightrest+(sma49-ma35)*sigweightlast


hist=macd-signal9

print(hist)
print(float(BTC[-1][4]))

解决方法

看一下 peerchemist/finta 中计算 MACD 的实现:

def MACD(
    cls,ohlc: DataFrame,period_fast: int = 12,period_slow: int = 26,signal: int = 9,column: str = "close",adjust: bool = True,) -> DataFrame:
    """
    MACD,MACD Signal and MACD difference.
    The MACD Line oscillates above and below the zero line,which is also known as the centerline.
    These crossovers signal that the 12-day EMA has crossed the 26-day EMA. The direction,of course,depends on the direction of the moving average cross.
    Positive MACD indicates that the 12-day EMA is above the 26-day EMA. Positive values increase as the shorter EMA diverges further from the longer EMA.
    This means upside momentum is increasing. Negative MACD values indicates that the 12-day EMA is below the 26-day EMA.
    Negative values increase as the shorter EMA diverges further below the longer EMA. This means downside momentum is increasing.
    Signal line crossovers are the most common MACD signals. The signal line is a 9-day EMA of the MACD Line.
    As a moving average of the indicator,it trails the MACD and makes it easier to spot MACD turns.
    A bullish crossover occurs when the MACD turns up and crosses above the signal line.
    A bearish crossover occurs when the MACD turns down and crosses below the signal line.
    """

    EMA_fast = pd.Series(
        ohlc[column].ewm(ignore_na=False,span=period_fast,adjust=adjust).mean(),name="EMA_fast",)
    EMA_slow = pd.Series(
        ohlc[column].ewm(ignore_na=False,span=period_slow,name="EMA_slow",)
    MACD = pd.Series(EMA_fast - EMA_slow,name="MACD")
    MACD_signal = pd.Series(
        MACD.ewm(ignore_na=False,span=signal,name="SIGNAL"
    )

    return pd.concat([MACD,MACD_signal],axis=1)

他使用了 pandas 中的 exponential weighted (EW) 函数。也许这对你有帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?