执行部署的数据检索

像往常一样,先导入并配置一些 Python 包。 In [1]: import os import time import numpy as np import pandas as pd from pprint import pprint from pylab import plt, mpl plt.style.use('seaborn') mpl.rcParams['savefig.dpi'] = 300 mpl.rcParams['font.family'] = 'serif' pd.set_option('mode.chained_assignment', None) pd.set_option('display.float_format', '{:.5f}'.format) np.set_printoptions(suppress=True, precision=4) os.environ['PYTHONHASHSEED'] = '0' 根据账户的相关管辖区,Oanda 提供了许多可交易的外汇和差价合约工具。以下 Python 代 码会检索给定账户的可用工具。286 | 第 12 章 In [2]: import tpqoa ➊ In [3]: api = tpqoa.tpqoa('../aiif.cfg') ➋ In [4]: ins = api.get_instruments() ➌ In [5]: ins[:5] ➍ Out[5]: [('AUD/CAD', 'AUD_CAD'), ('AUD/CHF', 'AUD_CHF'), ('AUD/CNY', 'AUD_CNY'), ('AUD/JPY', 'AUD_JPY'), ('AUD/NZD', 'AUD_NZD')] ➊ 导入 tpqoa 包。 ➋ 实例化给定账户凭证的 API 对象。 ➌ 以 (display_name, technical_name) 格式检索可用工具的列表。 ➍ 显示选择的少量工具。 Oanda 通过其 v20 API 提供了丰富的历史数据。以下示例会检索数据粒度被设置为 D(每 日)的欧元 / 美元货币对的历史数据。 图 12-1 展示了收盘价(卖出价)。 In [6]: raw = api.get_history(instrument='EUR_USD', ➊ start='2018-01-01', ➋ end='2020-07-31', ➌ granularity='D', ➍ price='A') ➎ In [7]: raw.info() DatetimeIndex: 671 entries, 2018-01-01 22:00:00 to 2020-07-30 21:00:00 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 o 671 non-null float64 1 h 671 non-null float64 2 l 671 non-null float64 3 c 671 non-null float64 4 volume 671 non-null int64 5 complete 671 non-null bool dtypes: bool(1), float64(4), int64(1) memory usage: 32.1 KB In [8]: raw.head() Out[8]: o h l c volume complete time 2018-01-01 22:00:00 1.20101 1.20819 1.20051 1.20610 35630 True 2018-01-02 22:00:00 1.20620 1.20673 1.20018 1.20170 31354 True 2018-01-03 22:00:00 1.20170 1.20897 1.20049 1.20710 35187 True 2018-01-04 22:00:00 1.20692 1.20847 1.20215 1.20327 36478 True 2018-01-07 22:00:00 1.20301 1.20530 1.19564 1.19717 27618 True In [9]: raw['c'].plot(figsize=(10, 6));执行与部署 | 287 ➊ 指定工具…… ➋ ……开始日期…… ➌ ……结束日期…… ➍ ……粒度(D = 每日)…… ➎ ……价格系列的类型(A = 要价)。 时间 2018年5月 2018年8月 2019年1月 2019年5月 2019年9月 2020年1月 2020年5月 2020年9月 2018年1月 图 12-1:Oanda 欧元 / 美元的每日收盘价历史数据 正如下面的代码所示,与日数据一样,日内数据很容易被检索和使用。图 12-2 显示了分钟 (中间)价格数据。 In [10]: raw = api.get_history(instrument='EUR_USD', start='2020-07-01', end='2020-07-31', granularity='M1', ➊ price='M') ➋ In [11]: raw.info() DatetimeIndex: 30728 entries, 2020-07-01 00:00:00 to 2020-07-30 23:59:00 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 o 30728 non-null float64 1 h 30728 non-null float64 2 l 30728 non-null float64 3 c 30728 non-null float64 4 volume 30728 non-null int64 5 complete 30728 non-null bool dtypes: bool(1), float64(4), int64(1) memory usage: 1.4 MB 288 | 第 12 章 In [12]: raw.tail() Out[12]: o h l c volume complete time 2020-07-30 23:55:00 1.18724 1.18739 1.18718 1.18738 57 True 2020-07-30 23:56:00 1.18736 1.18758 1.18722 1.18757 57 True 2020-07-30 23:57:00 1.18756 1.18756 1.18734 1.18734 49 True 2020-07-30 23:58:00 1.18736 1.18737 1.18713 1.18717 36 True 2020-07-30 23:59:00 1.18718 1.18724 1.18714 1.18722 31 True In [13]: raw['c'].plot(figsize=(10, 6)); ➊ 指定粒度(M1 = 1 分钟)…… ➋ ……和价格系列的类型(M = 中间价)。 时间 2020年7月5日 2020年7月9日 2020年7月13日 2020年7月17日 2020年7月21日 2020年7月25日 2020年7月29日 2020年8月1日 2020年7月1日 图 12-2:Oanda 欧元 / 美元的 1 分钟收盘价历史数据 对训练和测试一款交易机器人来说,历史数据很重要,但对进行算法交易的交易机器人来 说,实时(流)数据的部署是必要的。tpqoa 允许通过单一方法调用为所有可用的工具同 步实时数据流。该方法会默认打印时间戳和出价 / 要价。对算法交易来说,这种默认方式 是可以调整的,如 12.5 节所示。 In [14]: api.stream_data('EUR_USD', stop=10) 2020-08-13T12:07:09.735715316Z 1.18328 1.18342 2020-08-13T12:07:16.245253689Z 1.18329 1.18343 2020-08-13T12:07:16.397803785Z 1.18328 1.18342 2020-08-13T12:07:17.240232521Z 1.18331 1.18346 2020-08-13T12:07:17.358476854Z 1.18334 1.18348 2020-08-13T12:07:17.778061207Z 1.18331 1.18345 2020-08-13T12:07:18.016544856Z 1.18333 1.18346 2020-08-13T12:07:18.144762415Z 1.18334 1.18348 2020-08-13T12:07:18.689365678Z 1.18331 1.18345 2020-08-13T12:07:19.148039139Z 1.18331 1.18345

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值