mytradewizard 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/mytradewizard.rb +1 -0
- data/lib/mytradewizard/action.rb +8 -1
- data/lib/mytradewizard/daily_bars.rb +4 -0
- data/lib/mytradewizard/future_trading_system.rb +8 -8
- data/lib/mytradewizard/interactive_brokers.rb +83 -6
- data/lib/mytradewizard/order.rb +9 -0
- data/lib/mytradewizard/stock.rb +8 -0
- data/lib/mytradewizard/stock_trading_system.rb +20 -6
- data/lib/mytradewizard/trading_system.rb +30 -4
- data/lib/mytradewizard/version.rb +1 -1
- data/lib/mytradewizard/watch_list.rb +8 -0
- data/lib/mytradewizard/yahoo.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzZmYjUwYzM5NTUxNzZhOTQxZDFhMDJlNDE2YTRkNzk1Yzc2NjZjNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
M2MzMWVjYTAzMDU3NmVhNWQzMDdjYTUzMjI0YzhhZjdlZGM2YTJlZA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGYwNDMzYTIxYzQzZDkxM2ExOTQ0MGQ2OTM1NDA3ZmE1NDU2MjkyNGVlY2U2
|
10
|
+
MGUyMWNmMDM2NDBmMGZhZjVjYTBhZjExMjdkODcyNGNkZDBiNGQ4ZjU0YTNl
|
11
|
+
ZGY1NDBiMThkYTY4ZDdlMWU0MjEwNWE3NGRmZGQ3ZmFjODk2ZTI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGY2OTRlOTk5MDYxMmNmMDQ2ODAwYWFkNDRlYmZjZTcxODY3MmZmYjRkZDVl
|
14
|
+
ODlmMzBmZjM3ZDQ5OTQ0OGRkZWY5ZTE1MzJjMjE3ZmUxYWZlNDcyOWJkODlj
|
15
|
+
NjFhNzE3MzU0NTcyYWEyODYwNzcyNzVjMjllNjYwZDkwMjI2NTM=
|
data/lib/mytradewizard.rb
CHANGED
data/lib/mytradewizard/action.rb
CHANGED
@@ -20,17 +20,17 @@ module MyTradeWizard
|
|
20
20
|
@ib.hourly_bars(@contract, hourly_range, @live)
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
31
|
+
def place_orders(open_time, close_time)
|
31
32
|
unless @action.nil?
|
32
|
-
|
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 =>
|
118
|
-
:good_till_date =>
|
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)
|
data/lib/mytradewizard/stock.rb
CHANGED
@@ -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 :
|
8
|
+
attr_accessor :watchlist
|
9
9
|
attr_writer :position_size
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
super
|
13
|
-
@
|
14
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
18
|
+
make_live
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
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
|
data/lib/mytradewizard/yahoo.rb
CHANGED
@@ -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.
|
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-
|
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
|