Module: OptionLab::Utils

Defined in:
lib/option_lab/utils.rb

Class Method Summary collapse

Class Method Details

.get_nonbusiness_days(start_date, end_date, country = "US") ⇒ Integer

Get number of non-business days between dates

Parameters:

  • start_date (Date)

    Start date

  • end_date (Date)

    End date

  • country (String) (defaults to: "US")

    Country code

Returns:

  • (Integer)

    Number of non-business days



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
48
49
50
51
52
53
54
55
56
57
# File 'lib/option_lab/utils.rb', line 18

def get_nonbusiness_days(start_date, end_date, country = "US")
    if end_date <= start_date
      raise ArgumentError, "End date must be after start date!"
    end
    
    # Create cache key
    cache_key = "#{country}-#{start_date}-#{end_date}"
    
    # Return cached result if available
    return @holiday_cache[cache_key] if @holiday_cache.key?(cache_key)
    
    # Calculate number of days
    n_days = (end_date - start_date).to_i
    
    # Get holidays for the country (default to :us if there's an issue)
    begin
      holidays = Holidays.between(start_date, end_date, country.to_sym)
    rescue Holidays::InvalidRegion
      holidays = Holidays.between(start_date, end_date, :us)
    end
    holiday_dates = holidays.map { |h| h[:date].strftime('%Y-%m-%d') }
    
    # Count non-business days
    nonbusiness_days = 0
    
    n_days.times do |i|
      current_date = start_date + i
      
      # Check if weekend or holiday
      if current_date.saturday? || current_date.sunday? || 
         holiday_dates.include?(current_date.strftime('%Y-%m-%d'))
        nonbusiness_days += 1
      end
    end
    
    # Cache the result
    @holiday_cache[cache_key] = nonbusiness_days
    
    nonbusiness_days
end

.get_pl(outputs, leg = nil) ⇒ Array<Numo::DFloat, Numo::DFloat>

Get profit/loss data

Parameters:

  • outputs (Models::Outputs)

    Strategy outputs

  • leg (Integer, nil) (defaults to: nil)

    Strategy leg index

Returns:

  • (Array<Numo::DFloat, Numo::DFloat>)

    Stock prices and profits/losses



63
64
65
66
67
68
69
# File 'lib/option_lab/utils.rb', line 63

def get_pl(outputs, leg = nil)
    if outputs.data.profit.size > 0 && leg && leg < outputs.data.profit.shape[0]
      [outputs.data.stock_price_array, outputs.data.profit[leg, true]]
    else
      [outputs.data.stock_price_array, outputs.data.strategy_profit]
    end
end

.pl_to_csv(outputs, filename = "pl.csv", leg = nil) ⇒ void

This method returns an undefined value.

Save profit/loss data to CSV

Parameters:

  • outputs (Models::Outputs)

    Strategy outputs

  • filename (String, IO) (defaults to: "pl.csv")

    CSV filename or IO object

  • leg (Integer, nil) (defaults to: nil)

    Strategy leg index



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
# File 'lib/option_lab/utils.rb', line 76

def pl_to_csv(outputs, filename = "pl.csv", leg = nil)
  stock_prices, profits = get_pl(outputs, leg)
  
  # Create matrix with stock prices and profits
  data = Numo::DFloat.zeros(stock_prices.size, 2)
  data[true, 0] = stock_prices
  data[true, 1] = profits
  
  # Handle filename as string or IO object
  if filename.is_a?(String)
    # Save to CSV file
    File.open(filename, 'w') do |file|
      file.puts "StockPrice,Profit/Loss"
      
      data.shape[0].times do |i|
        file.puts "#{data[i, 0]},#{data[i, 1]}"
      end
    end
  elsif filename.respond_to?(:puts)
    # Write to IO object
    filename.puts "StockPrice,Profit/Loss"
    
    data.shape[0].times do |i|
      filename.puts "#{data[i, 0]},#{data[i, 1]}"
    end
  else
    raise ArgumentError, "Filename must be a String or an IO object"
  end
end