def estimate_bounces(price, plist): ''' Function to estimate the bounces at 'price' Parameters ---------- price: float Price used to identify bounces plist: PivotList object Returns ------- list List with bounces ''' hr = HArea(price=price, pips=hr_extension, instrument=args.instrument, granularity=args.granularity) print("upper: {0} - lower: {1}".format(hr.upper, hr.lower)) inarea_bounces = hr.inarea_bounces(plist=plist) if len(inarea_bounces) > 1: inarea_cl = CandleList(inarea_bounces, instrument=args.instrument, granularity=args.granularity) inarea_bounces = inarea_cl.improve_resolution(part='openAsk', price=price, min_dist=5) return inarea_bounces
def test_get_bounces(oanda_object): ''' Test function to set basic candle features based on price i.e. self.colour, upper_wick, etc... ''' candle_list = oanda_object.fetch_candleset() close_prices = [] datetimes = [] for c in oanda_object.fetch_candleset(): close_prices.append(c.closeAsk) datetimes.append(c.time) resist=HArea(price=1.15212,pips=100, instrument='EUR_AUD', granularity='D') (bounces,outfile)=resist.get_bounces(datetimes=datetimes, prices=close_prices, type='long')
def init_feats(self): ''' Function to initialise all object features Returns ------- It will initialise all object's features ''' warnings.warn("[INFO] Run init_feats") self.get_first_bounce(part= config.CTDBT['part']) self.get_second_bounce(part= config.CTDBT['part']) self.get_restofbounces(part= config.CTDBT['part']) self.set_rsi_1st(n=2) self.set_rsi_2nd(n=2) # if trend_i is not defined then calculate it if hasattr(self, 'trend_i'): self.trend_i = datetime.datetime.strptime(self.trend_i, '%Y-%m-%d %H:%M:%S') else: start = self.__get_time4candles(n=self.period_trend, anchor_point=self.bounce_2nd.time, roll=False) possible_clist_trend = self.clist_period.slice(start=start, end=self.bounce_2nd.time) startrend=possible_clist_trend.calc_itrend(th_up=0.02,th_down=-0.02) self.trend_i = startrend trend_i=None if isinstance(self.trend_i, datetime.date) is not True: trend_i = datetime.datetime.strptime(self.trend_i, "%Y-%m-%d %H:%M:%S") else: trend_i=self.trend_i assert self.bounce_2nd.time > trend_i, "Datetime for second bounce occurs earlier than self.trend_i. " \ "This is not valid!" outfile = "{0}/{1}.final_bounces.png".format(config.PNGFILES['bounces'], self.id.replace(' ', '_')) outfile_rsi = "{0}/{1}.final_rsi.png".format(config.PNGFILES['rsi'], self.id.replace(' ', '_')) self.plot_features(outfile_prices=outfile, outfile_rsi=outfile_rsi, part= config.CTDBT['part']) self.init_trend_feats() # instantiate an HArea object representing the self.SR in order to calculate the lasttime # price has been above/below SR resist = HArea(price=self.SR, pips=100, instrument=self.pair, granularity=self.timeframe) self.lasttime=self.clist_period.get_lasttime(resist) self.set_entry_onrsi(n=3) self.bounces_fromlasttime() self.set_diff(part= config.CTDBT['part']) self.set_diff_rsi() self.set_valley() warnings.warn("[INFO] Done init_feats")
def init_feats(self): ''' Function to initialise all object features Returns ------- It will initialise all object's features ''' warnings.warn("[INFO] Run init_feats") self.get_first_bounce(part='openAsk') self.get_second_bounce(part='openAsk') self.get_restofbounces(part='openAsk') self.set_rsi_1st() self.set_rsi_2nd() # if trend_i is not defined then calculate it if hasattr(self, 'trend_i'): self.trend_i = datetime.datetime.strptime(self.trend_i, '%Y-%m-%d %H:%M:%S') else: start = self.__get_time4candles(n=self.period_trend, anchor_point=self.bounce_2nd.time, roll=False) possible_clist_trend = self.clist_period.slice( start=start, end=self.bounce_2nd.time) startrend = possible_clist_trend.calc_itrend(th_up=0.05, th_down=-0.05) self.trend_i = startrend outfile = "{0}/{1}.final_bounces.png".format( config.PNGFILES['bounces'], self.id.replace(' ', '_')) self.plot_features(outfile=outfile, part='openAsk') self.init_trend_feats() resist = HArea(price=self.SR, pips=config.CTDBT['HR_pips'], instrument=self.pair, granularity=self.timeframe) self.lasttime = self.clist_period.get_lasttime(resist) self.set_entry_onrsi() self.bounces_fromlasttime() self.set_diff() self.set_diff_rsi() self.set_valley() self.set_rsibounces_feats() warnings.warn("[INFO] Done init_feats")
def get_lasttime(trade): ''' Function to calculate the last time price has been above/below a certain HArea Parameters ---------- trade : Trade object Used for the calculation Returns ------- Datetime ''' t_logger.debug("Running get_lasttime") # instantiate an HArea object representing the self.SR in order to calculate the lasttime # price has been above/below SR resist = HArea(price=trade.SR, pips=CONFIG.getint('harea', 'hr_pips'), instrument=trade.pair, granularity=trade.timeframe) return trade.period.get_lasttime(resist)
def run_trade(self): ''' Run the trade until conclusion from a start date ''' print("[INFO] Run run_trade with id: {0}".format(self.id)) entry = HArea(price=self.entry, pips=1, instrument=self.pair, granularity=self.timeframe) SL = HArea(price=self.SL, pips=1, instrument=self.pair, granularity=self.timeframe) TP = HArea(price=self.TP, pips=1, instrument=self.pair, granularity=self.timeframe) period = None if self.timeframe == "D": period = 24 else: period = int(self.timeframe.replace('H', '')) # generate a range of dates starting at self.start and ending numperiods later in order to assess the outcome # of trade and also the entry time self.start = datetime.datetime.strptime(str(self.start), '%Y-%m-%d %H:%M:%S') numperiods = 300 date_list = [ datetime.datetime.strptime(str(self.start.isoformat()), '%Y-%m-%dT%H:%M:%S') + datetime.timedelta(hours=x * period) for x in range(0, numperiods) ] entered = False for d in date_list: oanda = OandaAPI( url=config.OANDA_API['url'], instrument=self.pair, granularity=self.timeframe, dailyAlignment=config.OANDA_API['dailyAlignment'], alignmentTimezone=config.OANDA_API['alignmentTimezone']) oanda.run(start=d.isoformat(), count=1, roll=True) cl = oanda.fetch_candleset()[0] if entered is False: entry_time = entry.get_cross_time(candle=cl) warnings.warn("\t[INFO] Trade entered") if entry_time != 'n.a.': self.entry_time = entry_time.isoformat() else: warnings.warn( "No entry time was identified for this trade") entry_time = self.start self.entry_time = entry_time if entry_time is not None and entry_time != 'n.a.': entered = True if entered is True: failure_time = SL.get_cross_time(candle=cl) if failure_time is not None and failure_time != 'n.a.': self.outcome = 'failure' self.end = failure_time self.pips = float( calculate_pips(self.pair, abs(self.SL - self.entry))) * -1 warnings.warn("\t[INFO] S/L was hit") break if entered is True: success_time = TP.get_cross_time(candle=cl) if success_time is not None and success_time != 'n.a.': self.outcome = 'success' warnings.warn("\t[INFO] T/P was hit") self.end = success_time self.pips = float( calculate_pips(self.pair, abs(self.TP - self.entry))) break assert getattr(self, 'outcome') warnings.warn("[INFO] Done run_trade")