Class: IB::Alert

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/alerts/base-alert.rb,
lib/ib/alerts/order-alerts.rb,
lib/ib/alerts/gateway-alerts.rb,
lib/ib/gateway/account-infos.rb

Class Method Summary collapse

Class Method Details

.add_orderstate_alert(*codes) ⇒ Object

IB::Alert#AddOrderstateAlert

The OrderState-Record is used to record the history of the order. If selected Alert-Messages appear, they are added to the Order.order_state-Array. The last Status is available as Order.order_state, all states are accessible by Order.order_states

The TWS-Message-text is stored to the »warning-text«-field. The Status is always »rejected«. If the first OrderState-object of a Order is »rejected«, the order is not placed at all. Otherwise only the last action is not applied and the order is unchanged.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ib/alerts/order-alerts.rb', line 38

def add_orderstate_alert  *codes
	codes.each do |n|
		class_eval <<-EOD
			 def self.alert_#{n} msg

					 if msg.error_id.present?
							IB::Gateway.current.account_data do | account |
									order= account.locate_order( local_id: msg.error_id )
									if order.present? && ( order.order_state.status != 'Rejected' )
										order.order_states.update_or_create(  IB::OrderState.new( status: 'Rejected' ,
												perm_id: order.perm_id, 
												warning_text: '#{n}: '+  msg.message,
												local_id: msg.error_id ), :status ) 	

										IB::Gateway.logger.error{  msg.to_human  }
									end	# order present?
							 end	# mutex-environment
						end	# branch
				end	# def
		EOD
	end # loop
end

.alert_202(msg) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ib/alerts/order-alerts.rb', line 4

def self.alert_202 msg
	# do anything in a secure mutex-synchronized-environment
	any_order = IB::Gateway.current. do |  |
		order= .locate_order( local_id: msg.error_id )
		if order.present? && ( order.order_state.status != 'Cancelled' )
			order.order_states.update_or_create( IB::OrderState.new( status: 'Cancelled', 
																															perm_id: order.perm_id, 
																															local_id: order.local_id  ) ,
																															:status )

		end
		order # return_value
	end
	if any_order.compact.empty? 
		IB::Gateway.logger.error{"Alert 202: The deleted order was not registered: local_id #{msg.error_id}"}
	end

end

.alert_2101(msg) ⇒ Object



7
8
9
10
# File 'lib/ib/gateway/account-infos.rb', line 7

def alert_2101 msg
	logger.error {msg.message}
	@status_2101 = msg.dup	
end

.alert_2102(msg) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/ib/alerts/gateway-alerts.rb', line 5

def self.alert_2102 msg
	# Connectivity between IB and Trader Workstation has been restored - data maintained.
	sleep 0.1  #  no need to wait too long.
	if IB::Gateway.current.check_connection
		IB::Gateway.logger.debug { "Alert 2102: Connection stable" }
	else
		IB::Gateway.current.reconnect
	end
end

.ignore_alert(*codes) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/ib/alerts/base-alert.rb', line 66

def ignore_alert  *codes
  codes.each do |n|
    class_eval <<-EOD
      def self.alert_#{n} msg
		# even the log_debug entry is suppressed 
	end              
      EOD
  end
end

.log_alert_in_error(*codes) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ib/alerts/base-alert.rb', line 94

def log_alert_in_error  *codes
codes.each do |n|
    class_eval <<-EOD
      def self.alert_#{n} msg
	if msg.error_id.present? && msg.error_id > 0
	  logger.error {  msg.message + ' id: ' + msg.error_id.to_s }
	else
	  logger.error {  msg.message   }
	end
      end              
      EOD
  end
end

.log_alert_in_info(*codes) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/ib/alerts/base-alert.rb', line 75

def log_alert_in_info  *codes
  codes.each do |n|
    class_eval <<-EOD
      def self.alert_#{n} msg
      logger.info { msg.to_human }
	end              
      EOD
  end
end

.log_alert_in_warn(*codes) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/ib/alerts/base-alert.rb', line 84

def log_alert_in_warn  *codes
  codes.each do |n|
    class_eval <<-EOD
      def self.alert_#{n} msg
      logger.warn { msg.to_human }
	end              
      EOD
  end
end

.method_missing(method_id, msg, *args, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ib/alerts/base-alert.rb', line 45

def self.method_missing( method_id, msg , *args, &block )
	if msg.is_a?  IB::Messages::Incoming::Alert
		logger.debug { msg.to_human }
	else
		logger.error { "Argument to IB::Alert is not a IB::Messages::Incoming::Alert" }
		logger.error { "The object: #{msg.inspect} " }
	end
rescue NoMethodError
	unless logger.nil?
		logger.error { "The Argument is not a valid  IB::Messages:Incoming::Alert object"}
		logger.error { "The object: #{msg.inspect} " }
	else
		puts "No Logging-Device specified"
		puts "The object: #{msg.inspect} "
	end
end

.status_2101(account) ⇒ Object

resets status and raises IB::TransmissionError



12
13
14
15
# File 'lib/ib/gateway/account-infos.rb', line 12

def status_2101  # resets status and raises IB::TransmissionError
	error . + ": " +@status_2101.message, :reader unless @status_2101.nil?
	@status_2101 = nil  # always returns nil 
end