Ejemplo n.º 1
0
 def get_summary_data(self, tickers, threads=True):
     tickers = utils.format_tickers(tickers)
     # Clear shared._DSSS
     shared._DSSS = {}
     if threads:
         if threads is True:
             threadsQty = min([len(tickers), _multitasking.cpu_count() * 2])
             _multitasking.set_max_threads(threadsQty)
             for i, ticker in enumerate(tickers):
                 self.download_summary_threaded(ticker)
             while len(shared._DSSS) < len(tickers):
                 _time.sleep(0.01)
         else:
             if len(tickers) == 1:
                 ticker = tickers[0]
                 resultSummary = self.download_summary(ticker)
                 shared._DSSS[ticker.upper()] = resultSummary
             elif len(tickers) <= 5:
                 for i, ticker in enumerate(tickers):
                     resultSummary = self.download_summary(ticker)
                     shared._DSSS[ticker.upper()] = resultSummary
             else:
                 self.get_summary(tickers, threads=True)
     data = shared._DSSS
     return data
Ejemplo n.º 2
0
 def get_prices_data(self,
                     tickers,
                     start=0,
                     end=9999999999,
                     interval="1d",
                     threads=True):
     tickers = utils.format_tickers(tickers)
     shared._DSHP = {}
     if threads:
         if threads is True:
             threadsQty = min([len(tickers), _multitasking.cpu_count() * 2])
             _multitasking.set_max_threads(threadsQty)
             for i, ticker in enumerate(tickers):
                 self.download_prices_threaded(ticker, start, end, interval)
             while len(shared._DSHP) < len(tickers):
                 _time.sleep(0.01)
         else:
             if len(tickers) == 1:
                 ticker = tickers[0]
                 resultPrices = self.download_prices(
                     ticker, start, end, interval)
                 shared._DSHP[ticker.upper()] = resultPrices
             elif len(tickers) <= 5:
                 for i, ticker in enumerate(tickers):
                     resultPrices = self.download_prices(
                         ticker, start, end, interval)
                     shared._DSHP[ticker.upper()] = resultPrices
             else:
                 self.get_prices(tickers,
                                 start,
                                 end,
                                 interval,
                                 threads=True)
     return shared._DSHP
Ejemplo n.º 3
0
    def get_summary(self, tickers, threads=True, format="default"):
        tickers = utils.format_tickers(tickers)

        if tickers:
            if len(tickers) >= 1:
                data = self.get_summary_data(tickers, threads)
        # Process result data and build json object to append in list.
        resultConfig = utils.getConfig("Results", "SUMMARY")
        resultFormats = utils.getConfig("Results", "DEFAULT_valueFormat")
        resultExclusionConfig = utils.getConfig("Results", "DEFAULT_exclude").replace(" ", "")
        resultExclusionObj = resultExclusionConfig.split(",")
        resultObj = json.loads(resultConfig)
        formatsObj = json.loads(resultFormats)
        summaryResult = []
        if bool(data):
            for tkr in tickers:
                resultObjCopy = copy.copy(resultObj)
                tkrValues = data[tkr]["quoteSummary"]["result"] if tkr in data else False
                if tkrValues:
                    try:
                        for (key, value) in resultObj.items():
                            if key not in resultExclusionObj:
                                valueObj = tkrValues[0]["summaryDetail"][value] if value in tkrValues[0]["summaryDetail"] else []
                                if bool(valueObj):
                                    if hasattr(valueObj, "keys"):
                                        # Evaluate keys and determine what key to use
                                        # Majority of cases:
                                        #     raw - values as is,
                                        #     fmt - string value,
                                        #     longFmt - formatted string value
                                        if format in formatsObj:
                                            if format in valueObj:
                                                resultObjCopy[key] = valueObj[formatsObj[format]]
                                            else:
                                                if formatsObj[format] in valueObj:
                                                    resultObjCopy[key] = valueObj[formatsObj[format]]
                                                else:
                                                    resultObjCopy[key] = valueObj["raw"]
                                        else:
                                            resultObjCopy[key] = valueObj["raw"]
                                    else:
                                        resultObjCopy[key] = valueObj
                                else:
                                    resultObjCopy[key] = 0
                        resultObjCopy["errors"] = "Ok"
                        if "symbol" in resultObjCopy:
                            resultObjCopy["symbol"] = tkr
                    except:
                        resultObjCopy["errors"] = "Exception getting prices: {}"
                    summaryResult.append(resultObjCopy)
        return (summaryResult)
Ejemplo n.º 4
0
 def get_events(self,
                tickers,
                start=0,
                end=9999999999,
                interval="1mo",
                event="div",
                threads=True):
     tickers = utils.format_tickers(tickers)
     start = utils.format_date(start)
     end = utils.format_date(end)
     intervals = utils.getConfig("Configurations",
                                 "Intervals").replace(" ", "").split(",")
     if interval not in intervals:
         interval = intervals[len(intervals) - 2]
     eventsDefault = json.loads(utils.getConfig("Configurations", "EVENTS"))
     if event in eventsDefault:
         event = eventsDefault[event]
     else:
         event = eventsDefault["dividends"]
     if tickers:
         if len(tickers) >= 1:
             data = self.get_events_data(tickers, start, end, interval,
                                         event)
     # Process result data and build json object to append in list.
     resultConfig = utils.getConfig(
         "Results", "DIVIDEND" if event == "div" else "SPLIT")
     resultExclusionConfig = utils.getConfig(
         "Results",
         "DIVIDEND_exclude" if event == "div" else "SPLIT_exclude").replace(
             " ", "")
     resultEvent = utils.getConfig(
         "Results", "DIVIDEND_event" if event == "div" else "SPLIT_event")
     resultExclusionObj = resultExclusionConfig.split(",")
     resultObj = json.loads(resultConfig)
     resultEventObj = json.loads(resultEvent)
     eventResult = []
     if bool(data):
         for tkr in tickers:
             try:
                 resultObjCopy = copy.copy(resultObj)
                 tkrValues = data[tkr]["chart"]["result"]
                 if tkrValues:
                     for (key, value) in resultObj.items():
                         if key not in resultExclusionObj:
                             if isinstance(value, dict):
                                 if key in tkrValues[0]:
                                     valueObj = tkrValues[0][key]
                                 else:
                                     valueObj = {}
                             else:
                                 if value in tkrValues[0]:
                                     valueObj = tkrValues[0][value]
                                 else:
                                     valueObj = {}
                             if bool(valueObj):
                                 if hasattr(valueObj, "keys"):
                                     # print(valueObj[valueObj.keys()[0]])
                                     # info = valueObj[valueObj.keys()[0]] if len(valueObj.keys()) == 1 else valueObj[valueObj.keys()[0]]
                                     info = valueObj[next(iter(valueObj))]
                                     resultGrp = []
                                     for dt, k in info.items():
                                         resultEventObjCopy = copy.copy(
                                             resultEventObj)
                                         if hasattr(k, "keys"):
                                             for x, y in resultEventObjCopy.items(
                                             ):
                                                 resultEventObjCopy[
                                                     x] = utils.format_date(
                                                         k[x]
                                                     ) if y == "date" else k[
                                                         x]
                                             resultGrp.append(
                                                 resultEventObjCopy)
                                     resultObjCopy[key] = resultGrp
                                 else:
                                     resultObjCopy[key] = ""
                             else:
                                 resultObjCopy[key] = []
                     resultObjCopy["errors"] = "Ok"
                     if "symbol" in resultObjCopy:
                         if resultObjCopy["symbol"] == "symbol":
                             resultObjCopy["symbol"] = tkr
                     eventResult.append(resultObjCopy)
             except:
                 pass
     return eventResult
Ejemplo n.º 5
0
 def get_prices(self,
                tickers,
                start=0,
                end=9999999999,
                interval="1d",
                threads=True):
     tickers = utils.format_tickers(tickers)
     startdtx = utils.format_date(start, True)
     enddtx = utils.format_date(end, True)
     dates = self.validate_date_range(start=startdtx, end=enddtx)
     startdt = dates['start']
     enddt = dates['end']
     intervals = utils.getConfig("Configurations",
                                 "Intervals").replace(" ", "").split(",")
     if interval not in intervals:
         interval = intervals[len(intervals) - 5]
     if tickers:
         if len(tickers) >= 1:
             data = self.get_prices_data(tickers, startdt, enddt, interval)
     # Process result data and build json object to append in list.
     resultConfig = utils.getConfig("Results", "PRICES")
     resultObj = json.loads(resultConfig)
     pricesResult = []
     if bool(data):
         for tkr in tickers:
             try:
                 resultObjCopy = copy.copy(resultObj)
                 tkrValues = data[tkr]["chart"]["result"]
                 rangeResult = []
                 if tkrValues:
                     if not ('timestamp' in tkrValues[0]):
                         resultObjCopy["range"] = rangeResult
                         resultObjCopy[
                             "errors"] = "NOT VALUES IN RANGE PROVIDED."
                         if "symbol" in resultObjCopy:
                             if resultObjCopy["symbol"] == "symbol":
                                 resultObjCopy["symbol"] = tkr
                         pricesResult.append(resultObjCopy)
                     else:
                         dateValues = tkrValues[0]['timestamp']
                         pricesValues = tkrValues[0]['indicators'][
                             'adjclose']
                         if pricesValues:
                             closeValues = pricesValues[0]['adjclose']
                         for idx, dt in enumerate(dateValues):
                             closeValue = closeValues[
                                 idx] if closeValues else 0
                             if utils.format_date(dt) in ([
                                     duplicate['date']
                                     for duplicate in rangeResult
                             ]):
                                 duplicate = {
                                     "date": utils.format_date(dt),
                                     "price":
                                     utils.validate_value(closeValue)
                                 }
                             else:
                                 rangeResult.append({
                                     "date":
                                     utils.format_date(dt),
                                     "price":
                                     utils.validate_value(closeValue)
                                 })
                         resultObjCopy["range"] = rangeResult
                         resultObjCopy["errors"] = "Ok"
                         if "symbol" in resultObjCopy:
                             if resultObjCopy["symbol"] == "symbol":
                                 resultObjCopy["symbol"] = tkr
                         pricesResult.append(resultObjCopy)
             except ValueError as ex:
                 print(ex)
     return pricesResult