Ejemplo n.º 1
0
 def execExpression(self,data,date):
     """
     date 指买入日期
         >> accuracy.execExpression(data = {u'200056': {'statistics': {u'\u4e00\u65e5\u4e0a\u6da81%': 1.0,u'\u4e09\u5341\u65e5\u4e0a\u6da830%': 1.0,u'\u5341\u4e94\u65e5\u4e0a\u6da815%': 1.0000000000000002},'strategic': [u'\u77ed\u65f6\u95f4\u5185\u4e0b\u8dcc19%\u4ee5\u4e0a\u540e\u653e\u91cf\u6536\u7ea2']}}, date = '20110804')
         {u'\u5341\u4e94\u65e5\u4e0a\u6da815%': {'matchCode': [], 'notMatchCode': [u'200056']}, u'\u4e00\u65e5\u4e0a\u6da81%': {'matchCode': [], 'notMatchCode': [u'200056']}}
     """
     execResult = {}
     result = {}
     today = Date.getDate()
     for code in data:
         for statisticsName in data[code].get('statistics',{}):
             expression = self.expression.getExpressionByName(name = statisticsName,expressions = self.expressions)
             if not expression:
                 continue
             if not code in execResult:
                 execResult[code]={}
             offsetDate = Date.getDate(0-expression["offset"],date)
             if int(offsetDate) > int(today):
                 continue
             context = {
                 "result":execResult,
                 "code":code,
                 "name":statisticsName,
                 "stock":self.stock,
                 "date":offsetDate
             }
             self.expression.execExpression(expression = expression.get("content"), context = context)
             if execResult[code][statisticsName] == True:
                 sub = MATCH_KEY
             else:
                 sub = NOTMATCH_KEY
             if not statisticsName in result:
                 result[statisticsName]={MATCH_KEY:[],NOTMATCH_KEY:[]}
             result[statisticsName][sub].append(code)
     return result
 def __init__(self,conf={}):
     self.conf = conf
     self.dataHandler = Data(conf)
     self.allCode = self.dataHandler.get("allCode")
     self.date = conf.get('date',Date.getDate()) #抓取指定日期的detail
     self.sourceName=conf.get('SOURCE_NAME')
     self.threadNum = int(conf.get('THREAD_NUM',THREAD_NUM)) # 采用多线程模式抓取数据时的线程数
Ejemplo n.º 3
0
    def variance(self,stock,date = None):
        """
        方差公式:
        s^2 = [(x1-x)^2+(x2-x)^2+......(xn-x)^2]/n
        >> statistics.variance(stock = stock,date = '20111024')
        0.0002890704988633324
        """
        ex = self.expection(stock = stock,date = date)
        total = 0 
        count = 0

        if date == None:
            date = [Date.getDate()]
        elif date == "all":
            date = stock.allDate
        elif type(date) != list:
            date = [date]

        for date in date:
            for code in stock.allCode:
                change = stock[code][date].change
                if type(change) == float:
                    total +=(change - ex)**2
                    count +=1
        return total/count
Ejemplo n.º 4
0
    def statistics(self,subject,stock,date = None):
        """
        >> subject = [0.03,0.04]
        >> statistics.statistics(subject = subject,stock = stock,date = '20111024')
        0.19164835164835164
        """
        if type(subject) == list and len(subject) == 2:
            pass
        else:
            raise ValueError

        if date == None:
            date = [Date.getDate()]
        elif date == "all":
            date = stock.allDate
        elif type(date) != list:
            date = [date]
        total = 0
        count = 0
        for oneDate in date:
            for code in stock[oneDate].allCode:
                change = stock[code].change
                if  type(change)!=float :
                    continue
                minItem = subject[0]
                maxItem = subject[1]
                if float(minItem) <= change <float(maxItem): #Do note that left is <=
                    count += 1
                total += 1
        if not total:
            p = 0
        else:
            p = float(count)/total
        return p
Ejemplo n.º 5
0
 def __init__(self,conf={}):
     self.conf = conf
     self.stock = Stock(self.conf)
     self.date = conf.get('date',Date.getDate()) #抓取指定日期的detail
     self.sourceName=conf.get('DETAIL_SOURCE_NAME') or DETAIL_SOURCE_NAME
     self.threadMode = conf.get('THREAD_MODE',THREAD_MODE) # Detail的线程模式 single|mutil
     self.threadNum = int(conf.get('THREAD_NUM',THREAD_NUM)) # 采用多线程模式抓取数据时的线程数
     self.parser=Parser.ParserFactory(self.sourceName)
Ejemplo n.º 6
0
 def __init__(self,conf={}):
     self.date=conf.get("date",Date.getDate())
     self.stock = conf.get("stock",Stock(conf=conf))
     self.expression=Expression(conf)
     self.expressions = self.expression.load(\
     path = os.path.join(conf.get("SYS_HOME"),conf.get("STATISTICS_PROJECT_PATH",STATISTICS_PROJECT_PATH)))
     self.report = Report(conf=conf)
     self.data = {}
     self.conf=conf
Ejemplo n.º 7
0
    def collect(self,date=None):
        """
        收集需要被统计的数据

            >> accuracy.collect(date = "20110825")

        """
        conf = self.conf
        date = date or self.date or conf.get("date")
        detail={}
        dateRange = self.conf.get("STATISTICS_DATE_RANGE",STATISTICS_DATE_RANGE)
        for i in range(0,dateRange):
            for expression in self.expressions:
                curDate = Date.getDate(0-i,date)
                offsetDate = Date.getDate(expression['offset'],curDate)
                trade = self.report.getTrade(date = offsetDate)
                execDetail = self.execExpression(data = trade,date = offsetDate)
                if not execDetail:
                    continue
                detail.update({offsetDate:execDetail}) #将前一天统计数据加上今日统计数据便得到要生成的统计数据
        return detail
 def run(self):
     date = self.conf.get("date",Date.getDate())
     queue = self.conf.get('queue')
     handle = self.conf.get('handle')
     if queue:
         while True:
             code=queue.get()
             try:
                 handle(date = date ,code = code)
                 queue.task_done()
             except:
                 logging.error('Error hanpped when download data.')
                 queue.task_done()
Ejemplo n.º 9
0
    def collect(self,projectPath=None):
        """
            >> projectPath = os.path.join(conf.get("SYS_HOME"),case["collect"]['projectPath'])

            >> train.collect(projectPath)
            >> type(train.collect(projectPath)) == dict
            True
        """
        conf = self.conf
        data={}
        projectPath = projectPath or os.path.join(conf.get("SYS_HOME"),conf.get("STATISTICS_PROJECT_PATH"))
        expressions = self.expression.load(path=projectPath)
        for i in range(0,conf.get("MIN_DATA_NUM",MIN_DATA_NUM)):
            date=Date.getDate(0-i,conf.get('date',Date.getDate()))
            result={}
            for code in self.stock.allCode:
                result[code]={}
            self.expression.execute(expression= conf.get("expressions",expressions), context = {"result":result,"date":date})
            if not result:
                break;
            data[date]=result
        return data   
Ejemplo n.º 10
0
    def expection(self,stock,date = None):
        """
        期望值公式 : expection = (x1 + x2 + x3 +... +xn)/n
        >> statistics.expection(stock = stock,date = '20111024')
        0.02870564489608117
        """
        sumValue = 0
        count = 0
        if date == None:
            date = [Date.getDate()]
        elif date == "all":
            date = stock.allDate
        elif type(date) != list:
            date = [date]

        for oneDate in date:
            for code in stock[oneDate].allCode:
                change = stock[code].change
                if type(change) == float:
                    sumValue = change + sumValue
                    count +=1
        return sumValue / count
Ejemplo n.º 11
0
 def __init__(self,conf={}):
     self.conf=conf
     self.stock=conf.get("stock",Stock(conf))
     self.date=conf.get("date",Date.getDate())