https://wiki.haskell.org/api.php?action=feedcontributions&user=Hay.steve&feedformat=atomHaskellWiki - User contributions [en]2019-07-16T21:46:02ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Programming_performance/hay.steve_Python&diff=11884Programming performance/hay.steve Python2007-03-09T20:46:33Z<p>Hay.steve: </p>
<hr />
<div>* '''Implementation time''': 1.5 hours.<br />
* '''Experience''': 3 days.<br />
* '''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.<br />
<br />
=== Code ===<br />
<br />
<code-python><br />
from heapq import heappush, heappop, nsmallest<br />
<br />
verbose=False<br />
filename = "gspc.txt"<br />
<br />
days = []<br />
<br />
for f in open(filename):<br />
if f[0] == '#':<br />
continue<br />
linelist = f.split(' ')<br />
heappush( days, linelist )<br />
<br />
cash = 10000.00<br />
previousClose = -1.0<br />
currentClose = 0.00<br />
<br />
shares = []<br />
<br />
beginningBalance = cash<br />
<br />
print "Beginning Balance:", cash<br />
while days != []:<br />
day = heappop( days )<br />
i = day[0]<br />
currentClose = float( day[4] )<br />
<br />
if previousClose != -1.0:<br />
percentGain = ( currentClose - previousClose ) / previousClose<br />
if percentGain < -0.03:<br />
numShares = 0.1 * cash / currentClose<br />
heappush( shares, (currentClose, numShares) )<br />
cash = 0.9 * cash<br />
if verbose: print "On", i, ", bought" , numShares , "shares at" , currentClose , "."<br />
<br />
else :<br />
while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
if verbose: print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
previousClose = currentClose<br />
<br />
while shares != []:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
if verbose: print "Sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
print "Beginning balance:", beginningBalance<br />
print "Ending balance:", cash<br />
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."<br />
</code-python><br />
<br />
=== Some later refinements ===<br />
<code-python><br />
from heapq import heappush, heappop<br />
<br />
# iterates through a heap while destroying it (side effect)<br />
def heap_eater(heap):<br />
while heap: yield heappop( heap )<br />
<br />
# globals<br />
verbose = True<br />
filename = "gspc.txt"<br />
days = []<br />
positions = []<br />
beginningBalance = 10000.00<br />
cash = 10000.00<br />
close = 0.00<br />
<br />
# create the stock data heap<br />
for f in open(filename):<br />
if not f.startswith('#'):<br />
date, open, high, low, close, volume, adjClose = f.split(' ')<br />
day = ( date, float( close ) )<br />
heappush( days, day )<br />
<br />
print "Beginning Balance:", cash<br />
<br />
# iterate through the days<br />
startDate, oldClose = heappop( days )<br />
for day, close in heap_eater( days ):<br />
if close < 0.97 * oldClose:<br />
numShares = 0.1 * cash / close<br />
cash -= 0.1 * cash<br />
position = (close, numShares )<br />
heappush( positions, position )<br />
if verbose:<br />
print "On", day, ", bought" , numShares , "shares at" , close , "."<br />
else:<br />
for oldClose, numShares in heap_eater( positions ):<br />
if close >= 1.06 * oldClose:<br />
cash += numShares * close<br />
if verbose:<br />
print "On", day, "sold", numShares, "shares at", close, "."<br />
else:<br />
position = ( oldClose, numShares )<br />
heappush( positions, position )<br />
break<br />
oldClose = close<br />
<br />
# close out positions<br />
for price,qty in positions:<br />
cash += qty * close<br />
if verbose:<br />
print "Sold" , qty , "shares at" , close , "."<br />
<br />
print "Beginning balance:", beginningBalance<br />
print "Ending balance:", cash<br />
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."<br />
</code-python><br />
<br />
--[[User:Hay.steve|Hay.steve]] 01:53, 8 March 2007 (UTC)</div>Hay.stevehttps://wiki.haskell.org/index.php?title=Programming_performance&diff=11836Programming performance2007-03-08T05:04:16Z<p>Hay.steve: </p>
<hr />
<div>[[Category:Code]]<br />
<br />
== Goal ==<br />
<br />
The goal is to measure how long it takes to implement solutions to a standard problem in your programming language of choice and to see the various approaches different programmers take when implementing the solution. The goal isn't to finish the problem as quickly as possible but to rather to get some realistic measurements on programming speed and approaches.<br />
<br />
== Submitted results ==<br />
<br />
{| border="1" cellspacing="0" cellpadding="5" align="center" <br />
! Data<br />
! Language<br />
! Experience<br />
! Time<br />
|-<br />
| [[Programming performance/TimN Haskell]]<br />
| Haskell<br />
| Intermediate<br />
| 45min?<br />
|-<br />
| [[Programming performance/TimN Python]]<br />
| Python<br />
| Advanced<br />
| 30min?<br />
|-<br />
| [[Programming performance/Magnus Haskell]]<br />
| Haskell<br />
| Intermediate<br />
| 25min<br />
|-<br />
| [[Programming performance/kc5tja Forth]]<br />
| Forth<br />
| Advanced<br />
| 3.7hr<br />
|-<br />
| [[Programming performance/Apter K]] (added by kc5tja)<br />
| K<br />
| Advanced<br />
| 15min?<br />
|-<br />
| [[Programming performance/hay.steve Python]]<br />
| Python<br />
| Beginner<br />
| 1.5hr<br />
|-<br />
| [[Programming performance/JasonWoof Ruby]]<br />
| Ruby<br />
| Beginner<br />
| 50 minutes<br />
|-<br />
| [[Programming performance/RaymondH Python]]<br />
| Python<br />
| Advanced<br />
| 20-25 minutes<br />
|}<br />
<br />
<br />
<br />
<br />
== Instructions ==<br />
<br />
Implement a program that computes the solution to the following problem in your language of choice. Measure how long it took you to make your initial implementation. Post the code and the amount of time it took. Please submit only your time and code for your first implementation in a given language. If you have implemented a solution previously in another language please mention that. You may submit further refinements of your solution or new solutions on the same page as your initial submission but the primary goals is to measure your initial solution and the amount of time required. If you wish, you may look at other solutions but it is preferable that you do not.<br />
<br />
Submit your entry by creating a wiki page with your code, your time, the language used and a note about how comfortable you are with this language. If you relied on any help, such as reading other solutions, please state what sort of help you made use of. Please make just one page per participant per language. You may include any comments, refinements or alternate programs on this same page.<br />
<br />
<br />
<br />
<br />
== Problem ==<br />
<br />
Simulate the following strategy on the GSPC data set:<br />
<br />
* Whenever the closing cost is down 3% or more from the previous closing cost, buy shares with 10% of current cash.<br />
* Whenever the closing cost is up 6% or more from the original purchase price, sell back the full amount of stock purchased at that price.<br />
* Sell off any remaining assets after the last day.<br />
<br />
<br />
Run the strategy on the provided data starting with $10,000.00 in cash and no holdings.<br />
* All purchases are made at the closing cost.<br />
* Fractional shares may be purchased.<br />
* Fractional pennies may be used.<br />
* Do not simulate any trading costs, interest dividends or taxes.<br />
* Percent change is 100 percent times the change (new price minus old price) divided by the old price.<br />
<br />
<br />
=== Data ===<br />
<br />
The http://www.thenewsh.com/gspc.txt file contains the test data. Any lines not starting with a hash sign ("#") contain valid data. Data is in space seperated columns. Each line represents one day's data with most recent date first. The first column contains the date and the last column contains the closing price. The number of columns is always the same.<br />
<br />
<br />
=== Results ===<br />
<br />
Display the amount of cash held after executing the strategy. A mode that displays each buy and sell by date is useful, but optional.</div>Hay.stevehttps://wiki.haskell.org/index.php?title=Programming_performance/hay.steve_Python&diff=11834Programming performance/hay.steve Python2007-03-08T03:46:27Z<p>Hay.steve: </p>
<hr />
<div>* '''Implementation time''': 1.5 hours.<br />
* '''Experience''': 3 days.<br />
* '''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.<br />
<br />
=== Code ===<br />
<br />
<code-python><br />
from heapq import heappush, heappop, nsmallest<br />
<br />
verbose=False<br />
filename = "gspc.txt"<br />
<br />
days = []<br />
<br />
for f in open(filename):<br />
if f[0] == '#':<br />
continue<br />
linelist = f.split(' ')<br />
heappush( days, linelist )<br />
<br />
cash = 10000.00<br />
previousClose = -1.0<br />
currentClose = 0.00<br />
<br />
shares = []<br />
<br />
beginningBalance = cash<br />
<br />
print "Beginning Balance:", cash<br />
while days != []:<br />
day = heappop( days )<br />
i = day[0]<br />
currentClose = float( day[4] )<br />
<br />
if previousClose != -1.0:<br />
percentGain = ( currentClose - previousClose ) / previousClose<br />
if percentGain < -0.03:<br />
numShares = 0.1 * cash / currentClose<br />
heappush( shares, (currentClose, numShares) )<br />
cash = 0.9 * cash<br />
if verbose: print "On", i, ", bought" , numShares , "shares at" , currentClose , "."<br />
<br />
else :<br />
while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
if verbose: print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
previousClose = currentClose<br />
<br />
while shares != []:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
if verbose: print "Sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
print "Beginning balance:", beginningBalance<br />
print "Ending balance:", cash<br />
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."<br />
</code-python><br />
<br />
--[[User:Hay.steve|Hay.steve]] 01:53, 8 March 2007 (UTC)</div>Hay.stevehttps://wiki.haskell.org/index.php?title=Programming_performance/hay.steve_Python&diff=11818Programming performance/hay.steve Python2007-03-08T01:55:23Z<p>Hay.steve: Implementation time: 2 hours. Python experience: 3 days.</p>
<hr />
<div>from heapq import heappush, heappop, nsmallest<br />
<br />
print "Implementation time: 2 hours. Python experience: 3 days."<br />
<br />
filename = "gspc.txt"<br />
<br />
days = []<br />
<br />
for f in open(filename):<br />
if f[0] == '#':<br />
continue<br />
linelist = f.split(' ')<br />
heappush( days, linelist )<br />
<br />
cash = 10000<br />
<br />
previousClose = -1.0<br />
<br />
currentClose = 0<br />
<br />
shares = []<br />
<br />
beginningBalance = cash<br />
<br />
print "Beginning Balance:", cash<br />
while days != []:<br />
day = heappop( days )<br />
i = day[0]<br />
currentClose = float( day[4] )<br />
<br />
if previousClose != -1.0:<br />
percentGain = ( currentClose - previousClose ) / previousClose<br />
if percentGain < -0.03:<br />
numShares = 0.1 * cash / currentClose<br />
heappush( shares, (currentClose, numShares) )<br />
cash = 0.9 * cash<br />
print "On", i, ", bought" , numShares , "shares at" , currentClose , "."<br />
<br />
else :<br />
while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
previousClose = currentClose<br />
<br />
while shares != []:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
print "Sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
print "Beginning balance:", beginningBalance<br />
<br />
print "Ending balance:", cash<br />
<br />
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."<br />
<br />
--[[User:Hay.steve|Hay.steve]] 01:53, 8 March 2007 (UTC)</div>Hay.stevehttps://wiki.haskell.org/index.php?title=Programming_performance/hay.steve_Python&diff=11817Programming performance/hay.steve Python2007-03-08T01:53:53Z<p>Hay.steve: Implementation time: 2 hours. Python experience: 3 days.</p>
<hr />
<div>from heapq import heappush, heappop, nsmallest<br />
<br />
filename = "gspc.txt"<br />
<br />
days = []<br />
<br />
for f in open(filename):<br />
if f[0] == '#':<br />
continue<br />
linelist = f.split(' ')<br />
heappush( days, linelist )<br />
<br />
cash = 10000<br />
<br />
previousClose = -1.0<br />
<br />
currentClose = 0<br />
<br />
shares = []<br />
<br />
beginningBalance = cash<br />
<br />
print "Beginning Balance:", cash<br />
while days != []:<br />
day = heappop( days )<br />
i = day[0]<br />
currentClose = float( day[4] )<br />
<br />
if previousClose != -1.0:<br />
percentGain = ( currentClose - previousClose ) / previousClose<br />
if percentGain < -0.03:<br />
numShares = 0.1 * cash / currentClose<br />
heappush( shares, (currentClose, numShares) )<br />
cash = 0.9 * cash<br />
print "On", i, ", bought" , numShares , "shares at" , currentClose , "."<br />
<br />
else :<br />
while shares != [] and nsmallest(1, shares)[0][0] <= currentClose / 1.06:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
print "On", i, "sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
previousClose = currentClose<br />
<br />
while shares != []:<br />
sell = heappop( shares )<br />
cash += sell[1] * currentClose<br />
print "Sold" , sell[1] , "shares at" , currentClose , "."<br />
<br />
print "Beginning balance:", beginningBalance<br />
<br />
print "Ending balance:", cash<br />
<br />
print "Percent Gain:", (cash-beginningBalance)/beginningBalance*100, "%."<br />
<br />
--[[User:Hay.steve|Hay.steve]] 01:53, 8 March 2007 (UTC)</div>Hay.stevehttps://wiki.haskell.org/index.php?title=Programming_performance&diff=11816Programming performance2007-03-08T01:49:46Z<p>Hay.steve: </p>
<hr />
<div>[[Category:Code]]<br />
<br />
== Goal ==<br />
<br />
The goal is to measure how long it takes to implement solutions to a standard problem in your programming language of choice and to see the various approaches different programmers take when implementing the solution. The goal isn't to finish the problem as quickly as possible but to rather to get some realistic measurements on programming speed and approaches.<br />
<br />
== Submitted results ==<br />
<br />
* [[Programming performance/TimN Haskell]]<br />
* [[Programming performance/TimN Python]]<br />
* [[Programming performance/Magnus Haskell]]<br />
* [[Programming performance/kc5tja Forth]]<br />
* [[Programming performance/Apter K]] (added by kc5tja)<br />
* [[Programming performance/hay.steve Python]]<br />
<br />
<br />
== Instructions ==<br />
<br />
Implement a program that computes the solution to the following problem in your language of choice. Measure how long it took you to make your initial implementation. Post the code and the amount of time it took. Please submit only your time and code for your first implementation in a given language. If you have implemented a solution previously in another language please mention that. You may submit further refinements of your solution or new solutions on the same page as your initial submission but the primary goals is to measure your initial solution and the amount of time required. If you wish, you may look at other solutions but it is preferable that you do not.<br />
<br />
Submit your entry by creating a wiki page with your code, your time, the language used and a note about how comfortable you are with this language. If you relied on any help, such as reading other solutions, please state what sort of help you made use of. Please make just one page per participant per language. You may include any comments, refinements or alternate programs on this same page.<br />
<br />
<br />
<br />
<br />
== Problem ==<br />
<br />
Simulate the following strategy on the GSPC data set:<br />
<br />
* Whenever the closing cost is down 3% or more from the previous closing cost, buy shares with 10% of current cash.<br />
* Whenever the closing cost is up 6% or more from the original purchase price, sell back the full amount of stock purchased at that price.<br />
* Sell off any remaining assets after the last day.<br />
<br />
<br />
Run the strategy on the provided data starting with $10,000.00 in cash and no holdings.<br />
* All purchases are made at the closing cost.<br />
* Fractional shares may be purchased.<br />
* Fractional pennies may be used.<br />
* Do not simulate any trading costs, interest dividends or taxes.<br />
* Percent change is 100 percent times the change (new price minus old price) divided by the old price.<br />
<br />
<br />
=== Data ===<br />
<br />
The http://www.thenewsh.com/gspc.txt file contains the test data. Any lines not starting with a hash sign ("#") contain valid data. Data is in space seperated columns. Each line represents one day's data with most recent date first. The first column contains the date and the last column contains the closing price. The number of columns is always the same.<br />
<br />
<br />
=== Results ===<br />
<br />
Display the amount of cash held after executing the strategy. A mode that displays each buy and sell by date is useful, but optional.</div>Hay.steve