Exemple #1
0
symbols = [line.strip() for line in open("../data/goodstocks_vol.txt").readlines()]
# symbols = ["F", "AAPL"]
nsymbols = len(symbols)
tickerIDs = {} # key is stock name, val is a unique number (I will use iticker for it)

fname = "forBDT_092915.txt"
if len(sys.argv) > 1: fname = sys.argv[-1]
fh = open(fname,"w")
fhTickers = open(fname.replace(".txt","_tickers.txt"),"w")

valNamesDict, mainDict = {}, {}

for iticker,ticker in enumerate(symbols):
    # stock = gs.getStock(ticker, (2013, 1, 1), (2014, 12, 31))["days"] # for calculating
    # stock = gs.getStock(ticker, (2014, 1, 1), (2014, 12, 20))["days"] # for calculating
    stock = gs.getStock(ticker, (2014, 12, 20), (2015, 10, 1))["days"] # for calculating

    drawProgressBar(1.0*iticker/nsymbols)

    if(len(stock) < 50): continue

    quotes = []
    timesclose = []
    for day in sorted(stock.keys()):
        vals = stock[day]
        o, h, l, c, v = vals["o"], vals["h"], vals["l"], vals["c"], vals["v"]
        quotes.append( [day,o,h,l,c,v] )

    quotes = np.array(quotes)

    if(quotes[-1][4] > 600.0): continue # ignore expensive stocks
Exemple #2
0
    def doBenchmark(self, params={}, progressBar=True, userOnly=False, debug=False):
        self.cleanVars()

        if self.performChecks():
            print "[BT] Can't benchmark"
            return

        for isymbol, symbol in enumerate(self.symbols):
            self.isymbol = isymbol
            if progressBar:
                self.drawProgressBar()

            stock = gs.getStock(symbol, self.d0, self.d2)
            quotes = u.dictToList(stock)  # [day,o,h,l,c]
            if len(quotes) < 15:
                continue

            try:
                dBuy, dSell = self.strategy(quotes, self.d1, self.d2, params)
            except Exception as e:
                print "[BT] Problem running user strategy"
                print e
                continue

            if len(dBuy.keys()) < 1:
                continue  # pointless if we don't buy
            if len(dSell.keys()) < 1:
                continue  # pointless if we don't sell -- then it's just BAH

            try:
                self.report[symbol] = {}
                self.report[symbol]["ndays"] = len(quotes)

                # USER STRATEGY
                price = 0
                ledger = tr.Ledger(self.money)
                for quote in quotes:
                    day, price, h, l, c = quote
                    if day in dSell:
                        ledger.sellStock(symbol, day, price, fraction=dSell[day])
                    elif day in dBuy:
                        ledger.buyStock(symbol, day, price)
                ledger.sellStock(symbol, day, price)  # sell outstanding shares to finish up
                self.report[symbol]["user"] = [
                    ledger.getProfit(),
                    0.0,
                    ledger.getNumTrades(),
                    ledger.getWinPercent(),
                    ledger.getAvgWinProfitPercent(),
                    ledger.getAvgLossProfitPercent(),
                ]

                if not userOnly:
                    # RANDOM STRATEGY
                    profits = []
                    for i in range(100):
                        price = 0
                        ledgerRand = tr.Ledger(self.money)
                        days = quotes[:, 0]
                        np.random.shuffle(days)
                        # want to do a random trade on avg every 3-10 days
                        # so we take the first #days/rand(3,10) random days, then sort them
                        days = sorted(days[: len(days) // random.randint(3, 10)])
                        days = days[len(days) % 2 :]  # even number of entries, so we always sell what we buy
                        buy = True  # buy initially
                        for day in days:
                            if buy:
                                ledgerRand.buyStock(symbol, day, price=stock["days"][day]["c"])
                            else:
                                ledgerRand.sellStock(symbol, day, price=stock["days"][day]["c"])
                            buy = not buy  # alternate between buy and sell
                        profits.append(ledgerRand.getProfit())
                    profits = np.array(profits)

                    # BUY AND HOLD
                    ledgerBAH = tr.Ledger(self.money)  # buy and hold
                    ledgerBAH.buyStock(symbol, quotes[0][0], quotes[0][4])
                    ledgerBAH.sellStock(symbol, quotes[-1][0], quotes[-1][4])

                    self.report[symbol]["rand"] = [
                        round(np.mean(profits), 2),
                        round(np.std(profits)),
                        ledgerRand.getNumTrades(),
                        ledgerRand.getWinPercent(),
                        ledgerRand.getAvgWinProfitPercent(),
                        ledgerRand.getAvgLossProfitPercent(),
                    ]
                    self.report[symbol]["bah"] = [
                        ledgerBAH.getProfit(),
                        0.0,
                        ledgerBAH.getNumTrades(),
                        ledgerBAH.getWinPercent(),
                        ledgerBAH.getAvgWinProfitPercent(),
                        ledgerBAH.getAvgLossProfitPercent(),
                    ]

                    if debug:
                        ledger.printLedger()

            except Exception as e:
                print "[BT] Some other error"
                print e
                continue

        self.postBenchmark()
Exemple #3
0
import os, sys

import numpy as np
import sklearn.cluster as skc

import getStocks as gs
import utils as u
import indicators as ind

plotdir = "../plots/"

# stock = gs.getStock("AAPL", 2005, 2010)
stock = gs.getStock("DIS", (2008, 6, 20), (2010, 10, 7))["days"]

quotes = []
normquotes = []
fractions = []
timesclose = []
for day in sorted(stock.keys()):
    vals = stock[day]
    o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"]

    # "normalize" values (subtract out open price)
    no = round(o - o, 2)
    nh = round(h - o, 2)
    nl = round(l - o, 2)
    nc = round(c - o, 2)

    quotes.append([day, o, h, l, c])
    normquotes.append([day, no, nh, nl, nc])
    # fractions.append( abs(o-c)/(h-l) ) # what fraction of (high-low) is |open-close|?
Exemple #4
0
import os, sys, random

import numpy as np
import getStocks as gs
import utils as u
import indicators as ind
import tradeReport as tr

#symbol = "WMT"
symbol = "NFLX"
# symbol = "F"
stock = gs.getStock(symbol, (2011, 6, 1), (2015, 6, 1)) # for calculating
quotes = u.dictToList(stock) # [day,o,h,l,c]

timesclose = quotes[:,[0,4]]
emas = []
for i in [1,2]: emas.append( ind.ematimes(timesclose,10*i) )

crossovertimes = ind.crossovertimes(emas)
dCrossovers = {} # turn into dict for fast lookup
for time,rising in crossovertimes: dCrossovers[time] = rising


# CROSSOVER STRATEGY
price = 0
ledger = tr.Ledger(1000)
for quote in quotes:
    day,price,h,l,c = quote
    if(day in dCrossovers): 
        rising = dCrossovers[day]
Exemple #5
0
doCuts = False
writeBDTtext = True
makePlots = True
fh = None
dInds = {}
bkgPercentinc = np.array([])
sigPercentinc = np.array([])

if (writeBDTtext):
    fh = open("forBDT.txt", "w")

for iticker, ticker in enumerate(symbols):
    # stock = gs.getStock(ticker, (2013, 1, 1), (2014, 12, 31))["days"] # for calculating
    # stock = gs.getStock(ticker, (2014, 1, 1), (2014, 12, 20))["days"] # for calculating
    stock = gs.getStock(ticker, (2014, 12, 20),
                        (2015, 10, 1))["days"]  # for calculating

    drawProgressBar(1.0 * iticker / nsymbols)

    if (len(stock) < 50): continue

    quotes = []
    timesclose = []
    for day in sorted(stock.keys()):
        vals = stock[day]
        o, h, l, c, v = vals["o"], vals["h"], vals["l"], vals["c"], vals["v"]
        quotes.append([day, o, h, l, c, v])

    quotes = np.array(quotes)

    if (quotes[-1][4] > 300.0): continue  # ignore expensive stocks
Exemple #6
0
import os, sys

import numpy as np
import sklearn.cluster as skc

import getStocks as gs
import utils as u
import indicators as ind

plotdir = "../plots/"

# stock = gs.getStock("AAPL", 2005, 2010)
stock = gs.getStock("DIS", (2008, 6, 20), (2010, 10, 7))["days"]

quotes = []
normquotes = []
fractions = []
timesclose = []
for day in sorted(stock.keys()):
    vals = stock[day]
    o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"]

    # "normalize" values (subtract out open price)
    no = round(o - o, 2)
    nh = round(h - o, 2)
    nl = round(l - o, 2)
    nc = round(c - o, 2)

    quotes.append([day, o, h, l, c])
    normquotes.append([day, no, nh, nl, nc])
    # fractions.append( abs(o-c)/(h-l) ) # what fraction of (high-low) is |open-close|?
Exemple #7
0
    def doBenchmark(self,
                    params={},
                    progressBar=True,
                    userOnly=False,
                    debug=False):
        self.cleanVars()

        if self.performChecks():
            print "[BT] Can't benchmark"
            return

        for isymbol, symbol in enumerate(self.symbols):
            self.isymbol = isymbol
            if (progressBar): self.drawProgressBar()

            stock = gs.getStock(symbol, self.d0, self.d2)
            quotes = u.dictToList(stock)  # [day,o,h,l,c]
            if (len(quotes) < 15): continue

            try:
                dBuy, dSell = self.strategy(quotes, self.d1, self.d2, params)
            except Exception as e:
                print "[BT] Problem running user strategy"
                print e
                continue

            if (len(dBuy.keys()) < 1): continue  # pointless if we don't buy
            if (len(dSell.keys()) < 1):
                continue  # pointless if we don't sell -- then it's just BAH

            try:
                self.report[symbol] = {}
                self.report[symbol]["ndays"] = len(quotes)

                # USER STRATEGY
                price = 0
                ledger = tr.Ledger(self.money)
                for quote in quotes:
                    day, price, h, l, c = quote
                    if (day in dSell):
                        ledger.sellStock(symbol,
                                         day,
                                         price,
                                         fraction=dSell[day])
                    elif (day in dBuy):
                        ledger.buyStock(symbol, day, price)
                ledger.sellStock(symbol, day,
                                 price)  # sell outstanding shares to finish up
                self.report[symbol]["user"] = [
                    ledger.getProfit(), 0.0,
                    ledger.getNumTrades(),
                    ledger.getWinPercent(),
                    ledger.getAvgWinProfitPercent(),
                    ledger.getAvgLossProfitPercent()
                ]

                if (not userOnly):
                    # RANDOM STRATEGY
                    profits = []
                    for i in range(100):
                        price = 0
                        ledgerRand = tr.Ledger(self.money)
                        days = quotes[:, 0]
                        np.random.shuffle(days)
                        # want to do a random trade on avg every 3-10 days
                        # so we take the first #days/rand(3,10) random days, then sort them
                        days = sorted(days[:len(days) //
                                           random.randint(3, 10)])
                        days = days[
                            len(days) %
                            2:]  # even number of entries, so we always sell what we buy
                        buy = True  # buy initially
                        for day in days:
                            if (buy):
                                ledgerRand.buyStock(
                                    symbol, day, price=stock["days"][day]['c'])
                            else:
                                ledgerRand.sellStock(
                                    symbol, day, price=stock["days"][day]['c'])
                            buy = not buy  # alternate between buy and sell
                        profits.append(ledgerRand.getProfit())
                    profits = np.array(profits)

                    # BUY AND HOLD
                    ledgerBAH = tr.Ledger(self.money)  # buy and hold
                    ledgerBAH.buyStock(symbol, quotes[0][0], quotes[0][4])
                    ledgerBAH.sellStock(symbol, quotes[-1][0], quotes[-1][4])

                    self.report[symbol]["rand"] = [
                        round(np.mean(profits), 2),
                        round(np.std(profits)),
                        ledgerRand.getNumTrades(),
                        ledgerRand.getWinPercent(),
                        ledgerRand.getAvgWinProfitPercent(),
                        ledgerRand.getAvgLossProfitPercent()
                    ]
                    self.report[symbol]["bah"] = [
                        ledgerBAH.getProfit(), 0.0,
                        ledgerBAH.getNumTrades(),
                        ledgerBAH.getWinPercent(),
                        ledgerBAH.getAvgWinProfitPercent(),
                        ledgerBAH.getAvgLossProfitPercent()
                    ]

                    if debug:
                        ledger.printLedger()

            except Exception as e:
                print "[BT] Some other error"
                print e
                continue

        self.postBenchmark()
Exemple #8
0
import numpy as np
import sklearn.cluster as skc

import getStocks as gs
import utils as u
import indicators as ind
import tradeReport as tr
import random

plotdir = "../plots/"

#symbol = "WMT"
#symbol = "NFLX"
symbol = "F"
stock = gs.getStock(symbol, (2014, 6, 1), (2015, 6, 1))["days"] # for calculating
d1,d2 = (2014,3,10),(2014,6,15) # for plotting


quotes = []
normquotes = []
timesclose = []
for day in sorted(stock.keys()):
    vals = stock[day]
    o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"]

    # "normalize" values (subtract out open price)
    no = round(o-o,2)
    nh = round(h-o,2)
    nl = round(l-o,2)
    nc = round(c-o,2)
Exemple #9
0
import numpy as np
import sklearn.cluster as skc

import getStocks as gs
import utils as u
import indicators as ind
import tradeReport as tr
import random

plotdir = "../plots/"

#symbol = "WMT"
#symbol = "NFLX"
symbol = "F"
stock = gs.getStock(symbol, (2014, 6, 1),
                    (2015, 6, 1))["days"]  # for calculating
d1, d2 = (2014, 3, 10), (2014, 6, 15)  # for plotting

quotes = []
normquotes = []
timesclose = []
for day in sorted(stock.keys()):
    vals = stock[day]
    o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"]

    # "normalize" values (subtract out open price)
    no = round(o - o, 2)
    nh = round(h - o, 2)
    nl = round(l - o, 2)
    nc = round(c - o, 2)
Exemple #10
0
            rising = dCrossovers[day]
            if(rising):
                dBuy[day] = 1
                hasBought = True
                boughtPrice = quotes[i][4]

    return dBuy

symbols = [line.strip() for line in open("../data/nasdaqlisted.txt").readlines()][:1000]

today = datetime.datetime.today()
dtuple = (today.year,today.month,today.day-3)

goodsymbols = []
for symbol in symbols:
    stock = gs.getStock(symbol, (2015, 8, 1), dtuple)
    quotes = u.dictToList(stock) # [day,o,h,l,c]
    array_quotes = np.array(quotes)
    print symbol
    if(len(quotes) < 25): continue
    if np.mean(quotes[:,4]) > 20: continue
    if np.mean(quotes[:,4]) < 0.25: continue
    buys = crossover(quotes)
    if u.tuple2inum(dtuple) in buys:
        print symbol, "#"*40
        goodsymbols.append(symbol)

print "-"*40
print " ".join(goodsymbols)
print "-"*40
Exemple #11
0
import os, sys

import numpy as np
import sklearn.cluster as skc

import getStocks as gs
import utils as u
import indicators as ind
import cluster as cl

plotdir = "../plots/"

stock = gs.getStock("F", (2010, 1, 1), (2011, 10, 5))["days"] # for calculating
d1,d2 = (2010,8,1),(2011,3,5) # for plotting

quotes = []
timesclose = []
for day in sorted(stock.keys()):
    vals = stock[day]
    o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"]

    quotes.append( [day,o,h,l,c] )
    timesclose.append([day,c])

timesclose = np.array(timesclose)
bbands = ind.bbtimes(timesclose, 20)

emas = [ ind.ematimes(timesclose,i) for i in [5,10] ]

quotes = np.array(quotes)
rsis = ind.rsitimes(quotes[:,[0,1]],14)