def prepare_trade(tb_obj, type, SL, ic, harea_sel, delta, add_pips): ''' Prepare a Trade object and check if it is taken Parameters ---------- tb_obj : TradeBot object type : str, Type of trade. 'short' or 'long' SL : float, Adjusted (by '__get_trade_type') SL price ic : Candle object Indecision candle for this trade harea_sel : HArea of this trade delta : Timedelta object corresponding to the time that needs to be increased add_pips : Number of pips above/below SL and entry price to consider for recalculating the SL and entry. Default : None Returns ------- Trade object ''' startO = ic.time + delta if type == 'short': # entry price will be the low of IC entry_p = getattr(ic, "low{0}".format(CONFIG.get('general', 'bit'))) if add_pips is not None: SL = round(add_pips2price(tb_obj.pair, SL, add_pips), 4) entry_p = round(substract_pips2price(tb_obj.pair, entry_p, add_pips), 4) elif type == 'long': # entry price will be the high of IC entry_p = getattr(ic, "high{0}".format(CONFIG.get('general', 'bit'))) if add_pips is not None: entry_p = add_pips2price(tb_obj.pair, entry_p, add_pips) SL = substract_pips2price(tb_obj.pair, SL, add_pips) startO = ic.time+delta t = Trade( id='{0}.bot'.format(tb_obj.pair), start=startO.strftime('%Y-%m-%d %H:%M:%S'), pair=tb_obj.pair, timeframe=tb_obj.timeframe, type=type, entry=entry_p, SR=harea_sel.price, SL=SL, RR=CONFIG.getfloat('trade_bot', 'RR'), strat='counter') return t
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