def test_merge_pre(ix, pair, timeframe, id, start, end, date_pre, date_post, clean_tmp): ''' Test function 'merge_pre' to merge the 'pre' Segment ''' oanda = OandaAPI(instrument=pair, granularity=timeframe, settingf='../../data/settings.ini') oanda.run(start=start, end=end) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument=pair, id=id, settingf='../../data/settings.ini') pl = cl.get_pivotlist( th_bounces=cl.settings.getfloat('pivots', 'th_bounces')) pivot = pl.plist[ix] # Check pivot.pre.start() before running 'merge_pre' assert date_post == pivot.pre.start() # run 'merge_pre' function pivot.merge_pre(slist=pl.slist, n_candles=cl.settings.getint('pivots', 'n_candles'), diff_th=cl.settings.getint('pivots', 'diff_th')) # Check pivot.pre.start() after running 'merge_pre' assert date_pre == pivot.pre.start()
def __initclist(self): ''' Private function to initialize the CandleList object that goes from self.start to self.period This will set the self.clist_period class attribute ''' delta_period=periodToDelta(self.period, self.timeframe) delta_1=periodToDelta(1, self.timeframe) start = self.start - delta_period # get the start datetime for this CandleList period end = self.start + delta_1 # increase self.start by one candle to include self.start oanda = OandaAPI(url=config.OANDA_API['url'], instrument=self.pair, granularity=self.timeframe, alignmentTimezone=config.OANDA_API['alignmentTimezone'], dailyAlignment=config.OANDA_API['dailyAlignment']) oanda.run(start=start.isoformat(), end=end.isoformat(), roll=True ) candle_list = oanda.fetch_candleset(vol_cutoff=0) cl = CandleList(candle_list, self.pair, granularity=self.timeframe, id=self.id, type=self.type) self.clist_period=cl
def fetch_candlelist(self): ''' This function returns a CandleList object for this Trade Returns ------- A CandleList object ''' oanda = OandaAPI(instrument=self.pair, granularity=self.timeframe, settingf=self.settingf, settings=self.settings) if isinstance(self.start, datetime) is True: astart = self.start else: astart = try_parsing_date(self.start) if isinstance(self.end, datetime) is True: anend = self.end else: anend = try_parsing_date(self.end) oanda.run(start=astart.isoformat(), end=anend.isoformat()) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, type=self.type, settingf=self.settingf, settings=self.settings) return cl
def __initclist(self): ''' Private function to initialize the CandleList object that goes from self.trade.start to self.period This will set the self.clist_period class attribute ''' delta_period = periodToDelta(self.settings.getint('counter', 'period'), self.trade.timeframe) delta_1 = periodToDelta(1, self.trade.timeframe) start = self.trade.start - delta_period # get the start datetime for this CandleList period end = self.trade.start + delta_1 # increase self.start by one candle to include self.start oanda = OandaAPI(url=self.settings.get('oanda_api', 'url'), instrument=self.trade.pair, granularity=self.trade.timeframe, settingf=self.settingf, settings=self.settings) oanda.run(start=start.isoformat(), end=end.isoformat()) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, settingf=self.settingf, settings=self.settings, instrument=self.trade.pair, granularity=self.trade.timeframe, id=self.trade.id, type=self.trade.type) self.clist_period = cl
def test_run_with_seralized_data(clean_tmp): ''' test oanda.run with serialized data ''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings_serialised.ini') oanda.run(start='2015-01-25T22:00:00', count=10)
def test_OandaAPI_e(): ''' Test a simple query to Oanda's REST API using a H12 timeframe and a non conventional time that will raise an Exception ''' with pytest.raises(Exception): oanda = OandaAPI(instrument='AUD_USD', granularity='H12') oanda.run(start='2018-11-12T10:00:00', end='2018-11-14T11:00:00')
def __initclist(self, timeframe, end, pair): ''' Private function to initialize the CandleList object that goes from trade.start to self.settings.getint('counter', 'period') Parameters ---------- timeframe : Timeframe end : Datetime object that will be the end of this CandleList pair : pair Returns ------- Candlelist or None if Oanda API query was not successful ''' delta_period = periodToDelta(self.settings.getint('trade_bot', 'period_range'), timeframe) delta_1 = periodToDelta(1, timeframe) start = end - delta_period # get the start datetime for this CandleList period end = end + delta_1 # increase end by one candle to include end tb_logger.debug("Fetching candlelist for period: {0}-{1}".format(start, end)) oanda = OandaAPI(url=self.settings.get('oanda_api', 'url'), instrument=pair, granularity=timeframe, settingf=self.settingf, settings=self.settings) if self.ser_data_obj is None: tb_logger.info("Fetching data from API") resp = oanda.run(start=start.isoformat(), end=end.isoformat()) else: tb_logger.info("Fetching data from File") oanda.data = self.ser_data_obj.slice(start=start, end=end) resp = 200 if resp == 200: candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, settingf=self.settingf, settings=self.settings, instrument=pair, granularity=timeframe, ser_data_obj=self.ser_data_obj, id="test", type="short") cl.calc_rsi() return cl else: t_logger.warn("API query was not OK. No CandleList created ") return None
def test_OandaAPI(i, g, s, e, resp): ''' Test a simple query to Oanda's REST API using a start and end datetimes ''' oanda = OandaAPI(instrument=i, granularity=g, settingf='../../data/settings.ini') respl = oanda.run(start=s, end=e) assert respl == resp
def test_indecision_c(pair, timeframe, time, is_it): oanda = OandaAPI(instrument=pair, granularity=timeframe, settingf="../../data/settings.ini") oanda.run(start=time, count=1) candle_list = oanda.fetch_candleset() candle_list[0].set_candle_features() result = candle_list[0].indecision_c() assert is_it == result
def test_OandaAPI_e(): ''' Test a simple query to Oanda's REST API using a non-valid pair ''' oanda = OandaAPI(instrument='AUD_MOCK', granularity='H12', settingf='../../data/settings.ini') respl = oanda.run(start='2018-11-12T10:00:00', end='2018-11-14T11:00:00') assert respl == 400
def test_OandaAPI_count(): ''' Test a simple query to Oanda's REST API using a D timeframe and using the 'count' parameter instead of 'end' ''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2018-05-21T22:00:00', count=1) assert 1
def __initclist(self): ''' Private function to initialize the CandleList object that goes from self.trade.start to self.settings.getint('counter', 'period') This will set the self.clist_period class attribute Returns ------- None if Oanda API query was not successful ''' delta_period = periodToDelta(self.settings.getint('counter', 'period'), self.trade.timeframe) delta_1 = periodToDelta(1, self.trade.timeframe) start = self.trade.start - delta_period # get the start datetime for this CandleList period end = self.trade.start + delta_1 # increase self.start by one candle to include self.start c_logger.debug("Fetching candlelist for period: {0}-{1}".format( start, end)) oanda = OandaAPI(url=self.settings.get('oanda_api', 'url'), instrument=self.trade.pair, granularity=self.trade.timeframe, settingf=self.settingf, settings=self.settings) resp = None if self.ser_data_obj is None: c_logger.debug("Fetching data from API") resp = oanda.run(start=start.isoformat(), end=end.isoformat()) else: c_logger.debug("Fetching data from File") oanda.data = self.ser_data_obj.slice(start=start, end=end) resp = 200 if resp == 200: candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, settingf=self.settingf, settings=self.settings, instrument=self.trade.pair, granularity=self.trade.timeframe, id=self.trade.id, ser_data_obj=self.ser_data_obj, type=self.trade.type) self.clist_period = cl else: c_logger.warn( "API query was not OK. 'self.clist_period' will be None ") self.clist_period = None
def test_OandaAPI2_e(): ''' Test a simple query to Oanda's REST API using a H8 timeframe and a non conventional time that will raise an Exception ''' with pytest.raises(Exception): oanda = OandaAPI(url='https://api-fxtrade.oanda.com/v1/candles?', instrument='AUD_USD', granularity='H8', alignmentTimezone='Europe/London', dailyAlignment=22) oanda.run(start='2018-11-12T10:00:00', end='2018-11-14T11:00:00')
def get_max_min(self, adateObj): ''' Function to get the price range for identifying S/R by checking the max and min price for CandleList starting in 'adateObj'- self.settings.getint('trade_bot', 'period_range') and ending in 'adateObj' Parameters ---------- datetime object used for identifying S/R areas Returns ------- max, min floats ''' oanda = OandaAPI(instrument=self.pair, granularity=self.timeframe, settingf=self.settingf, settings=self.settings) delta_period = periodToDelta( self.settings.getint('trade_bot', 'period_range'), self.timeframe) delta_1 = periodToDelta(1, self.timeframe) start = adateObj - delta_period # get the start datetime for this CandleList period end = adateObj + delta_1 # increase self.start by one candle to include self.start oanda.run(start=start.isoformat(), end=end.isoformat()) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument=self.pair, id='test', granularity=self.timeframe, settingf=self.settingf, settings=self.settings) max = cl.get_highest() min = cl.get_lowest() # add a number of pips to max,min to be sure that we # also detect the extreme pivots max = add_pips2price(self.pair, max, self.settings.getint('trade_bot', 'add_pips')) min = substract_pips2price( self.pair, min, self.settings.getint('trade_bot', 'add_pips')) return max, min
def test_OandaAPI_vol(): ''' Test a simple query to Oanda's REST API using a H12 timeframe and using a volume cutoff for 'fetch_candleset' function ''' oanda = OandaAPI(instrument='NZD_JPY', granularity='H12', settingf='../../data/settings.ini') oanda.run(start='2011-09-02T21:00:00', end='2011-09-06T22:00:00') candle_list = oanda.fetch_candleset(vol_cutoff=20) assert len(candle_list) == 6
def test_fetch_one_candle(): oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2015-01-25T22:00:00', count=1) candle_list = oanda.fetch_candleset() assert candle_list[0].highBid == 0.79329 assert candle_list[0].openBid == 0.7873 assert candle_list[0].lowBid == 0.7857 assert candle_list[0].representation == 'bidask' assert candle_list[0].lowAsk == 0.786 assert candle_list[0].complete == True assert candle_list[0].openAsk == 0.7889
def cl_object(): '''Returns a CandleList object''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2019-03-06T23:00:00', end='2020-01-03T23:00:00') candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') return cl
def clO(): """Returns a CandleList object""" oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2019-03-06T23:00:00', end='2020-01-24T23:00:00') candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument='AUD_USD', id='test_AUD_USD_clist', granularity='D', settingf='../../data/settings.ini') return cl
def cl_object(clean_tmp): '''Returns CandleList object''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2015-06-24T22:00:00', end='2019-06-21T22:00:00') candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument='AUD_USD', id='AUD_USD_testclist', type='long', settingf='../../data/settings.ini') return cl
def test_set_candle_features(pair, timeframe, time, colour, upper_wick, lower_wick): ''' Test function to set basic candle features based on price i.e. self.colour, upper_wick, etc... ''' oanda = OandaAPI(instrument=pair, granularity=timeframe, settingf="../../data/settings.ini") oanda.run(start=time, count=1) candle_list = oanda.fetch_candleset() candle_list[0].set_candle_features() assert candle_list[0].colour == colour assert candle_list[0].upper_wick == upper_wick assert candle_list[0].lower_wick == lower_wick
def test_calc_itrend(pair, start, end, t_type, itrend, settings_obj, clean_tmp): settings_obj.set('it_trend', 'th_bounces', '0.02') settings_obj.set('it_trend', 'n_candles', '12') oanda = OandaAPI(instrument=pair, granularity='D', settings=settings_obj) oanda.run(start=start.isoformat(), end=end.isoformat()) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument=pair, id='test_clist', granularity='D', settings=settings_obj) assert itrend == cl.calc_itrend(t_type=t_type)
def calc_pips_c_trend(self): ''' Function to calculate the pips_c_trend Returns ------- Float with number of pips for the trend_i ''' oanda = OandaAPI(url=self.settings.get('oanda_api', 'url'), instrument=self.trade.pair, granularity=self.trade.timeframe, settingf=self.settingf, settings=self.settings) resp = None if self.ser_data_obj is None: c_logger.debug("Fetching data from API") resp = oanda.run(start=self.trend_i.isoformat(), end=self.trade.start.isoformat()) else: c_logger.debug("Fetching data from File") oanda.data = self.ser_data_obj.slice(start=self.trend_i, end=self.trade.start) resp = 200 if resp == 200: candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, settingf=self.settingf, settings=self.settings, instrument=self.trade.pair, granularity=self.trade.timeframe, id=self.trade.id, type=self.trade.type) pips_c_trend = cl.get_length_pips() / cl.get_length_candles() return round(pips_c_trend, 1) else: c_logger.warn( "API query was not OK. 'pips_c_trend' could not be calculated")
def pl_object(): '''Returns PivotList object''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2019-03-08T22:00:00', end='2019-08-09T22:00:00') candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument='AUD_USD', id='AUD_USD_test', type='long', settingf='../../data/settings.ini') pl = cl.get_pivotlist( th_bounces=cl.settings.getfloat('pivots', 'th_bounces')) return pl
def set_valley(self): ''' Function to calculate the length of the valley between bounce_1st & bounce_2nd Returns ------- It will set the 'valley' attribute of the class ''' oanda = OandaAPI(url=config.OANDA_API['url'], instrument=self.pair, granularity=self.timeframe, alignmentTimezone=config.OANDA_API['alignmentTimezone'], dailyAlignment=config.OANDA_API['dailyAlignment']) oanda.run(start=self.bounce_2nd.time.isoformat(), end=self.bounce_1st.time.isoformat()) candle_list = oanda.fetch_candleset(vol_cutoff=0) self.valley = len(candle_list)
def test_adjust_pivottime(ix, pair, timeframe, id, start, end, new_b, clean_tmp): oanda = OandaAPI(instrument=pair, granularity=timeframe, settingf='../../data/settings.ini') oanda.run(start=start, end=end) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument=pair, id=id, settingf='../../data/settings.ini') pl = cl.get_pivotlist( th_bounces=cl.settings.getfloat('pivots', 'th_bounces')) p = pl.plist[ix] newt = p.adjust_pivottime(clistO=cl) assert new_b == newt
def serialize_object(): oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2015-01-25T22:00:00', count=10) oanda.serialize_data(outfile="../../data/out.data")
def test_get_lasttime(): oanda = OandaAPI(instrument='AUD_CHF', granularity='H12', settingf='../../data/settings.ini') resist = HArea(price=1.00721, pips=45, instrument='AUD_CHF', granularity='H12', settingf='../../data/settings.ini') oanda.run(start='2004-11-07T10:00:00', end='2010-04-30T09:00:00') candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, instrument='AUD_CHF', id='test_AUD_CHF_clist', granularity='H12', type='short', settingf='../../data/settings.ini') lasttime = cl.get_lasttime(resist) assert lasttime == datetime.datetime(2007, 11, 9, 10, 0)
def test_serialize_data(): ''' test of function 'serialize_data' ''' oanda = OandaAPI(instrument='AUD_USD', granularity='D', settingf='../../data/settings.ini') oanda.run(start='2015-01-25T22:00:00', count=10) oanda.serialize_data(outfile="../../data/out.data")
def fetch_candlelist(self): ''' This function returns a CandleList object for this Trade Returns ------- A CandleList object ''' oanda = OandaAPI(instrument=self.pair, granularity=self.timeframe, settingf=self.settingf, settings=self.settings) if isinstance(self.start, datetime) is True: astart = self.start else: astart = try_parsing_date(self.start) if isinstance(self.end, datetime) is True: anend = self.end else: anend = try_parsing_date(self.end) if self.ser_data_obj is None: t_logger.debug("Fetching data from API") oanda.run(start=astart.isoformat(), end=anend.isoformat()) else: t_logger.debug("Fetching data from File") oanda.data = self.ser_data_obj.slice(start=astart, end=anend) candle_list = oanda.fetch_candleset() cl = CandleList(candle_list, type=self.type, settingf=self.settingf, settings=self.settings) return cl
parser = argparse.ArgumentParser(description='Script to calculate some basis stats on the trend momentum of a Candle List ranging from a certain timepoint to the current time') parser.add_argument('--instrument', type=str, required=True, help='Instrument used for retrieving the Candle List. i.e. AUD_USD or EUR_USD' ) parser.add_argument('--granularity', type=str, required=True, help='Timeframe used. i.e. D, H12,H8,H4 ' ) parser.add_argument('--start', type=str, required=True, help='Start time. i.e. 2017-12-13T08:00:00' ) parser.add_argument('--end', type=str, required=False, help='End time. If defined, do the analysis for time period between start and end' ) parser.add_argument('--trade_type', type=str, required=True, help='Type of trade. Possible values are short, long' ) args = parser.parse_args() oanda=None if args.end is not None: oanda=OandaAPI(url='https://api-fxtrade.oanda.com/v1/candles?', instrument=args.instrument, granularity=args.granularity, alignmentTimezone='Europe/London', dailyAlignment=22, start=args.start, end=args.end ) else: oanda=OandaAPI(url='https://api-fxtrade.oanda.com/v1/candles?', instrument=args.instrument, granularity=args.granularity, alignmentTimezone='Europe/London', dailyAlignment=22, start=args.start) candle_list=oanda.fetch_candleset() trade_type=str(args.trade_type) cl=CandleList(candle_list, type=trade_type)