Module: IB::OrderPrototype

Instance Method Summary collapse

Instance Method Details

#aliasesObject



88
89
90
# File 'lib/ib/order-prototypes.rb', line 88

def aliases
	{  total_quantity: :size }
end

#alternative_parametersObject



73
74
75
# File 'lib/ib/order-prototypes.rb', line 73

def alternative_parameters
	{}
end

#defaultsObject



80
81
82
# File 'lib/ib/order-prototypes.rb', line 80

def defaults
	{  tif: :good_till_cancelled }
end

#optionalObject



84
85
86
# File 'lib/ib/order-prototypes.rb', line 84

def optional
	{ account: 'Account(number) to trade on' }
end

#order(**fields) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ib/order-prototypes.rb', line 43

def order **fields

	# special treatment of size:  positive numbers --> buy order, negative: sell 
	if fields[:size].present? && fields[:action].blank?
		error "Size = 0 is not possible" if fields[:size].zero?
		fields[:action] = fields[:size] >0 ? :buy  : :sell
		fields[:size] = fields[:size].abs
	end
	# change aliases  to the original. We are modifying the fields-hash.
	fields.keys.each{|x| fields[aliases.key(x)] = fields.delete(x) if aliases.has_value?(x)}
	# inlcude defaults (arguments override defaults)
	the_arguments = defaults.merge fields
	# check if requirements are fullfilled
	necessary = requirements.keys.detect{|y| the_arguments[y].nil?}
	if necessary.present?
		msg =self.name + ".order -> A necessary field is missing: #{necessary}: --> #{requirements[necessary]}"
		error msg, :args, nil
	end
	if alternative_parameters.present?
		unless ( alternative_parameters.keys  & the_arguments.keys ).size == 1
			msg =self.name + ".order -> One of the alternative fields needs to be specified: \n\t:" +
				"#{alternative_parameters.map{|x| x.join ' => '}.join(" or \n\t:")}"
			error msg, :args, nil
		end
	end

	# initialise order with given attributes	
	IB::Order.new **the_arguments
end

#parametersObject



92
93
94
95
96
97
98
# File 'lib/ib/order-prototypes.rb', line 92

def parameters
	the_output = ->(var){ var.map{|x| x.join(" --> ") }.join("\n\t: ")}

	"Required : " + the_output[requirements] + "\n --------------- \n" +
		"Optional : " + the_output[optional] + "\n --------------- \n" 

end

#requirementsObject



76
77
78
# File 'lib/ib/order-prototypes.rb', line 76

def requirements
	{ action: IB::VALUES[:side], total_quantity: 'also aliased as :size' }
end