Stock Fun

Now with my enhanced free time, between cover letters I do some coding. I need a standard to measure any stock strategy against. So I came-up with a random buy and sell at >$1 increase. There is so much potential money just with random buys, but buy & hold easily trumps it. To me this explains the people that write a book and say they are an expert. They probably just got lucky.


BUY,  1999-10-25, 42.88
SELL,  1999-10-27, 50.0
BUY,  1999-11-03, 66.5
SELL,  1999-11-08, 75.38
BUY,  1999-11-09, 72.25
SELL,  1999-11-18, 77.75
BUY,  1999-11-19, 88.0
SELL,  2000-01-04, 93.0
BUY,  2000-01-07, 87.5
SELL,  2000-01-10, 90.0
BUY,  2000-01-12, 85.88
SELL,  2000-01-14, 91.12
BUY,  2000-01-19, 115.12
SELL,  2000-01-20, 130.75
BUY,  2000-01-21, 120.0
SELL,  2000-01-26, 131.5
BUY,  2000-01-27, 135.5
SELL,  2000-02-03, 156.0
BUY,  2000-02-10, 220.0
SELL,  2000-02-11, 239.5
BUY,  2000-02-14, 230.44
SELL,  2000-02-29, 258.38
BUY,  2000-03-03, 260.0
SELL,  2000-03-06, 266.0
BUY,  2000-03-08, 280.5
SELL,  2000-03-09, 315.0
Profit, 169.81
------------------------
https://codeformatter.blogspot.com/

 #!/usr/bin/env python  
 import urllib2  
 import sys  
 import json  
 import psycopg2  
 import re  
 import string  
 import random  
 minPDelta = 1  
 minVDelta = -.10  
 minPRise= 1  
 con = None  
 con = psycopg2.connect(database='EXPENSIVEDB', user='DUMMY-USER',host='SOME-HOST')  
 stockStart = "2001-1-1"  
 if len(sys.argv) == 1 :  
   #print "too few, get a random one."  
   cur = con.cursor()  
   cur.execute("select distinct on (dq_symbol) dq_symbol,dq_date,dq_close from daily_quote where dq_close > 50 and dq_close_vol > 1000000 limit 1 offset random()*1000;")  
   rsRow = cur.fetchone()  
   if rsRow == None:  
     stockSymbol = "MSFT"  
   else:  
     stockSymbol = rsRow[0]  
   cur.close()  
   #print "Getting start date"  
   cur = con.cursor()  
   cur.execute("select dq_symbol,dq_date,dq_close from daily_quote where dq_close > 50 and dq_close_vol > 1000000 limit 1 offset random()*1000;")  
   rsRow = cur.fetchone()  
   if rsRow != None:  
     stockStart = rsRow[1]  
   cur.close()  
 else:  
   stockSymbol = sys.argv[1]  
   stockStart = sys.argv[2]  
 #print "Working on %s" %(stockSymbol)  
 cur = con.cursor()  
 cur.execute("select dq_symbol,dq_date,dq_close,dq_close_vol from daily_quote where dq_symbol = '%s' and dq_date > '%s' order by dq_date limit 100;" % (stockSymbol,stockStart))  
 prevVol=None  
 prevPrice=None  
 holding = False  
 buyPrice=None  
 random.seed()  
 runTotal=0  
 while True:  
   rsRow = cur.fetchone()  
   goodVol = False  
   goodPrice = False  
   printData = False  
   if rsRow == None:  
     break  
   if prevVol !=None and prevPrice != None and prevVol > 0:  
     if holding and rsRow[2] > buyPrice+minPRise:  
       print "%s, SELL, "%(stockSymbol),  
       holding = False  
       printData = True  
       runTotal += rsRow[2] - buyPrice  
     else:  
       if random.random() < .5 and not holding:  
         #print "BUY,"  
         print "%s, BUY, "%(stockSymbol),  
         holding = True  
         printData = True  
         buyPrice = rsRow[2]  
   #Prepare for next day  
   #if holding or printData:  
   if printData:  
     print "%s, %s, %s" %(rsRow[1],rsRow[2],rsRow[3])  
   prevPrice = rsRow[2]  
   prevVol = rsRow[3]  
 cur.close()  
 print "%s, Profit, %s" % (stockSymbol,runTotal)  
 if con:  
   con.close()  

Comments

Popular posts from this blog

Blocking Bad Light Sources(Sjogren's?)

Hot Spots for Possible B-Cell Disorders(Lupus/Sjogren/Chronic Fatigue)

HttpWebRequest in 3.5