def calcPatternPosition(self, stockCode, processedData): patternDataObjects = [] open = numpy.array(map(lambda item: item[1], processedData)) high = numpy.array(map(lambda item: item[2], processedData)) low = numpy.array(map(lambda item: item[3], processedData)) close = numpy.array(map(lambda item: item[4], processedData)) #ジグザグの座標を取得 pivots = zigzag.peak_valley_pivots2(open, high, low, close, 0.05, -0.05) pivotPeakPoses = numpy.arange(len(processedData))[pivots == 1] pivotBottomPoses = numpy.arange(len(processedData))[pivots == -1] for currentPos in range(len(processedData)): peakPoses = filter(lambda p: p < currentPos, pivotPeakPoses) bottomPoses = filter(lambda p: p < currentPos, pivotBottomPoses) peakBottomPosFlag = True extreme1 = 0 extreme2 = 0 extreme3 = 0 extreme4 = 0 extreme5 = 0 extreme1X = 0 extreme2X = 0 extreme3X = 0 extreme4X = 0 extreme5X = 0 if(len(peakPoses) > 2 and len(bottomPoses) > 2): if(peakPoses[-1] > bottomPoses[-1]): extreme1X = peakPoses[-1] extreme1 = high[extreme1X] extreme2X = bottomPoses[-1] extreme2 = low[extreme2X] extreme3X = peakPoses[-2] extreme3 = high[extreme3X] extreme4X = bottomPoses[-2] extreme4 = low[extreme4X] extreme5X = peakPoses[-3] extreme5 = high[extreme5X] else: peakBottomPosFlag = False if(peakBottomPosFlag): extremArray = numpy.array([extreme1, extreme2, extreme3, extreme4, extreme5]) withinMean = numpy.mean(extremArray) trendLineRange = numpy.mean([extreme1, extreme5]) - numpy.mean([extreme2, extreme4]) peakLineMax = max([extreme1, extreme5]) #各頂点の位置での判定 if(extreme3 > extreme5 and extreme3 > extreme1 and math.fabs(extreme5 - extreme1)/ withinMean < 0.015 and math.fabs(extreme4 - extreme2) / withinMean < 0.015 and extreme3 - peakLineMax > trendLineRange * 0.5 ): bottomLine = lineUtils.Line(extreme4X, extreme4, extreme2X, extreme2, processedData[extreme4X][0], processedData[extreme2X][0], extreme4, extreme2) #伸ばす bottomLine = lineUtils.LineUtils.extendTrendLine([bottomLine], processedData, False, 0, currentPos)[0] #ピーク peakLine = lineUtils.Line(extreme5X, extreme5, extreme1X, extreme1, processedData[extreme5X][0], processedData[extreme1X][0], extreme5, extreme1) upperFlag = isTwiceUpLine(extreme3X, peakLine, processedData, 1) and isTwiceUpLine(extreme3X, peakLine, processedData, -1) #ボトムの値が上のラインより上にいかない事を確認 for pos in range(currentPos, peakLine.endX, -1): if(processedData[pos][2] > peakLine.endPriceY): upperFlag = False break #まぁこちらも for pos in range(bottomLine.startX, peakLine.startX): if(processedData[pos][2] > peakLine.startPriceY): upperFlag = False break if(upperFlag and currentPos <= bottomLine.endX and processedData[currentPos][3] < bottomLine.getYPoint(currentPos)): patternDataObjects.append(patternDataObject.PatternDataObject(stockCode, currentPos, processedData[currentPos][0], processedData[currentPos][4], bottomLine, peakLine, "", "down")) return patternDataObjects
def calcPatternPosition(self, stockCode, resultRaw, upperLineSlopFn, lowerLineSlopFn, hasVertexX, isLatest): #print hasVertexX #print isLatest result = resultRaw fortyFiveLine = float(1) / float(len(result)) patternDataObjects = [] rangeArray = range(len(result) - 1, len(result)) if isLatest else range(1, len(result)) #print rangeArray for currentPos in rangeArray: peakPoses, bottomPoses = self.calcPeakBottomPoint( result[0:currentPos]) filter_peak_poses = filter(lambda pos: pos < currentPos, peakPoses) filter_bottom_poses = filter(lambda pos: pos < currentPos, bottomPoses) #スタート地点の計算 peakStartTerm = self.getTrendStartPosition(currentPos, filter_peak_poses) bottomStartTerm = self.getTrendStartPosition( currentPos, filter_bottom_poses) startTerm = min(peakStartTerm, bottomStartTerm) registLines = self.drawTrendLine(filter_peak_poses, result, True, startTerm, currentPos) supportLines = self.drawTrendLine(filter_bottom_poses, result, False, startTerm, currentPos) slopRegists = [] """-----------------------------上のトレンドライン------------------------------------""" for line in filter( lambda hoge: self.isDrawLine(hoge, peakStartTerm, currentPos), registLines): slopNumber = self.getSlopNumber(line[0], line[1]) if (upperLineSlopFn(slopNumber)): slopRegists.append([line[0], line[1]]) slopSupports = [] """-----------------------------下のトレンドライン------------------------------------""" for line in filter( lambda hoge: self.isDrawLine(hoge, bottomStartTerm, currentPos), supportLines): supportNumber = self.getSlopNumber(line[0], line[1]) if (lowerLineSlopFn(supportNumber)): slopSupports.append([line[0], line[1]]) """ここからは上下のトレンドラインがある場合にだけ選別する""" if (len(slopSupports) > 0 and len(slopRegists) > 0): regArray = slopRegists[0] supArray = slopSupports[0] #交点 futureVertexX = self.getFutureVertex( patternDataObject.PosXY(regArray[0][0], regArray[1][0]), patternDataObject.PosXY(regArray[0][1], regArray[1][1]), patternDataObject.PosXY(supArray[0][0], supArray[1][0]), patternDataObject.PosXY(supArray[0][1], supArray[1][1])) narrowPosition = max( filter(lambda index: index <= regArray[0][1], range(len(result)))) narrowPosition2 = max( filter(lambda index: index <= supArray[0][1], range(len(result)))) #直近のやつだけ拾ってくる if ((not hasVertexX or futureVertexX - 3 < currentPos) and (currentPos == narrowPosition or currentPos == narrowPosition2)): minPos = supArray[0][0] maxArray = regArray minArray = supArray """ここで長いトレンドラインに合わせる作業を行う""" if (supArray[0][0] > regArray[0][0]): minPos = regArray[0][0] minArray = regArray maxArray = supArray maxArray[1][0] = self.getYPosition(maxArray, minPos) maxArray[0][0] = minPos upDownStr = self.getUpDownStr(result, regArray, supArray, currentPos) patternDataObjects.append( patternDataObject.PatternDataObject( stockCode, currentPos, resultRaw[currentPos][0], resultRaw[currentPos][4], #ここでライン lineUtils.Line(regArray[0][0], regArray[1][0], regArray[0][1], regArray[1][1], result[regArray[0][0]][0], result[regArray[0][1]][0], regArray[1][0], regArray[1][1]), #ここでライン lineUtils.Line(supArray[0][0], supArray[1][0], supArray[0][1], supArray[1][1], result[supArray[0][0]][0], result[supArray[0][1]][0], supArray[1][0], supArray[1][1]), "penant", upDownStr)) return patternDataObjects
def IcalcPatternPosition(self, stockCode, processedData): patternDataObjects = [] open = numpy.array(map(lambda item: item[1], processedData)) high = numpy.array(map(lambda item: item[2], processedData)) low = numpy.array(map(lambda item: item[3], processedData)) close = numpy.array(map(lambda item: item[4], processedData)) #ジグザグの座標を取得 pivots = zigzag.peak_valley_pivots2(open, high, low, close, 0.05, -0.05) pivotPeakPoses = numpy.arange(len(processedData))[pivots == 1] pivotBottomPoses = numpy.arange(len(processedData))[pivots == -1] for currentPos in range(len(processedData)): peakPoses = filter(lambda p: p < currentPos, pivotPeakPoses) bottomPoses = filter(lambda p: p < currentPos, pivotBottomPoses) peakBottomPosFlag = True extreme1 = 0 extreme2 = 0 extreme3 = 0 extreme4 = 0 extreme5 = 0 extreme1X = 0 extreme2X = 0 extreme3X = 0 extreme4X = 0 extreme5X = 0 if(len(peakPoses) > 2 and len(bottomPoses) > 2): if(peakPoses[-1] < bottomPoses[-1]): extreme1X = bottomPoses[-1] extreme1 = low[extreme1X] extreme2X = peakPoses[-1] extreme2 = high[extreme2X] extreme3X = bottomPoses[-2] extreme3 = low[extreme3X] extreme4X = peakPoses[-2] extreme4 = high[extreme4X] extreme5X = bottomPoses[-3] extreme5 = low[extreme5X] else: peakBottomPosFlag = False trendLineRange = 0 peakLineMax = 0 if(peakBottomPosFlag): extremArray = numpy.array([extreme1, extreme2, extreme3, extreme4, extreme5]) withinMean = numpy.mean(extremArray) trendLineRange = numpy.mean([extreme1, extreme5]) - numpy.mean([extreme2, extreme4]) peakLineMax = max([extreme1, extreme5]) #各頂点の位置での判定 if(extreme3 < extreme1 and extreme3 < extreme5 and math.fabs(extreme5 - extreme1)/ withinMean < 0.015 and math.fabs(extreme4 - extreme2) / withinMean < 0.015 and math.fabs(extreme3 - peakLineMax) > math.fabs(trendLineRange * 0.5) ): bottomLine = lineUtils.Line(extreme5X, extreme5, extreme1X, extreme1, processedData[extreme5X][0], processedData[extreme1X][0], extreme5, extreme1) #ピーク peakLine = lineUtils.Line(extreme4X, extreme4, extreme2X, extreme2, processedData[extreme4X][0], processedData[extreme2X][0], extreme4, extreme2) #伸ばす peakLine = lineUtils.LineUtils.extendTrendLine([peakLine], processedData, True, 0, currentPos)[0] upperFlag = isTwiceDownLine(extreme3X, bottomLine, processedData, 1) and isTwiceDownLine(extreme3X, bottomLine, processedData, -1) if(upperFlag and currentPos <= peakLine.endX): patternDataObjects.append(patternDataObject.PatternDataObject(stockCode, currentPos, processedData[currentPos][0], processedData[currentPos][4], bottomLine, peakLine, "", "down")) return patternDataObjects