#! /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/ltc-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!!!")