示例#1
0
def output(context, date, l_sell, l_keep, l_buy):
    subject = '%s盘后计划' % date
    content = ''
    content += '<table border="1">'
    content += '<thead>'
    content += '<tr><th>代码</th><th>名称</th><th>策略</th></tr>'
    content += '</thead>'
    content += '<tbody>'
    for item in l_sell:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        content += '<tr><td>%s</td><td>%s</td><td><font color="green">卖出%s</font></td></tr>' \
                   % (item['symbol'], name, item['policy'])
    for item in l_keep:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        content += '<tr><td>%s</td><td>%s</td><td>%s</td></tr>' \
                   % (item['symbol'], name, '')
    for item in l_buy:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        vacancies = c - len(l_keep)
        cash = context.account().cash['available']
        volume = floor(cash / vacancies / item['price'] /
                       100) * 100 if vacancies > 0 else 0
        content += '<tr><td>%s</td><td>%s</td><td><font color="red">买入%s %.2f * %d</font></td></tr>' \
                   % (item['symbol'], name, item['policy'], item['price'], volume)
    content += '</tbody>'
    content += '</table>'
    send_all(subject, content)
示例#2
0
def get_and_store_stock_detail_data(stock_code,date,type=1):
    """
    保存单只股票数据csv文件,自定义文件名
    tye:1:股票。2:指数。
    """
    fn = str(stock_code) + "_" + str(date)
    if type != 1:
        fn = fn.replace('.','')

    pd = load_csv_detail_data(fn)
    if pd is None:
        set_token("8e1026d2dfd455be2e1f239e50004b35a481061e")
        if type == 1:
            symbols = ['SZSE.'+str(stock_code),'SHSE.'+str(stock_code)]
            data = get_instrumentinfos(symbols=symbols, exchanges=None, sec_types=1, names=None, fields=None, df=True)
            symbol = data[data.sec_id == str(stock_code)].symbol.values[0]
        else:
            symbol = stock_code
        start_date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
        end_date = start_date + datetime.timedelta(days=1)
        pd = history(symbol, "60s", start_date, end_date, fields=None, skip_suspended=True,
                        fill_missing=None, adjust=0, adjust_end_time='', df=True)

        # pd = ts.get_tick_data(stock_code, date=date, src='tt')
        if pd is None:
            return None
        else :
            pd = cacle_column(pd,fn)
            return pd
    else:
        pd = cacle_column(pd,fn)
        return pd
示例#3
0
def output(context, date, l_sell, l_keep, l_buy):
    message = '%s盘后计划' % date
    for item in l_sell:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        message += '\n\033[1;32m%s %s 卖出%s\033[0m' \
                   % (item['symbol'], name, item['policy'])
    for item in l_keep:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        message += '\n%s %s 分值:%s' \
                   % (item['symbol'], name, item['score'])
    for item in l_buy:
        ins = get_instrumentinfos(symbols=item['symbol'])[0]
        name = ins['sec_name']
        vacancies = c - len(l_keep)
        cash = context.account().cash['available']
        volume = floor(cash / vacancies / item['price'] / 100) * 100 if vacancies > 0 else 0
        message += '\n\033[1;31m%s %s 买入%s %.2f * %d\033[0m' \
                   % (item['symbol'], name, item['policy'], item['price'], volume)
    print(message)
示例#4
0
    def init(self) -> bool:
        """"""
        if self.inited:
            return True

        try:
            set_token(JJ_TOKEN)

            # 获取全部合约
            instrumentinfos = get_instrumentinfos(symbols=None, exchanges=None, sec_types=None, names=None, fields=None, df=True)
            self.symbols = [k for k in instrumentinfos["symbol"]]

        except:
            return False

        self.inited = True
        return True
示例#5
0
def full_build():
    count = 0
    read = read_conn()
    write = write_conn()
    cursor = write.cursor()
    cursor.execute('DELETE FROM instrumentinfos')
    infos = get_instrumentinfos(
        exchanges=['SHSE', 'SZSE'],
        sec_types=[1],
        fields='symbol,sec_name,sec_abbr,price_tick,listed_date,delisted_date')
    for i, info in enumerate(infos):
        print('\rBuilding instrumentinfos: %.2f%%' % (i * 100 / len(infos)),
              end='')
        cursor.execute(
            'INSERT INTO instrumentinfos (symbol, sec_name, sec_abbr, price_tick, listed_date, delisted_date) VALUES (%s,%s,%s,%s,%s,%s)',
            (info['symbol'], info['sec_name'], info['sec_abbr'],
             info['price_tick'], info['listed_date'], info['delisted_date']))
    cursor.close()
    write.close()
    read.close()
    print('\rBuilding instrumentinfos: Finish')
    return count
示例#6
0
def plot_order_jubaopen_myself(order, start=60, end=10):
    """
    绘制订单分时图
    :param order: AbuOrder对象序列
    """
    stock_code = order.symbol
    set_token("8e1026d2dfd455be2e1f239e50004b35a481061e")
    data = get_instrumentinfos(symbols=None,
                               exchanges=None,
                               sec_types=1,
                               names=None,
                               fields=None,
                               df=True)
    symbol = data[data.sec_id == stock_code].symbol.values[0]
    start_date_order = datetime.datetime.strptime(
        str(order.buy_time)[0:18], "%Y-%m-%d %H:%M:%S").date()
    start_date = start_date_order + datetime.timedelta(days=-1)
    end_date = start_date_order + datetime.timedelta(days=9)

    kl_pd = history(symbol,
                    "60s",
                    start_date,
                    end_date,
                    fields=None,
                    skip_suspended=True,
                    fill_missing=None,
                    adjust=ADJUST_PREV,
                    adjust_end_time='',
                    df=True)
    bench_kl_pd = history('SHSE.000001',
                          "60s",
                          start_date,
                          end_date,
                          fields=None,
                          skip_suspended=True,
                          fill_missing=None,
                          adjust=ADJUST_PREV,
                          adjust_end_time='',
                          df=True)

    bench_kl_pd = bench_kl_pd[bench_kl_pd['bob'].isin(kl_pd['bob'].tolist())]
    bench_kl_pd.index = np.arange(0, len(bench_kl_pd))

    kl_pd['date'] = kl_pd['bob'].apply(
        lambda x: ABuDateUtil.date_time_str_to_int(str(x)))
    kl_pd['time'] = kl_pd['bob'].apply(
        lambda x: ABuDateUtil.date_time_str_to_time_str(str(x)))

    kl_pd_time = kl_pd[kl_pd.time == '093000']
    kl_pd_buy_time = kl_pd[kl_pd.bob == order.buy_time]
    kl_pd_sell_time = kl_pd[kl_pd.bob == order.sell_time]

    kl_pd['p_change'] = (kl_pd.close - kl_pd['close'][0]) / kl_pd['close'][0]
    bench_kl_pd['p_change'] = (
        bench_kl_pd.close - bench_kl_pd['close'][0]) / bench_kl_pd['close'][0]
    kl_pd['p_change_update'] = (kl_pd.p_change - bench_kl_pd.p_change)

    window_volume = 30
    window_close = 30
    kl_pd['p_change_5ma'] = kl_pd.p_change.rolling(window=window_close).mean()
    kl_pd['p_change_update_5ma'] = kl_pd.p_change_update.rolling(
        window=window_close).mean()
    bench_kl_pd['p_change_5ma'] = bench_kl_pd.p_change.rolling(
        window=window_close).mean()

    kl_pd['volume_ma'] = kl_pd.volume.rolling(window=window_volume).mean()

    kl_pd['p_change_5ma_up_rate'] = (kl_pd.p_change_5ma -
                                     kl_pd.p_change_5ma.shift(5))
    kl_pd['p_change_update_5ma_up_rate'] = (kl_pd.p_change_update_5ma -
                                            kl_pd.p_change_update_5ma.shift(5))
    bench_kl_pd['p_change_5ma_up_rate'] = (bench_kl_pd.p_change_5ma -
                                           bench_kl_pd.p_change_5ma.shift(5))
    kl_pd['zero_line'] = 0

    kl_pd['volume_ma_up_rate'] = (kl_pd.volume_ma - kl_pd.volume_ma.shift(5))
    kl_pd[kl_pd['p_change_5ma_up_rate'] > 0.01] = 0.01
    kl_pd[kl_pd['p_change_5ma_up_rate'] < -0.01] = -0.01
    max_p_change = kl_pd['p_change_5ma_up_rate'].max()
    min_p_change = kl_pd['p_change_5ma_up_rate'].min()
    max_volume = kl_pd['volume_ma_up_rate'].max()
    min_volume = kl_pd['volume_ma_up_rate'].min()

    vs_rate1 = max_p_change / max_volume
    vs_rate2 = min_p_change / min_volume
    vs_rate = vs_rate1 if vs_rate1 >= vs_rate2 else vs_rate2
    kl_pd['volume_ma_up_rate'] = (kl_pd.volume_ma -
                                  kl_pd.volume_ma.shift(5)) * vs_rate
    # kl_pd[kl_pd['volume_ma_up_rate'] > 0.0025] = 0.0025
    # kl_pd[kl_pd['volume_ma_up_rate'] < -0.0025] = -0.0025
    # kl_pd['volume_ma_up_rate'] = kl_pd['volume_ma_up_rate']  * 4
    # max_volume = kl_pd['volume_ma_up_rate'].max()
    # min_volume = kl_pd['volume_ma_up_rate'].min()
    #
    # vs_rate1 = max_p_change / max_volume
    # vs_rate2 = min_p_change / min_volume
    # vs_rate = vs_rate1 if vs_rate1 >= vs_rate2 else vs_rate2
    # kl_pd['volume_ma_up_rate'] = (kl_pd.volume_ma - kl_pd.volume_ma.shift(5)) * vs_rate

    title = str(stock_code) + '_' + str(order.buy_time)[0:10]

    # plt.plot(kl_pd.index, kl_pd['p_change'], label='p_change', color='blue') #基础p_change
    # plt.plot(kl_pd.index, bench_kl_pd['p_change'], label='bench_p_change', color='green') #大盘p_change
    # plt.plot(kl_pd.index, kl_pd['p_change_5ma'], label='close60', color='red') #基础p_change均线
    # plt.plot(kl_pd.index, bench_kl_pd['p_change_5ma'], label='close60', color='red') #基础大盘p_change均线
    # plt.plot(kl_pd.index, kl_pd['p_change_update'],'--', label='p_change_update', color='red') #修正后涨跌幅

    plt.plot(kl_pd.index, kl_pd['p_change'], label='p_change',
             color='blue')  #基础p_change
    plt.plot(bench_kl_pd.index,
             bench_kl_pd['p_change'],
             label='bench_p_change',
             color='green')  #大盘p_change
    # plt.plot(kl_pd.index, kl_pd['p_change_5ma'], label='close60', color='red') #基础p_change均线
    # plt.plot(kl_pd.index, bench_kl_pd['p_change_5ma'], label='close60', color='red') #基础大盘p_change均线
    # plt.plot(kl_pd.index, kl_pd['p_change_update'],'--', label='p_change_update', color='red') #修正后涨跌幅

    plt.plot(kl_pd.index, kl_pd['zero_line'], label='0_line',
             color='black')  # 0线
    plt.vlines(kl_pd_time.index, -0.005, 0.005, color="black")  #日期分割线
    plt.vlines(kl_pd_buy_time.index, -0.01, 0.01, color="red")  #买入时间线
    plt.vlines(kl_pd_sell_time.index, -0.02, 0.02, color="blue")  #卖出时间线
    plt.title(title)
    plt.legend(loc='upper left')
    # plt.show()
    png_name = generatePngName(stock_code)
    plt.savefig(png_name)
    plt.close()

    # 获得日分时数据。
    kl_pd = get_and_store_stock_detail_data(stock_code, str(start_date_order))
    kl_pd['zero_line'] = 0
    # plt.plot(kl_pd.index, kl_pd['volume_30ma_up_rate'], label='volume_30ma_up_rate', color='blue') #基础p_change
    plt.plot(kl_pd.index,
             kl_pd['volume_30ma'],
             label='volume_30ma',
             color='blue')  #基础p_change
    plt.plot(kl_pd.index,
             kl_pd['volume_5ma'],
             label='volume_5ma',
             color='green')  #基础p_change
    plt.plot(kl_pd.index, kl_pd['zero_line'], label='0_line',
             color='black')  # 0线
    plt.title(title)
    plt.legend(loc='upper left')
    # plt.show()
    png_name = generatePngName(stock_code)
    plt.savefig(png_name)
    plt.close()

    bench_kl_pd = get_and_store_SHSE000001_detail_data(str(start_date_order))

    plt.plot(kl_pd.index,
             kl_pd['p_change_30ma_up_rate'],
             label='p_change_30ma_up_rate',
             color='red')  # 基础均线增长斜率
    # plt.plot(kl_pd.index, kl_pd['p_change_update_5ma_up_rate'], '--', label='close60', color='blue')  # 修正均线增长斜率
    plt.plot(bench_kl_pd.index,
             bench_kl_pd['p_change_30ma_up_rate'],
             label='bench_p_change_30ma_up_rate',
             color='green')  # 大盘增长斜率

    plt.plot(kl_pd.index, kl_pd['zero_line'], label='0_line',
             color='black')  # 0线
    # plt.plot(kl_pd.index, kl_pd['volume_ma'], label='volume_ma', color='blue') #量均值
    # plt.plot(kl_pd.index, kl_pd['volume_30ma_up_rate'], '--', label='volume_30ma_up_rate', color='blue')  # 量增长斜率
    plt.title(title)
    plt.legend(loc='upper left')
    # plt.show()
    png_name = generatePngName(stock_code)
    plt.savefig(png_name)
    plt.close()

    plt.plot(kl_pd.index,
             kl_pd['p_change_30ma'],
             label='p_change_30ma',
             color='red')  # 基础均线增长斜率
    plt.plot(bench_kl_pd.index,
             bench_kl_pd['p_change_30ma'],
             label='bench_p_change_30ma',
             color='green')  # 大盘增长斜率

    plt.plot(kl_pd.index, kl_pd['zero_line'], label='0_line',
             color='black')  # 0线
    plt.title(title)
    plt.legend(loc='upper left')
    png_name = generatePngName(stock_code)
    plt.savefig(png_name)
    plt.close()
    pass