首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当尝试在python中下载价格历史数据时出现异常

当尝试在python中下载价格历史数据时出现异常
EN

Stack Overflow用户
提问于 2021-07-16 07:48:56
回答 1查看 263关注 0票数 0

我正在尝试使用forexconnect (可以与pip install forexconnect一起安装)和Python下载历史价格数据。我一直在成功地做到这一点,但突然之间,我的尝试返回了一个错误信息,我无法解释。我的网络连接是通过光纤和工作没有任何问题。

forexconnect的支持团队建议我使用特定的web地址来安装forexconnect。

因此,我进行了如下工作:

卸载了以前的版本

代码语言:javascript
运行
复制
(base) C:\Users\user>pip uninstall forexconnect
Found existing installation: forexconnect 1.6.41
Uninstalling forexconnect-1.6.41:
Would remove:
c:\users\user\anaconda3\lib\site-packages\forexconnect-1.6.41.dist-info\*
c:\users\user\anaconda3\lib\site-packages\forexconnect\*
Proceed (y/n)? y
Successfully uninstalled forexconnect-1.6.41

使用指向(https://test.pypi.org/project/forexconnect/#files):的网站中的pip命令安装了

代码语言:javascript
运行
复制
(base) C:\Users\user>pip install -i https://test.pypi.org/simple/ forexconnect
Looking in indexes: https://test.pypi.org/simple/
Collecting forexconnect
  Downloading https://test-files.pythonhosted.org/packages/db/40/978d5b11e3b144bf77023f624cf607d4fc35b5caaa369fe484bab4ab0765/forexconnect-1.6.41-cp37-cp37m-win_amd64.whl (5.9 MB)
     |████████████████████████████████| 5.9 MB 1.3 MB/s
Installing collected packages: forexconnect
Successfully installed forexconnect-1.6.41

验证了forexconnect:的安装

代码语言:javascript
运行
复制
pip show forexconnect

Name: forexconnect
Version: 1.6.41
Summary: ForexConnect API is a trading API for the FXCM Group: https://www.fxcm.com/uk/
Home-page: https://github.com/gehtsoft/forex-connect
Author: Gehtsoft USA, LLC
Author-email: contact@gehtsoftusa.com
License: Other/Proprietary License
Location: c:\users\user\anaconda3\lib\site-packages
Requires: 
Required-by: 
Note: you may need to restart the kernel to use updated packages.

运行代码下载历史价格数据:

代码语言:javascript
运行
复制
import forexconnect
 
import pandas as pd
 
pair_string = 'USD/CNH'
 
time_interval_string = 'm30'
 
start_date = pd.Timestamp('1990-01-01')
 
from forexconnect import fxcorepy, ForexConnect
 
def session_status_changed(session: fxcorepy.O2GSession,   status: fxcorepy.AO2GSessionStatus.O2GSessionStatus):
    print("Trading session status: " + str(status))
 
with ForexConnect() as fx:
 
    try:

        fx.login("D261296694", "Oop5f", "fxcorporate.com/Hosts.jsp",
                 "Demo", session_status_callback=session_status_changed)

        history = fx.get_history(pair_string, time_interval_string, start_date)

        fx_df = pd.DataFrame(history)

        fx_df['Ticker'] = pair_string

    except Exception as e:
        print("Exception: " + str(e))

    try:
        fx.logout()
    except Exception as e:
        print("Exception: " + str(e))

如前所述收到以下错误消息:

代码语言:javascript
运行
复制
Trading session status: CONNECTING
Trading session status: CONNECTED
Exception: QuotesManager error: HTTP request failed object='/pricearchive/catalog.xml' errorCode=52 code: QuotesServerConnectionError subcode: 52
Trading session status: DISCONNECTING
Trading session status: DISCONNECTED
EN

回答 1

Stack Overflow用户

发布于 2021-07-16 08:29:50

这个片段应该能正常工作:

代码语言:javascript
运行
复制
import datetime as dt

pair_string = 'USD/CNH'

time_interval_string = 'M30'

start_date = dt.datetime.strptime("MM.DD.YYYY HH:MM:SS", '%m.%d.%Y %H:%M:%S').replace(tzinfo=datetime.timezone.utc)
end_date = dt.datetime.strptime("MM.DD.YYYY HH:MM:SS", '%m.%d.%Y %H:%M:%S').replace(tzinfo=datetime.timezone.utc)

with ForexConnect() as fx:
 
    try:

        fx.login("D261296694", "Oop5f", "fxcorporate.com/Hosts.jsp",
                 "Demo", session_status_callback=session_status_changed)

        history = fx.get_history(pair_string, time_interval_string, start_date, end_date)

        fx_df = pd.DataFrame(history)

        fx_df['Ticker'] = pair_string

    except Exception as e:
        print("Exception: " + str(e))

注意,必须通过将"MM.DD.YYYY HH:MM:SS"替换为所需的日期来正确设置日期。Eg "1.1.1990 00:00:00"

完整示例这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68405373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档