with open(model_save_path, 'rb') as f:
    model = pickle.load(f)
logger.info("model loaded from: %s"%model_save_path)
logger.info(model)

raw_data_file = CONFIG.get('Paths','raw_data_path') + '/raw_data.csv'
test_data_file = CONFIG.get('Paths','test_data_path') + '/test_data.npy'
logger.info("raw data file is %s\n, test_data_file is: %s" % (raw_data_file, test_data_file))

# load test data
logger.info("load the test data from %s."%test_data_file)
#filecp = codecs.open(test_data_file, encoding='UTF-8')
#data_test_loaded = np.loadtxt(filecp,delimiter=',')
data_test_loaded = np.load(test_data_file)
logger.info(str(data_test_loaded))
X = data_test_loaded[:,0:int(n)]
y = data_test_loaded[:,-1].reshape((X.shape[0],1))
# data cleaning config
dropFeat = CONFIG.getboolean('data_cleaning','drop_feature')
#dropFeat = CONFIG['data_cleaning']['drop_feature']
#clearning
new_feature_num = CONFIG['data_cleaning']['reduced_col_num']
if dropFeat:
    X_dropped = X[:,0:int(new_feature_num)]
#prediction
y_hat = model.predict(X_dropped)
logger.info("Predicted value is: %s"%str(y_hat))
logger.info("True value is: %s"%str(y))
logger.info("Production is finished. Last step.")
Ejemplo n.º 2
0
def calc_SR(clO, outfile):
    '''
    Function to calculate S/R lines

    Parameters
    ----------
    clO: CandleList object
         Used for calculation
    outfile : str
              Output filename for .png file

    Return
    ------
    HAreaList object
    '''
    PL = clO.get_pivotlist(th_bounces=CONFIG.getfloat('pivots', 'th_bounces'))

    ## now calculate the price range for calculating the S/R
    # add a number of pips to max,min to be sure that we
    # also detect the extreme pivots
    ul = add_pips2price(clO.data['instrument'], clO.get_highest(),
                        CONFIG.getint('trade_bot', 'add_pips'))
    ll = substract_pips2price(clO.data['instrument'], clO.get_lowest(),
                              CONFIG.getint('trade_bot', 'add_pips'))

    cl_logger.debug("Running calc_SR for estimated range: {0}-{1}".format(
        ll, ul))

    prices, bounces, score_per_bounce, tot_score = ([] for i in range(4))

    # the increment of price in number of pips is double the hr_extension
    prev_p = None
    ##
    ll = 0.6444
    ##
    p = float(ll)

    while p <= float(ul):
        cl_logger.debug("Processing S/R at {0}".format(round(p, 4)))
        # get a PivotList for this particular S/R
        newPL = PL.inarea_pivots(SR=p)
        if len(newPL.plist) == 0:
            mean_pivot = 0
        else:
            mean_pivot = newPL.get_avg_score()

        prices.append(round(p, 5))
        bounces.append(len(newPL.plist))
        tot_score.append(newPL.get_score())
        score_per_bounce.append(mean_pivot)
        # increment price to following price.
        # Because the increment is made in pips
        # it does not suffer of the JPY pairs
        # issue
        p = add_pips2price(clO.data['instrument'], p,
                           2 * CONFIG.getint('candlelist', 'i_pips'))
        if prev_p is None:
            prev_p = p
        else:
            increment_price = round(p - prev_p, 5)
            prev_p = p

    data = {
        'price': prices,
        'bounces': bounces,
        'scores': score_per_bounce,
        'tot_score': tot_score
    }

    df = pd.DataFrame(data=data)

    ### establishing bounces threshold as the args.th quantile
    # selecting only rows with at least one pivot and tot_score>0,
    # so threshold selection considers only these rows
    # and selection is not biased when range of prices is wide
    dfgt1 = df.loc[(df['bounces'] > 0)]
    dfgt2 = df.loc[(df['tot_score'] > 0)]
    bounce_th = dfgt1.bounces.quantile(CONFIG.getfloat('trade_bot', 'th'))
    score_th = dfgt2.tot_score.quantile(CONFIG.getfloat('trade_bot', 'th'))

    print("Selected number of pivot threshold: {0}".format(bounce_th))
    print("Selected tot score threshold: {0}".format(round(score_th, 1)))

    # selecting records over threshold
    dfsel = df.loc[(df['bounces'] > bounce_th) | (df['tot_score'] > score_th)]

    # repeat until no overlap between prices
    ret = calc_diff(dfsel, increment_price)
    dfsel = ret[0]
    tog_seen = ret[1]
    while tog_seen is True:
        ret = calc_diff(dfsel, increment_price)
        dfsel = ret[0]
        tog_seen = ret[1]

    # iterate over DF with selected SR to create a HAreaList
    halist = []
    for index, row in dfsel.iterrows():
        resist = HArea(price=row['price'],
                       pips=CONFIG.getint('pivots', 'hr_pips'),
                       instrument=clO.data['instrument'],
                       granularity=clO.data['granularity'],
                       no_pivots=row['bounces'],
                       tot_score=round(row['tot_score'], 5))
        halist.append(resist)

    halistObj = HAreaList(halist=halist)

    # Plot the HAreaList
    dt_str = clO.data['candles'][-1]['time'].strftime("%d_%m_%Y_%H_%M")

    if CONFIG.getboolean('images', 'plot') is True:
        halistObj.plot(clO=clO, outfile=outfile)

    cl_logger.info("Run done")

    return halistObj