| | | | | import pandas as pd | | import requests | | import json | | import numpy as np | | api_url = "https://www.okex.me/v2/spot/instruments/BTC-USDT/candles?granularity=86400&size=22" | | html = requests.get(api_url) | | col_header = 0 | | stock_datas = pd.DataFrame() | | for i in json.loads(html.text)['data']: | | col_header += 1 | | sort = str(col_header) | | df_insert = pd.DataFrame( | | {'sort': sort, '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 = [5, 20] | | for ma in ma_list: | | stock_datas['MA_' + str(ma)] = np.round(pd.Series.rolling(stock_datas['close'], window=ma).mean(), 1) | | result = stock_datas[stock_datas['sort'] == '22'][['MA_5', 'MA_20']] | | if result.__len__() > 0: | | result = "1D MA5:{} MA20:{}\r\n".format(result['MA_5'].values[0], result['MA_20'].values[0],) | | print(result) | | with open('a.txt', 'a+') as f: | | f.write(result) | | else: | | print("Found nothing!!!") | | input("关闭窗口")COPY |
现在代码单独运算可以,如果弄多几个均线计算就太麻烦了。我想要循环运行15分钟,1小时,4小时,1天的均线,麻烦各位大神了。
有两组变量(900,3600,14400, 86400),(15M,1H,4H,1D)
granularity=86400&size=22 result = "1H MA5:{} MA20:{}\r\n" |