import sys
from struct import calcsize, unpack_from
from collections import namedtuple
Tick = namedtuple('Tick', ['open', 'close', 'high', 'low', 'time'])
YEARS = range(2002, 2013 + 1)
ASSETS = [
'AUDUSD', 'EURUSD', 'EURCHF', 'GBPUSD', 'GER30', 'NAS100',
'NZDUSD', 'SPX500', 'UK100', 'US30', 'USDCAD', 'USDCHF',
'USDJPY', 'USOil', 'XAGUSD', 'XAUUSD'
]
def parse_bar_file(filename):
ticks = open(filename, 'rb').read()
format = '<ffffd'
format_size = calcsize(format)
num_ticks = len(ticks) / format_size
return [Tick(*unpack_from(format, ticks, format_size * i)) for i in xrange(num_ticks)]
def count_wicks(ticks):
return len([t for t in ticks if t.high > t.open or t.low < t.close])
def print_summary_matrix():
print "%6s | %s" % ("Asset", " ".join(map(str, YEARS)))
print "-" * 80
for asset in ASSETS:
wick_percentages = []
for year in YEARS:
try:
ticks = parse_bar_file("%s_%d.bar" % (asset, year))
except IOError:
# Some years are missing
wick_percentages.append(" - ")
continue
num_wicks = count_wicks(ticks)
if len(ticks) == 0:
# Some files are empty
prc = wick_percentages.append(" - ")
continue
prc = num_wicks / float(len(ticks))
wick_percentages.append("%4.0f " % (prc * 100))
print "%6s | %s" % (asset, " ".join(wick_percentages))
print_summary_matrix()