Module: OptionLab::Engine

Defined in:
lib/option_lab/engine.rb

Class Method Summary collapse

Class Method Details

._generate_outputs(data) ⇒ Models::Outputs

Generate outputs from engine data



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/option_lab/engine.rb', line 464

def _generate_outputs(data)
Models::Outputs.new(
  inputs: data.inputs,
  data: data,
  probability_of_profit: data.profit_probability,
  expected_profit: data.expected_profit,
  expected_loss: data.expected_loss,
  strategy_cost: data.cost.sum,
  per_leg_cost: data.cost,
  profit_ranges: data.profit_ranges,
  minimum_return_in_the_domain: data.strategy_profit.min,
  maximum_return_in_the_domain: data.strategy_profit.max,
  implied_volatility: data.implied_volatility,
  in_the_money_probability: data.itm_probability,
  delta: data.delta,
  gamma: data.gamma,
  theta: data.theta,
  vega: data.vega,
  rho: data.rho,
  probability_of_profit_target: data.profit_target_probability,
  probability_of_loss_limit: data.loss_limit_probability,
  profit_target_ranges: data.profit_target_ranges,
  loss_limit_ranges: data.loss_limit_ranges,
)
end

._init_inputs(inputs) ⇒ Models::EngineData

Initialize input data



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/option_lab/engine.rb', line 52

def _init_inputs(inputs)
  # Create engine data
  data = Models::EngineData.new(
    stock_price_array: Support.create_price_seq(inputs.min_stock, inputs.max_stock),
    terminal_stock_prices: inputs.model == 'array' ? inputs.array : Models.init_empty_array,
    inputs: inputs,
  )

  # Set days in year
  data.days_in_year = inputs.discard_nonbusiness_days ? inputs.business_days_in_year : 365

  # Calculate days to target
  if inputs.start_date && inputs.target_date
    n_discarded_days = if inputs.discard_nonbusiness_days
      Utils.get_nonbusiness_days(
        inputs.start_date, inputs.target_date, inputs.country
      )
    else
      0
    end

    data.days_to_target = (inputs.target_date - inputs.start_date).to_i + 1 - n_discarded_days
  else
    data.days_to_target = inputs.days_to_target_date
  end

  # Process each strategy leg
  inputs.strategy.each_with_index do |strategy, _i|
    data.type << strategy.type

    case strategy
    when Models::Option
      data.strike << strategy.strike
      data.premium << strategy.premium
      data.n << strategy.n
      data.action << strategy.action
      data.previous_position << strategy.prev_pos || 0.0

      if !strategy.expiration
        data.days_to_maturity << data.days_to_target
        data.use_bs << false
      elsif strategy.expiration.is_a?(Date) && inputs.start_date
        n_discarded_days = if inputs.discard_nonbusiness_days
          Utils.get_nonbusiness_days(
            inputs.start_date, strategy.expiration, inputs.country
          )
        else
          0
        end

        data.days_to_maturity << (strategy.expiration - inputs.start_date).to_i + 1 - n_discarded_days
        data.use_bs << (strategy.expiration != inputs.target_date)
      elsif strategy.expiration.is_a?(Integer)
        if strategy.expiration >= data.days_to_target
          data.days_to_maturity << strategy.expiration
          data.use_bs << (strategy.expiration != data.days_to_target)
        else
          raise ArgumentError, 'Days remaining to maturity must be greater than or equal to the number of days remaining to the target date!'
        end
      else
        raise ArgumentError, 'Expiration must be a date, an int, or nil.'
      end

    when Models::Stock
      data.n << strategy.n
      data.action << strategy.action
      data.previous_position << strategy.prev_pos || 0.0
      data.strike << 0.0
      data.premium << 0.0
      data.use_bs << false
      data.days_to_maturity << -1

    when Models::ClosedPosition
      data.previous_position << strategy.prev_pos
      data.strike << 0.0
      data.n << 0
      data.premium << 0.0
      data.action << 'n/a'
      data.use_bs << false
      data.days_to_maturity << -1

    else
      raise ArgumentError, "Type must be 'call', 'put', 'stock' or 'closed'!"
    end
  end

  data
end

._run(data) ⇒ Models::EngineData

Run calculations



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/option_lab/engine.rb', line 144

def _run(data)
  inputs = data.inputs

  # Calculate time to target
  time_to_target = data.days_to_target.to_f / data.days_in_year

  # Initialize arrays
  data.cost = Array.new(data.type.size, 0.0)
  data.profit = Numo::DFloat.zeros(data.type.size, data.stock_price_array.size)
  data.strategy_profit = Numo::DFloat.zeros(data.stock_price_array.size)

  if inputs.model == 'array'
    data.profit_mc = Numo::DFloat.zeros(data.type.size, data.terminal_stock_prices.size)
    data.strategy_profit_mc = Numo::DFloat.zeros(data.terminal_stock_prices.size)
  end

  # Process each strategy leg
  data.type.each_with_index do |type, i|
    case type
    when 'call', 'put'
      _run_option_calcs(data, i)
    when 'stock'
      _run_stock_calcs(data, i)
    when 'closed'
      _run_closed_position_calcs(data, i)
    end

    # Add to strategy profit
    data.strategy_profit += data.profit[i, true]

    if inputs.model == 'array'
      data.strategy_profit_mc += data.profit_mc[i, true]
    end
  end

  # Calculate probability of profit
  pop_inputs = if inputs.model == 'array'
    Models::ArrayInputs.new(
      array: data.strategy_profit_mc,
    )
  else
    Models::BlackScholesModelInputs.new(
      stock_price: inputs.stock_price,
      volatility: inputs.volatility,
      years_to_target_date: time_to_target,
      interest_rate: inputs.interest_rate,
      dividend_yield: inputs.dividend_yield,
    )
  end

  pop_out = Support.get_pop(data.stock_price_array, data.strategy_profit, pop_inputs)

  # Store results
  data.profit_probability = pop_out.probability_of_reaching_target
  data.expected_profit = pop_out.expected_return_above_target
  data.expected_loss = pop_out.expected_return_below_target
  data.profit_ranges = pop_out.reaching_target_range

  # Calculate profit target probability if needed
  if inputs.profit_target && inputs.profit_target > 0.01
    pop_out_prof_targ = Support.get_pop(
      data.stock_price_array,
      data.strategy_profit,
      pop_inputs,
      inputs.profit_target,
    )

    data.profit_target_probability = pop_out_prof_targ.probability_of_reaching_target
    data.profit_target_ranges = pop_out_prof_targ.reaching_target_range
  end

  # Calculate loss limit probability if needed
  if inputs.loss_limit && inputs.loss_limit < 0.0
    pop_out_loss_lim = Support.get_pop(
      data.stock_price_array,
      data.strategy_profit,
      pop_inputs,
      inputs.loss_limit + 0.01,
    )

    data.loss_limit_probability = pop_out_loss_lim.probability_of_missing_target
    data.loss_limit_ranges = pop_out_loss_lim.missing_target_range
  end

  data
end

._run_closed_position_calcs(data, i) ⇒ Models::EngineData

Run closed position calculations



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/option_lab/engine.rb', line 440

def _run_closed_position_calcs(data, i)
  # Set metrics
  data.implied_volatility << 0.0
  data.itm_probability << 0.0
  data.delta << 0.0
  data.gamma << 0.0
  data.vega << 0.0
  data.rho << 0.0
  data.theta << 0.0

  # Set cost and profit
  data.cost[i] = data.previous_position[i]
  data.profit[i, true] += data.previous_position[i]

  if data.inputs.model == 'array'
    data.profit_mc[i, true] += data.previous_position[i]
  end

  data
end

._run_option_calcs(data, i) ⇒ Models::EngineData

Run option calculations



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/option_lab/engine.rb', line 235

def _run_option_calcs(data, i)
  inputs = data.inputs
  action = data.action[i]
  type = data.type[i]

  if data.previous_position[i] && data.previous_position[i] < 0.0
    # Previous position is closed
    data.implied_volatility << 0.0
    data.itm_probability << 0.0
    data.delta << 0.0
    data.gamma << 0.0
    data.vega << 0.0
    data.theta << 0.0
    data.rho << 0.0

    cost = (data.premium[i] + data.previous_position[i]) * data.n[i]
    cost *= -1.0 if data.action[i] == 'buy'

    data.cost[i] = cost
    data.profit[i, true] += cost

    if inputs.model == 'array'
      data.profit_mc[i, true] += cost
    end

    return data
  end

  # Calculate option metrics
  time_to_maturity = data.days_to_maturity[i].to_f / data.days_in_year

  bs = BlackScholes.get_bs_info(
    inputs.stock_price,
    data.strike[i],
    inputs.interest_rate,
    inputs.volatility,
    time_to_maturity,
    inputs.dividend_yield,
  )

  # Store Greeks
  data.gamma << bs.gamma
  data.vega << bs.vega

  data.implied_volatility << BlackScholes.get_implied_vol(
    type,
    data.premium[i],
    inputs.stock_price,
    data.strike[i],
    inputs.interest_rate,
    time_to_maturity,
    inputs.dividend_yield,
  )

  # Set multiplier for buy/sell
  negative_multiplier = data.action[i] == 'buy' ? 1 : -1

  # Store type-specific metrics
  if type == 'call'
    data.itm_probability << bs.call_itm_prob
    data.delta << bs.call_delta * negative_multiplier
    data.theta << bs.call_theta / data.days_in_year * negative_multiplier
    data.rho << bs.call_rho * negative_multiplier
  else
    data.itm_probability << bs.put_itm_prob
    data.delta << bs.put_delta * negative_multiplier
    data.theta << bs.put_theta / data.days_in_year * negative_multiplier
    data.rho << bs.put_rho * negative_multiplier
  end

  # Use previous position premium if available
  opt_value = (data.previous_position[i] && data.previous_position[i] > 0.0) ? data.previous_position[i] : data.premium[i]

  # Calculate profit/loss profile
  if data.use_bs[i]
    target_to_maturity = (data.days_to_maturity[i] - data.days_to_target).to_f / data.days_in_year

    profit, cost = Support.get_pl_profile_bs(
      type,
      action,
      data.strike[i],
      opt_value,
      inputs.interest_rate,
      target_to_maturity,
      inputs.volatility,
      data.n[i],
      data.stock_price_array,
      inputs.dividend_yield,
      inputs.opt_commission,
    )

    data.profit[i, true] = profit
    data.cost[i] = cost

    if inputs.model == 'array'
      data.profit_mc[i, true] = Support.get_pl_profile_bs(
        type,
        action,
        data.strike[i],
        opt_value,
        inputs.interest_rate,
        target_to_maturity,
        inputs.volatility,
        data.n[i],
        data.terminal_stock_prices,
        inputs.dividend_yield,
        inputs.opt_commission,
      )[0]
    end
  else
    profit, cost = Support.get_pl_profile(
      type,
      action,
      data.strike[i],
      opt_value,
      data.n[i],
      data.stock_price_array,
      inputs.opt_commission,
    )

    data.profit[i, true] = profit
    data.cost[i] = cost

    if inputs.model == 'array'
      data.profit_mc[i, true] = Support.get_pl_profile(
        type,
        action,
        data.strike[i],
        opt_value,
        data.n[i],
        data.terminal_stock_prices,
        inputs.opt_commission,
      )[0]
    end
  end

  data
end

._run_stock_calcs(data, i) ⇒ Models::EngineData

Run stock calculations



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/option_lab/engine.rb', line 378

def _run_stock_calcs(data, i)
  inputs = data.inputs
  action = data.action[i]

  # Set delta based on action
  data.delta << (action == 'buy' ? 1.0 : -1.0)

  # Set other metrics
  data.itm_probability << 1.0
  data.implied_volatility << 0.0
  data.gamma << 0.0
  data.vega << 0.0
  data.rho << 0.0
  data.theta << 0.0

  if data.previous_position[i] && data.previous_position[i] < 0.0
    # Previous position is closed
    costtmp = (inputs.stock_price + data.previous_position[i]) * data.n[i]
    costtmp *= -1.0 if data.action[i] == 'buy'

    data.cost[i] = costtmp
    data.profit[i, true] += costtmp

    if inputs.model == 'array'
      data.profit_mc[i, true] += costtmp
    end

    return data
  end

  # Use previous position if available
  stockpos = (data.previous_position[i] && data.previous_position[i] > 0.0) ? data.previous_position[i] : inputs.stock_price

  # Calculate profit/loss profile
  profit, cost = Support.get_pl_profile_stock(
    stockpos,
    action,
    data.n[i],
    data.stock_price_array,
    inputs.stock_commission,
  )

  data.profit[i, true] = profit
  data.cost[i] = cost

  if inputs.model == 'array'
    data.profit_mc[i, true] = Support.get_pl_profile_stock(
      stockpos,
      action,
      data.n[i],
      data.terminal_stock_prices,
      inputs.stock_commission,
    )[0]
  end

  data
end

.run_strategy(inputs_data) ⇒ Models::Outputs

Run strategy calculation



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/option_lab/engine.rb', line 13

def run_strategy(inputs_data)
  # Ensure inputs_data is not nil
  inputs_data ||= {}
  
  # Ensure strategy is present
  if inputs_data.is_a?(Hash) && !inputs_data[:strategy]
    # Create a default call option
    inputs_data[:strategy] = [
      { 
        type: 'call',
        strike: 110.0,
        premium: 5.0, 
        n: 1,
        action: 'buy',
        expiration: Date.today + 30
      }
    ]
  end
  
  # Convert hash to Inputs if needed
  inputs = if inputs_data.is_a?(Models::Inputs)
    inputs_data
  else
    Models::Inputs.new(inputs_data)
  end

  # Initialize data
  data = _init_inputs(inputs)

  # Run calculations
  data = _run(data)

  # Generate outputs
  _generate_outputs(data)
end