Module: InteractiveBrokers2TastyWorks::Utils

Defined in:
lib/interactive_brokers_2_tasty_works.rb

Class Method Summary collapse

Class Method Details

.build_action(trade) ⇒ Object



149
150
151
# File 'lib/interactive_brokers_2_tasty_works.rb', line 149

def build_action(trade)
  "#{trade[:buySell]}_TO_#{trade[:openCloseIndicator] == 'O' ? 'OPEN' : 'CLOSE'}"
end

.build_commission(trade) ⇒ Object



188
189
190
191
# File 'lib/interactive_brokers_2_tasty_works.rb', line 188

def build_commission(trade)
  c = trade[:ibCommission]
  c.to_f == -0.0 ? '0' : c
end

.build_date(str) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/interactive_brokers_2_tasty_works.rb', line 141

def build_date(str)
  return nil if str.to_s.strip == ''
  day = str[6..7]
  month = str[4..5]
  year = str[2..3]
  "#{month}/#{day}/#{year}"
end

.build_date_time_str(trade) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/interactive_brokers_2_tasty_works.rb', line 130

def build_date_time_str(trade)
  d, t = trade[:tradeDate], trade[:tradeTime]
  year = d[0..3]
  month = d[4..5]
  day = d[6..7]
  hour = t[0..1]
  min = t[2..3]
  sec = t[4..5]
  Time.zone.local(year,month,day,hour,min,sec).strftime('%FT%T%z')
end

.build_description(trade) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/interactive_brokers_2_tasty_works.rb', line 162

def build_description(trade)
  case trade[:buySell]
  when 'SELL'; str = 'Sold'
  when 'BUY'; str = 'Bought'
  end

  str += " #{trade[:quantity].to_i.abs} "

  str += case (ac = trade[:assetCategory])
  when 'OPT'
    exp = build_date(trade[:expiry])
    "#{trade[:symbol]} #{exp} #{put_or_call(trade)} #{trade[:strike]} @ #{trade[:tradePrice]}"
  when 'STK'
    "#{trade[:symbol]} @ #{trade[:tradePrice]}"
  else
    raise ArgumentError.new("Unknown asset category: #{ac}")
  end

  str
end

.build_instrument_type(trade) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/interactive_brokers_2_tasty_works.rb', line 153

def build_instrument_type(trade)
  case (ac = trade[:assetCategory])
  when 'OPT'; 'Equity Option'
  when 'STK'; 'Equity'
  else
    raise ArgumentError.new("Unknown asset category: #{ac}")
  end
end

.build_value(trade) ⇒ Object



183
184
185
186
# File 'lib/interactive_brokers_2_tasty_works.rb', line 183

def build_value(trade)
  v = trade[:proceeds]
  v.to_f == -0.0 ? '0' : v
end

.indifferent_fetch(h, key) ⇒ Object



198
199
200
201
202
# File 'lib/interactive_brokers_2_tasty_works.rb', line 198

def indifferent_fetch(h, key)
  h.fetch(key)
rescue KeyError
  h[key.is_a?(String) ? key.to_sym : key.to_s]
end

.put_or_call(trade) ⇒ Object



193
194
195
196
# File 'lib/interactive_brokers_2_tasty_works.rb', line 193

def put_or_call(trade)
  return nil unless trade[:assetCategory] == 'OPT'
  trade[:putCall] == 'P' ? 'PUT' : 'CALL'
end