historical_data = CSV.read("monero.csv") row_of_price = historical_data[0].index("price") # Or 'open' or whatever, it gets the column of the CSV you want to check the price based on header name invest = 30.0 # Let's say we want to invest 30.0 euro everytime total_dca_stocks = 0 # How much stocks will we have at the end of the loop with DCA total_dca_money = 0.0 # How much total money we will have invested historical_data.each_with_index do |row, i| next if i.zero? # Skip headers total_dca_money += invest.round(2) total_dca_stocks += invest / row[row_of_price].to_d end puts "I have #{total_dca_money} invested" puts "I have #{total_dca_stocks} total stocks" puts "Current price #{historical_data.last[row_of_price]} of one stock and my total money #{historical_data.last[row_of_price].to_d * total_dca_stocks}" puts "------------------------------------------------------------------------" begin_stock_invest = total_dca_money.to_d / historical_data[1][row_of_price].to_d # Start from the 2nd row to avoid headers puts "If I had invested the total amount of money at the beginning I would have: #{begin_stock_invest.to_d * historical_data.last[row_of_price].to_d}"