python - 在python中从头开始计算macd
问题描述
我尝试使用 anaconda 和 spyder 软件从一开始就计算 macd 值。脚本如下。但是我的值没有为直方图提供正确的值。有谁知道为什么?我将这个公式用于移动平均线:
剧本:
- - 编码:utf-8 - -
创建于 2020 年 11 月 4 日星期三 14:47:40
@作者: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, adjust=adjust).mean(),
name="EMA_slow",
)
MACD = pd.Series(EMA_fast - EMA_slow, name="MACD")
MACD_signal = pd.Series(
MACD.ewm(ignore_na=False, span=signal, adjust=adjust).mean(), name="SIGNAL"
)
return pd.concat([MACD, MACD_signal], axis=1)
他使用pandasexponential weighted (EW)
的函数。也许这对你有帮助。
推荐阅读
- mysql - MySQL 5.5 - 每天计算未清项目
- google-chrome - Chrome 桌面浏览器没有 A-Frame 声音
- c# - 如何在 C# 中捕获 Python 脚本的输出,因为它在运行时打印文本?
- javascript - 回复提示();使用 Python 网络爬虫
- angular - .subscribe 没有将数据放入我的数组中
- c# - 使用 c# 在 SQL Server 上创建登录名和密码
- java - 在 Collectors.toMap() 函数中处理重复键
- c# - 使用数组 C# WPF 中的项目填充列表框
- permissions - Zapier 更新在 podio 中说我没有权限,但很明显我有
- javascript - 如何让更简洁的 webpack 插件正常工作