Skip to content

Instantly share code, notes, and snippets.

@DarwinSenior
Created September 14, 2015 16:46
Show Gist options
  • Select an option

  • Save DarwinSenior/c8061d508d39b70aa8bd to your computer and use it in GitHub Desktop.

Select an option

Save DarwinSenior/c8061d508d39b70aa8bd to your computer and use it in GitHub Desktop.
code2
import os
class Data(object):
def __init__(self, line):
self.parse(line)
def parse(line):
numbers = line.split(' ')
self.date = numbers[0]
self.open_price = float(numbers[1])
self.high = float(numbers[2])
self.low = float(numbers[3])
self.closing_price = float(numbers[4])
self.total_volum = int(numbers[5])
def lowest_change(prices):
lowest = prices[:]
for i in range(len(prices)-1):
lowest[i+1] = min(lowest[i+1], lowest[i])
return max(map(lambda x,y: x-y, prices, lowest))
def hight_change(prices):
highest = prices[:]
for i in range(len(prices)-1):
highest[i+1] = max(highest[i+1], highest[i])
return min(map(lambda x,y: x-y, highest, prices))
def process_file(file_names):
info = {}
pos_percent = {}
neg_percent = {}
for file_name in file_names:
with open(file_name, 'r') as input_file:
data[file_name] = [Data(line) for line in input_file]
highest_close = max(data, key=lambda x: x.closing_price)
lowest_close = min(data, key=lambda x: x.closing_price)
# info[(file_name, source)] = 'source: %s highest close: %0.2f on %d, lowest close: %0.2f on %d'%(
print 'source: %s highest close: %0.2f on %d, lowest close: %0.2f on %d'%(
file_name,
highest_close.closing_price,
highest_close.date,
lowest_close.closing_price
lowest_close.date)
for datium in data:
date = data.date
percent = (data.closing_price - data.open_price)*100/data.open_price
if percent > 0:
pos_percent[date] = max((pos_percent[date] or 0, None), (percent, file_name))
else:
neg_percent[date] = min((neg_percent[date] or 0, None), (percent, file_name))
for (date, (percent,name)) in pos_percent.items():
print 'source: %s greatest positive percent change: %0.1f% on %s'%(name, percent, name)
for (date, (percent,name)) in neg_percent.items():
print 'source: %s greatest negative percent change: %0.1f% on %s'%(name, percent, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment