def getapis(): '''some RESTful APIS''' mk = ManageKeys() key = mk.getKey('av') return (EXAMPLES['api1'].format(key=key), EXAMPLES['api2'].format(key=key), EXAMPLES['api3'].format(key=key), EXAMPLES['api4'].format(key=key), EXAMPLES['api5'].format(key=key))
def getKey(): mk = ManageKeys() return mk.getKey('av')
def getApiKey(): mk = ManageKeys() key = mk.getKey('tgo') return key
class StockApi(QDialog): ''' [ibRealPort, ibRealId, ibPaperPort, ibPaperId, ibRealCb, ibPaperCb, bcCb, avCb, fhCb, tgoCb, APIPref] The api keys are held in the database ''' def __init__(self, settings, db=None): super().__init__() self.apiset = settings self.mk = ManageKeys(db=db) ui = SapiDlg() ui.setupUi(self) self.ui = ui checkForIbapi() self.setWindowIcon(QIcon('structjour/images/ZSLogo.png')) self.ui.ibRealCb.clicked.connect(self.ibClicked) self.ui.ibPaperCb.clicked.connect(self.ibPaperclicked) self.ui.ibRealPort.editingFinished.connect(self.setIbRealPort) self.ui.ibRealId.editingFinished.connect(self.setIbRealId) self.ui.ibPaperPort.editingFinished.connect(self.setIbPaperPort) self.ui.ibPaperId.editingFinished.connect(self.setIbPaperId) self.ui.tgoCb.clicked.connect(self.setTgoCb) self.ui.tgoKey.editingFinished.connect(self.setTgoKey) self.ui.bcCb.clicked.connect(self.setBcCb) self.ui.bcKey.editingFinished.connect(self.setBcKey) self.ui.avCb.clicked.connect(self.setAvCb) self.ui.avKey.editingFinished.connect(self.setAvKey) self.ui.fhCb.clicked.connect(self.setFhCb) self.ui.fhKey.editingFinished.connect(self.setFhKey) self.ui.APIPref.textChanged.connect(self.colorApis) self.ui.APIPref.editingFinished.connect(self.orderApis) self.ui.okBtn.pressed.connect(self.okPressed) self.ui.APIPref.setStyleSheet('color: black;') v = QIntValidator() self.ui.ibRealPort.setValidator(v) self.ui.ibRealId.setValidator(v) self.ui.ibPaperPort.setValidator(v) self.ui.ibPaperId.setValidator(v) self.initFromSettings() self.gotibapi = checkForIbapi() # self.sortIt(None) # self.orderApis() self.show() def initFromSettings(self): val = self.apiset.value('ibRealPort', '') self.ui.ibRealPort.setText(val) val = self.apiset.value('ibRealId') self.ui.ibRealId.setText(val) val = self.apiset.value('ibPaperPort') self.ui.ibPaperPort.setText(val) val = self.apiset.value('ibPaperId') self.ui.ibPaperId.setText(val) val = self.apiset.value('ibRealCb', False, bool) self.ui.ibRealCb.setChecked(val) val = self.apiset.value('ibPaperCb', False, bool) self.ui.ibPaperCb.setChecked(val) val = self.apiset.value('tgoCb', False, bool) self.ui.tgoCb.setChecked(val) val = self.mk.getKey('tgo') self.ui.tgoKey.setText(val) val = self.apiset.value('bcCb', False, bool) self.ui.bcCb.setChecked(val) val = self.mk.getKey('bc') self.ui.bcKey.setText(val) val = self.apiset.value('avCb', False, bool) self.ui.avCb.setChecked(val) val = self.mk.getKey('av') self.ui.avKey.setText(val) val = self.apiset.value('fhCb', False, bool) self.ui.fhCb.setChecked(val) val = self.mk.getKey('fh') self.ui.fhKey.setText(val) val = self.apiset.value('APIPref') self.ui.APIPref.setText(val) self.sortIt(None) def setIbRealPort(self): text = self.ui.ibRealPort.text() self.apiset.setValue('ibRealPort', text) def setIbRealId(self): text = self.ui.ibRealId.text() self.apiset.setValue('ibRealId', text) def setIbPaperId(self): text = self.ui.ibPaperId.text() self.apiset.setValue('ibPaperId', text) def setIbPaperPort(self): text = self.ui.ibPaperPort.text() self.apiset.setValue('ibPaperPort', text) def okPressed(self): self.orderApis() self.close() def colorApis(self, val): # val = self.ui.APIPref() self.apiset.setValue('APIPref', val) self.colorIt(val) def orderApis(self): val = self.ui.APIPref.text() self.apiset.setValue('APIPref', val) self.sortIt(None) def ibClicked(self, b): if not self.gotibapi: self.apiset.setValue('ibRealCb', False) self.apiset.setValue('ibPaperCb', False) self.ui.ibRealCb.setChecked(False) self.ui.ibPaperCb.setChecked(False) return # if self.apiset.value() self.apiset.setValue('ibRealCb', b) if b: self.apiset.setValue('ibPaperCb', not b) self.ui.ibPaperCb.setChecked(not b) self.appendOrRemoveAPI('ib', b) self.sortIt('ib') def ibPaperclicked(self, b): if not self.gotibapi: self.apiset.setValue('ibRealCb', False) self.apiset.setValue('ibPaperCb', False) self.ui.ibRealCb.setChecked(False) self.ui.ibPaperCb.setChecked(False) return # self.ui.ibPaperCb.setChecked(b) self.apiset.setValue('ibPaperCb', b) if b: self.apiset.setValue('ibRealCb', not b) self.ui.ibRealCb.setChecked(not b) self.appendOrRemoveAPI('ib', b) self.sortIt('ib') def appendOrRemoveAPI(self, api, b): ul = self.ui.APIPref.text() ulist = ul.replace(' ', '').split(',') if ul else [] if b and api not in ulist: if len(ulist) == 0: self.ui.APIPref.setText(self.ui.APIPref.text() + api) else: self.ui.APIPref.setText(self.ui.APIPref.text() + f', {api}') elif not b and api in ulist: ulist.remove(api) newul = self.setAPIPrefFromList(ulist) self.ui.APIPref.setText(newul) def setTgoCb(self, b): ''' Check or uncheck the tgo checkbox and append or remove 'tgo' from the pref list ''' self.apiset.setValue('tgoCb', b) self.appendOrRemoveAPI('tgo', b) self.sortIt('tgo') def setTgoKey(self): val = self.ui.tgoKey.text() self.mk.updateKey('tgo', val) def setBcCb(self, b): ''' Check or uncheck the bc checkbox and append or remove 'bc' from the pref list ''' self.apiset.setValue('bcCb', b) self.appendOrRemoveAPI('bc', b) self.sortIt('bc') def setBcKey(self): val = self.ui.bcKey.text() self.mk.updateKey('bc', val) def setAvCb(self, b): self.apiset.setValue('avCb', b) self.appendOrRemoveAPI('av', b) self.sortIt('av') def setAvKey(self): val = self.ui.avKey.text() self.mk.updateKey('av', val) def setFhCb(self, b): self.apiset.setValue('fhCb', b) self.appendOrRemoveAPI('fh', b) self.sortIt('fh') def setFhKey(self): val = self.ui.fhKey.text() self.mk.updateKey('fh', val) def setAPIPrefFromList(self, ulist): newul = '' for x in ulist: newul = newul + x + ', ' if newul: newul = newul[:-2] newul return newul def reorderAPIPref(self, last): ul = self.ui.APIPref.text() ulist = ul.replace(' ', '').split(',') if ul else [] if last and last in ulist: ulist.remove(last) ulist.append(last) ul = self.setAPIPrefFromList(ulist) # for x in ulist: # newul = newul + x + ', ' # if newul: # newul = newul[:-2] # ul = newul self.apiset.setValue('APIPref', ul) self.ui.APIPref.setText(ul) return ul, ulist def colorIt(self, strPref): ''' Color the API pref as red or green as user edits ''' ulist = strPref.split(',') ulist = [x.strip() for x in ulist] if not set(ulist).issubset(set(['bc', 'ib', 'av', 'fh', 'tgo'])): self.ui.APIPref.setStyleSheet('color: red;') return else: self.ui.APIPref.setStyleSheet('color: green;') wdict = { 'bc': self.ui.bcCb, 'av': self.ui.avCb, 'ib': [self.ui.ibRealCb, self.ui.ibPaperCb], 'fh': self.ui.fhCb, 'tgo': self.ui.tgoCb } for token in ulist: if token == 'ib': if not wdict[token][0].isChecked and not wdict[token][ 1].isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return elif not wdict[token].isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return self.ui.APIPref.setStyleSheet('color: green;') def sortIt(self, last): ''' Color and order the api pref after done editing ''' ul, ulist = self.reorderAPIPref(last) compareList = list() # If list contains an unchecked API, show error as red if 'ib' in ulist: if not self.ui.ibRealCb.isChecked( ) and not self.ui.ibPaperCb.isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return compareList.append('ib') if 'tgo' in ulist: if not self.ui.tgoCb.isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return compareList.append('tgo') if 'bc' in ulist: if not self.ui.bcCb.isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return compareList.append('bc') if 'av' in ulist: if not self.ui.avCb.isChecked(): self.ui.APIPref.setStyleSheet('color: red;') return compareList.append('av') if 'fh' in ulist: if not self.ui.fhCb.isChecked(): self.ui.APIPref.setStyleSheet('color: red') return compareList.append('fh') # Validate the string as a list and reorder last element if len(ulist) > len(compareList): self.ui.APIPref.setStyleSheet('color: red;') return for x in ulist: if x not in compareList: self.ui.APIPref.setStyleSheet('color: red;') return self.ui.APIPref.setStyleSheet('color: green;') self.ui.APIPref.setText(ul)
def getApiKey(): '''Returns the key for the barchart API ''' mk = ManageKeys() return mk.getKey('bc')
def apiChooserList(self, start, end, api=None): ''' Given the current list of apis as av, bc, wtd, fh and ib, determine if the given api will likely return data for the given times. :params start: A datetime object or time stamp indicating the intended start of the chart. :params end: A datetime object or time stamp indicating the intended end of the chart. :params api: Param must be one of mav, bc, fh, wtd or ib. If given, the return value in (api, x, x)[0] will reflect the bool result of the api :return: (bool, rulesviolated, suggestedStocks) The first entry is only valid if api is an argument. ''' start = pd.Timestamp(start) end = pd.Timestamp(end) # Need a naive time showing NewYorkTime right now ne = pd.Timestamp.now("US/Eastern") n = pd.Timestamp(ne.year, ne.month, ne.day, ne.hour, ne.minute, ne.second) violatedRules = [] suggestedApis = self.getPreferences() if len(suggestedApis) == 0: self.api = None return (False, ['No stock Api is selected'], []) # nopen = dt.datetime(n.year, n.month, n.day, 9, 30) nclose = dt.datetime(n.year, n.month, n.day, 16, 30) # Rule 1 Barchart will not return todays data till 16:30 # Rule 1a Barchart will not return yesterdays data after 12 till 1630 tradeday = pd.Timestamp(start.year, start.month, start.day) todayday = pd.Timestamp(n.year, n.month, n.day) yday = todayday - pd.Timedelta(days=1) y = pd.Timestamp(yday.year, yday.month, yday.day, 11, 59) if tradeday == todayday and n < nclose and 'bc' in suggestedApis: suggestedApis.remove('bc') violatedRules.append( 'Barchart free data will not return todays data till 16:30') if tradeday == yday and end > y and n < nclose and 'bc' in suggestedApis: suggestedApis.remove('bc') violatedRules.append( 'Barchart free data will not yesterdays data after 12 till today at 16:30' ) # Rule 2 No support any charts greater than 7 days prior to today for Alphavantage # Rule 2 No support any charts greated than 7 days prior to tody for World Trade Data # Rule 2 No support any charts greater than 30 days for Barchart if n > start: delt = n - start if delt.days > 31 and 'bc' in suggestedApis: suggestedApis.remove('bc') lastday = n - pd.Timedelta(days=31) violatedRules.append( 'Barchart data before {} is unavailable.'.format( lastday.strftime("%b %d"))) if delt.days > 6 and 'av' in suggestedApis: suggestedApis.remove('av') lastday = n - pd.Timedelta(days=6) violatedRules.append( 'AlphaVantage data before {} is unavailable.'.format( lastday.strftime("%b %d"))) if delt.days > 6 and 'wtd' in suggestedApis: suggestedApis.remove('wtd') lastday = n - pd.Timedelta(days=6) violatedRules.append( 'WorldTradeData data before {} is unavailable in 1 minute candles.' .format(lastday.strftime("%b %d"))) # Rule 3 Don't call ib if the library is not installed # Rule 4 Don't call ib if its not connected if self.apiset.value('gotibapi', type=bool): if 'ib' in suggestedApis and not ib.isConnected(): suggestedApis.remove('ib') violatedRules.append('IBAPI is not connected.') elif 'ib' in suggestedApis: suggestedApis.remove('ib') violatedRules.append('IBAPI is not installed') # Rule 5 No data is available for the future if start > n: suggestedApis = [] violatedRules.append('No data is available for the future.') # Rule No 6 Don't call barchart if there is no apikey in settings # Rule No 6 Don't call WorldTradeDate if there is no apikey in settings # Rule No 6 Don't call alphavantage if there is no apikey in settings # Rule No 6 Don't call finnhub if there is no api key in settings mk = ManageKeys() bc_key = mk.getKey('bc') av_key = mk.getKey('av') wtd_key = mk.getKey('wtd') fh_key = mk.getKey('fh') if not bc_key and 'bc' in suggestedApis: suggestedApis.remove('bc') violatedRules.append( 'There is no apikey in the database for barchart') if not av_key and 'av' in suggestedApis: suggestedApis.remove('av') violatedRules.append( 'There is no apikey in the database for alphavantage') if not wtd_key and 'wtd' in suggestedApis: suggestedApis.remove('wtd') violatedRules.append( 'There is no apikey in the database for WorldTradeData') if not fh_key and 'fh' in suggestedApis: suggestedApis.remove('fh') violatedRules.append( 'There is no apikey in the database for finnhub') # Rule No 7 API limit has been reached [bc, av, fh, wtd] deleteme = [] for token in suggestedApis: if token == 'ib': continue if getLimitReached(token, self.apiset): deleteme.append(token) violatedRules.append( f'You have reached your quota for {token}') for token in deleteme: suggestedApis.remove(token) api = api in suggestedApis if api else False self.api = suggestedApis[0] if suggestedApis else None self.violatedRules = violatedRules return (api, violatedRules, suggestedApis)