def pullSource2Data(args): try: configuration = finnhub.Configuration(api_key={'token': finhub_key}) finnhub_client = finnhub.DefaultApi(finnhub.ApiClient(configuration)) #collect data at regular intervals for ticker in tickers_set: #print(data[ticker].getTicker()) ret = finnhub_client.quote(ticker) now = datetime.now() dt_string = now.strftime("%Y-%m-%d-%H:%M") new_entry = dt_string + " " + str(ret.c) #print(type(ret) ) #print(new_entry) data[ticker].setSource2Data(new_entry) #print(data[ticker].getSource2Data()) #return 0 #return 2 except: print("problems with pullSource2Data") return 1
def _getFinnhubClient(self): with open('config/config.json') as config_file: api_key = json.load(config_file)["api_key"] configuration = finnhub.Configuration( api_key={'token': api_key}) finnhub_client = finnhub.DefaultApi( finnhub.ApiClient(configuration)) return finnhub_client
def fetch_timeseries_finnhub(self, ticker_symbol, interval, from_date, to_date): """ from=1590988249 to=1591852249 Interval <code>1, 5, 15, 30, 60, D, W, M </code> """ if not from_date is None and not to_date is None: fq_filename = \ self._config['fq_data_output_path'] + '/' + self.data_source + '/' + ticker_symbol + '_' + \ interval + '_' + from_date[:10] + '-' + to_date[:10] else: err_msg = "Error: datetime string is empty." print(err_msg) return {'status': 'error', 'message': err_msg} from_epoch = h.datetime_string_to_epoch(from_date) to_epoch = h.datetime_string_to_epoch(to_date) if from_epoch == 0 or to_epoch == 0: err_msg = "Error: datetime string could not be converted to epoch time." return {'status': 'error', 'message': err_msg} #print('{}:{}:{}:{}'.format(ticker_symbol, interval, from_epoch, to_epoch)) try: finnhub_client = finnhub.DefaultApi( finnhub.ApiClient(self.configuration)) a = finnhub_client.stock_candles(ticker_symbol, interval, from_epoch, to_epoch) a = a.to_dict() if a['s'] == 'ok': a.pop('s', None) else: err_msg = "Error: response not in expected format." return {'status': 'error', 'message': err_msg} df = pd.DataFrame( {key: pd.Series(value) for key, value in a.items()}) df.to_csv(fq_filename, encoding='utf-8', index=False) h.print_timestamped_text('Finished [' + ticker_symbol + ':' + interval + '] successfully.') return {'status': 'succeeded'} except FileNotFoundError: err_msg = 'Error: file {} was not found!'.format(fq_filename) h.print_timestamped_text(err_msg) return {'status': 'error', 'message': err_msg} except: err_msg = 'Error: issue with interval [' + interval + '] for [' + ticker_symbol + ']!!!' h.print_timestamped_text(err_msg) return {'status': 'error', 'message': err_msg}
def getQuote(self): # Configure API key configuration = finnhub.Configuration( api_key={'token': keys.FINNHUB_KEY}) with finnhub.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = finnhub.DefaultApi(api_client) try: # Aggregate Indicators return api_instance.quote(self.stock.ticker_symbol) except ApiException as e: print( "Exception when calling DefaultApi->aggregate_indicator: %s\n" % e) return None
import finnhub import locale import json import err from datetime import datetime from dotenv import load_dotenv from discord.ext import commands load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') FIN_TOKEN = os.getenv('FINNHUB_KEY') configuration = finnhub.Configuration(api_key={'token': FIN_TOKEN}) finnhub_client = finnhub.DefaultApi(finnhub.ApiClient(configuration)) locale.setlocale(locale.LC_ALL, '') bot = commands.Bot(command_prefix='$') @bot.event async def on_ready(): print(f'{bot.user.name} has connected to Discord') @bot.command(name='roll_dice', help='Simulates rolling dice.') async def roll_dice(ctx, number_of_dice: int, number_of_sides: int): dice = [ str(random.choice(range(1, number_of_sides + 1)))