|
1 |
| -#火币行情功能函数 |
| 1 | +# HUOBI |
2 | 2 | import json,requests,urllib3,datetime; import pandas as pd; import numpy as np ; urllib3.disable_warnings()
|
3 | 3 |
|
4 |
| -huobi_domain='api.huobi.de.com' #API地址 'api.huobi.pro' api.huobi.be没有被墙 api.huobi.de.com |
| 4 | +huobi_domain='api.huobi.pro' |
5 | 5 |
|
6 |
| -class Context: #根类 |
| 6 | +class Context: |
7 | 7 | def __init__(self):
|
8 | 8 | self.current_dt=datetime.datetime.now()
|
9 | 9 |
|
10 |
| -# #1d:1天 4h:4小时 60m: 60分钟 15m:15分钟 |
| 10 | +#1d:1DAT 4h:4HOUR 60m: 60MIN 15m:15MIN |
11 | 11 | def get_price(code, end_date=None, count=1,frequency='1d', fields=['close']):
|
12 | 12 | code=code.replace('.','')
|
13 | 13 | frequency=frequency.replace('d','day').replace('m','min').replace('h','hour')
|
14 | 14 | url = f'https://{huobi_domain}/market/history/kline?period={frequency}&size={count}&symbol={code}'
|
15 | 15 | res = requests.get(url,verify=False).text
|
16 |
| - df=pd.DataFrame(json.loads(res)['data']); df=df.iloc[::-1] #排序翻转 |
17 |
| - df["time"] = pd.to_datetime(df["id"]+8*3600, unit='s') #时间戳转时间,北京时间+8小时 |
18 |
| - df=df[['time','open','close','high','low','vol']] #更正排序 |
19 |
| - df.set_index(["time"], inplace=True); df.index.name='' #处理索引 |
| 16 | + df=pd.DataFrame(json.loads(res)['data']); df=df.iloc[::-1] |
| 17 | + df["time"] = pd.to_datetime(df["id"]+8*3600, unit='s') |
| 18 | + df=df[['time','open','close','high','low','vol']] |
| 19 | + df.set_index(["time"], inplace=True); df.index.name='' |
20 | 20 | return df
|
21 | 21 |
|
22 |
| -def get_last_price(code): #获取某只股票最新价格函数04-25 |
| 22 | +def get_last_price(code): |
23 | 23 | return get_price(code,count=1,frequency='1m', fields=['close']).close[0]
|
24 | 24 |
|
25 |
| -def attribute_history(security, count, unit='1d', fields=['open', 'close', 'high', 'low', 'volume', 'money']): #获取历史行情 |
| 25 | +def attribute_history(security, count, unit='1d', fields=['open', 'close', 'high', 'low', 'volume', 'money']): |
26 | 26 | return get_price(security = security, end_date = context.current_dt, frequency=unit, fields=fields, fq = fq, count = count)[:-1]
|
27 | 27 |
|
28 | 28 |
|
0 commit comments