Skip to content

Commit

Permalink
Re-adding davek42's braket order example
Browse files Browse the repository at this point in the history
  • Loading branch information
arvicco committed Oct 31, 2012
1 parent 4870e00 commit 16310b1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rvm use jruby-1.6.7@ib-ruby --create
rvm use jruby-1.7.0@ib-ruby --create
62 changes: 62 additions & 0 deletions example/place_bracket_order
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env ruby
#
# This script places WFC bracketed buy order.
# Two child orders are attached:
# STOP order
# LIMIT order (profit target)

require 'rubygems'
require 'bundler/setup'
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'ib-ruby'

# 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 }

symbol = IB::Symbols::Stocks[:wfc]

order_price = 33.80
stop_price = order_price - 0.25
profit_price = order_price + 0.25

#-- Parent Order --
buy_order = IB::Order.new :total_quantity => 100,
:limit_price => order_price,
:action => 'BUY',
:order_type => 'LMT',
:algo_strategy => '',
:transmit => false
ib.wait_for :NextValidId

#-- Child STOP --
stop_order = IB::Order.new :total_quantity => 100,
:limit_price => 0,
:aux_price => stop_price,
:action => 'SELL',
:order_type => 'STP',
:parent_id => buy_order.local_id,
:transmit => true
#-- Profit LMT
profit_order = IB::Order.new :total_quantity => 100,
:limit_price => profit_price,
:action => 'SELL',
:order_type => 'LMT',
:parent_id => buy_order.local_id,
:transmit => true

# place parent order
ib.place_order buy_order, symbol
stop_order.parent_id = buy_order.local_id
profit_order.parent_id = buy_order.local_id

# place child orders
ib.place_order stop_order, symbol
ib.place_order profit_order, symbol

ib.send_message :RequestAllOpenOrders

puts "\n******** Press <Enter> to cancel... *********\n\n"
STDIN.gets

0 comments on commit 16310b1

Please sign in to comment.