Example #1
0
def crossover(quotes, d1, d2, params={}):
    # define a function that takes a quotes object, so it is an array with elements [day,o,h,l,c]
    # and returns a dictionary with keys of buy and sell times
    # values are not used (1 here), so that gives us room to pack in more information with this function
    print d1, d2

    timesclose = quotes[:,[0,4]]
    p1, p2 = params["p1"], params["p2"]
    p1, p2 = min(p1,p2), max(p1,p2)
    if(p2 - p1 <= 1): return { }, { }

    emas = [ ind.ematimes(timesclose,i) for i in [p1, p2] ]
    crossovertimes = ind.crossovertimes(emas)
    for t in crossovertimes:
        print u.inum2tuple(t[0])
    dCrossovers = {} # turn into dict for fast lookup
    for time,rising in crossovertimes: dCrossovers[time] = rising
    dBuy = { }
    dSell = { }

    hasBought = False
    boughtPrice = -1.0
    for i,day in enumerate(quotes[:,0]):
        # only trade within specified window
        if( day > u.tuple2inum(d2) or day < u.tuple2inum(d1) ): continue

        if(day in dCrossovers): 
            rising = dCrossovers[day]
            if(rising):
                dBuy[day] = 1
                hasBought = True
                boughtPrice = quotes[i][4]
            else:
                if(hasBought): # don't sell if we already sold
                    dSell[day] = 1
                    hasBought = False

        # if price is x% lower than what we bought for, get the hell out and sell a certain %
        if( hasBought and (0.0 < quotes[i][4] / boughtPrice < 0.90) ):
            dSell[day] = 1
            hasBought = False

    return dBuy, dSell
Example #2
0
def crossover(quotes):

    timesclose = quotes[:,[0,4]]

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

    hasBought = False
    boughtPrice = -1.0
    for i,day in enumerate(quotes[:,0]):
        #print day
        if(day in dCrossovers): 
            rising = dCrossovers[day]
            if(rising):
                dBuy[day] = 1
                hasBought = True
                boughtPrice = quotes[i][4]

    return dBuy
Example #3
0
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]

        if(rising): ledger.buyStock(symbol, price)
        else: ledger.sellStock(symbol,price)
        
Example #4
0
File: avg.py Project: aminnj/makers
dBuy = {}
dSell = {}
for i,clusterIdx in enumerate(clusters[:-2]):
    profit = clustersAndProfits[clusterIdx]

    if(profit > 0.03):
        dBuy[times[i+1]] = 1
        dSell[times[i+2]] = 1

# colors = list("rbgkcmyw") # only allows up to 8
# clustershading = []
# for i, day in enumerate(times):
#     clustershading.append([ day, colors[clusters[i]] ])

crossovershading = []
for day, rising in ind.crossovertimes(emas):
    crossovershading.append([day, 'g' if rising else 'r'])


# u.makeCandlestick(clusterQuotes, "../plots/candlestick.png",title="cluster centers",vlines=vlines)

u.makeCandlestick(quotes, "../plots/candlestick.png", title="WMT (2009)", \
        # shadings=[clustershading,crossovershading], \
        shadings=[crossovershading], \
        bbands=bbands, \
        window=[d1,d2], \
        averages=emas, \
        rsis=rsis, \
        )

u.web("../plots/candlestick.png")