Exemple #1
0
def drawBuyA(row, col, symbols, start, end):
    fig = plt.figure(figsize=(16, 16))
    i = 0
    for symbol in symbols:
        print(symbol)
        i += 1
        data = TushareStore.get_a_daily_data_ind(table='a_daily',
                                                 symbol=symbol,
                                                 start_date=start,
                                                 end_date=end,
                                                 append_ind=True)
        close, pdi, wr, wr_89, bias = data['close'], data['pdi'], data[
            'willr'], data['willr_89'], data['bias']
        ax = fig.add_subplot(row, col, i)
        ax.plot(close, c='grey')
        # buy = close[ind.LESS_THAN(bias.shift(1), -13) & ind.BOTTOM(bias)]
        buy = close[ind.LESS_THAN(wr.shift(1), -88) & ind.BOTTOM(wr)]
        # print buy
        ax.scatter(buy.index, buy, s=20, c='green')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_ylabel(symbol)
    plt.legend()
    plt.subplots_adjust(hspace=1)
    plt.show()
Exemple #2
0
def drawBuy(row, col, sector, symbols, start, end):
    fig = plt.figure(figsize=(16, 8))
    i = 0
    for symbol in symbols:
        i += 1
        data = TushareStore.get_usa_daily_data_ind(sector=sector,
                                                   symbol=symbol,
                                                   start_date=start,
                                                   end_date=end,
                                                   append_ind=True)
        # data = store.get_chart_data_from_db("600%d.SH" % code, '20180101')
        # data = get_chart_data_from_web(code, '1/1/2018', '1/30/2019')
        close, pdi, wr, wr_89, bias = data['close'], data['pdi'], data[
            'willr'], data['willr_89'], data['bias']
        ax = fig.add_subplot(row, col, i)
        ax.plot(close, c='grey')
        # buy = close[(wr <= -98)]
        # ax.scatter(buy.index, buy, c='red')
        # buy = close[(wr <= -93) & (wr > -98)]
        # ax.scatter(buy.index, buy, c='orange')
        # buy = close[(wr <= -88) & (wr > -93)]

        # ax.scatter(buy.index, buy, c='yellow')
        # buy = close[(wr <= -83) & (wr > -88)]
        # ax.scatter(buy.index, buy, c='green')

        # buy = close[(pdi <= 10) & (wr < -88)]
        # ax.scatter(buy.index, buy, c='red')
        # buy = close[(pdi <= 12) & (pdi > 10) & (wr < -88)]
        # ax.scatter(buy.index, buy, c='orange')
        # buy = close[(pdi <= 16) & (pdi > 12) & (wr < -88)]
        # ax.scatter(buy.index, buy, c='yellow')
        # buy = close[(pdi <= 20) & (pdi > 16) & (wr < -88)]
        # ax.scatter(buy.index, buy, c='green')

        # buy = close[ind.UP_CROSS(wr_89, -83.5) & ind.LESS_THAN(wr, -50)]
        # buy = close[ind.LESS_THAN(wr_89, -97)  & ind.BOTTOM(wr_89)]
        # buy = close[ind.LESS_THAN(bias, -12) & ind.BOTTOM(bias) & ind.LESS_THAN(wr_89, -83.5)]
        buy = close[ind.LESS_THAN(bias.shift(1), -13) & ind.BOTTOM(bias)]
        ax.scatter(buy.index, buy, s=20, c='green')
        # ax = plt.twinx()
        # ax.plot(bias)
        # ax.plot(wr)
        # drawHline(ax, [-12])
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_ylabel(symbol)

    plt.legend()
    plt.subplots_adjust(hspace=0.1)
    plt.show()
Exemple #3
0
def drawMINMAX(ax, data, periods=[20]):
    for period in periods:
        if data.__contains__('min_%d' % period) & data.__contains__(
                'max_%d' % period):
            min = data['min_%d' % period]
            max = data['max_%d' % period]
        # elif data.__contains__('min') & data.__contains__('max'):
        #     min = data['min']
        #     max = data['max']
        else:
            close = data['close']
            min = ind.MIN(close, period)
            max = ind.MAX(close, period)
        ax.plot(min, label='min%d' % period, linewidth=1)
        ax.plot(max, label='max%d' % period, linewidth=1)
        ax.set_ylabel('MIN_MAX')
Exemple #4
0
def drawDMI(ax, data, periods=[6, 14], hlines=[10, 12, 16, 20, 22]):
    if data.__contains__('pdi') & data.__contains__('mdi') & data.__contains__(
            'adx') & data.__contains__('adxr'):
        pdi = data['pdi']
        mdi = data['mdi']
        adx = data['adx']
        adxr = data['adxr']
    else:
        close, high, low = data['close'], data['high'], data['low']
        pdi, mdi = ind.DI(high, low, close, time_period=periods[0])
        adx = ind.ADX(high, low, close, time_period=periods[1])
        adxr = ind.ADXR(high, low, close, time_period=periods[1])
    ax.plot(pdi, label='pdi%d' % periods[0])
    # ax.plot(mdi, label='mdi%d' % periods[0])
    # ax.plot(adx, label='adx%d' % periods[1])
    # ax.plot(adxr, label='adxr%d' % periods[1])
    drawHline(ax, hlines)
    ax.set_ylabel('DMI')
Exemple #5
0
def drawOBV(ax, data, hlines=[]):
    if data.__contains__('obv'):
        obv = data['obv']
    else:
        close, volume = data['close'], data['volume']
        obv = ind.OBV(close, volume)
    ax.plot(obv, label='obv')
    drawHline(ax, hlines)
    ax.set_ylabel('OBV')
Exemple #6
0
def drawCCI(ax, data, periods=[14], hlines=[-231, -138, -110, -83, 50]):
    for period in periods:
        if data.__contains__('cci'):
            cci = data['cci']
        else:
            close, high, low = data['close'], data['high'], data['low']
            cci = ind.CCI(high, low, close, time_period=period)
        ax.plot(cci, label='cci%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('CCI')
Exemple #7
0
def drawSMA(ax, data, periods=[5, 10, 20, 30, 60, 120]):
    for period in periods:
        if data.__contains__('sma_%d' % period):
            sma = data['sma_%d' % period]
        elif data.__contains__('sma'):
            sma = data['sma']
        else:
            close = data['close']
            sma = ind.SMA(close, period)
        ax.plot(sma, label='sma_%d' % period, linewidth=1)
Exemple #8
0
def drawEMA(ax, data, periods=[5, 10, 20, 30, 60, 120]):
    for period in periods:
        if data.__contains__('ema%d' % period):
            ema = data['ema%d' % period]
        elif data.__contains__('ema'):
            ema = data['ema']
        else:
            close = data['close']
            ema = ind.SMA(close, period)
        ax.plot(ema, label='ema%d' % period)
Exemple #9
0
def save_industry_sat():
    industry_sats = pd.DataFrame()
    basic_stock = query_basic_stock()
    print(basic_stock.head(5))
    i = 0
    for industry in basic_stock['industry'].unique():
        i = i + 1
        symbols = basic_stock[basic_stock['industry'] == industry]['ts_code']
        industry_sat = pd.DataFrame()
        for symbol in symbols:
            print(symbol)
            # try:
            data = query_a_daily_data_ind(symbol=symbol,
                                          trade_date='',
                                          start_date='2019-01-01',
                                          end_date='2019-04-30')
            sat = ind.sat(data)
            sat['symbol'] = symbol
            industry_sat = industry_sat.append(sat)
            industry_sat = industry_sat.sort_values(by=['pct_sum_90_max'],
                                                    ascending=False)
            # except:
            #     print 'error', symbol
            # print industry_top
        industry_sats = industry_sats.append(industry_sat)
    basic_stock['symbol'] = basic_stock['ts_code']
    industry_sats = pd.merge(basic_stock,
                             industry_sats,
                             on=['symbol'],
                             how='inner')
    industry_sats.to_csv('industry_sats.csv')
    engine = create_engine(
        'mysql://*****:*****@127.0.0.1:3306/Stock?charset=utf8')
    # industry_sats.to_sql('industry_sats', engine, if_exists='append', index=False)

    industry_sats_1 = industry_sats[0:1000]
    industry_sats_1.to_sql('industry_sats',
                           engine,
                           if_exists='append',
                           index=False)
    industry_sats_2 = industry_sats[1000:2000]
    industry_sats_2.to_sql('industry_sats',
                           engine,
                           if_exists='append',
                           index=False)
    industry_sats_3 = industry_sats[2000:3000]
    industry_sats_3.to_sql('industry_sats',
                           engine,
                           if_exists='append',
                           index=False)
    industry_sats_4 = industry_sats[3000:]
    industry_sats_4.to_sql('industry_sats',
                           engine,
                           if_exists='append',
                           index=False)
Exemple #10
0
def drawWR(ax, data, periods=[6, 89], hlines=[-98, -83.5, -25, -11]):
    for period in periods:
        if data.__contains__('willr_%d' % period):
            willr = data['willr_%d' % period]
        elif data.__contains__('willr'):
            willr = data['willr']
        else:
            close, high, low = data['close'], data['high'], data['low']
            willr = ind.WILLR(high, low, close, time_period=period)
        ax.plot(willr, label='wr%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('WR')
Exemple #11
0
def drawRSI(ax, data, periods=[6, 12, 24], hlines=[20, 50, 80]):
    for period in periods:
        if data.__contains__('rsi%d' % period):
            rsi = data['rsi%d' % period]
        elif data.__contains__('rsi'):
            rsi = data['rsi']
        else:
            price = data['close']
            rsi = ind.RSI(price, time_period=period)
        ax.plot(rsi, label='rsi%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('RSI')
Exemple #12
0
def drawROC(ax, data, periods=[14], hlines=[]):
    for period in periods:
        if data.__contains__('roc%d' % period):
            roc = data['roc%d' % period]
        elif data.__contains__('roc'):
            roc = data['roc']
        else:
            price = data['close']
            roc = ind.ROC(price, time_period=period)
        ax.plot(roc, label='roc%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('ROC')
Exemple #13
0
def drawTRIX(ax, data, periods=[14], hlines=[]):
    for period in periods:
        if data.__contains__('trix%d' % period):
            trix = data['rsi%d' % period]
        elif data.__contains__('trix'):
            trix = data['trix']
        else:
            price = data['close']
            trix = ind.TRIX(price, time_period=period)
        ax.plot(trix, label='trix%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('TRIX')
Exemple #14
0
def drawEMV(ax, data, periods=[14], hlines=[]):
    for period in periods:
        if data.__contains__('emv%d' % period):
            emv = data['emv%d' % period]
        elif data.__contains__('emv'):
            emv = data['emv']
        else:
            low, high, volume = data['low'], data['high'], data['volume']
            emv = ind.EMV(low, high, volume, time_period=period)
        ax.plot(emv, label='emv%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('EMV')
Exemple #15
0
def drawMFI(ax, data, periods=[14], hlines=[]):
    for period in periods:
        if data.__contains__('mfi%d' % period):
            mfi = data['mfi%d' % period]
        elif data.__contains__('mfi'):
            mfi = data['mfi']
        else:
            high, low, close, volume = data['low'], data['high'], data[
                'close'], data['volume']
            mfi = ind.MFI(high, low, close, volume, time_period=period)
        ax.plot(mfi, label='mfi%d' % period)
    drawHline(ax, hlines)
    ax.set_ylabel('MFI')
Exemple #16
0
def drawBBANDS(ax, data, period=20):
    if data.__contains__('upper_band') & data.__contains__(
            'middle_band') & data.__contains__('lower_band'):
        upper_band = data['upper_band']
        middle_band = data['middle_band']
        lower_band = data['lower_band']
    else:
        close = data['close']
        upper_band, middle_band, lower_band = ind.BBANDS(close,
                                                         time_period=period)
    ax.plot(upper_band, label='upper_band')
    ax.plot(middle_band, label='middle_band')
    ax.plot(lower_band, label='lower_band')
Exemple #17
0
def save_a_daily_data_ind(start_date, end_date):
    engine = create_engine(
        'mysql://*****:*****@127.0.0.1:3306/Stock?charset=utf8')
    symbols = query_symbols()
    for symbol in symbols:
        data = query_a_daily_data(symbol,
                                  trade_date='',
                                  start_date=start_date,
                                  end_date=end_date)
        open, close, high, low, volume = data['open'], data['close'], data[
            'high'], data['low'], data['volume']
        ochl2ind = ind.ochl2buy(open, close, high, low, volume)
        data = data.join(ochl2ind, how='left')
        data.to_sql('a_daily_ind', engine, if_exists='append', index=False)
        print(symbol, 'loaded')
Exemple #18
0
def drawKDJ(ax, data, periods=[9, 3, 3], hlines=[-14, -3, 6.5, 17, 95]):
    if data.__contains__('slow_k') & data.__contains__('slow_d'):
        slow_k = data['slow_k']
        slow_d = data['slow_d']
    else:
        close, high, low = data['close'], data['high'], data['low']
        slow_k, slow_d = ind.STOCH(high,
                                   low,
                                   close,
                                   fastk_period=periods[0],
                                   slowk_period=periods[1],
                                   slowd_period=periods[2])
    ax.plot(slow_d, label='d')
    ax.plot(slow_k, label='k')
    ax.plot(3 * slow_k - 2 * slow_d, label='j')
    drawHline(ax, hlines)
    # ax.fill_between(time, 80, rsi, where=rsi >= 80, facecolor='green')
    # ax.fill_between(time, 20, rsi, where=rsi <= 20, facecolor='red')
    ax.set_ylabel('KDJ')
Exemple #19
0
def get_chart_data_from_web(code, start_date='', end_date='', append_ind=True):
    data = web.DataReader(code,
                          data_source='yahoo',
                          start=start_date,
                          end=end_date)
    data.rename(columns={
        'Open': 'open',
        'Close': 'close',
        'High': 'high',
        'Low': 'low',
        'Volume': 'volume'
    },
                inplace=True)
    if append_ind:
        open, close, high, low, volume = data['open'], data['close'], data[
            'high'], data['low'], data['volume']
        ochl2ind = ind.ochl2ind(open, close, high, low, volume)
        data = data.join(ochl2ind, how='left')
    data['date'] = data.index
    data['date'].apply(lambda date: date.date())
    data.index = np.arange(0, 0 + len(data))
    return data
Exemple #20
0
def show_buy(ax, data, type):
    close, low, high = data['close'], data['low'], data['high']
    ma3, ma5, ma20, ma60 = data['sma_3'], data['sma_5'], data['sma_20'], data[
        'sma_60']
    diff, dea = data['dif'], data['dea']
    close, willr, willr_34, willr_89, = data['close'], data['willr'], data[
        'willr_34'], data['willr_89']
    bias, pdi = data['bias'], data['pdi']
    buy = ind.LESS_THAN(willr, -88)
    # & ind.LESS_THAN(willr_34, -88)
    # ind.GREAT_THAN(bias, 3)
    # & ind.GREAT_THAN(willr_89, -28)
    # & ind.LESS_THAN(willr, -70)

    # ind.LESS_THAN(bias.shift(1), -3) & ind.BOTTOM(bias) &
    # & ind.GREAT_THAN(willr, -40)
    # & ind.LESS_THAN(bias.shift(), 3)
    # & ind.BOTTOM(willr)
    # & ind.LESS_THAN(willr_34.shift(1), -88)
    # & ind.BOTTOM(willr_34)
    # & ind.LESS_THAN(willr_89.shift(1), -88)
    # & ind.BOTTOM(willr_89)

    buy = ind.TOUCH(close, ma20) & ind.TOUCH(diff, dea, 0.3)

    # diff,dea 死叉 close>ma20 会上涨 close<ma20 会下跌
    # 上升趋势 diff,dea,close,ma20 同时死叉 最佳买点 会涨
    # 下跌趋势 diff,dea,close,ma20 同时金叉 反弹最高点,会跌

    # 上升趋势 diff,dea,close,ma20 同时金叉 最佳买点 会涨
    buy = ind.DEAD_CROSS(diff, dea)

    # diff,dea 金叉,close>ma20 会震荡回调 close<ma20 会补涨
    # 上升趋势 diff,dea,close,ma20 同时金叉 最佳买点 会涨
    # 下跌趋势 diff,dea,close,ma20 同时金叉 反弹最高点,会跌
    # buy = ind.GOLDEN_CROSS(diff, dea) | ind.DEAD_CROSS(diff, dea)

    k_golden_cross = ind.GOLDEN_CROSS(close, ma20) | ind.GOLDEN_CROSS(
        close, ma60)
    k_dead_cross = ind.DEAD_CROSS(close, ma20) | ind.DEAD_CROSS(close, ma60)
    macd_golden_cross = ind.GOLDEN_CROSS(diff, dea) | ind.UP_CROSS(diff, 0)
    macd_dead_cross = ind.DEAD_CROSS(diff, dea) | ind.DOWN_CROSS(diff, 0)
    if type == 'k':
        # ax.scatter(close[k_golden_cross].index, close[k_golden_cross], s=20, c='darkgoldenrod', zorder=200)
        # ax.scatter(close[k_dead_cross].index, close[k_dead_cross], s=20, c='black', zorder=200)
        ax.scatter(close[macd_golden_cross].index,
                   close[macd_golden_cross],
                   s=20,
                   c='darkgoldenrod',
                   zorder=200)
        # ax.scatter(close[macd_dead_cross].index, close[macd_dead_cross], s=20, c='black', zorder=200)
    else:
        # ax.scatter(diff[k_golden_cross].index, diff[k_golden_cross], s=20, c='darkgoldenrod', zorder=200)
        # ax.scatter(diff[k_dead_cross].index, diff[k_dead_cross], s=20, c='black', zorder=200)
        ax.scatter(diff[macd_golden_cross].index,
                   diff[macd_golden_cross],
                   s=20,
                   c='darkgoldenrod',
                   zorder=200)
Exemple #21
0
def show_trend(ax, data):
    close, low, high = data['close'], data['low'], data['high']
    ma3, ma5, ma20, ma60 = data['sma_3'], data['sma_5'], data['sma_20'], data[
        'sma_60']
    diff, dea, hist = data['dif'], data['dea'], data['hist']

    ax = plt.twinx()
    ax.set_yticks([])
    condition_up = (ind.UP(ma3) | ind.UP(ma20)) & (ind.UP(diff) | ind.UP(dea))
    condition_up = ind.UP(dea)
    condition_down = (ind.DOWN(ma3) | ind.DOWN(ma20)) & (ind.DOWN(diff)
                                                         | ind.DOWN(dea))
    condition_down = ind.DOWN(dea)
    # 强上涨
    ax.bar(data[condition_up & ind.SEQ(0, dea, diff)].index,
           1,
           facecolor='darkred',
           width=1,
           alpha=0.7)
    # ax.bar(data[ind.UP(close) & condition_up & ind.SEQ(0, dea, diff)].index, 1, facecolor='darkred', width=1, alpha=0.2)
    # 弱上涨
    ax.bar(data[condition_up & ind.SEQ(dea, diff, 0)].index,
           1,
           facecolor='darkred',
           width=1,
           alpha=0.2)
    # # 努力上升
    ax.bar(data[condition_up & ind.SEQ(dea, 0, diff)].index,
           1,
           facecolor='darkred',
           width=1,
           alpha=0.5)

    # 强下跌
    ax.bar(data[condition_down & ind.SEQ(diff, dea, 0)].index,
           1,
           facecolor='darkgreen',
           width=1,
           alpha=0.8)
    # 弱下跌
    ax.bar(data[condition_down & ind.SEQ(0, diff, dea)].index,
           1,
           facecolor='darkgreen',
           width=1,
           alpha=0.2)
    # # 努力下跌
    ax.bar(data[condition_down & ind.SEQ(diff, 0, dea)].index,
           1,
           facecolor='darkgreen',
           width=1,
           alpha=0.5)
Exemple #22
0

# AMZN
# GOOG
# .HK .SZ .HZ
# 300059.sz
# pdi 10~20  13~16
# mdi
data = web.DataReader('300059.sz',
                      data_source='yahoo',
                      start='1/1/2018',
                      end='1/30/2019')
data = pd.DataFrame(data)
high, low, close, volume = data['High'], data['Low'], data['Close'], data[
    'Volume']
pdi, mdi = ind.DI(high, low, close, time_period=14)
adx = ind.ADX(high, low, close, time_period=6)
upper_band, middle_band, lower_band = ind.BBANDS(close, time_period=20)
m, s, macd_histogram = ind.MACD(close,
                                fast_period=12,
                                slow_period=26,
                                signal_period=9)
rsi = ind.RSI(close, time_period=14)
min = ind.MIN(close, 20)
max = ind.MAX(close, 50)

df = pd.DataFrame()
df['close'] = close
df['fast'] = m
df['slow'] = s
df['ref'] = df['fast'].shift(1) < df['slow'].shift(1)