# -*- coding: utf-8 -*-
import time
import requests
import json
import talib
# 币安交易所API
url = 'https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=5m&limit=1000'
while True:
try:
# 获取K线数据
response = requests.get(url).text
klines = json.loads(response)
# 计算macd
close = [float(x[4]) for x in klines]
macd, signal, hist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
# 判断顶背离
if macd[-1] > signal[-1] and macd[-2] < signal[-2]:
print('发现顶背离!')
# 判断底背离
if macd[-1] < signal[-1] and macd[-2] > signal[-2]:
print('发现底背离!')
# 每5分钟检测一次
time.sleep(300)
except Exception as e:
print(e)
里插入代码片