Пример #1
0
def computeCandle(records):
    count = len(records)
    for i in range(0,count):
        r = records[i]
        result = comm.get_candle_2(r.open,r.close,r.high,r.low)
        up_or_down = 2 if result[1]>0 else 1
        sql = 'update stock_daily set candle_sort=%s,up_or_down=%s where pk_id=%s' % (result[4],up_or_down,records[i].pk_id) 
        dbw.query(sql) 
Пример #2
0
def computeCandle(records):
    count = len(records)
    for i in range(0, count):
        r = records[i]
        result = comm.get_candle_2(r.open, r.close, r.high, r.low)
        up_or_down = 2 if result[1] > 0 else 1
        sql = 'update stock_daily set candle_sort=%s,up_or_down=%s where pk_id=%s' % (
            result[4], up_or_down, records[i].pk_id)
        dbw.query(sql)
Пример #3
0
def computeLastClosePrice(records):
    count = len(records)
    for i in range(0, count):
        if (count - i) == 1:
            break
        last_close = records[i + 1].close
        open_last_close = records[i].open - records[i + 1].close
        high_low = records[i].high - records[i].low
        close_open = records[i].close - records[i].open
        price_rate = (records[i].close - last_close) / last_close
        high_rate = (records[i].high - last_close) / last_close
        low_rate = (records[i].low - last_close) / last_close
        high_low_rate = high_rate - low_rate
        candle = comm.get_candle_2(records[i].open, records[i].close,
                                   records[i].high, records[i].low)
        range_1 = candle[0]
        range_2 = candle[1]
        range_3 = candle[2]

        jump_rate = open_last_close / last_close
        jump_level = 0
        if jump_rate * 100 >= 2:
            jump_level = 3
        elif jump_rate * 100 >= 0 and jump_rate * 100 < 2:
            jump_level = 2
        elif jump_rate * 100 < 0:
            jump_level = 1
        else:
            jump_level = 50

        pk_id = records[i].pk_id
        dbw.update('stock_daily',
                   high_low=high_low,
                   last_close=last_close,
                   open_last_close=open_last_close,
                   close_open=close_open,
                   price_rate=price_rate,
                   high_rate=high_rate,
                   low_rate=low_rate,
                   hig_low_rate=high_low_rate,
                   range_1=range_1,
                   range_2=range_2,
                   range_3=range_3,
                   jump_level=jump_level,
                   jump_rate=jump_rate,
                   where="pk_id=$pk_id",
                   vars=locals())
Пример #4
0
def computeLastClosePrice(records):
    count = len(records)
    for i in range(0,count):
        if (count-i) == 1:
            break
        last_close = records[i+1].close
        open_last_close = records[i].open - records[i+1].close
        high_low = records[i].high - records[i].low
        close_open = records[i].close - records[i].open
        price_rate = (records[i].close - last_close)/last_close
        high_rate = (records[i].high - last_close)/last_close
        low_rate = (records[i].low - last_close)/last_close
        high_low_rate = high_rate - low_rate
        candle = comm.get_candle_2(records[i].open,records[i].close,records[i].high,records[i].low)
        range_1 = candle[0]
        range_2 = candle[1]
        range_3 = candle[2]

        jump_rate = open_last_close / last_close 
        jump_level = 0
        if jump_rate*100>=2:
            jump_level = 3
        elif jump_rate*100>=0 and jump_rate*100<2:
            jump_level = 2
        elif jump_rate*100<0:
            jump_level = 1
        else:
            jump_level = 50  

        pk_id = records[i].pk_id
        dbw.update('stock_daily',high_low=high_low,last_close=last_close,open_last_close=open_last_close,
            close_open = close_open,
            price_rate=price_rate,high_rate=high_rate,low_rate=low_rate,hig_low_rate=high_low_rate,
            range_1 = range_1,range_2 = range_2,range_3 = range_3,
            jump_level = jump_level, jump_rate=jump_rate,
            where="pk_id=$pk_id",vars=locals())
Пример #5
0
def process1(stock_no):  
    records = datafile.load_raw_records(stock_no)
    count = len(records)    
    # print 'trade_date,close,peak5,peak10'   

    for i in range(0,count):
        stock_no_infos = stock_no.split('.')
        records[i].stock_no = stock_no  #stock_no_infos[0]
        records[i].stock_pycode = stock_no_infos[1]

        #5日内波峰波谷
        records[i].peak_trough_5 = comm.get_peak_trough(records,count,i,3)
        ##records[i].peak_trough_10 = comm.get_peak_trough(records,count,i,5) #和5得出的结论基本吻合

        #100天内,最高值high,最低值low分布, 日期&具体的值
        rows = sorted(records[i:i+100], cmp=lambda x,y : cmp(x.close, y.close))  
        records[i].days100_low_close = rows[0].close
        records[i].days100_low_date = rows[0].trade_date 
        records[i].days100_high_close = rows[-1].close
        records[i].days100_high_date = rows[-1].trade_date   

        #成交量
        r10 = records[i:i+10]        
        l = [r.volume for r in r10]
        volume_avg_10 = reduce(lambda x, y: x + y, l) / len(l)
        volume_p = float(records[i].volume) / volume_avg_10  if volume_avg_10 else 0
        records[i].volume_avg_10 = volume_avg_10
        records[i].volume_level = comm.get_volume_level(volume_p)

        #移动平均线
        MAs = comm.get_ma(records,i)  
        # print MAs       
        ma_5 = records[i].ma_5 = MAs['ma_5']
        ma_10 = records[i].ma_10 = MAs['ma_10']
        records[i].ma_20 = MAs['ma_20']
        records[i].ma_50 = MAs['ma_50']
        records[i].ma_100 = MAs['ma_100']
        records[i].ma_200 = MAs['ma_200']

        records[i].ma_p_2 = MAs['ma_p_2']
        records[i].ma_p_3 = MAs['ma_p_3']
        records[i].ma_p_4 = MAs['ma_p_4']
        records[i].ma_p_5 = MAs['ma_p_5']

        records[i].close_ma_5 = records[i].close > MAs['ma_5']
        records[i].close_ma_10 = records[i].close > MAs['ma_10']        
        records[i].close_ma_20 = records[i].close > MAs['ma_20']
        records[i].close_ma_50 = records[i].close > MAs['ma_50']
        records[i].close_ma_100 = records[i].close > MAs['ma_100']
        records[i].close_ma_200 = records[i].close > MAs['ma_200']

        # for i in range(2,6):
        #     key = 'ma_p_%s' % (i)
        #     records[i][key] = MAs[key] #几条移动平均线的上下关系
        
        ma_5_10 = 0
        if ma_5<>0 and ma_10<>0:
            ma_5_10 = 2 if ma_5>ma_10 else 1 
        records[i].ma_5_10 = ma_5_10

        records[i].volume_cos_10 = comm.cos_dist( [r.volume for r in records[i:i+5]])

        #蜡烛图形态
        candles = comm.get_candle_2(records[i].open,records[i].close,records[i].high,records[i].low)
        records[i].range_1 = candles[0]
        records[i].range_2 = candles[1]
        records[i].range_3 = candles[2]
        records[i].candle_sort  = candles[4]
        records[i].up_or_down = 2 if candles[1]>0 else 1

        records[i].high_low = records[i].high - records[i].low
        records[i].close_open = records[i].close - records[i].open 

        records[i].jump_level = 0
        if (count-i) > 1: 
            records[i].last_close =  records[i+1].close  
            records[i].last_acp =  records[i+1].acp  
            records[i].open_lastclose = records[i].open - records[i].last_close  
            records[i].jump_rate = records[i].open_lastclose / records[i].last_close  
            records[i].jump_level = comm.get_jump_level(records[i].jump_rate)
            records[i].price_rate = (records[i].close - records[i].last_close) / records[i].last_close  
            records[i].high_rate = (records[i].high - records[i].last_close) / records[i].last_close  
            records[i].low_rate = (records[i].low - records[i].last_close) / records[i].last_close  
            records[i].hig_low_rate = records[i].high_rate - records[i].low_rate 

        
        # current_record = records[i]
        # print '%s,%s,%s,%s' %(current_record.trade_date,current_record.close,records[i].peak_trough_5,records[i].peak_trough_10)
  
        
                   
        records[i].trend_3 = comm.get_trend(records[i:i+3]) if count-i>2 else 0                    
        records[i].trend_5 = comm.get_trend(records[i:i+5]) if count-i>4 else 0

        

        records[i].future1_prate = 0
        records[i].future1_range = 0
        if i>1:
            prate = (records[i-2].close - records[i-1].close)  / records[i-1].close
            frange = comm.getFutureRange(prate) 
            records[i].future1_prate = prate
            records[i].future1_range = frange
        records[i].future2_prate = 0
        records[i].future2_range = 0  
        if i>2:
            prate = (records[i-3].close - records[i-1].close) / records[i-1].close
            frange = comm.getFutureRange(prate)
            records[i].future2_prate = prate
            records[i].future2_range = frange
        records[i].future3_prate = 0
        records[i].future3_range = 0  
        if i>3:
            prate = (records[i-4].close - records[i-1].close) / records[i-1].close
            frange = comm.getFutureRange(prate)
            records[i].future3_prate = prate
            records[i].future3_range = frange    
        #print records[i] 

        # comm.get_test(records,i)
        # datafile.gen_date_file(records[i])

    datafile.save_stocks(stock_no,records)
    return records
Пример #6
0
def process1(stock_no):
    records = datafile.load_raw_records(stock_no)
    count = len(records)
    # print 'trade_date,close,peak5,peak10'

    for i in range(0, count):
        stock_no_infos = stock_no.split('.')
        records[i].stock_no = stock_no  #stock_no_infos[0]
        records[i].stock_pycode = stock_no_infos[1]

        #5日内波峰波谷
        records[i].peak_trough_5 = comm.get_peak_trough(records, count, i, 3)
        ##records[i].peak_trough_10 = comm.get_peak_trough(records,count,i,5) #和5得出的结论基本吻合

        #100天内,最高值high,最低值low分布, 日期&具体的值
        rows = sorted(records[i:i + 100],
                      cmp=lambda x, y: cmp(x.close, y.close))
        records[i].days100_low_close = rows[0].close
        records[i].days100_low_date = rows[0].trade_date
        records[i].days100_high_close = rows[-1].close
        records[i].days100_high_date = rows[-1].trade_date

        #成交量
        r10 = records[i:i + 10]
        l = [r.volume for r in r10]
        volume_avg_10 = reduce(lambda x, y: x + y, l) / len(l)
        volume_p = float(
            records[i].volume) / volume_avg_10 if volume_avg_10 else 0
        records[i].volume_avg_10 = volume_avg_10
        records[i].volume_level = comm.get_volume_level(volume_p)

        #移动平均线
        MAs = comm.get_ma(records, i)
        # print MAs
        ma_5 = records[i].ma_5 = MAs['ma_5']
        ma_10 = records[i].ma_10 = MAs['ma_10']
        records[i].ma_20 = MAs['ma_20']
        records[i].ma_50 = MAs['ma_50']
        records[i].ma_100 = MAs['ma_100']
        records[i].ma_200 = MAs['ma_200']

        records[i].ma_p_2 = MAs['ma_p_2']
        records[i].ma_p_3 = MAs['ma_p_3']
        records[i].ma_p_4 = MAs['ma_p_4']
        records[i].ma_p_5 = MAs['ma_p_5']

        records[i].close_ma_5 = records[i].close > MAs['ma_5']
        records[i].close_ma_10 = records[i].close > MAs['ma_10']
        records[i].close_ma_20 = records[i].close > MAs['ma_20']
        records[i].close_ma_50 = records[i].close > MAs['ma_50']
        records[i].close_ma_100 = records[i].close > MAs['ma_100']
        records[i].close_ma_200 = records[i].close > MAs['ma_200']

        # for i in range(2,6):
        #     key = 'ma_p_%s' % (i)
        #     records[i][key] = MAs[key] #几条移动平均线的上下关系

        ma_5_10 = 0
        if ma_5 <> 0 and ma_10 <> 0:
            ma_5_10 = 2 if ma_5 > ma_10 else 1
        records[i].ma_5_10 = ma_5_10

        records[i].volume_cos_10 = comm.cos_dist(
            [r.volume for r in records[i:i + 5]])

        #蜡烛图形态
        candles = comm.get_candle_2(records[i].open, records[i].close,
                                    records[i].high, records[i].low)
        records[i].range_1 = candles[0]
        records[i].range_2 = candles[1]
        records[i].range_3 = candles[2]
        records[i].candle_sort = candles[4]
        records[i].up_or_down = 2 if candles[1] > 0 else 1

        records[i].high_low = records[i].high - records[i].low
        records[i].close_open = records[i].close - records[i].open

        records[i].jump_level = 0
        if (count - i) > 1:
            records[i].last_close = records[i + 1].close
            records[i].last_acp = records[i + 1].acp
            records[i].open_lastclose = records[i].open - records[i].last_close
            records[i].jump_rate = records[i].open_lastclose / records[
                i].last_close
            records[i].jump_level = comm.get_jump_level(records[i].jump_rate)
            records[i].price_rate = (records[i].close - records[i].last_close
                                     ) / records[i].last_close
            records[i].high_rate = (records[i].high - records[i].last_close
                                    ) / records[i].last_close
            records[i].low_rate = (
                records[i].low - records[i].last_close) / records[i].last_close
            records[
                i].hig_low_rate = records[i].high_rate - records[i].low_rate

        # current_record = records[i]
        # print '%s,%s,%s,%s' %(current_record.trade_date,current_record.close,records[i].peak_trough_5,records[i].peak_trough_10)

        records[i].trend_3 = comm.get_trend(records[i:i +
                                                    3]) if count - i > 2 else 0
        records[i].trend_5 = comm.get_trend(records[i:i +
                                                    5]) if count - i > 4 else 0

        records[i].future1_prate = 0
        records[i].future1_range = 0
        if i > 1:
            prate = (records[i - 2].close -
                     records[i - 1].close) / records[i - 1].close
            frange = comm.getFutureRange(prate)
            records[i].future1_prate = prate
            records[i].future1_range = frange
        records[i].future2_prate = 0
        records[i].future2_range = 0
        if i > 2:
            prate = (records[i - 3].close -
                     records[i - 1].close) / records[i - 1].close
            frange = comm.getFutureRange(prate)
            records[i].future2_prate = prate
            records[i].future2_range = frange
        records[i].future3_prate = 0
        records[i].future3_range = 0
        if i > 3:
            prate = (records[i - 4].close -
                     records[i - 1].close) / records[i - 1].close
            frange = comm.getFutureRange(prate)
            records[i].future3_prate = prate
            records[i].future3_range = frange
        #print records[i]

        # comm.get_test(records,i)
        # datafile.gen_date_file(records[i])

    datafile.save_stocks(stock_no, records)
    return records