- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试通过 Ibpy 从 Interactive Brokers (IB) 获取历史数据。我已经为这个任务尝试了几个脚本,这些脚本是我从其他表明它应该工作的人那里改编而来的。但是,它们都不适合我!我是 python 的新手,所以我承认我对这些方法的工作原理并不完全了解——但是,我应该尝试最明显的修复。下面我列出了我尝试过的两个脚本。我正在使用 python 2x。
在交易平台我有以下设置:
选中:启用 ActiveX 和套接字客户端。未选中:启用 DDE 客户端。未选中:只读 API。选中:在连接时下载打开的订单。选中:发送投资组合时包括外汇头寸。选中:发送 EEP 的状态更新。套接字端口 = 7496。checked:使用负数绑定(bind)自动订单。未选中:创建 API 消息日志文件。未选中:在 API 日志文件中包含市场数据。日志记录级别 = 错误。主 API 客户端 ID = 222。向 API 发送批量数据的超时时间为 30 秒。组分交换分隔符 = 空白。选中:仅允许来自本地主机的连接。
API - 检查的预防措施:绕过 API 订单的订单预防措施。此选项卡中的其他所有内容均未选中。
当我运行 python 脚本时,我已经登录并运行了 TWS,并且与其他人在网上所说的相比,上面的 TWS API 设置似乎是正确的。我有一个订阅美股数据的真实IB账户。还应该提到的是,我也尝试运行另一个通过 IBPY 下订单的脚本 - 这有效,所以问题似乎只存在(至少目前)关于获取历史数据。
脚本 1:
from time import sleep, strftime, localtime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
new_symbolinput = ['AAPL']
newDataList = []
dataDownload = []
def historical_data_handler(msg):
global newDataList
print (msg.reqId, msg.date, msg.close)
if ('finished' in str(msg.date)) == False:
new_symbol = new_symbolinput[msg.reqId]
dataStr = '%s, %s, %s' % (new_symbol, strftime("%Y-%m-%d", localtime(int(msg.date))), msg.close)
newDataList = newDataList + [dataStr]
else:
new_symbol = new_symbolinput[msg.reqId]
filename = 'minutetrades' + new_symbol + '.csv'
csvfile = open('IBdata/' + filename,'w')
for item in newDataList:
csvfile.write('{} \n'.format(item))
csvfile.close()
newDataList = []
global dataDownload
dataDownload.append(new_symbol)
con = ibConnection(port=7496, clientId=222)
con.register(historical_data_handler, message.historicalData)
con.connect()
symbol_id = 0
for i in new_symbolinput:
print (i)
qqq = Contract()
qqq.m_symbol = i
qqq.m_secType = 'STK'
qqq.m_exchange = 'SMART'
qqq.m_currency = 'USD'
con.reqHistoricalData(symbol_id, qqq, '20161101', '1 W', '1 D', 'MIDPOINT', 1, 2)
symbol_id = symbol_id + 1
sleep(10)
print (dataDownload)
filename = 'downloaded_symbols.csv'
csvfile = open('IBdata/' + filename,'w')
for item in dataDownload:
csvfile.write('%s \n' % item)
csvfile.close()
这应该返回一个 csv 文件中的数据。 csv 文件已创建,但它是空的。
响应:
Server Version: 76
TWS Time at connection:20170315 14:18:06 CET
AAPL
[]
所以它显然没有返回任何东西。
脚本 2:
from time import sleep, strftime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
def my_account_handler(msg):
print(msg)
def my_tick_handler(msg):
print(msg)
def my_hist_data_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection(port=7496,clientId=222)
con.register(my_account_handler, 'UpdateAccountValue')
con.register(my_tick_handler, message.tickSize, message.tickPrice)
con.register(my_hist_data_handler, message.historicalData)
con.connect()
print(con.isConnected())
def inner():
qqqq = Contract()
qqqq.m_secType = "STK"
qqqq.m_symbol = "AAPL"
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART"
endtime = strftime('%Y%m%d %H:%M:%S')
print(endtime)
print(con.reqHistoricalData(1,qqqq,endtime,"1 W","1 D","MIDPOINT",1,2))
sleep(10)
inner()
sleep(5)
print('disconnected', con.disconnect())
print(con.isConnected())
这里的响应:
Server Version: 76
TWS Time at connection:20170315 14:29:53 CET
True
20170315 14:30:05
None
('disconnected', True)
False
再次没有任何返回。我不知道为什么,因为它似乎对其他人有用。我可能错过了一些基本的东西,因为我对 Python 还很陌生?
非常感谢任何帮助。
最佳答案
始终实现错误处理程序,API 会告诉您出了什么问题。在这种情况下,它表示使用“1 天”作为条形大小。
没必要 sleep 。使用 nextValidId
了解连接何时准备就绪。使用不同的结束方法来了解您何时完成。 historicalDataEnd
似乎还没有在 IBpy 中实现,所以只需查找“完成”
不要关闭 api 日志记录,它会显示错误以及发送到 TWS 和从 TWS 发送的所有不同消息。您可以关闭日志文件中的市场数据,因为它很多。在您的 jts 目录中查找文件“api.222.Wed.log”。
from time import sleep, strftime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
import pandas as pd
import numpy as np
def nextValidId_handler(msg):
print(msg)
inner()
hist = []
def my_hist_data_handler(msg):
print(msg)
if "finished" in msg.date:
print('disconnecting', con.disconnect())
df = df = pd.DataFrame(index=np.arange(0, len(hist)), columns=('date', 'close', 'volume'))
for index, msg in enumerate(hist):
df.loc[index,'date':'volume'] = msg.date, msg.close, msg.volume
print(df )
else:
hist.append(msg)
def error_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection(port=7497,clientId=222)
con.register(error_handler, message.Error)
con.register(nextValidId_handler, message.nextValidId)
con.register(my_hist_data_handler, message.historicalData)
con.connect()
print(con.isConnected())
def inner():
qqqq = Contract()
qqqq.m_secType = "STK"
qqqq.m_symbol = "AAPL"
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART"
endtime = strftime('%Y%m%d %H:%M:%S')
print(endtime)
con.reqHistoricalData(1,qqqq,endtime,"1 W","1 day","MIDPOINT",1,2)
print(con.isConnected())
关于python - IBPY 中的 reqHistoricalData 不返回任何内容 [python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811393/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!