#!/usr/bin/env ruby # # This script allows you to cancel either a set of open Orders by their ids, # or ALL Orders opened via IB API at once. The latter is useful when your # robot goes crazy and opens gazillions of wrong limit orders. require 'rubygems' require 'pathname' require 'bundler/setup' require 'ib-ruby' # First, connect to IB TWS. ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS # Subscribe to TWS alerts/errors and order-related messages ib.subscribe(:Alert, :OpenOrder, :OrderStatus, :OpenOrderEnd) { |msg| puts msg.to_human } if ARGV.empty? ib.send_message :RequestGlobalCancel else # Will only work for Orders placed under the same :client_id ib.cancel_order *ARGV end ib.send_message :RequestAllOpenOrders sleep 3