Last active
May 14, 2016 21:04
-
-
Save zackster/42c5a4db9201737e64a828d9365d72d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# credit to this person for a js implementation of the function, http://stackoverflow.com/users/3473058/user3473058 | |
def find_local_maxima(input_array) | |
goAsc = false # ascending move | |
goDesc = false # descending move | |
local_maxima = [] | |
index = 0 | |
while (index < (input_array.length - 1)) do | |
firstDiff = (input_array[index] - input_array[index + 1]) | |
goAsc = true if firstDiff > 0 | |
goDesc = true if firstDiff < 0 | |
if goAsc && goDesc | |
if (firstDiff > 0) | |
local_maxima.push(input_array[index]) | |
end | |
goAsc = false | |
goDesc = false | |
end | |
index += 1 | |
end | |
local_maxima | |
end | |
def find_most_recent_local_max(date, local_max) | |
local_max.keys.select{|key_date| key_date < date}.sort.last | |
end | |
symbols = $client.query("select distinct symbol from adj_eod").map{|x|x['symbol']} | |
opportunity_list = {} | |
symbols.each do |symbol| | |
myArray = $client.query("select date,close from adj_eod where symbol='#{symbol}'").map{|x|[x['date'], x['close']]}.to_h | |
local_max_indices = find_local_maxima(myArray.values) | |
local_max = local_max_indices.map{|x| [myArray.keys[x], myArray.values[x]]}.to_h | |
next if local_max == {} | |
opportunity_list[symbol] = [] | |
myPrices = $client.query("select date,close from adj_eod where symbol='#{symbol}' and date>'#{local_max.keys.first}'").map{|x|[x['date'], x['close']]}.to_h | |
myPrices.each do |today, price| | |
most_recent_local_max = find_most_recent_local_max(today, local_max) | |
puts "#{today}: Most recent local max: #{most_recent_local_max}" | |
high_price = local_max[most_recent_local_max] | |
threshold_price = 0.10 * high_price | |
puts "threshold_price: #{threshold_price}. Price: #{price}" | |
if price <= threshold_price | |
opportunity_list[symbol] << today | |
end | |
end | |
end | |
returns = {} | |
opportunity_list.each do |symbol, dropdate| | |
date = Date.parse(dropdate.first).to_s | |
prices = $client.query("select close from adj_eod where symbol='#{symbol}' and date>='#{date}' order by date asc").map{|x|x['close']} | |
returns[symbol] = ((prices.last - prices.first) / prices.first).to_f | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment