标题: [问题求助] 循环变量运行求改Python代码 [打印本页]
作者: keshun 时间: 2020-7-1 21:19 标题: 循环变量运行求改Python代码
- #! /usr/bin/env python3
- # -*- coding: UTF-8 -*-
- 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] # ma移动平均
- 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("关闭窗口")
复制代码
现在代码单独运算可以,如果弄多几个均线计算就太麻烦了。我想要循环运行15分钟,1小时,4小时,1天的均线,麻烦各位大神了。
有两组变量(900,3600,14400, 86400),(15M,1H,4H,1D)
granularity=86400&size=22 result = "1H MA5:{} MA20:{}\r\n"
作者: leo.liu 时间: 2020-7-2 19:56
你用的什么运行库,什么库
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |