mytradewizard 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGM5MDcyYjkzMmQ1NzQ5YzRlYmI1NmQ1MWVlODRmZGU0NGYxNjVjYg==
4
+ YzZmYjUwYzM5NTUxNzZhOTQxZDFhMDJlNDE2YTRkNzk1Yzc2NjZjNA==
5
5
  data.tar.gz: !binary |-
6
- M2I3OGY0ODI3YzBiNzU4YTNhNjIwZjg0NGY1YWQ4OGYyZGNmMTVhYQ==
6
+ M2MzMWVjYTAzMDU3NmVhNWQzMDdjYTUzMjI0YzhhZjdlZGM2YTJlZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzI5YmJlN2I2MjM0NGI1YmUxODI2OGJmNmVlMjFhMWZhM2UyMzgyMWI1ZmJm
10
- MmFhYzVjYTRmMjhlNGQyZjUwMDJiNDZiMzNhMmNkZDYyNzJjNGExOTU5YmI0
11
- MjEyYzY4M2I2YzVkNTUwMDQ1NmU5MmE3OWUzMDgzNDU4NzFlMmI=
9
+ OGYwNDMzYTIxYzQzZDkxM2ExOTQ0MGQ2OTM1NDA3ZmE1NDU2MjkyNGVlY2U2
10
+ MGUyMWNmMDM2NDBmMGZhZjVjYTBhZjExMjdkODcyNGNkZDBiNGQ4ZjU0YTNl
11
+ ZGY1NDBiMThkYTY4ZDdlMWU0MjEwNWE3NGRmZGQ3ZmFjODk2ZTI=
12
12
  data.tar.gz: !binary |-
13
- NTVmZGI2OWY4ZWM3ODQzMzdlNDNkNzA4MDg2ZDQ4MzljM2NhNGJmMGY2YjZm
14
- N2Y0OWU2NTQzMjYzZjg3NzYyYzljZDIzYTI2NTU2M2ZlMzlkN2FkMWYwNDE3
15
- YjcxZGZkZDk4MmIzYzYxZmE3MzM2ZjJjZTIzYTMyNTA3OTQ3Zjg=
13
+ OGY2OTRlOTk5MDYxMmNmMDQ2ODAwYWFkNDRlYmZjZTcxODY3MmZmYjRkZDVl
14
+ ODlmMzBmZjM3ZDQ5OTQ0OGRkZWY5ZTE1MzJjMjE3ZmUxYWZlNDcyOWJkODlj
15
+ NjFhNzE3MzU0NTcyYWEyODYwNzcyNzVjMjllNjYwZDkwMjI2NTM=
data/lib/mytradewizard.rb CHANGED
@@ -14,6 +14,7 @@ require 'mytradewizard/stock_trading_system'
14
14
  require 'mytradewizard/future_trading_system'
15
15
  require 'mytradewizard/action'
16
16
  require 'mytradewizard/position'
17
+ require 'mytradewizard/order'
17
18
 
18
19
  module MyTradeWizard
19
20
  end
@@ -2,7 +2,14 @@ module MyTradeWizard
2
2
  class Action
3
3
 
4
4
  def initialize(signal)
5
- @signal = signal
5
+ case signal
6
+ when "BUY"
7
+ @signal = 1
8
+ when "SELL"
9
+ @signal = -1
10
+ else
11
+ @signal = signal
12
+ end
6
13
  end
7
14
 
8
15
  def to_s
@@ -17,6 +17,10 @@ module MyTradeWizard
17
17
  @hash.keys.max
18
18
  end
19
19
 
20
+ def most_recent
21
+ @hash[today]
22
+ end
23
+
20
24
  def day(day)
21
25
  @hash[day]
22
26
  end
@@ -20,17 +20,17 @@ module MyTradeWizard
20
20
  @ib.hourly_bars(@contract, hourly_range, @live)
21
21
  end
22
22
 
23
- def place_opening_trade_between(good_after, good_till)
24
- unless @action.nil?
25
- print "Opening trade: "
26
- @ib.place_market_order_between(@account, @action, @quantity, @contract, good_after, good_till)
27
- end
23
+ def actionize(signal)
24
+ @action = MyTradeWizard::Action.new(signal)
25
+ end
26
+
27
+ def action
28
+ @action
28
29
  end
29
30
 
30
- def place_closing_trade_between(good_after, good_till)
31
+ def place_orders(open_time, close_time)
31
32
  unless @action.nil?
32
- print "Closing trade: "
33
- @ib.place_market_order_between(@account, @action.reverse, @quantity, @contract, good_after, good_till)
33
+ @ib.place_market_orders(@account, @action, @quantity, @contract, open_time, close_time)
34
34
  end
35
35
  end
36
36
 
@@ -87,7 +87,7 @@ module MyTradeWizard
87
87
 
88
88
  def positions(account)
89
89
  p = Array.new
90
- @ib_ruby.subscribe(:AccountValue, :PortfolioValue, :AccountUpdateTime, :AccountDownloadEnd) do |msg|
90
+ @ib_ruby.subscribe(:Alert, :AccountValue, :PortfolioValue, :AccountUpdateTime, :AccountDownloadEnd) do |msg|
91
91
  if msg.message_type == :PortfolioValue
92
92
  if msg.contract.sec_type == :stock
93
93
  stock = MyTradeWizard::Stock.new(msg.contract.symbol)
@@ -96,26 +96,46 @@ module MyTradeWizard
96
96
  end
97
97
  end
98
98
  end
99
- @ib_ruby.send_message :RequestAccountData, :account_code => account
99
+ @ib_ruby.send_message :RequestAccountData, :subscribe => true, :account_code => account
100
+ @ib_ruby.wait_for :AccountDownloadEnd
101
+ @ib_ruby.send_message :RequestAccountData, :subscribe => false, :account_code => account
102
+ @ib_ruby.received[:AccountDownloadEnd].clear
100
103
  p
101
104
  end
102
105
 
106
+ def buying_power(account)
107
+ buying_power = nil
108
+ @ib_ruby.subscribe(:Alert, :AccountValue, :PortfolioValue, :AccountUpdateTime, :AccountDownloadEnd) do |msg|
109
+ if msg.message_type == :AccountValue
110
+ if msg.data[:key] == "BuyingPower"
111
+ buying_power = msg.data[:value]
112
+ end
113
+ end
114
+ end
115
+ @ib_ruby.send_message :RequestAccountData, :subscribe => true, :account_code => account
116
+ @ib_ruby.wait_for :AccountDownloadEnd
117
+ @ib_ruby.send_message :RequestAccountData, :subscribe => false, :account_code => account
118
+ @ib_ruby.received[:AccountDownloadEnd].clear
119
+ buying_power.to_f
120
+ end
121
+
103
122
  def place_market_order(account, action, quantity, contract)
104
123
  order = IB::Order.new :total_quantity => quantity,
105
124
  :action => action.to_s,
106
125
  :order_type => 'MKT',
126
+ :tif => 'OPG',
107
127
  :account => account
108
128
  @ib_ruby.wait_for :NextValidId
109
129
  @ib_ruby.place_order order, contract
110
- puts "#{action} #{quantity} #{contract.symbol}"
130
+ #puts "#{action} #{quantity} #{contract.symbol}"
111
131
  end
112
132
 
113
133
  def place_market_order_between(account, action, quantity, contract, good_after, good_till)
114
134
  order = IB::Order.new :total_quantity => quantity,
115
135
  :action => action.to_s,
116
136
  :order_type => 'MKT',
117
- :good_after_time => "#{Time.now.utc.strftime('%Y%m%d')} #{good_after}",
118
- :good_till_date => "#{Time.now.utc.strftime('%Y%m%d')} #{good_till}",
137
+ :good_after_time => good_after,
138
+ :good_till_date => good_till,
119
139
  :tif => 'GTD',
120
140
  :account => account
121
141
  @ib_ruby.wait_for :NextValidId
@@ -123,12 +143,69 @@ module MyTradeWizard
123
143
  puts "#{action} #{quantity} #{contract.symbol} @ #{good_after}"
124
144
  end
125
145
 
146
+ def place_market_orders(account, action, quantity, contract, open_time, close_time)
147
+ good_after_open = Time.parse(open_time).strftime("%Y%m%d %H:%M:%S %Z")
148
+ good_till_open = (Time.parse(open_time) + 60).strftime("%Y%m%d %H:%M:%S %Z")
149
+ good_after_close = Time.parse(close_time).strftime("%Y%m%d %H:%M:%S %Z")
150
+ good_till_close = (Time.parse(close_time) + 60).strftime("%Y%m%d %H:%M:%S %Z")
151
+ if quantity.is_a? Fixnum
152
+ place_market_order_between(account, action, quantity, contract, good_after_open, good_till_open)
153
+ @ib_ruby.subscribe(:OrderStatus, :OpenOrderEnd) do |msg|
154
+ if msg.message_type == :OrderStatus
155
+ if msg.status == 'Submitted'
156
+ place_market_order_between(account, action.reverse, quantity, contract, good_after_close, good_till_close)
157
+ elsif msg.status == 'Cancelled'
158
+ puts "CANCELLED: #{action} #{quantity} #{contract.symbol} @ #{open_time}"
159
+ end
160
+ end
161
+ end
162
+ @ib_ruby.send_message :RequestAllOpenOrders
163
+ @ib_ruby.wait_for :OrderStatus
164
+ elsif quantity.is_a? Range
165
+ quantity.each do |q|
166
+ if q == quantity.first
167
+ place_market_order_between(account, action, q, contract, good_after_open, good_till_open)
168
+ @ib_ruby.subscribe(:OrderStatus, :OpenOrderEnd) do |msg|
169
+ if msg.message_type == :OrderStatus
170
+ if msg.status == 'Submitted'
171
+ place_market_order_between(account, action.reverse, q, contract, good_after_close, good_till_close)
172
+ elsif msg.status == 'Cancelled'
173
+ puts "CANCELLED: #{action} #{q} #{contract.symbol} @ #{open_time}"
174
+ break
175
+ end
176
+ end
177
+ end
178
+ @ib_ruby.send_message :RequestAllOpenOrders, :subscribe => true, :account_code => account
179
+ @ib_ruby.wait_for :OrderStatus
180
+ @ib_ruby.send_message :RequestAllOpenOrders, :subscribe => false, :account_code => account
181
+ @ib_ruby.received[:OrderStatus].clear
182
+ else
183
+ place_market_order_between(account, action, 1, contract, good_after_open, good_till_open)
184
+ @ib_ruby.subscribe(:OrderStatus, :OpenOrderEnd) do |msg|
185
+ if msg.message_type == :OrderStatus
186
+ if msg.status == 'Submitted'
187
+ place_market_order_between(account, action.reverse, 1, contract, good_after_close, good_till_close)
188
+ elsif msg.status == 'Cancelled'
189
+ puts "CANCELLED: #{action} 1 #{contract.symbol} @ #{open_time}"
190
+ break
191
+ end
192
+ end
193
+ end
194
+ @ib_ruby.send_message :RequestAllOpenOrders, :subscribe => true, :account_code => account
195
+ @ib_ruby.wait_for :OrderStatus
196
+ @ib_ruby.send_message :RequestAllOpenOrders, :subscribe => false, :account_code => account
197
+ @ib_ruby.received[:OrderStatus].clear
198
+ end
199
+ end
200
+ end
201
+ end
202
+
126
203
  def front_month(symbol)
127
204
  expiry = MyTradeWizard::CME.first_month(symbol)
128
205
  if invalid contract(symbol, expiry)
129
206
  expiry = MyTradeWizard::CME.second_month(symbol)
130
207
  if invalid contract(symbol, expiry)
131
- raise ErrorDeterminingFrontMonth
208
+ raise 'ErrorDeterminingFrontMonth'
132
209
  end
133
210
  end
134
211
  contract(symbol, expiry)
@@ -0,0 +1,9 @@
1
+ module MyTradeWizard
2
+ class Order
3
+
4
+ attr_accessor :action
5
+ attr_accessor :quantity
6
+ attr_accessor :stock
7
+
8
+ end
9
+ end
@@ -11,5 +11,13 @@ module MyTradeWizard
11
11
  @contract = IB::Contract.new(:sec_type => :stock, :symbol => symbol, :currency => "USD")
12
12
  end
13
13
 
14
+ def ==(other)
15
+ other.class == self.class && other.symbol == symbol
16
+ end
17
+
18
+ def price
19
+ MyTradeWizard::Yahoo.OHLC(symbol, 1).most_recent.close
20
+ end
21
+
14
22
  end
15
23
  end
@@ -5,13 +5,13 @@ module MyTradeWizard
5
5
 
6
6
  include MyTradeWizard::WatchLists
7
7
 
8
- attr_accessor :watch_list
8
+ attr_accessor :watchlist
9
9
  attr_writer :position_size
10
10
 
11
11
  def initialize
12
12
  super
13
- @watch_list = MyTradeWizard::WatchList.new
14
- @position_size = 5000
13
+ @watchlist = MyTradeWizard::WatchList.new
14
+ #@position_size = 5000
15
15
  @data = nil
16
16
  end
17
17
 
@@ -35,12 +35,26 @@ module MyTradeWizard
35
35
  @data.day(day).close
36
36
  end
37
37
 
38
- def buy
39
- place_market_order "BUY", @position_size/close(t).floor, @stock.contract
38
+ def buy(stock)
39
+ order = MyTradeWizard::Order.new
40
+ order.action = MyTradeWizard::Action.new("BUY")
41
+ order.stock = stock
42
+ return order
40
43
  end
41
44
 
42
45
  def sell(position)
43
- place_market_order "SELL", position.size, @stock.contract
46
+ output "SELL #{position.size} #{position.stock.symbol}"
47
+ place_market_order "SELL", position.size, position.stock.contract
48
+ end
49
+
50
+ def place(orders)
51
+ total_buying_power = @ib.buying_power(@account)
52
+ buying_power_per_order = total_buying_power / orders.length
53
+ orders.each do |order|
54
+ order.quantity = (buying_power_per_order / order.stock.price).floor
55
+ output "#{order.action} #{order.quantity} #{order.stock.symbol}"
56
+ place_market_order order.action.to_s, order.quantity, order.stock.contract
57
+ end
44
58
  end
45
59
 
46
60
  end
@@ -9,16 +9,16 @@ module MyTradeWizard
9
9
  @ib_ruby = @ib.connect
10
10
  @live = false
11
11
  @account = @ib.accounts.first
12
- fetch_positions
12
+ #fetch_positions
13
+ @orders = []
13
14
  end
14
15
 
15
16
  def account=(a)
16
- @live = true
17
17
  @account = a
18
- fetch_positions
18
+ make_live
19
19
  end
20
20
 
21
- def fetch_positions
21
+ def positions
22
22
  @positions = @ib.positions(@account)
23
23
  end
24
24
 
@@ -26,5 +26,31 @@ module MyTradeWizard
26
26
  @ib.place_market_order(@account, action, quantity, contract)
27
27
  end
28
28
 
29
+ def make_live
30
+ @live = true
31
+ @logger = Logger.new("/mtw/log/#{self.class}/#{Time.now.strftime('%Y%m%d-%H%M')}.log")
32
+ #fetch_positions
33
+ end
34
+
35
+ def at(hour, minute)
36
+ wait_until(hour, minute) if @live
37
+ end
38
+
39
+ def idle(seconds)
40
+ sleep(seconds) if @live
41
+ end
42
+
43
+ def output(msg)
44
+ if @logger.present?
45
+ @logger.info msg
46
+ else
47
+ puts msg
48
+ end
49
+ end
50
+
51
+ def test?
52
+ !@live
53
+ end
54
+
29
55
  end
30
56
  end
@@ -1,3 +1,3 @@
1
1
  module MyTradeWizard
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -20,5 +20,13 @@ module MyTradeWizard
20
20
  end
21
21
  end
22
22
 
23
+ def each(&block)
24
+ @stocks.each(&block)
25
+ end
26
+
27
+ def remove(positions)
28
+ @stocks.reject { |stock| positions.any? { |position| position.stock == stock } }
29
+ end
30
+
23
31
  end
24
32
  end
@@ -46,6 +46,7 @@ module MyTradeWizard
46
46
  end
47
47
 
48
48
  def url(symbol, start_month, start_day, start_year, end_month, end_day, end_year, frequency)
49
+ symbol = symbol.gsub(" ", "-")
49
50
  query_string = "?s=#{symbol}&a=#{start_month}&b#{start_day}&c=#{start_year}&d=#{end_month}&e=#{end_day}&f=#{end_year}&g=#{frequency}"
50
51
  url = 'http://ichart.finance.yahoo.com/table.csv' + query_string
51
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mytradewizard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - mytradewizard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-01 00:00:00.000000000 Z
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -196,6 +196,7 @@ files:
196
196
  - lib/mytradewizard/generators/interactive_brokers.rb
197
197
  - lib/mytradewizard/interactive_brokers.rb
198
198
  - lib/mytradewizard/ohlc.rb
199
+ - lib/mytradewizard/order.rb
199
200
  - lib/mytradewizard/position.rb
200
201
  - lib/mytradewizard/stock.rb
201
202
  - lib/mytradewizard/stock_trading_system.rb