class StockPriceImitation::Generator

Public Class Methods

new(tick_count, first_tick_price=1000) click to toggle source
# File lib/stock_price_imitation/generator.rb, line 2
def initialize(tick_count, first_tick_price=1000)
  @tick_count = tick_count
  @first_tick_price = first_tick_price
  @ticks = []
end

Public Instance Methods

execute() click to toggle source
# File lib/stock_price_imitation/generator.rb, line 8
def execute
  generate!
  @ticks
end

Private Instance Methods

generate!() click to toggle source
# File lib/stock_price_imitation/generator.rb, line 15
def generate!
  @tick_count.times.each do |i|
    if i == 0
      setup_first_tick
      next
    end

    @ticks << generate_tick(@ticks[i-1])
  end
end
generate_tick(previous_price) click to toggle source
# File lib/stock_price_imitation/generator.rb, line 30
def generate_tick(previous_price)
  to_zero_if_negative(previous_price + increase)
end
increase() click to toggle source
# File lib/stock_price_imitation/generator.rb, line 34
def increase
  @first_tick_price * rand(0..0.1) * [-1,1].sample
end
setup_first_tick() click to toggle source
# File lib/stock_price_imitation/generator.rb, line 26
def setup_first_tick
  @ticks << @first_tick_price
end
to_zero_if_negative(number) click to toggle source
# File lib/stock_price_imitation/generator.rb, line 38
def to_zero_if_negative(number)
  if number < 0
    return 0
  end

  number
end