mytradewizard 0.0.4 → 0.0.5

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
- NjUzNjhhZjAwM2M3OTI2NDEzYjMxNzM0ODU4NDI1NjIxNzBlYzNlNw==
4
+ ZGM5MDcyYjkzMmQ1NzQ5YzRlYmI1NmQ1MWVlODRmZGU0NGYxNjVjYg==
5
5
  data.tar.gz: !binary |-
6
- ZjE4MjgzMDc1MmZhMTZkZmIwMzVhMWRmNWU5ODQwZjNmOGQ2MzVhYQ==
6
+ M2I3OGY0ODI3YzBiNzU4YTNhNjIwZjg0NGY1YWQ4OGYyZGNmMTVhYQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YWY1MmNkNDc4MzQ2MWIxOTdjYjQ4ZDg5ZjFlNGEwMzIwYzViNWI3YzY2ODI4
10
- MjAyYjA0ZGNiZjUzNDVmNmQzYjM4NmIyNDBmMDY4ZmFlYjNjZDZiNDdiODJk
11
- ZWNhMDgzNDY0MzMwOWZhYzUwM2IzMTI4YWZlYzk4YjRmMzc0ZTY=
9
+ MzI5YmJlN2I2MjM0NGI1YmUxODI2OGJmNmVlMjFhMWZhM2UyMzgyMWI1ZmJm
10
+ MmFhYzVjYTRmMjhlNGQyZjUwMDJiNDZiMzNhMmNkZDYyNzJjNGExOTU5YmI0
11
+ MjEyYzY4M2I2YzVkNTUwMDQ1NmU5MmE3OWUzMDgzNDU4NzFlMmI=
12
12
  data.tar.gz: !binary |-
13
- NTJlMDIzZDM4MjBjMDg5MDlmNDUyNWIyMzJlMDZlZTM2MzM3Zjk0MjhkNGRk
14
- ODZmNGMwNThhNmZiMzM1MjQwZWZhNjA1YzBiZDhlMWZmMWQ1NTI3MjQyNjQ1
15
- MGI2ZjUyNTY5ZmRkNGIzNjY2NDFiOTA0Mjk3NTk5MjM1YjgyYTY=
13
+ NTVmZGI2OWY4ZWM3ODQzMzdlNDNkNzA4MDg2ZDQ4MzljM2NhNGJmMGY2YjZm
14
+ N2Y0OWU2NTQzMjYzZjg3NzYyYzljZDIzYTI2NTU2M2ZlMzlkN2FkMWYwNDE3
15
+ YjcxZGZkZDk4MmIzYzYxZmE3MzM2ZjJjZTIzYTMyNTA3OTQ3Zjg=
@@ -0,0 +1,29 @@
1
+ module MyTradeWizard
2
+ class DailyBars
3
+
4
+ def initialize
5
+ @hash = Hash.new
6
+ end
7
+
8
+ def length
9
+ @hash.length
10
+ end
11
+
12
+ def add(day, bar)
13
+ @hash[day] = bar
14
+ end
15
+
16
+ def today
17
+ @hash.keys.max
18
+ end
19
+
20
+ def day(day)
21
+ @hash[day]
22
+ end
23
+
24
+ def days(daily_range)
25
+ @hash.select { |day, bar| daily_range.begin <= day && day <= daily_range.end }.values
26
+ end
27
+
28
+ end
29
+ end
@@ -1,28 +1,32 @@
1
1
  module MyTradeWizard
2
- class DateTime
3
- class << self
2
+ module DateTime
4
3
 
5
- def WaitUntil(hour, minute)
6
- print "Waiting until #{hour}:#{minute} ..."
7
- until time_is(hour, minute)
8
- sleep 60
9
- print '.'
10
- end
4
+ HR = 360
5
+
6
+ def wait_until(hour, minute)
7
+ print "Waiting until #{hour}:#{minute} ..."
8
+ until time_is(hour, minute)
9
+ sleep 60
10
+ print '.'
11
11
  end
12
+ end
12
13
 
13
- private
14
+ def friday
15
+ Time.now.utc.wday == 5
16
+ end
14
17
 
15
- def time_is(hour, minute)
16
- if (1..5).include?(Time.now.utc.wday)
17
- if Time.now.utc.hour == hour
18
- if Time.now.utc.min >= minute
19
- return true
20
- end
18
+ private
19
+
20
+ def time_is(hour, minute)
21
+ if (1..5).include?(Time.now.utc.wday)
22
+ if Time.now.utc.hour == hour
23
+ if Time.now.utc.min >= minute
24
+ return true
21
25
  end
22
26
  end
23
- false
24
27
  end
25
-
28
+ false
26
29
  end
30
+
27
31
  end
28
32
  end
@@ -0,0 +1,38 @@
1
+ require 'mytradewizard'
2
+
3
+ module MyTradeWizard
4
+ class FutureTradingSystem < MyTradeWizard::TradingSystem
5
+
6
+ attr_writer :quantity
7
+
8
+ def initialize
9
+ super
10
+ @quantity = 0
11
+ @action = nil
12
+ end
13
+
14
+ def symbol=(s)
15
+ @contract = @ib.front_month(s)
16
+ puts @contract
17
+ end
18
+
19
+ def hours(hourly_range)
20
+ @ib.hourly_bars(@contract, hourly_range, @live)
21
+ end
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
28
+ end
29
+
30
+ def place_closing_trade_between(good_after, good_till)
31
+ unless @action.nil?
32
+ print "Closing trade: "
33
+ @ib.place_market_order_between(@account, @action.reverse, @quantity, @contract, good_after, good_till)
34
+ end
35
+ end
36
+
37
+ end
38
+ end
@@ -91,7 +91,8 @@ module MyTradeWizard
91
91
  if msg.message_type == :PortfolioValue
92
92
  if msg.contract.sec_type == :stock
93
93
  stock = MyTradeWizard::Stock.new(msg.contract.symbol)
94
- p << stock
94
+ size = msg.position
95
+ p << MyTradeWizard::Position.new(stock, size)
95
96
  end
96
97
  end
97
98
  end
@@ -99,7 +100,17 @@ module MyTradeWizard
99
100
  p
100
101
  end
101
102
 
102
- def place_market_order(account, action, quantity, contract, good_after, good_till)
103
+ def place_market_order(account, action, quantity, contract)
104
+ order = IB::Order.new :total_quantity => quantity,
105
+ :action => action.to_s,
106
+ :order_type => 'MKT',
107
+ :account => account
108
+ @ib_ruby.wait_for :NextValidId
109
+ @ib_ruby.place_order order, contract
110
+ puts "#{action} #{quantity} #{contract.symbol}"
111
+ end
112
+
113
+ def place_market_order_between(account, action, quantity, contract, good_after, good_till)
103
114
  order = IB::Order.new :total_quantity => quantity,
104
115
  :action => action.to_s,
105
116
  :order_type => 'MKT',
@@ -15,5 +15,10 @@ module MyTradeWizard
15
15
  def close
16
16
  @hash['Close']
17
17
  end
18
+ #def sign
19
+ # return 1 if close > open
20
+ # return -1 if close < open
21
+ # return 0 if close == open
22
+ #end
18
23
  end
19
24
  end
@@ -0,0 +1,13 @@
1
+ module MyTradeWizard
2
+ class Position
3
+
4
+ attr_reader :stock
5
+ attr_reader :size
6
+
7
+ def initialize(stock, size)
8
+ @stock = stock
9
+ @size = size
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,47 @@
1
+ require 'mytradewizard'
2
+
3
+ module MyTradeWizard
4
+ class StockTradingSystem < MyTradeWizard::TradingSystem
5
+
6
+ include MyTradeWizard::WatchLists
7
+
8
+ attr_accessor :watch_list
9
+ attr_writer :position_size
10
+
11
+ def initialize
12
+ super
13
+ @watch_list = MyTradeWizard::WatchList.new
14
+ @position_size = 5000
15
+ @data = nil
16
+ end
17
+
18
+ def preload(days, stock)
19
+ @stock = stock
20
+ @data = MyTradeWizard::Yahoo.OHLC(stock.symbol, days)
21
+ if @data.length < days
22
+ puts "ERROR: Only #{@data.length}/#{days} days of data for #{stock.symbol}"
23
+ end
24
+ end
25
+
26
+ def t
27
+ @data.today
28
+ end
29
+
30
+ def days(daily_range)
31
+ @data.days(daily_range)
32
+ end
33
+
34
+ def close(day)
35
+ @data.day(day).close
36
+ end
37
+
38
+ def buy
39
+ place_market_order "BUY", @position_size/close(t).floor, @stock.contract
40
+ end
41
+
42
+ def sell(position)
43
+ place_market_order "SELL", position.size, @stock.contract
44
+ end
45
+
46
+ end
47
+ end
@@ -1,6 +1,5 @@
1
1
  module MyTradeWizard
2
2
  module TechnicalIndicator
3
- class << self
4
3
 
5
4
  def SMA(bars)
6
5
  x = 0
@@ -12,6 +11,5 @@ module MyTradeWizard
12
11
  return x / n
13
12
  end
14
13
 
15
- end
16
14
  end
17
15
  end
@@ -1,9 +1,29 @@
1
1
  module MyTradeWizard
2
2
  class TradingSystem
3
3
 
4
+ include MyTradeWizard::DateTime
5
+ include MyTradeWizard::TechnicalIndicator
6
+
4
7
  def initialize
5
8
  @ib = MyTradeWizard::InteractiveBrokers.new
6
9
  @ib_ruby = @ib.connect
10
+ @live = false
11
+ @account = @ib.accounts.first
12
+ fetch_positions
13
+ end
14
+
15
+ def account=(a)
16
+ @live = true
17
+ @account = a
18
+ fetch_positions
19
+ end
20
+
21
+ def fetch_positions
22
+ @positions = @ib.positions(@account)
23
+ end
24
+
25
+ def place_market_order(action, quantity, contract)
26
+ @ib.place_market_order(@account, action, quantity, contract)
7
27
  end
8
28
 
9
29
  end
@@ -1,3 +1,3 @@
1
1
  module MyTradeWizard
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -3,8 +3,9 @@ module MyTradeWizard
3
3
 
4
4
  attr_accessor :stocks
5
5
 
6
- def initialize
6
+ def initialize(stocks = [])
7
7
  @stocks = []
8
+ self << stocks
8
9
  end
9
10
 
10
11
  def <<(x)
@@ -0,0 +1,9 @@
1
+ module MyTradeWizard
2
+ module WatchLists
3
+
4
+ DOW30 = MyTradeWizard::WatchList.new %w(MMM AXP T BA CAT CVX CSCO KO DD XOM GE GS HD INTC IBM JNJ JPM MCD MRK MSFT NKE PFE PG TRV UNH UTX VZ V WMT DIS)
5
+ NAZ100 = MyTradeWizard::WatchList.new %w(FOXA ATVI ADBE AKAM ALXN ALTR AMZN AMGN ADI AAPL AMAT ADSK ADP AVGO BIDU BBBY BIIB BRCM CHRW CA CTRX CELG CERN CHTR CHKP CSCO CTXS CTSH CMCSA COST DELL XRAY DTV DISCA DLTR EBAY EQIX EXPE EXPD ESRX FFIV FB FAST FISV FOSL GRMN GILD GOOG GMCR HSIC INTC INTU ISRG KLAC KRFT LBTYA LINTA LMCA LLTC MAT MXIM MCHP MU MSFT MDLZ MNST MYL NTAP NFLX NUAN NVDA ORLY PCAR PAYX PCLN QCOM GOLD REGN ROST SNDK SBAC STX SHLD SIAL SIRI SPLS SBUX SRCL SYMC TSLA TXN VRSK VRTX VIAB VOD WDC WFM WYNN XLNX YHOO)
6
+ SP500 = MyTradeWizard::WatchList.new %w(MMM ABT ABBV ANF ACE ACN ACT ADBE ADT AMD AES AET AFL A GAS APD ARG AKAM AA ALXN ATI AGN ALL ALTR MO AMZN AEE AEP AXP AIG AMT AMP ABC AMGN APH APC ADI AON APA AIV AAPL AMAT ADM AIZ T ADSK ADP AN AZO AVB AVY AVP BHI BLL BAC BK BCR BAX BBT BEAM BDX BBBY BMS BRK.B BBY BIIB BLK HRB BA BWA BXP BSX BMY BRCM BF.B CHRW CA CVC COG CAM CPB COF CAH CFN KMX CCL CAT CBG CBS CELG CNP CTL CERN CF SCHW CHK CVX CMG CB CI CINF CTAS CSCO C CTXS CLF CLX CME CMS COH KO CCE CTSH CL CMCSA CMA CSC CAG COP CNX ED STZ GLW COST COV CCI CSX CMI CVS DHI DHR DRI DVA DE DELL DLPH DAL DNR XRAY DVN DO DTV DFS DISCA DG DLTR D DOV DOW DPS DTE DD DUK DNB ETFC EMN ETN EBAY ECL EIX EW EA EMC EMR ESV ETR EOG EQT EFX EQR EL EXC EXPE EXPD ESRX XOM FFIV FDO FAST FDX FIS FITB FSLR FE FISV FLIR FLS FLR FMC FTI F FRX FOSL BEN FCX FTR GME GCI GPS GRMN GD GE GIS GM GPC GNW GILD GS GT GOOG GWW HAL HOG HAR HRS HIG HAS HCP HCN HP HES HPQ HD HON HRL HSP HST HCBK HUM HBAN ITW IR TEG INTC ICE IBM IGT IP IPG IFF INTU ISRG IVZ IRM JBL JEC JDSU JNJ JCI JOY JPM JNPR KSU K KEY KMB KIM KMI KLAC KSS KRFT KR LTD LLL LH LRCX LM LEG LEN LUK LIFE LLY LNC LLTC LMT L LO LOW LSI LYB MTB MAC M MRO MPC MAR MMC MAS MA MAT MKC MCD MHFI MCK MJN MWV MDT MRK MET MCHP MU MSFT MOLX TAP MDLZ MON MNST MCO MS MOS MSI MUR MYL NBR NDAQ NOV NTAP NFLX NWL NFX NEM NWSA NEE NLSN NKE NI NE NBL JWN NSC NTRS NOC NU NRG NUE NVDA NYX ORLY OXY OMC OKE ORCL OI PCG PCAR PLL PH PDCO PAYX BTU JCP PNR PBCT POM PEP PKI PRGO PETM PFE PM PSX PNW PXD PBI PCL PNC RL PPG PPL PX PCP PCLN PFG PG PGR PLD PRU PEG PSA PHM PVH QEP PWR QCOM DGX RRC RTN RHT REGN RF RSG RAI RHI ROK COL ROP ROST RDC R SWY SAI CRM SNDK SCG SLB SNI STX SEE SRE SHW SIAL SPG SLM SJM SNA SO LUV SWN SE STJ SWK SPLS SBUX HOT STT SRCL SYK STI SYMC SYY TROW TGT TEL TE THC TDC TER TSO TXN TXT HSY TRV TMO TIF TWX TWC TJX TMK TSS TRIP FOXA TSN TYC USB UNP UNH UPS X UTX UNM URBN VFC VLO VAR VTR VRSN VZ VIAB V VNO VMC WMT WAG DIS WPO WM WAT WLP WFC WDC WU WY WHR WFM WMB WIN WEC WPX WYN WYNN XEL XRX XLNX XL XYL YHOO YUM ZMH ZION ZTS)
7
+
8
+ end
9
+ end
@@ -17,8 +17,8 @@ module MyTradeWizard
17
17
  day = -1
18
18
  end
19
19
  row = 1
20
- bars = Hash.new
21
- while (bars.length < days)
20
+ daily_bars = MyTradeWizard::DailyBars.new
21
+ while (daily_bars.length < days)
22
22
  bar = Hash.new
23
23
  csv[row].each_with_index do |col, i|
24
24
  name = header[i]
@@ -32,11 +32,11 @@ module MyTradeWizard
32
32
  end
33
33
  bar[name] = value
34
34
  end
35
- bars[day] = MyTradeWizard::OHLC.new(bar)
35
+ daily_bars.add(day, MyTradeWizard::OHLC.new(bar))
36
36
  row += 1
37
37
  day -= 1
38
38
  end
39
- bars
39
+ daily_bars
40
40
  end
41
41
 
42
42
  private
data/lib/mytradewizard.rb CHANGED
@@ -3,12 +3,17 @@ require 'mytradewizard/interactive_brokers'
3
3
  require 'mytradewizard/cme'
4
4
  require 'mytradewizard/yahoo'
5
5
  require 'mytradewizard/ohlc'
6
+ require 'mytradewizard/daily_bars'
6
7
  require 'mytradewizard/date_time'
7
8
  require 'mytradewizard/stock'
8
9
  require 'mytradewizard/watch_list'
10
+ require 'mytradewizard/watch_lists'
9
11
  require 'mytradewizard/technical_indicator'
10
12
  require 'mytradewizard/trading_system'
13
+ require 'mytradewizard/stock_trading_system'
14
+ require 'mytradewizard/future_trading_system'
11
15
  require 'mytradewizard/action'
16
+ require 'mytradewizard/position'
12
17
 
13
18
  module MyTradeWizard
14
19
  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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mytradewizard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-11 00:00:00.000000000 Z
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -189,16 +189,21 @@ files:
189
189
  - lib/mytradewizard/action.rb
190
190
  - lib/mytradewizard/cli.rb
191
191
  - lib/mytradewizard/cme.rb
192
+ - lib/mytradewizard/daily_bars.rb
192
193
  - lib/mytradewizard/date_time.rb
194
+ - lib/mytradewizard/future_trading_system.rb
193
195
  - lib/mytradewizard/generators/config/interactive_brokers.rb
194
196
  - lib/mytradewizard/generators/interactive_brokers.rb
195
197
  - lib/mytradewizard/interactive_brokers.rb
196
198
  - lib/mytradewizard/ohlc.rb
199
+ - lib/mytradewizard/position.rb
197
200
  - lib/mytradewizard/stock.rb
201
+ - lib/mytradewizard/stock_trading_system.rb
198
202
  - lib/mytradewizard/technical_indicator.rb
199
203
  - lib/mytradewizard/trading_system.rb
200
204
  - lib/mytradewizard/version.rb
201
205
  - lib/mytradewizard/watch_list.rb
206
+ - lib/mytradewizard/watch_lists.rb
202
207
  - lib/mytradewizard/yahoo.rb
203
208
  - mytradewizard.gemspec
204
209
  - spec/cme_spec.rb