#!/usr/bin/env ruby # # This script places GOOG option butterfly combo order require 'rubygems' require 'bundler/setup' $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'ib-ruby' # Utility method that helps us build multi-legged (BAG) Orders def butterfly symbol, expiry, right, *strikes raise 'No Connection!' unless @ib && @ib.connected? legs = strikes.zip([1, -2, 1]).map do |strike, weight| # Create contract contract = IB::Option.new :symbol => symbol, :expiry => expiry, :right => right, :strike => strike # Find out contract's con_id @ib.clear_received :ContractData, :ContractDataEnd @ib.send_message :RequestContractData, :id => strike, :contract => contract @ib.wait_for :ContractDataEnd, 3 con_id = @ib.received[:ContractData].last.contract.con_id # Create Comboleg from con_id and weight IB::ComboLeg.new :con_id => con_id, :weight => weight end # Create new Combo contract IB::Bag.new :symbol => symbol, :currency => "USD", # Only US options in combo Contracts :exchange => "SMART", :legs => legs end # First, connect to IB TWS. Arbitrary :client_id is used to identify your script @ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS # Subscribe to TWS alerts/errors and order-related messages @ib.subscribe(:Alert, :OpenOrder, :OrderStatus) { |msg| puts msg.to_human } @ib.wait_for :NextValidId # Create multi-legged option Combo using utility method above combo = butterfly 'GOOG', '201301', 'CALL', 500, 510, 520 # Create Order stub order = IB::Order.new :total_quantity => 10, # 10 butterflies :limit_price => 0.01, # at 0.01 x 100 USD per contract :action => 'BUY', :order_type => 'LMT' @ib.place_order order, combo @ib.wait_for [:OpenOrder, 3], [:OrderStatus, 2] puts "\n******** Press to cancel... *********\n\n" gets