Esempio n. 1
0
    def calculate(self, timePositions, iTimePositionDict):
        ''' calculate basic metrics '''
        if not timePositions:
            return self.result

        # get max value, min value, max draw down
        lastHigest = 0
        maxDrawDownTimeStamp = 0
        maxDrawDownPosition = 0
        for (timeStamp, position) in timePositions:
            if self.result[BasicMetric.MAX_TIME_VALUE][0] is None or self.result[BasicMetric.MAX_TIME_VALUE][1] < position:
                self.result[BasicMetric.MAX_TIME_VALUE] = timeStamp, position
            if self.result[BasicMetric.MIN_TIME_VALUE][0] is None or self.result[BasicMetric.MIN_TIME_VALUE][1] > position:
                self.result[BasicMetric.MIN_TIME_VALUE] = timeStamp, position
            if position > lastHigest:
                lastHigest = position
                maxDrawDownPosition = position
                maxDrawDownTimeStamp = timeStamp
            elif maxDrawDownPosition > position:
                maxDrawDownPosition = position
                maxDrawDownTimeStamp = timeStamp

        self.result[BasicMetric.MAX_DRAW_DOWN] = (0, 0) if lastHigest == 0 else \
            (maxDrawDownTimeStamp, 1 - (maxDrawDownPosition / lastHigest))
        self.result[BasicMetric.START_TIME] = timePositions[0][0]
        self.result[BasicMetric.END_TIME] = timePositions[-1][0]
        self.result[BasicMetric.END_VALUE] = timePositions[-1][1]
        self.result[BasicMetric.STDDEV] = stddev([timePosition[1] for timePosition in timePositions])
        self.result[BasicMetric.SRATIO] = sharpeRatio([timePosition[1] for timePosition in timePositions])
        self.result[BasicMetric.R_SQUARED] = rsquared([tp[1] for tp in timePositions], [iTimePositionDict.get(tp[0], tp[1]) for tp in timePositions])

        return self.result
Esempio n. 2
0
    def calculate(self, timePositions):
        ''' calculate basic metrics '''
        for (timeStamp, position) in timePositions:
            if self.result[BasicMetric.MAX][0] is None or self.result[BasicMetric.MAX][1] < position:
                self.result[BasicMetric.MAX] = timeStamp, position
            if self.result[BasicMetric.MIN][0] is None or self.result[BasicMetric.MIN][1] > position:
                self.result[BasicMetric.MIN] = timeStamp, position

        self.result[BasicMetric.STDDEV] = stddev([timePosition[1] for timePosition in timePositions])
        self.result[BasicMetric.SRATIO] = sharpeRatio([timePosition[1] for timePosition in timePositions])

        return self.result
Esempio n. 3
0
    def calculate(self, timePositions):
        ''' calculate basic metrics '''
        for (timeStamp, position) in timePositions:
            if self.result[BasicMetric.MAX][0] is None or self.result[
                    BasicMetric.MAX][1] < position:
                self.result[BasicMetric.MAX] = timeStamp, position
            if self.result[BasicMetric.MIN][0] is None or self.result[
                    BasicMetric.MIN][1] > position:
                self.result[BasicMetric.MIN] = timeStamp, position

        self.result[BasicMetric.STDDEV] = stddev(
            [timePosition[1] for timePosition in timePositions])
        self.result[BasicMetric.SRATIO] = sharpeRatio(
            [timePosition[1] for timePosition in timePositions])

        return self.result
Esempio n. 4
0
dam = DAMFactory.createDAM("sql", {'db': 'sqlite:////data/stock.sqlite'})
dateTicks = dam.readBatchTupleQuotes(symbols, 20031210, 20131210)

symbolTicks = {}
for timeStamp in sorted(dateTicks.iterkeys()):
    for symbol, tick in dateTicks[timeStamp].iteritems():
        if symbol not in symbolTicks:
            symbolTicks[symbol] = []

        symbolTicks[symbol].append(tick)

bads = []
goods = {}  # symbol as key, std as value
for symbol, ticks in symbolTicks.iteritems():
    avgClose = mean([tick.close for tick in ticks])
    std = 100 * stddev([tick.close for tick in ticks]) / avgClose
    print "std for %s is %f" % (symbol, std)

    ticks = ticks[-30:]
    avgVolumnDollar = mean([tick.volume * tick.close for tick in ticks])

    if avgVolumnDollar > 1000000 and avgClose > 6 and avgClose < 100:
        goods[symbol] = std
    else:
        bads.append(symbol)

print "=========bad %s==============" % len(bads)
print bads

sortedList = sorted(goods.iteritems(), key=lambda x: x[1])
print "=========good %s==============" % len(sortedList)
Esempio n. 5
0
dateTicks = dam.readBatchTupleQuotes(symbols, 20031210, 20131210)


symbolTicks = {}
for timeStamp in sorted(dateTicks.iterkeys()):
    for symbol, tick in dateTicks[timeStamp].iteritems():
        if symbol not in symbolTicks:
            symbolTicks[symbol] = []

        symbolTicks[symbol].append(tick)

bads = []
goods = {} # symbol as key, std as value
for symbol, ticks in symbolTicks.iteritems():
    avgClose = mean([tick.close for tick in ticks])
    std = 100 * stddev([tick.close for tick in ticks])/avgClose
    print "std for %s is %f" % (symbol, std)

    ticks = ticks[-30:]
    avgVolumnDollar = mean([tick.volume * tick.close for tick in ticks])

    if avgVolumnDollar > 1000000 and avgClose > 6 and avgClose < 100:
        goods[symbol] = std
    else:
        bads.append(symbol)

print "=========bad %s==============" % len(bads)
print bads

sortedList = sorted(goods.iteritems(), key=lambda x: x[1])
print "=========good %s==============" % len(sortedList)