Personal tools

Programming performance/RaymondH Python

From HaskellWiki

< Programming performance
Revision as of 03:15, 8 March 2007 by Rhettinger (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This took about 20 minutes to write with plain vanilla Python,

data = [] for line in open('gspc.txt'):

   if not line.startswith('#'):
       data.append(float(line.split()[-1]))

data.reverse()

purchases = {} # price --> quantity at that price cash = 10000.00 for prev, close in zip(data, data[1:]):

   change = close / prev
   if change <= 0.97:
       invest = cash * 0.10
       cash -= invest
       purchases[close] = purchases.get(close, 0) + invest / close
   for pprice, qty in purchases.items():
       if close >= pprice * 1.06:
           cash += qty * close
           del purchases[pprice]

cash += sum(purchases.values()) * close print cash