Exemplo n.º 1
0
def save_df(df_list, ifcode, today, period_short, period_long):
    df = pd.DataFrame(df_list, columns=['time_index', 'price', 'volume'])
    price = list(df['price'])
    df['ema_short'] = Indicator.ema_metric(period_short, price)
    df['ema_long'] = Indicator.ema_metric(period_long, price)

    #macd
    macd_df = point_prosess_v3(df, 8)
    macd_df['ma_short'] = Indicator.ewma_metric(12, macd_df[['price']], 'price', False)
    macd_df['ma_long'] = Indicator.ewma_metric(26, macd_df[['price']], 'price', False)
    macd_df['macd_dif'] = macd_df['ma_short'] - macd_df['ma_long']
    macd_dif = list(macd_df['macd_dif'])
    macd_df['macd_dem'] = Indicator.ewma_metric(9, macd_df[['macd_dif']], 'macd_dif')

    _macd_file = '%s/%s_%s_macd' % (validate_df_dir, ifcode, today)
    macd_df.to_csv(_macd_file, index=False)

    sig_infos = PushSellSignal.compare_sig(macd_df, 'macd_dif', 'macd_dem', 16)
    profit_infos = PushSellSignal.profit_infos(sig_infos)
    print '*******macd******'
    print profit_infos
    print len(profit_infos)

    _file = '%s/%s_%s' % (validate_df_dir, ifcode, today)
    df.to_csv(_file, index=False)

    sig_infos = PushSellSignal.compare_sig(df, 'ema_short', 'ema_long')
    profit_infos = PushSellSignal.profit_infos(sig_infos)
    print '*******ema*******'
    print profit_infos
    print len(profit_infos)
Exemplo n.º 2
0
def push_signal(df_list, code, ifcode, today, period_short, period_long):
    df = pd.DataFrame(df_list, columns=['time_index', 'price', 'volume'])
    price = list(df['price'])
    df['ema_short'] = Indicator.ema_metric(period_short, price)
    df['ema_long'] = Indicator.ema_metric(period_long, price)
    sig_infos = PushSellSignal.compare_ema(df)
    profit_infos = PushSellSignal.profit_infos(sig_infos)
    if is_push(profit_infos, ifcode, today):
        if len(profit_infos) == 1:
            print '*' * 20
            print 'init message~!'
            time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            info_dict = profit_infos[0]
            theevent = info_dict.get('event', '')
            is_success = 'failed'
            #添加交易接口
            if theevent == '卖出信号':
                trans_session = yinhe_trans()
                r = trans_short_start(trans_session, code, ifcode)
                if 'error_info' not in r:
                    is_success = '交易成功'
            elif theevent == '买入信号':
                trans_session = yinhe_trans()
                r = trans_long_start(trans_session, code, ifcode)
                if 'error_info' not in r:
                    is_success = '交易成功'
            tip = 'ema 策略: \n%s, 交易价格: %s; 时间: %s; %s' % (
                theevent, info_dict.get('price', 0), time_str, is_success)
            push_sig(tip)
        elif len(profit_infos) >= 2:
            print '*' * 20
            print 'push message~!'
            time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            info_dict_1 = profit_infos[-2]
            info_dict_2 = profit_infos[-1]
            _p = info_dict_2.get('price', 0)
            trading_fee = cal_fee(_p)
            _g = info_dict_1.get('gain', 0)
            gain = _g - trading_fee

            theevent = info_dict_2.get('event', '')
            is_success = 'failed'
            #添加交易接口
            if theevent == '卖出信号':
                trans_session = yinhe_trans()
                r1 = trans_short_close(trans_session, code, ifcode)
                r2 = trans_short_start(trans_session, code, ifcode)
                if 'error_info' not in r1 and 'error_info' not in r2:
                    is_success = '交易成功'
            elif theevent == '买入信号':
                trans_session = yinhe_trans()
                r1 = trans_long_close(trans_session, code, ifcode)
                r2 = trans_long_start(trans_session, code, ifcode)
                if 'error_info' not in r1 and 'error_info' not in r2:
                    is_success = '交易成功'
            tip = 'ema 策略: \n%s, 盈利: %s, 实际盈利: %s, 交易费用: %s; \n%s, 交易价格: %s; 时间: %s; %s' % (
                info_dict_1.get('event', ''), _g, gain, trading_fee,
                info_dict_2.get('event', ''), _p, time_str, is_success)
            push_sig(tip)
Exemplo n.º 3
0
 def ema_df(cls, date, ifcode, period_short, period_long):
     data = BacktestData.get_data_by_ifcode(date, ifcode)
     if not data:
         return pd.DataFrame()
     df = pd.DataFrame(data, columns=['time_index', 'price', 'volume'])
     price = list(df['price'])
     df['ema_short'] = Indicator.ema_metric(period_short, price)
     df['ema_long'] = Indicator.ema_metric(period_long, price)
     return df
Exemplo n.º 4
0
 def ema_chart(cls, date, ifcode, period_short, period_long):
     _file = '%s/%s_%s_%s_%s' % (ema_file_dir, date, ifcode, period_short,
                                 period_long)
     if os.path.isfile(_file):
         df = read_df(_file)
     else:
         data = BacktestData.get_data_by_ifcode(date, ifcode)
         df = pd.DataFrame(data, columns=['time_index', 'price', 'volume'])
         price = list(df['price'])
         df['ema_short'] = Indicator.ema_metric(period_short, price)
         df['ema_long'] = Indicator.ema_metric(period_long, price)
     price = df[['time_index', 'price']].values.tolist()
     ema_short = df[['time_index', 'ema_short']].values.tolist()
     ema_long = df[['time_index', 'ema_long']].values.tolist()
     volume = df[['time_index', 'volume']].values.tolist()
     return [price, ema_short, ema_long, volume]