Skip to content

Instantly share code, notes, and snippets.

@fliiiix
Created April 7, 2014 19:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fliiiix/10036406 to your computer and use it in GitHub Desktop.
require 'csv'
#static var
TIMESLICE = 20
FIBONACCI_RATIO_1_3 = 1.382.to_f
FIBONACCI_RATIO_1_6 = 1.6185.to_f
#schema: Date,Open,High,Low,Close,Volume,Adj Close
#read data
rawdata = File.read("data.csv")
data = Array.new
csv = CSV.new(rawdata, :headers => true, :header_converters => :symbol)
csv.to_a.map do |row|
data << row.to_hash
end
data.each_slice(TIMESLICE).each do |dataSlice|
#get min and max value for the given time slide
highRow = dataSlice.max{ |x,y| x[:high].to_f <=> y[:high].to_f}
lowRow = dataSlice.min{ |x,y| x[:low].to_f <=> y[:low].to_f}
puts "High: #{highRow}"
puts "Low: #{lowRow}"
#calculate the diffenz
differenz = highRow[:high].to_f - lowRow[:low].to_f
puts differenz
if Date.parse(highRow[:date]) > Date.parse(lowRow[:date])
puts "uptrend"
#calculate next posible values for this uptrend
puts "next up:"
puts "0.382 ratio: #{lowRow[:low].to_f + (differenz * FIBONACCI_RATIO_1_3)}"
puts "0.618 ratio: #{lowRow[:low].to_f + (differenz * FIBONACCI_RATIO_1_6)}"
else
puts "downtrend"
#calculate next posible values for this downtrend
puts "next up:"
puts "0.382 ratio: #{highRow[:high].to_f - (differenz * FIBONACCI_RATIO_1_3)}"
puts "0.618 ratio: #{highRow[:high].to_f - (differenz * FIBONACCI_RATIO_1_6)}"
end
puts "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment