本文主要是介绍2.4 Binance_interface APP 现货行情-历史K线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Binance_interface APP 现货行情-历史K线
- Github地址
- PyTed量化交易研究院
目录
- Binance_interface APP 现货行情-历史K线
- 1. APP 现货行情-历史K线函数总览
- 2. 模型实例化
- 3. 获取产品的历史K线数据 get_history_candle
- 4. 获取产品指定数量的最新历史K线数据 get_history_candle_latest
- 5. 获取产品指定日期的历史K线数据 get_history_candle_by_date
- 6. 获取历史K线数据中最新的毫秒时间戳 get_history_candle_latest_ts
- 7. 更新产品历史K线数据到指定时间 update_history_candle
- 8. 将Binance的candle转化为DataFrame candle_to_df
1. APP 现货行情-历史K线函数总览
方法 | 解释 |
---|---|
get_history_candle | 获取产品的历史K线数据 |
get_history_candle_latest | 获取产品指定数量的最新历史K线数据 |
get_history_candle_by_date | 获取产品指定日期的历史K线数据 |
get_history_candle_latest_ts | 获取历史K线数据中最新的毫秒时间戳 |
update_history_candle | 更新产品历史K线数据到指定时间 |
candle_to_df | 将Binance的candle转化为DataFrame |
2. 模型实例化
from binance_interface.app import BinanceSPOT
from binance_interface.app.utils import eprint
import paux.date
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'binanceSPOT = BinanceSPOT(key=key,secret=secret,proxy_host=proxy_host,timezone='Asia/Shanghai',
)
market = binanceSPOT.market
3. 获取产品的历史K线数据 get_history_candle
candle_result = market.get_history_candle(symbol='BTCUSDT',start='2023-01-01 00:00:00',end='2023-01-01 23:59:00',bar='1m',
)
eprint(candle_result)
输出:
>>> {'code': 200,
>>> 'data': array([[1.67250240e+12, 1.65901000e+04, 1.65969700e+04, ...,
>>> 7.80654600e+01, 1.29542721e+06, 0.00000000e+00],
>>> [1.67250246e+12, 1.65960900e+04, 1.65973100e+04, ...,
>>> 4.77967500e+01, 7.93208688e+05, 0.00000000e+00],
>>> [1.67250252e+12, 1.65965800e+04, 1.65977700e+04, ...,
>>> 5.80070800e+01, 9.62726268e+05, 0.00000000e+00],
>>> ...,
>>> [1.67258862e+12, 1.65549300e+04, 1.65585000e+04, ...,
>>> 4.06363700e+01, 6.72814001e+05, 0.00000000e+00],
>>> [1.67258868e+12, 1.65578200e+04, 1.65599900e+04, ...,
>>> 4.00219800e+01, 6.62708243e+05, 0.00000000e+00],
>>> [1.67258874e+12, 1.65589800e+04, 1.65597700e+04, ...,
>>> 4.60841800e+01, 7.63086276e+05, 0.00000000e+00]]),
>>> 'msg': ''}
4. 获取产品指定数量的最新历史K线数据 get_history_candle_latest
candle_result = market.get_history_candle_latest(symbol='BTCUSDT',length=600,bar='1m',
)
eprint(candle_result)
输出:
>>> {'code': 200,
>>> 'data': array([[1.70605188e+12, 3.95820100e+04, 3.95983100e+04, ...,
>>> 1.11380100e+01, 4.40831314e+05, 0.00000000e+00],
>>> [1.70605194e+12, 3.95983100e+04, 3.96278200e+04, ...,
>>> 1.59375200e+01, 6.31265989e+05, 0.00000000e+00],
>>> [1.70605200e+12, 3.96235500e+04, 3.96235500e+04, ...,
>>> 3.86567000e+00, 1.53123466e+05, 0.00000000e+00],
>>> ...,
>>> [1.70608770e+12, 4.01000000e+04, 4.01049500e+04, ...,
>>> 8.39811000e+00, 3.36733060e+05, 0.00000000e+00],
>>> [1.70608776e+12, 4.00526100e+04, 4.00958300e+04, ...,
>>> 1.75785300e+01, 7.04393469e+05, 0.00000000e+00],
>>> [1.70608782e+12, 4.00710000e+04, 4.00958300e+04, ...,
>>> 1.23730500e+01, 4.95945541e+05, 0.00000000e+00]]),
>>> 'msg': ''}
5. 获取产品指定日期的历史K线数据 get_history_candle_by_date
candle_result = market.get_history_candle_by_date(symbol='BTCUSDT',date='2023-01-01', # 默认时区为美国时区bar='1m',
)
eprint(candle_result)
输出:
>>> {'code': 200,
>>> 'data': array([[1.67250240e+12, 1.65901000e+04, 1.65969700e+04, ...,
>>> 7.80654600e+01, 1.29542721e+06, 0.00000000e+00],
>>> [1.67250246e+12, 1.65960900e+04, 1.65973100e+04, ...,
>>> 4.77967500e+01, 7.93208688e+05, 0.00000000e+00],
>>> [1.67250252e+12, 1.65965800e+04, 1.65977700e+04, ...,
>>> 5.80070800e+01, 9.62726268e+05, 0.00000000e+00],
>>> ...,
>>> [1.67258862e+12, 1.65549300e+04, 1.65585000e+04, ...,
>>> 4.06363700e+01, 6.72814001e+05, 0.00000000e+00],
>>> [1.67258868e+12, 1.65578200e+04, 1.65599900e+04, ...,
>>> 4.00219800e+01, 6.62708243e+05, 0.00000000e+00],
>>> [1.67258874e+12, 1.65589800e+04, 1.65597700e+04, ...,
>>> 4.60841800e+01, 7.63086276e+05, 0.00000000e+00]]),
>>> 'msg': ''}
6. 获取历史K线数据中最新的毫秒时间戳 get_history_candle_latest_ts
ts_result = market.get_history_candle_latest_ts(bar='1m',
)
eprint(ts_result)
输出:
>>> {'code': 200, 'data': 1706087820000.0, 'msg': ''}
7. 更新产品历史K线数据到指定时间 update_history_candle
# 获取candle1,待更新
candle1 = market.get_history_candle(symbol='BTCUSDT',start='2023-01-01 10:00:00',end='2023-01-02 10:00:00',bar='1m',
)['data']
# 更新candle1到指定日期时间(智能节约权重)
candle_result = market.update_history_candle(candle=candle1, # 支持candle1为空symbol='BTCUSDT',length=1440, # 保留数量end='2023-01-02 23:59:00', # end默认为本地计算机时间戳bar='1m',
)
eprint(candle_result)candle_start = paux.date.to_fmt(candle_result['data'][0, 0],timezone='Asia/Shanghai',
)candle_end = paux.date.to_fmt(candle_result['data'][-1, 0],timezone='Asia/Shanghai',
)candle_length = candle_result['data'].shape[0]print('历史K线时间起点:', candle_start)
print('历史K线时间终点:', candle_end)
print('历史K线时间长度:', candle_length)
输出:
>>> {'code': 200,
>>> 'data': array([[1.67258880e+12, 1.65587300e+04, 1.65610900e+04, ...,
>>> 7.35524600e+01, 1.21800174e+06, 0.00000000e+00],
>>> [1.67258886e+12, 1.65595700e+04, 1.65653700e+04, ...,
>>> 7.42564800e+01, 1.22986767e+06, 0.00000000e+00],
>>> [1.67258892e+12, 1.65648400e+04, 1.65846600e+04, ...,
>>> 2.65374950e+02, 4.39791119e+06, 0.00000000e+00],
>>> ...,
>>> [1.67267502e+12, 1.67370500e+04, 1.67387700e+04, ...,
>>> 2.54655500e+01, 4.26162346e+05, 0.00000000e+00],
>>> [1.67267508e+12, 1.67331500e+04, 1.67342900e+04, ...,
>>> 2.68978800e+01, 4.50016189e+05, 0.00000000e+00],
>>> [1.67267514e+12, 1.67301100e+04, 1.67378900e+04, ...,
>>> 7.75499100e+01, 1.29767302e+06, 0.00000000e+00]]),
>>> 'msg': ''}
>>> 历史K线时间起点: 2023-01-02 00:00:00
>>> 历史K线时间终点: 2023-01-02 23:59:00
>>> 历史K线时间长度: 1440
8. 将Binance的candle转化为DataFrame candle_to_df
candle = candle_result['data']
candle
输出:
>>> array([[1.67258880e+12, 1.65587300e+04, 1.65610900e+04, ...,
>>> 7.35524600e+01, 1.21800174e+06, 0.00000000e+00],
>>> [1.67258886e+12, 1.65595700e+04, 1.65653700e+04, ...,
>>> 7.42564800e+01, 1.22986767e+06, 0.00000000e+00],
>>> [1.67258892e+12, 1.65648400e+04, 1.65846600e+04, ...,
>>> 2.65374950e+02, 4.39791119e+06, 0.00000000e+00],
>>> ...,
>>> [1.67267502e+12, 1.67370500e+04, 1.67387700e+04, ...,
>>> 2.54655500e+01, 4.26162346e+05, 0.00000000e+00],
>>> [1.67267508e+12, 1.67331500e+04, 1.67342900e+04, ...,
>>> 2.68978800e+01, 4.50016189e+05, 0.00000000e+00],
>>> [1.67267514e+12, 1.67301100e+04, 1.67378900e+04, ...,
>>> 7.75499100e+01, 1.29767302e+06, 0.00000000e+00]])
df = market.candle_to_df(candle)
df.head()
输出:
>>> openTs open high low close volume \
>>> 0 2023-01-02 00:00:00 16558.73 16561.09 16558.00 16559.17 139.80668
>>> 1 2023-01-02 00:01:00 16559.57 16565.37 16558.45 16565.31 156.18004
>>> 2 2023-01-02 00:02:00 16564.84 16584.66 16563.68 16574.35 448.30474
>>> 3 2023-01-02 00:03:00 16574.35 16574.52 16565.43 16566.26 196.98567
>>> 4 2023-01-02 00:04:00 16566.70 16566.70 16562.03 16563.20 96.06997
>>>
>>> closeTs turnover tradeNum buyVolume buyTurnover
>>> 0 2023-01-02 00:00:59 2.315116e+06 4247.0 73.55246 1.218002e+06
>>> 1 2023-01-02 00:01:59 2.586717e+06 3350.0 74.25648 1.229868e+06
>>> 2 2023-01-02 00:02:59 7.429515e+06 7990.0 265.37495 4.397911e+06
>>> 3 2023-01-02 00:03:59 3.263886e+06 5183.0 104.42095 1.730175e+06
>>> 4 2023-01-02 00:04:59 1.591356e+06 2601.0 39.03212 6.465452e+05
这篇关于2.4 Binance_interface APP 现货行情-历史K线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!