Ejemplo n.º 1
0
	def getFromServer(self, request, status = False):
		'''Return True if new stock data is received'''
		
		if appGlobal.getFailConnected():
			return False
		
		# Encode as string
		icarraTickers = {}
		lastTicker = False
		for (ticker, date) in request.items():
			icarraTicker = self.getIcarraTicker(ticker)
			icarraTickers[icarraTicker] = ticker
			
			# Change request to icarraTicker if necessary
			if icarraTicker != ticker:
				request[icarraTicker] = date.strftime("%Y-%m-%d %H:%M:%S")
				del request[ticker]
			else:
				request[ticker] = date.strftime("%Y-%m-%d %H:%M:%S")
		request["__UNIQUEID__"] = str(appGlobal.getApp().getUniqueId())

		try:
			if status:
				status.setStatus("Receiving Stock Data", 70)
			data = self.s.getStockZip(request)
		except Exception, inst:
			print traceback.format_exc()
			appGlobal.setFailConnected(True)
			return False
Ejemplo n.º 2
0
    def getFromServer(self, request, status=False):
        '''Return True if new stock data is received'''

        if appGlobal.getFailConnected():
            return False

        # Encode as string
        icarraTickers = {}
        for (ticker, date) in request.items():
            icarraTicker = self.getIcarraTicker(ticker)
            icarraTickers[icarraTicker] = ticker

            # Change request to icarraTicker if necessary
            if icarraTicker != ticker:
                request[icarraTicker] = date.strftime("%Y-%m-%d %H:%M:%S")
                del request[ticker]
            else:
                request[ticker] = date.strftime("%Y-%m-%d %H:%M:%S")
        request["__UNIQUEID__"] = str(appGlobal.getApp().getUniqueId())

        try:
            if status:
                status.setStatus("Receiving Stock Data", 70)
            data = self.s.getStockZip(request)
        except Exception, inst:
            appGlobal.setFailConnected(True)
            return False
Ejemplo n.º 3
0
	def getFromServer(self, request, status = False):
		'''Return True if new stock data is received'''
		global stocks_db
		global stocks_lastDate
		global stocks_gotData

		stocks_db = self.db
		stocks_lastDate = {}
		stocks_gotData = False
		
		if appGlobal.getFailConnected():
			return False
		#appGlobal.setFailConnected(True)
		stocks_gotData = False
		inStatus = False

		if status:
			status.setStatus("Updating Stock Database", 80)

		for (ticker, date) in request.items():
			inStatus = True
			appGlobal.setBackgroundStatus("Downloading " + ticker)
			tocRecord = self.yahoo.recordFactory.createTocRecord(
					ticker,
					ticker,
					minDate = self.getFirstDate(ticker),
					maxDate = self.getLastDate(ticker))
			self.db.beginTransaction()
			self.yahoo.updateHistorical(tocRecord)
			self.db.commitTransaction()

		# Update last dates
		for ticker in stocks_lastDate:
			if ticker == "__UNIQUEID__":
				continue
			self.updateLastDate(ticker, stocks_lastDate[ticker])
		for (ticker, date) in request.items():
			if ticker == "__UNIQUEID__":
				continue
			self.setLastDownload(ticker)
		
		if inStatus:
			appGlobal.clearBackgroundStatus()

		return stocks_gotData
Ejemplo n.º 4
0
    def getFromServer(self, request, status=False):
        '''Return True if new stock data is received'''
        global stocks_db
        global stocks_lastDate
        global stocks_gotData

        stocks_db = self.db
        stocks_lastDate = {}
        stocks_gotData = False

        if appGlobal.getFailConnected():
            return False
        #appGlobal.setFailConnected(True)
        stocks_gotData = False
        inStatus = False

        if status:
            status.setStatus("Updating Stock Database", 80)

        for (ticker, date) in request.items():
            inStatus = True
            appGlobal.setBackgroundStatus("Downloading " + ticker)
            tocRecord = self.yahoo.recordFactory.createTocRecord(
                ticker,
                ticker,
                minDate=self.getFirstDate(ticker),
                maxDate=self.getLastDate(ticker))
            self.db.beginTransaction()
            self.yahoo.updateHistorical(tocRecord)
            self.db.commitTransaction()

        # Update last dates
        for ticker in stocks_lastDate:
            if ticker == "__UNIQUEID__":
                continue
            self.updateLastDate(ticker, stocks_lastDate[ticker])
        for (ticker, date) in request.items():
            if ticker == "__UNIQUEID__":
                continue
            self.setLastDownload(ticker)

        if inStatus:
            appGlobal.clearBackgroundStatus()

        return stocks_gotData
Ejemplo n.º 5
0
    def checkStatus(self):
        app = appGlobal.getApp()

        # Return 10 by default
        ret = 10

        if appGlobal.getFailConnected():
            self.running = False
            return ret

        # Sleep twice to make sure everything was updated
        # 94 = slept once
        # 95 = slept twice
        if autoUpdater.sleeping():
            if self.sleptOnce:
                return 95
            else:
                self.sleptOnce = True
                autoUpdater.wakeUp()
                return 94

        ret = 10 + 84 * autoUpdater.percentDone() / 100
        return ret
Ejemplo n.º 6
0
	def checkStatus(self):
		app = appGlobal.getApp()

		# Return 10 by default
		ret = 10

		if appGlobal.getFailConnected():
			self.running = False
			return ret

		# Sleep twice to make sure everything was updated
		# 94 = slept once
		# 95 = slept twice
		if autoUpdater.sleeping():
			if self.sleptOnce:
				return 95
			else:
				self.sleptOnce = True
				autoUpdater.wakeUp()
				return 94
		
		ret = 10 + 84 * autoUpdater.percentDone() / 100
		return ret
Ejemplo n.º 7
0
    def run(self):
        global haveKeyring
        app = appGlobal.getApp()
        while self.running:
            self.freshStart = False
            now = datetime.datetime.now()

            # print "Check for new stock data at %s" % now.strftime("%Y-%m-%d %H:%M:%S")

            # Determine the cutoff date for downloading new stock data
            # If before 7pm EST then go back to 7pm EST of the previous day
            # Go back by one day until we hit a week day
            # Then zero out H:M:S
            cutoffTime = datetime.datetime.utcnow() - datetime.timedelta(hours=5)
            if cutoffTime.hour < 19:
                cutoffTime -= datetime.timedelta(days=1)
            while cutoffTime.weekday() >= 5:
                cutoffTime -= datetime.timedelta(days=1)
            cutoffTime = datetime.datetime(cutoffTime.year, cutoffTime.month, cutoffTime.day, 0, 0, 0)

            # Load all portfolios
            # Build list of tickers, update stock data
            names = self.prefs.getPortfolios()
            tickers = []
            max = len(names)
            tickerPorts = {}
            ports = {}
            for name in names:
                p = portfolio.Portfolio(name)
                p.readFromDb()
                ports[name] = p

                # Auto update
            if app.prefs.getBackgroundImport() and haveKeyring:
                # Import once every 20 hours
                diff = now - app.prefs.getLastBackgroundImport()
                # print "time since last auto import", diff
                if diff > datetime.timedelta(hours=20):
                    print "Background import transactions at %s" % now.strftime("%Y-%m-%d %H:%M:%S")
                    for name, p in ports.items():
                        if not self.running:
                            break

                        if p.isBrokerage() and p.brokerage != "" and p.username != "" and p.account != "":
                            # check for password and brokerage
                            brokerage = app.plugins.getBrokerage(p.brokerage)
                            if not brokerage:
                                continue
                            try:
                                password = keyring.get_password("Icarra-ofx-" + name, p.username)
                                if not password:
                                    continue
                            except:
                                haveKeyring = False
                                continue
                            print "import from", name
                            # Get ofx data, update if not empty
                            # It may be an error string, in which case it's ignored
                            ofx = getOfx(p.username, password, brokerage, p.account)
                            if ofx != "":
                                (numNew, numOld, newTickers) = p.updateFromFile(ofx, app)
                                if numNew > 0 or newTickers:
                                    p.portPrefs.setDirty(True)
                            print "imported"

                            # Update time only if not aborted
                    if self.running:
                        app.prefs.setLastBackgroundImport()

                        # Build list of tickers
            for name, p in ports.items():
                pTickers = p.getTickers(includeAllocation=True)
                for ticker in pTickers:
                    if ticker in ["__CASH__", "__COBMINED__"]:
                        continue

                        # Add to tickerPorts map
                    if ticker in tickerPorts:
                        tickerPorts[ticker].append(p)
                    else:
                        tickerPorts[ticker] = [p]

                        # Add to list of tickers
                    if not ticker in tickers:
                        tickers.append(ticker)

                        # Remove tickers that do not need to be updated
            for ticker in tickers[:]:
                # Check if we do not have data after the cutoffTime
                last = self.stockData.getLastDate(ticker)
                if 0 and last and last >= cutoffTime:
                    tickers.remove(ticker)
                    continue

                    # Check if we tried downloading within one hour
                lastDownload = self.stockData.getLastDownload(ticker)
                if lastDownload and datetime.datetime.now() - lastDownload < datetime.timedelta(hours=1):
                    tickers.remove(ticker)
                    continue

            portsToUpdate = {}
            self.tickersToImport = len(tickers)

            # Lump all tickers that are less than 2 weeks old into one request
            updateNow = []
            for ticker in tickers[:]:
                lastDownload = self.stockData.getLastDownload(ticker)
                if lastDownload and datetime.datetime.now() - lastDownload < datetime.timedelta(days=14):
                    updateNow.append(ticker)
                    tickers.remove(ticker)
                    continue

                    # Download the 2 week lump 10 tickers at a time
            while updateNow and self.running and not appGlobal.getFailConnected():
                downloadPart = updateNow[0:10]
                updateNow = updateNow[10:]
                try:
                    new = self.stockData.updateStocks(downloadPart)
                    appGlobal.setConnected(True)
                except Exception, e:
                    appGlobal.setFailConnected(True)
                    break
                for ticker in downloadPart:
                    self.tickerCount += 1
                    if new:
                        for p in tickerPorts[ticker]:
                            portsToUpdate[p.name] = True

                            # Update each remaining ticker while still running
            for ticker in tickers:
                if self.running and not appGlobal.getFailConnected():
                    try:
                        new = self.stockData.updateStocks([ticker])
                        appGlobal.setConnected(True)
                    except Exception, e:
                        appGlobal.setFailConnected(True)
                        break
                    if new:
                        for p in tickerPorts[ticker]:
                            # Add 3 for every port
                            if not p.name in portsToUpdate:
                                if app.prefs.getBackgroundRebuild():
                                    self.tickersToImport += 3
                                portsToUpdate[p.name] = True

                self.tickerCount += 1
Ejemplo n.º 8
0
    def run(self):
        global haveKeyring
        app = appGlobal.getApp()
        while self.running:
            self.freshStart = False
            now = datetime.datetime.now()

            #print "Check for new stock data at %s" % now.strftime("%Y-%m-%d %H:%M:%S")

            # Determine the cutoff date for downloading new stock data
            # If before 7pm EST then go back to 7pm EST of the previous day
            # Go back by one day until we hit a week day
            # Then zero out H:M:S
            cutoffTime = datetime.datetime.utcnow() - datetime.timedelta(
                hours=5)
            if cutoffTime.hour < 19:
                cutoffTime -= datetime.timedelta(days=1)
            while cutoffTime.weekday() >= 5:
                cutoffTime -= datetime.timedelta(days=1)
            cutoffTime = datetime.datetime(cutoffTime.year, cutoffTime.month,
                                           cutoffTime.day, 0, 0, 0)

            # Load all portfolios
            # Build list of tickers, update stock data
            names = self.prefs.getPortfolios()
            tickers = []
            max = len(names)
            tickerPorts = {}
            ports = {}
            for name in names:
                p = portfolio.Portfolio(name)
                p.readFromDb()
                ports[name] = p

            # Auto update
            if app.prefs.getBackgroundImport() and haveKeyring:
                # Import once every 20 hours
                diff = now - app.prefs.getLastBackgroundImport()
                #print "time since last auto import", diff
                if diff > datetime.timedelta(hours=20):
                    print "Background import transactions at %s" % now.strftime(
                        "%Y-%m-%d %H:%M:%S")
                    for name, p in ports.items():
                        if not self.running:
                            break

                        if p.isBrokerage(
                        ) and p.brokerage != "" and p.username != "" and p.account != "":
                            # check for password and brokerage
                            brokerage = app.plugins.getBrokerage(p.brokerage)
                            if not brokerage:
                                continue
                            try:
                                password = keyring.get_password(
                                    "Icarra-ofx-" + name, p.username)
                                if not password:
                                    continue
                            except:
                                haveKeyring = False
                                continue
                            print "import from", name
                            # Get ofx data, update if not empty
                            # It may be an error string, in which case it's ignored
                            ofx = getOfx(p.username, password, brokerage,
                                         p.account)
                            if ofx != "":
                                (numNew, numOld,
                                 newTickers) = p.updateFromFile(ofx, app)
                                if numNew > 0 or newTickers:
                                    p.portPrefs.setDirty(True)
                            print "imported"

                    # Update time only if not aborted
                    if self.running:
                        app.prefs.setLastBackgroundImport()

            # Build list of tickers
            for name, p in ports.items():
                pTickers = p.getTickers(includeAllocation=True)
                for ticker in pTickers:
                    if ticker in ["__CASH__", "__COBMINED__"]:
                        continue

                    # Add to tickerPorts map
                    if ticker in tickerPorts:
                        tickerPorts[ticker].append(p)
                    else:
                        tickerPorts[ticker] = [p]

                    # Add to list of tickers
                    if not ticker in tickers:
                        tickers.append(ticker)

            # Remove tickers that do not need to be updated
            for ticker in tickers[:]:
                # Check if we do not have data after the cutoffTime
                last = self.stockData.getLastDate(ticker)
                if 0 and last and last >= cutoffTime:
                    tickers.remove(ticker)
                    continue

                # Check if we tried downloading within one hour
                lastDownload = self.stockData.getLastDownload(ticker)
                if lastDownload and datetime.datetime.now(
                ) - lastDownload < datetime.timedelta(hours=1):
                    tickers.remove(ticker)
                    continue

            portsToUpdate = {}
            self.tickersToImport = len(tickers)

            # Lump all tickers that are less than 2 weeks old into one request
            updateNow = []
            for ticker in tickers[:]:
                lastDownload = self.stockData.getLastDownload(ticker)
                if lastDownload and datetime.datetime.now(
                ) - lastDownload < datetime.timedelta(days=14):
                    updateNow.append(ticker)
                    tickers.remove(ticker)
                    continue

            # Download the 2 week lump 10 tickers at a time
            while updateNow and self.running and not appGlobal.getFailConnected(
            ):
                downloadPart = updateNow[0:10]
                updateNow = updateNow[10:]
                try:
                    new = self.stockData.updateStocks(downloadPart)
                    appGlobal.setConnected(True)
                except Exception, e:
                    appGlobal.setFailConnected(True)
                    break
                for ticker in downloadPart:
                    self.tickerCount += 1
                    if new:
                        for p in tickerPorts[ticker]:
                            portsToUpdate[p.name] = True

            # Update each remaining ticker while still running
            for ticker in tickers:
                if self.running and not appGlobal.getFailConnected():
                    try:
                        new = self.stockData.updateStocks([ticker])
                        appGlobal.setConnected(True)
                    except Exception, e:
                        appGlobal.setFailConnected(True)
                        break
                    if new:
                        for p in tickerPorts[ticker]:
                            # Add 3 for every port
                            if not p.name in portsToUpdate:
                                if app.prefs.getBackgroundRebuild():
                                    self.tickersToImport += 3
                                portsToUpdate[p.name] = True

                self.tickerCount += 1