Module: IB::Symbols

Included in:
Bonds, CFD, Combo, Commodity, Forex, Futures, Index, Options, Stocks, Unspecified
Defined in:
lib/ib/symbols_base.rb,
lib/ib/symbols.rb,
lib/ib/symbols/cfd.rb,
lib/ib/symbols/bonds.rb,
lib/ib/symbols/combo.rb,
lib/ib/symbols/forex.rb,
lib/ib/symbols/index.rb,
lib/ib/symbols/stocks.rb,
lib/ib/symbols/futures.rb,
lib/ib/symbols/options.rb,
lib/ib/symbols/version.rb,
lib/ib/symbols/abstract.rb,
lib/ib/symbols/commodity.rb

Overview

end

Defined Under Namespace

Modules: Bonds, CFD, Combo, Commodity, Forex, Futures, Index, Options, Stocks, Unspecified Classes: Error

Constant Summary collapse

VERSION =
"1.5"
@@dir =

set the Pathname to “ib-api/symbols” by default

Pathname.new File.expand_path("../../../../symbols/", __FILE__ )

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ib/symbols.rb', line 61

def method_missing(method, *key)
	if key.empty? 
		if contracts.has_key?(method)
			contracts[method]
			elsif methods.include?(:each) && each.methods.include?(method)
					self.each.send method  				
			else
			error "contract #{method} not defined. Try »all« for a list of defined Contracts.", :symbol
		end
	else
		error "method missing"
	end
end

Class Method Details

.allocate_collection(name) ⇒ Object

name might be a string or a symbol



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ib/symbols/abstract.rb', line 36

def self.allocate_collection name  # name might be a string or a symbol
	symbol_table = Module.new do
		extend Symbols
		extend Enumerable
		def self.yml_file
			@@dir + name.to_s.downcase.split("::").last.concat( ".yml" )
		end

		def self.each &b
			contracts.values.each &b
		end
	end   # module new
	name =  name.to_s.camelize.to_sym
	the_collection = if Symbols.send  :const_defined?, name  
			 Symbols.send :const_get, name
			 else
			 Symbols.const_set  name, symbol_table   	
			 end
	if the_collection.is_a? Symbols
		the_collection.send :read_collection if the_collection.all.empty?
		the_collection # return_value
	else
		error "#{the_collection} is already a Class" 
		nil
	end
end

.set_origin(directory) ⇒ Object



29
30
31
32
33
34
# File 'lib/ib/symbols/abstract.rb', line 29

def self.set_origin directory
	p = Pathname.new directory
	@@dir = p if p.directory? 
rescue Errno::ENOENT
	error "Setting up origin for symbol-files --> Directory (#{directory}) does not exist" 
end

Instance Method Details

#[](symbol) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/ib/symbols.rb', line 88

def [] symbol
	if c=contracts[symbol]
		return c
	else
		# symbol probably has not been predefined, tell user about it
		file = self.to_s.split(/::/).last.downcase
		msg = "Unknown symbol :#{symbol}, please pre-define it in lib/ib/symbols/#{file}.rb"
		error msg, :symbol
	end
end

#add_contract(symbol, contract) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ib/symbols/abstract.rb', line 104

def add_contract symbol, contract
	if symbol.is_a? String
		symbol.to_sym
	elsif symbol.is_a? Symbol
		symbol
	else
		symbol.to_i
	end
# ensure that evey Sybmol::xxx.yyy entry has a description
	contract.description =  contract.to_human[1..-2] if contract.description.nil?
	# overwrite contract if existing
	contracts[ symbol ] = contract.essential
	store_collection
end

#allObject



75
76
77
# File 'lib/ib/symbols.rb', line 75

def all
	contracts.keys.sort rescue contracts.keys
end

#bunch(bunch_count = 50, sleeping_time = 1) ⇒ Object

cuts the Collection in ‘bunch_count` pieces. Each bunch is delivered to the block.

Sleeps for ‘sleeping time` between processing bunches

Returns count of created bunches



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ib/symbols/abstract.rb', line 75

def bunch( bunch_count = 50 , sleeping_time = 1)
	en = self.each
	the_size =  en.size
	i =  0 
	loop do
		the_start = i * bunch_count
		the_end =  the_start + bunch_count
		the_end = the_size -1 if the_end >= the_size
		it  = the_start .. the_end
		yield it.map{|x| en.next rescue nil}.compact
		break if  the_end == the_size -1 
		i+=1
		sleep sleeping_time
	end 
	 i -1  # return counts of bunches
end

#contractsObject



81
82
83
84
85
86
87
# File 'lib/ib/symbols.rb', line 81

def contracts
	if @contracts.present?
		@contracts
	else
		@contracts = Hash.new
	end
end

#hardcoded?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/ib/symbols.rb', line 58

def hardcoded?
	!self.methods.include? :yml_file
end


78
79
80
# File 'lib/ib/symbols.rb', line 78

def print_all
	puts contracts.sort.map{|x,y| [x,y.description].join(" -> ")}.join "\n"
end

#purge_collectionObject



63
64
65
66
# File 'lib/ib/symbols/abstract.rb', line 63

def purge_collection
	yml_file.delete
	@contracts =  nil
end

#read_collectionObject



92
93
94
95
96
97
98
# File 'lib/ib/symbols/abstract.rb', line 92

def read_collection
	if  yml_file.exist?
		contracts.merge! YAML.load_file yml_file rescue contracts
	else
		 yml_file.open( "w"){}
	end
end

#remove_contract(symbol) ⇒ Object



119
120
121
122
# File 'lib/ib/symbols/abstract.rb', line 119

def remove_contract symbol
	@contracts.delete  symbol 
	store_collection
end

#store_collectionObject



100
101
102
# File 'lib/ib/symbols/abstract.rb', line 100

def store_collection
	 yml_file.open( 'w' ){|f| f.write @contracts.to_yaml}
end

#to_humanObject



125
126
127
# File 'lib/ib/symbols/abstract.rb', line 125

def to_human
	self.to_s.split("::").last
end