本帖最后由 ivor 于 2020-6-14 14:21 编辑
测试过数据完全准确
开始用的这个接口:https://www.okex.me/docs/zh/#spot-line,发现数据不正确, 后面发现原因日期有错位
后改用这个不算官方正规的接口:https://www.okex.me/v2/spot/inst ... amp;t=1592091928445
granularity=86400 时间粒度,86400日线
size=1000 最大k线数量,不能超过1000否则报错。- #! /usr/bin/env python3
- # -*- coding: UTF-8 -*-
-
-
- import pandas as pd
- import requests
- import json
- import numpy as np
- import datetime
-
- api_url = "https://www.okex.me/v2/spot/instruments/BTC-USDT/candles?granularity=86400&size=1000&t=1592091928445"
-
- html = requests.get(api_url)
- stock_datas = pd.DataFrame()
-
- for i in json.loads(html.text)['data']:
- date = (datetime.datetime.strptime(i[0].split('T')[0], "%Y-%m-%d") + datetime.timedelta(days=1)).strftime("%Y%m%d")
- df_insert = pd.DataFrame(
- {'date': date, 'open': i[1], 'high': i[2], 'low': i[3], 'close': np.float(i[4]), 'volume': i[5]}, index=[0])
- stock_datas = stock_datas.append(df_insert)
-
- ma_list = [7, 30] # ma移动平均
-
- for ma in ma_list:
- stock_datas['MA_' + str(ma)] = np.round(pd.Series.rolling(stock_datas['close'], window=ma).mean(), 1)
-
- low_list = stock_datas['low'].rolling(9, min_periods=9).min()
- low_list.fillna(value=stock_datas['low'].expanding().min(), inplace=True)
- high_list = stock_datas['high'].rolling(9, min_periods=9).max()
- high_list.fillna(value=stock_datas['high'].expanding().max(), inplace=True)
- rsv = (stock_datas['close'] - low_list) / (high_list - low_list) * 100
-
- stock_datas['K'] = pd.Series.ewm(rsv, com=2).mean()
- stock_datas['D'] = pd.Series.ewm(stock_datas['K'], com=2).mean()
- stock_datas['J'] = 3 * stock_datas['K'] - 2 * stock_datas['D']
-
- custom_date = input("""
- Example:20200401 2020年04月01日
- Enter datetime please:"""
- )
- result = stock_datas[stock_datas['date'] == custom_date][['date', 'K', 'D', 'J', 'MA_7', 'MA_30']]
- if result.__len__() > 0:
- result = "MA7:{}\r\nMA30:{}\r\nK:{}\r\nD:{}\r\nJ:{}\r\n".format(result['MA_7'].values[0], result['MA_30'].values[0],
- np.round(result['K'].values[0], 2),
- np.round(result['D'].values[0], 2),
- np.round(result['J'].values[0], 2))
- print(result)
- with open('a.txt', 'w+') as f:
- f.write(result)
- else:
- print("Found nothing!!!")
复制代码
Example:20200401 2020年04月01日
Enter datetime please:20200401
MA7:6372.9
MA30:6767.3
K:49.17
D:57.07
J:33.38 |