Example #1
0
def analyze_stock(stock, force=False):
    stock = str(stock)
    sd = StockData(stock, 'basic', 'today', addition=14 + 60) # today + 14 for observation, 60 for trend line plotting
    df = sd.data
    if df is None or len(df) == 0:
        return
    if stock == 'index':
        stock_name = 'index'
    else:
        stock_name = stock_list.loc[stock]['Catagory'] + '-' + stock + '-' + stock_list.loc[stock]['Name']

    res = 1
    idx = 1
    fig = plt.figure()
    period = df.index[0] + ' ~ ' + df.index[-1]
    fig.suptitle(stock_name + ', ' + datetime.now().date().strftime('%Y/%m/%d') + '更新', fontproperties=my_font)
    if res != 0 or force:
        plt.subplot(3, 2, idx)
        idx += 1
        res = stock_filter.is_breakthrough_trend(df['收盤'], observe_days=15, lower_bound=3, upper_bound=7, plot=True)
        if res == 0:
            res = stock_filter.is_breakthrough_trend(df['收盤'], observe_days=15, lower_bound=3, upper_bound=7, plot=True, direction='down')
            res *= -1
        plt.title('突破: 3%~7%', fontproperties=my_font)
        plt.xlabel(period)
        plt.xticks([])
    #if res != 0:
        #plt.subplot(3, 2, idx)
        #idx += 1
        #res = stock_filter.is_correction(df['收盤'], check_region=5, percent=2, height_ratio=14, plot_title=stock_name)
    if res != 0 or force:
        plt.subplot(3, 2, idx)
        idx += 1
        plot_title = '成交量>3000張'
        res *= stock_filter.is_at_least(df['成交股數'] / 1000, check_region=5, quantity=3000, plot=True)
        plt.title(plot_title, fontproperties=my_font)
        plt.xlabel(period)
        plt.xticks([])
        
    if res != 0 or force:
        plt.subplot(3, 2, idx)
        idx += 1
        plot_title = '成交量放大'
        res *= stock_filter.is_steeper(df['成交股數'] / 1000, check_region=5, plot=True)
        plt.title(plot_title, fontproperties=my_font)
        plt.xlabel(period)
        plt.xticks([])
    if res != 0 or force:
        plt.subplot(3, 2, idx)
        idx += 1
        sd = StockData(stock, 'revenue', '2013/6/1-today')
        df = sd.data
        if df is not None:
            plot_title = '營收'
            stock_filter.is_positive_step(df['營業收入'], step=12, plot=True, direction='up')
            # ignore ret
            plt.title(plot_title, fontproperties=my_font)
            plt.xlabel('2013/6/1 ~' + df.index[-1].strftime('%Y/%m/%d'))
            plt.xticks([])
        else:
            res = 0
    if res != 0 or force:
        plt.subplot(3, 2, idx)
        idx += 1
        sd = StockData(stock, 'basic', '2012/1/1-today')
        df = sd.data
        plot_title = '週線'
        stock_filter.sample_and_plot(df['收盤'], sample_distance=5, plot=True, check_sample=4)
        plt.title(plot_title, fontproperties=my_font)
        plt.xlabel('2012/1/1 ~ ' + df.index[-1])
        plt.xticks([])
    if res != 0 or force:
        plt.tight_layout()
        plt.subplots_adjust(top=0.90) # put this line after subplot, or it takes no effect
        if force:
            plt.savefig(output_path + '/interest/' + stock_name + '.png')
        elif res == 1:
            plt.savefig(output_path + '/up/' + stock_name + '.png')
        else:
            plt.savefig(output_path + '/down/' + stock_name + '.png')
        
    #plt.clf()
    plt.close(fig)
    print(stock, res)
Example #2
0
            print('wait for start time')
            time.sleep(5) 


    batch = []
    end_time = datetime.strptime("13:40:00", "%H:%M:%S").time()
    group_index = 0
    for stock in stock_data.stock_list.index:
        stock = str(stock)
        print(stock)
        sd = StockData(stock, 'basic', '2016/4/1-today')
        df = sd.data
        if len(df) == 0:
            continue
        if 0:
            res = stock_filter.is_breakthrough_trend(df['收盤'], check_region=5, lower_bound=5,\
                  upper_bound=10, plot_title='向上突破: 5%~10%', preselection=True)
            if not res:
                continue
            y_tomorrow = res[1] 
            res = res[0] 
        #if res == False and y_tomorrow / df['收盤'][-1] >= 1.1:
        if 0:
            #print(stock, 'is not possible to meet target price')
            pass
        else:
            batch.append([stock, df['收盤'][-1], 10, 3, 1])
            if len(batch) >= 30:
                p = Process(target=monitor_stocks, args=(batch, end_time, '[group ' + str(group_index) + ']',))
                p.start()
                group_index += 1
                batch = []