首页
/ IB-Ruby 项目技术文档

IB-Ruby 项目技术文档

2024-12-23 08:22:36作者:魏献源Searcher

1. 安装指南

1.1 环境准备

  • Ruby 解释器:至少需要 Ruby 2.4 版本,推荐使用 2.5 或更高版本。
  • Interactive Brokers 软件:安装 TWSGateway
  • API 连接配置:确保 TWS 或 Gateway 配置为允许从运行 IB-Ruby 的计算机进行 API 连接。

1.2 安装步骤

  1. 克隆项目仓库:
    $ git clone https://github.com/ib-ruby/ib-ruby
    
  2. 进入项目目录:
    $ cd ib-ruby
    
  3. 安装依赖:
    $ bundle install
    $ bundle update
    
  4. 或者在 Gemfile 中指定:
    gem 'ib-ruby', git: 'https://github.com/ib-ruby/ib-ruby.git'
    

2. 项目使用说明

2.1 基本使用

IB-Ruby 是一个纯 Ruby 实现的 Interactive Brokers API,通过直接使用 socket API 与 TWS 服务器进行通信。它提供了 Order-PrototypesSpread-PrototypesAccount-Abstractions 等功能,简化了与 TWS 的交互。

2.2 示例代码

以下是一个简单的示例,请求并打印账户数据,然后下达一个限价单购买 100 手 WFC:

require 'ib-ruby'
ib = IB::Connection.new( port: 7497 ) do | gw |
  gw.subscribe(:Alert, :AccountValue) { |msg| puts msg.to_human }
  gw.subscribe(:OpenOrder) { |msg| puts "Placed: #{msg.order}!" }
  gw.subscribe(:ExecutionData) { |msg| puts "Filled: #{msg.execution}!" }
end

ib.send_message :RequestAccountData, account_code: 'U123456'
ib.wait_for :AccountDownloadEnd

contract = IB::Stock.new symbol: 'WFC'
buy_order = IB::Limit.order size: 100, price: 21.00, action: :buy,
                              tif: :good_till_cancelled, account_code: 'U123456'

ib.place_order buy_order, contract
ib.wait_for :ExecutionData

3. 项目 API 使用文档

3.1 连接与订阅

  • 连接 TWS 服务器
    ib = IB::Connection.new( port: 7497 )
    
  • 订阅消息
    gw.subscribe(:Alert, :AccountValue) { |msg| puts msg.to_human }
    

3.2 订单操作

  • 创建订单
    contract = IB::Stock.new symbol: 'WFC'
    buy_order = IB::Limit.order size: 100, price: 21.00, action: :buy,
                                  tif: :good_till_cancelled, account_code: 'U123456'
    
  • 下达订单
    ib.place_order buy_order, contract
    

3.3 账户数据请求

  • 请求账户数据
    ib.send_message :RequestAccountData, account_code: 'U123456'
    ib.wait_for :AccountDownloadEnd
    

4. 项目安装方式

4.1 通过 Git 安装

$ git clone https://github.com/ib-ruby/ib-ruby
$ cd ib-ruby
$ bundle install
$ bundle update

4.2 通过 Gemfile 安装

在 Gemfile 中添加:

gem 'ib-ruby', git: 'https://github.com/ib-ruby/ib-ruby.git'

4.3 启动 TWS 或 Gateway

确保在运行 IB-Ruby 之前启动 TWS 或 Gateway,并配置为允许 API 连接。

5. 贡献指南

  1. 创建一个新的 fork。
  2. 克隆你的 fork 到本地。
  3. 添加主仓库为上游:
    git remote add upstream git://github.com/ib-ruby/ib-ruby.git
    
  4. 创建新的特性分支:
    git checkout -b my-new-feature
    
  5. 修改代码并提交更改。
  6. 拉取最新的上游更改:
    git fetch upstream -v
    git merge upstream/master
    
  7. 推送更改到你的分支。
  8. 在 GitHub 上创建新的 Pull Request。

6. 许可证

本软件基于 LGPL 许可证发布,详细信息请参阅 LICENSE 文件。

登录后查看全文
热门项目推荐

热门内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
261
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
860
511
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
259
300
kernelkernel
deepin linux kernel
C
22
5
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
595
57
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
332
1.08 K