def test_get_candles(self): """ Test obtaining daily candles correctly.""" candles = rates.get_daily_candles('EUR_USD', '2015-11-07', '2015-11-11') self.assertEqual(len(candles), 3) self.assertEqual(candles[0]['lowBid'], 1.07186) self.assertEqual(candles[1]['highAsk'], 1.07649) self.assertEqual(candles[2]['volume'], 26025) return
def get_yesterdays_candle(instrument): """ Get the (supposedly complete) candle of yesterday just as today begins. Args: instrument: string. The currency pair. e.g. 'EUR_USD'. Returns: candle: dict. Standard candle with time, bid/ask OHLC and volume. """ # Get start/end dates. start_date = str(datetime.date.today() - datetime.timedelta(3)) end_date = str(datetime.date.today()) # Intend to run this script at day's open (e.g. 17:01) so the last candle # is yesterday's candle. candle = rates.get_daily_candles(instrument, start_date, end_date)[-1] return candle
def test_write_candles(self): """ Test writing candles to file.""" # Get the candles. candles = rates.get_daily_candles('USD_JPY', '2013-11-08', '2013-11-20') # Write the candles to file. rates.write_candles_to_csv(candles, self.tmp_file) # Read the file and check the results. results = [] with open(self.tmp_file, 'r') as csv_handle: reader = csv.reader(csv_handle, delimiter=' ') for row in reader: results.append(row) self.assertEqual(len(results), 10) self.assertEqual(len(results[0]), 10) self.assertTrue('time' in results[0]) self.assertTrue('99.115' in results[5]) self.assertTrue('2013-11-19' in results[9]) return