Reputation: 15
Thank you in advance for any help. I have been trying to match the EMA/MACD on Binance for several months off and on. I have tried several methods but the data never lines up with what is showing on the mobile app for the exchange. I read they use TEMA rather then EMA and that the implementation of TA-Lib's MACD matches the exchange. However I have been unable to figure out how to get it to read the Dataframe without giving an error. I have only been programming in Python for a few months but this has stumped me the entire time. If someone could please show me how to fix this. The technic version works with the Dataframe but it does not match the exchange. I have tried a bunch of things and errors range from no index to unable to convert.
Regards, Jeff
import talib
import btalib
import numpy
import pandas as pd
import requests
import time
import json
import technic as ta
from datetime import datetime
from os import system, name
api_key = ''
api_secret = ''
from binance.client import Client
client = Client(api_key, api_secret)
sym = "TKOUSDT"
while 1 == 1:
data = client.get_historical_klines(sym, Client.KLINE_INTERVAL_5MINUTE, "135 mins ago UTC")
data2 = pd.DataFrame(data, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ])
clse = data2['close']
# print(clse)
clse2 = clse.to_numpy()
# print(clse2)
clse3 = pd.to_numeric(clse2, downcast='float')
# print(clse3)
# This one works... Sorta... The values do not match the Binance exchange! Was told Binance uses TEMA and that the TA-Lib MACD matches the exchange.
macd1 = ta.tmacd(data2['close'], w_slow=26, w_fast=12, w_signal=9)
print(macd1)
# Will not read the Dataframe
macd, signal, hist = talib.MACD(data2['close'], fastperiod=12, slowperiod=26, signalperiod=9)
# print(macd,signal,hist)
# Will not read the Dataframe
macd2 = btalib.macd(data2['close'], pfast=20, pslow=50, psignal=13)
# print(macd2)
time.sleep(10)
Upvotes: 0
Views: 1019
Reputation: 11
Will try and simplify to answer 1) "how to get it to read the Dataframe without giving an error", and 2) why you may see different calculations.
For #1, btalib was not accepting the RangeIndex for some reason (although in the documentation it states it should). When working with this type of data it's typically useful to set a DatetimeIndex or TimedeltaIndex anyways, so adding that and validating the OHLCV data types corrected the errors.
As a side note, btalib will accept an entire dataframe as long as it contains columns like Open/open, etc.
import pandas as pd
import talib
import btalib
import technic
from binance.client import Client # pip install python-binance
api_key = ''
api_secret = ''
client = Client(api_key, api_secret)
sym = "TKOUSDT"
client_columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore']
data = client.get_historical_klines(sym, Client.KLINE_INTERVAL_1MINUTE, "135 mins ago UTC")
data2 = pd.DataFrame(data, columns=client_columns)
# set time index
data2['timestamp'] = pd.to_timedelta(data2['timestamp'])
data2.set_index('timestamp', inplace=True)
# validate data types for ohlcv columns
ohlcv_columns = ['open', 'high', 'low', 'close', 'volume']
data2[ohlcv_columns] = data2[ohlcv_columns].astype('float')
# calculate
technic_macd = technic.tmacd(data2['close'], w_slow=26, w_fast=12, w_signal=9)
talib_macd = pd.concat(talib.MACD(data2['close'], fastperiod=12, slowperiod=26, signalperiod=9), axis=1)
btalib_macd = btalib.macd(data2, pfast=12, pslow=26, psignal=9).df
For #2, the different calculations are based on methodology, where btalib tries to rectify issues with TA-Lib (see here, where MACD is one of the examples). The differences vs. Binance will depend on what methodology/library Binance is using.
Upvotes: 1