Difference between revisions of "Programming performance/hay.steve Python"

From HaskellWiki
Jump to: navigation, search
(Deleting page that hasn't been updated for over 10 years)
m (Reverted edits by Tomjaguarpaw (talk) to last revision by Hay.steve)
Line 1: Line 1:
 +
* '''Implementation time''': 1.5 hours.
 +
* '''Experience''': 3 days.
 +
* '''Comments''': I spent most of my time noodling on the syntax of Python and familiarizing myself with the documentation.  I also spent some time getting the heap to work instead of taking multiple passes with that data.  I originally said it took 2 hours, but I am bringing it down to 1.5 because I was eating dinner while and watching a movie while coding.  Also, I came back later and spent a few minutes adding a verbosity feature as suggested in the problem statement.
  
 +
=== Code ===
 +
 +
<code-python>
 +
from heapq import heappush, heappop, nsmallest
 +
 +
verbose=False
 +
filename = "gspc.txt"
 +
 +
days = []
 +
 +
for f in open(filename):
 +
        if f[0] == '#':
 +
                continue
 +
        linelist = f.split(' ')
 +
        heappush( days, linelist )
 +
 +
cash = 10000.00
 +
previousClose = -1.0
 +
currentClose = 0.00
 +
 +
shares = []
 +
 +
beginningBalance = cash
 +
 +
print "Beginning Balance:", cash
 +
while days != []:
 +
        day = heappop( days )
 +
        i = day[0]
 +
        currentClose = float( day[4] )
 +
 +
        if previousClose != -1.0:
 +
                percentGain = ( currentClose - previousClose ) / previousClose
 +
                if percentGain < -0.03:
 +
                        numShares = 0.1 * cash / currentClose
 +
                        heappush( shares, (currentClose, numShares) )
 +
                        cash = 0.9 * cash
 +
                        if verbose: print "On", i, ", bought" , numShares , "shares at" , currentClose , "."
 +
 +
                else :
 +
                        while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:
 +
                                sell = heappop( shares )
 +
                                cash += sell[1] * currentClose
 +
                                if verbose: print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."
 +
 +
        previousClose = currentClose
 +
 +
while shares != []:
 +
        sell = heappop( shares )
 +
        cash += sell[1] * currentClose
 +
        if verbose: print "Sold" , sell[1] , "shares at" , currentClose , "."
 +
 +
print "Beginning balance:", beginningBalance
 +
print "Ending balance:", cash
 +
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."
 +
</code-python>
 +
 +
=== Some later refinements ===
 +
<code-python>
 +
from heapq import heappush, heappop
 +
 +
# iterates through a heap while destroying it (side effect)
 +
def heap_eater(heap):
 +
        while heap: yield heappop( heap )
 +
 +
# globals
 +
verbose = True
 +
filename = "gspc.txt"
 +
days = []
 +
positions = []
 +
beginningBalance = 10000.00
 +
cash = 10000.00
 +
close = 0.00
 +
 +
# create the stock data heap
 +
for f in open(filename):
 +
        if not f.startswith('#'):
 +
                date, open, high, low, close, volume, adjClose = f.split(' ')
 +
                day = ( date, float( close ) )
 +
                heappush( days, day )
 +
 +
print "Beginning Balance:", cash
 +
 +
# iterate through the days
 +
startDate, oldClose = heappop( days )
 +
for day, close in heap_eater( days ):
 +
        if close < 0.97 * oldClose:
 +
                numShares = 0.1 * cash / close
 +
                cash -= 0.1 * cash
 +
                position = (close, numShares )
 +
                heappush( positions, position )
 +
                if verbose:
 +
                        print "On", day, ", bought" , numShares , "shares at" , close , "."
 +
        else:
 +
                for oldClose, numShares in heap_eater( positions ):
 +
                        if close >= 1.06 * oldClose:
 +
                                cash += numShares * close
 +
                                if verbose:
 +
                                        print "On", day, "sold", numShares, "shares at", close, "."
 +
                        else:
 +
                                position = ( oldClose, numShares )
 +
                                heappush( positions, position )
 +
                                break
 +
        oldClose = close
 +
 +
# close out positions
 +
for price,qty in positions:
 +
        cash += qty * close
 +
        if verbose:
 +
                print "Sold" , qty , "shares at" , close , "."
 +
 +
print "Beginning balance:", beginningBalance
 +
print "Ending balance:", cash
 +
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."
 +
</code-python>
 +
 +
--[[User:Hay.steve|Hay.steve]] 01:53, 8 March 2007 (UTC)

Revision as of 15:19, 6 February 2021

  • Implementation time: 1.5 hours.
  • Experience: 3 days.
  • Comments: I spent most of my time noodling on the syntax of Python and familiarizing myself with the documentation. I also spent some time getting the heap to work instead of taking multiple passes with that data. I originally said it took 2 hours, but I am bringing it down to 1.5 because I was eating dinner while and watching a movie while coding. Also, I came back later and spent a few minutes adding a verbosity feature as suggested in the problem statement.

Code

<code-python> from heapq import heappush, heappop, nsmallest

verbose=False filename = "gspc.txt"

days = []

for f in open(filename):

       if f[0] == '#':
               continue
       linelist = f.split(' ')
       heappush( days, linelist )

cash = 10000.00 previousClose = -1.0 currentClose = 0.00

shares = []

beginningBalance = cash

print "Beginning Balance:", cash while days != []:

       day = heappop( days )
       i = day[0]
       currentClose = float( day[4] )
       if previousClose != -1.0:
               percentGain = ( currentClose - previousClose ) / previousClose
               if percentGain < -0.03:
                       numShares = 0.1 * cash / currentClose
                       heappush( shares, (currentClose, numShares) )
                       cash = 0.9 * cash
                       if verbose: print "On", i, ", bought" , numShares , "shares at" , currentClose , "."
               else :
                       while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:
                               sell = heappop( shares )
                               cash += sell[1] * currentClose
                               if verbose: print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."
       previousClose = currentClose

while shares != []:

       sell = heappop( shares )
       cash += sell[1] * currentClose
       if verbose: print "Sold" , sell[1] , "shares at" , currentClose , "."

print "Beginning balance:", beginningBalance print "Ending balance:", cash print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%." </code-python>

Some later refinements

<code-python> from heapq import heappush, heappop

  1. iterates through a heap while destroying it (side effect)

def heap_eater(heap):

       while heap: yield heappop( heap )
  1. globals

verbose = True filename = "gspc.txt" days = [] positions = [] beginningBalance = 10000.00 cash = 10000.00 close = 0.00

  1. create the stock data heap

for f in open(filename):

       if not f.startswith('#'):
               date, open, high, low, close, volume, adjClose = f.split(' ')
               day = ( date, float( close ) )
               heappush( days, day )

print "Beginning Balance:", cash

  1. iterate through the days

startDate, oldClose = heappop( days ) for day, close in heap_eater( days ):

       if close < 0.97 * oldClose:
               numShares = 0.1 * cash / close
               cash -= 0.1 * cash
               position = (close, numShares )
               heappush( positions, position )
               if verbose:
                       print "On", day, ", bought" , numShares , "shares at" , close , "."
       else:
               for oldClose, numShares in heap_eater( positions ):
                       if close >= 1.06 * oldClose:
                               cash += numShares * close
                               if verbose:
                                       print "On", day, "sold", numShares, "shares at", close, "."
                       else:
                               position = ( oldClose, numShares )
                               heappush( positions, position )
                               break
       oldClose = close
  1. close out positions

for price,qty in positions:

       cash += qty * close
       if verbose:
               print "Sold" , qty , "shares at" , close , "."

print "Beginning balance:", beginningBalance print "Ending balance:", cash print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%." </code-python>

--Hay.steve 01:53, 8 March 2007 (UTC)