# -*- coding: utf-8 -*-
"""
Created on Tue Jun  6 09:28:17 2017

@author: Administrator
"""

import pandas as pd
import ToolModule as tm
#import datetime
import FilterDataModule as fd

def getData(code=None,startDate=None,endDate=None,ft=None):
    data_list = []
    code_list = []
    turnover_rate_list = []
    if code is None:
        allCode = fd.all_share_codes
    else:
        allCode = code
    #allCode = [x for x in allCode if (int(x) > 300999 or int(x) < 300000)]
    #print(len(allCode))
    for code in allCode:
        data = fd.getHisDataByCode(code,startDate,endDate)
        if isinstance(data,pd.DataFrame) and not data.empty and (ft is None or ft(data)):
            data = data[data.turnover > 0]
            turnover_rate_list.extend(list(data.turnover/data.turnover.min()))
            code_list.extend([code]*len(data))
            data_list.append(data)
            #print('==================')
            #print(data)
    #print(len(data_list))    
    frame = pd.concat(data_list)#,ignore_index=True)
    frame.loc[:,'code'] = code_list
    frame.loc[:,'turnover_rate'] = turnover_rate_list
    return frame

"""
factor 大势因子，要根据宏观环境给出一个估计（0,1],0表示整个市场不具有任何投资机会，
       1表示随机投资就可以赚钱
startDate ，endDate 基于那段区间的历史数据做出的判断
"""
def selectData(factor,startDate,endDate):
    infoData = fd.all_share_list
    #排除创业板
    infoData = infoData[(infoData.code_int > 300999) | (infoData.code_int < 300000) ]
    allData = getData(infoData.index,startDate,endDate)
    allDataGroup = allData.groupby('code')
    data0th = allDataGroup.nth(0)
    data0th = data0th.loc[data0th.p_change > 0]
    data1th = allDataGroup.nth(1)
    data1th = data1th.loc[(data1th.p_change > 7) & (data1th.p_change < 10)]
    data = data0th[data0th.index.isin(data1th.index)]
    print(data.turnover_rate)
    print(infoData[infoData.index.isin(data.index)])
    
selectData(1,'2017-07-05','2017-06-01')
@@ -1284,16 +1284,28 @@
 ,endDate
+,baseNth = 1
 ):%0A    i
@@ -1537,84 +1537,523 @@
    d
-ata0th = allDataGroup.nth(0)%0A    data0th = data0th.loc%5Bdata0th.p_change %3E 0%5D
+ef getMaxMin(item):%0A        pChangeMax = None%0A        pChangeMin = None%0A        pChangeMean = None%0A        pChangeArray = item.p_change%0A        if len(pChangeArray) %3E= baseNth+1:%0A            pChangeSub = pChangeArray.iloc%5B0:baseNth%5D%0A            pChangeMax = pChangeSub.max()%0A            pChangeMin = pChangeSub.min()%0A            pChangeMean = pChangeSub.mean()%0A        return pd.Series(%7B%22p_change_max%22:pChangeMax,'p_change_min':pChangeMin,'p_change_mean':pChangeMean%7D)%0A    data0Group = allDataGroup.apply(getMaxMin)
 %0A   
@@ -2080,17 +2080,23 @@
 oup.nth(
-1
+baseNth
 )%0A    da
@@ -2185,18 +2185,24 @@
 ata0
-th
+Group
 %5Bdata0
-th
+Group
 .ind
@@ -2233,44 +2233,22 @@
     
-print(data.turnover_rate)%0A    print(
+infoDataSub = 
 info
@@ -2284,16 +2284,76 @@
 .index)%5D
+%0A    print(pd.concat(%5Bdata,infoDataSub%5D,axis=1,join='inner')
 )%0A    %0As
@@ -2373,17 +2373,17 @@
 2017-07-
-0
+1
 5','2017
@@ -2389,9 +2389,21 @@
 7-06-01'
+,baseNth = 2
 )

fix bug