#!/usr/bin/python

from StockerApi import StockTrigger
import numpy
import pandas

class NewHighLowTrigger(StockTrigger):
    def isBullish(self, pricesMap):
        lows = pricesMap["low"]
        count = len(lows)
        lowest = lows[count-1] # get today's low

        # go through the days backward, checking if the prices are getting higher every single day
        consecutiveHigherLows = 0
        for i in xrange(count-2, -1, -1):
            if lowest < lows[i]:
                break
            consecutiveHigherLows += 1

        print "consecutive higher lows in the past: %d days " % consecutiveHigherLows

        threshold = 2
        if consecutiveHigherLows > threshold:
            print "consecutive higher lows for more than %d days, this could be bullish" % threshold
            return True

        print "consecutive higher lows less than %d days, not sure if it's bullish yet" % threshold
        return False

class SMATrigger(StockTrigger):
    def isBullish(self, pricesMap):
        lows = pricesMap["low"]
        count = len(lows)
        # create the pandas series
        series = pandas.Series(lows, dtype=float)
        # create moving average
        mva = series.rolling(5, center=False).mean()
        print mva

        # if price is above the sma, then it's bullish
        if lows[count-1] > mva[count-1]:
            print "today's low %f > today's sma %f" % (lows[count-1], mva[count-1])
            return True
        return False



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""" TESTING """"""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

def TestPassFail(name, res, expected):
    passfail = "Failed"
    if res == expected:
        passfail = "Passed"
        print "Test %s : %s" % (name, passfail)
    else:
        print "Test %s : %s (res: %s, expected: %s)" % (name, passfail, res, expected)



 # NewHighLowTrigger tests
t = NewHighLowTrigger()
a = [1, 2, 3, 4, 5]
map = {"low": a}
res = t.isBullish(map)
TestPassFail("NewHighLowTrigger.bullish", res, True)

a = [1, 2, 3, 4, 5, 2, 3]
map = {"low": a}
res = t.isBullish(map)
TestPassFail("NewHighLowTrigger.bearish", res, False)
