Class: StockPriceImitation::Generator::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/stock_price_imitation/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(seed_price, volatility) ⇒ Step

Returns a new instance of Step.



40
41
42
43
44
45
46
47
# File 'lib/stock_price_imitation/generator.rb', line 40

def initialize(seed_price, volatility)
  bell = RandomBell.new

  @seed_price = seed_price

  prices = 2.times.map{ @seed_price * bell.rand }
  @low_price, @high_price = prices.sort
end

Instance Method Details

#closing_priceObject



61
62
63
# File 'lib/stock_price_imitation/generator.rb', line 61

def closing_price
  to_zero_if_negative (rand low_price..high_price)
end

#high_priceObject



53
54
55
# File 'lib/stock_price_imitation/generator.rb', line 53

def high_price
  to_zero_if_negative @high_price
end

#low_priceObject



49
50
51
# File 'lib/stock_price_imitation/generator.rb', line 49

def low_price
  to_zero_if_negative @low_price
end

#opening_priceObject



57
58
59
# File 'lib/stock_price_imitation/generator.rb', line 57

def opening_price
  to_zero_if_negative (rand low_price..high_price)
end

#to_hashObject



73
74
75
76
77
78
79
80
# File 'lib/stock_price_imitation/generator.rb', line 73

def to_hash
  {
    opening_price: self.opening_price,
    closing_price: self.closing_price,
    low_price:     self.low_price,
    high_price:    self.high_price,
  }
end

#to_zero_if_negative(number) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/stock_price_imitation/generator.rb', line 65

def to_zero_if_negative(number)
  if number < 0
    return 0
  end

  number
end