How to use the persistence.StratExecConfig function in Persistence

To help you get started, we’ve selected a few Persistence examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gbeced / pyalgotrade / googleappengine / app / pages / strategy.py View on Github external
templateValues["error"] = "Failed to load strategy '%s': %s" % (strategyClassName, e)
            path = os.path.join(os.path.dirname(__file__), "..", "templates", 'error.html')
            self.response.out.write(template.render(path, templateValues))
            return

        # persistence.StratExecConfig.getByClass(strategyClassName)

        # Template values
        strategyValues = {}
        strategyValues["class"] = strategyClassName
        templateValues["logout_url"] = users.create_logout_url("/")
        templateValues["user"] = users.get_current_user()
        templateValues["strategy"] = strategyValues
        templateValues["queue_execution_url"] = StrategyExecutionPage.getUrl(strategyClassName)

        templateValues["active_executions"] = get_stratexecconfig_for_template(persistence.StratExecConfig.getByClass(strategyClassName, [persistence.StratExecConfig.Status.ACTIVE]))
        templateValues["finished_executions"] = get_stratexecconfig_for_template(persistence.StratExecConfig.getByClass(strategyClassName, [persistence.StratExecConfig.Status.FINISHED, persistence.StratExecConfig.Status.CANCELED_TOO_MANY_ERRORS]))

        # Build the response using the template.
        path = os.path.join(os.path.dirname(__file__), "..", "templates", 'strategy.html')
        self.response.out.write(template.render(path, templateValues))
github gbeced / pyalgotrade / googleappengine / app / usertasks.py View on Github external
def run(self):
        stratExecConfig = persistence.StratExecConfig.getByKey(self.__stratExecConfigKey)

        # Update best result.
        if stratExecConfig.bestResult is None or self.__result > stratExecConfig.bestResult:
            # Need to convert paramValues to a list before storing that.
            paramValues = [value for value in self.__paramValues]
            stratExecConfig.bestResult = self.__result
            stratExecConfig.bestResultParameters = paramValues

        stratExecConfig.executionsFinished += self.__executions
        stratExecConfig.errors += self.__errors
        if stratExecConfig.executionsFinished == stratExecConfig.totalExecutions:
            stratExecConfig.status = persistence.StratExecConfig.Status.FINISHED
        # If we got more that 1000 errors, cancel the strategy execution to avoid wasting resources.
        elif stratExecConfig.errors > 1000:
            stratExecConfig.status = persistence.StratExecConfig.Status.CANCELED_TOO_MANY_ERRORS

        stratExecConfig.put()
github gbeced / pyalgotrade / googleappengine / app / pages / strategy.py View on Github external
def __buildStratExecConfig(self, className, strategyParams, form):
        totalExecutions = 1
        parameterRanges = []
        for strategyParam in strategyParams:
            beginParamValue, endParamValue = form.getParamRange(strategyParam)
            endParamValue += 1
            parameterRanges.append(beginParamValue)
            parameterRanges.append(endParamValue)
            totalExecutions *= (endParamValue - beginParamValue)

        ret = persistence.StratExecConfig(
            className=className,
            instrument=form.getInstrument(),
            barType=persistence.Bar.Type.DAILY,
            firstDate=form.getParamAsDateTime(QueueStrategyExecutionForm.beginDateParam),
            lastDate=form.getParamAsDateTime(QueueStrategyExecutionForm.endDateParam),
            parameterNames=strategyParams,
            parameterRanges=parameterRanges,
            created=datetime.datetime.now(),
            status=persistence.StratExecConfig.Status.ACTIVE,
            totalExecutions=totalExecutions
            )

        return ret
github gbeced / pyalgotrade / googleappengine / app / pages / strategy.py View on Github external
beginDate = form.getParamAsDateTime(QueueStrategyExecutionForm.beginDateParam)
                endDate = form.getParamAsDateTime(QueueStrategyExecutionForm.endDateParam)
                if not persistence.Bar.hasBars(instrument, persistence.Bar.Type.DAILY, beginDate, endDate):
                    raise Exception("There are no bars loaded for '%s' between '%s' and '%s'" % (instrument, beginDate, endDate))

                # Queue the strategy execution config and redirect.
                stratExecConfig = self.__buildStratExecConfig(strategyClassName, strategyParams, form)
                stratExecConfig.put()
                usertasks.MasterTask(1, stratExecConfig).queue()
                self.redirect(StrategyPage.getUrl(strategyClassName))
            except Exception, e:
                templateValues["submit_error"] = str(e)
        # If this is a re-execution request, load form values from strat exec config.
        elif stratExecConfigKey != "":
            try:
                stratExecConfig = persistence.StratExecConfig.getByKey(stratExecConfigKey)
                form.loadFromStratExecConfig(stratExecConfig)
            except Exception, e:
                templateValues["error"] = "Failed to load previous execution: %s" % (e)
                path = os.path.join(os.path.dirname(__file__), "..", "templates", 'error.html')
                self.response.out.write(template.render(path, templateValues))
                return

        # Template values
        strategyValues = {}
        strategyValues["class"] = strategyClassName

        templateValues["logout_url"] = users.create_logout_url("/")
        templateValues["user"] = users.get_current_user()
        templateValues["strategy"] = strategyValues
        templateValues["form"] = form.getTemplateValues()
github gbeced / pyalgotrade / googleappengine / app / pages / strategy.py View on Github external
def get_stratexecconfig_for_template(stratExecConfigs):
    ret = []
    for stratExecConfig in stratExecConfigs:
        props = {}
        props["class"] = stratExecConfig.className
        props["created"] = stratExecConfig.created.strftime("%d/%b/%Y %H:%M")
        props["executions_finished"] = stratExecConfig.executionsFinished
        props["total_executions"] = stratExecConfig.totalExecutions
        if stratExecConfig.bestResult:
            props["best_result"] = stratExecConfig.bestResult
            props["best_result_parameters"] = stratExecConfig.bestResultParameters

        if stratExecConfig.status in [persistence.StratExecConfig.Status.ACTIVE, persistence.StratExecConfig.Status.FINISHED]:
            if stratExecConfig.errors > 0:
                props["additional_info"] = "%d errors were found. Take a look at the application logs." % (stratExecConfig.errors)
            if stratExecConfig.status == persistence.StratExecConfig.Status.FINISHED:
                props["reexecute_url"] = StrategyExecutionPage.getReExecuteUrl(stratExecConfig.className, stratExecConfig.key())
        elif stratExecConfig.status == persistence.StratExecConfig.Status.CANCELED_TOO_MANY_ERRORS:
            props["additional_info"] = "The strategy execution was cancelled because it generated too many errors."
        else:
            assert(False)  # Invalid state

        ret.append(props)
    return ret
github gbeced / pyalgotrade / googleappengine / app / usertasks.py View on Github external
def getNextWorker(self):
        # Check if we need to abort executions.
        # We're doing this only once per MasterTask execution to avoid calling the db too much.
        if not self.__tooManyErrosChecked:
            self.__tooManyErrosChecked = True
            stratExecConfig = persistence.StratExecConfig.getByKey(self.__stratExecConfigKey)
            if stratExecConfig.status == persistence.StratExecConfig.Status.CANCELED_TOO_MANY_ERRORS:
                self.__logger.error("Dropping execution of '%s' due to too many errors" % (stratExecConfig.className))
                return None

        chunkSize = 1000  # Max executions per task.
        ret = None
        if not self.isFinished():
            # Clone self.__paramsIt before building WorkerTask because we'll modify it immediately.
            paramsIt = copy.deepcopy(self.__paramsIt)
            ret = WorkerTask(1, self.__stratExecConfigKey, paramsIt, chunkSize)

            # Advance parameters iterator for the next worker.
            for i in xrange(chunkSize):
                self.__paramsIt.moveNext()
        return ret
github gbeced / pyalgotrade / googleappengine / app / usertasks.py View on Github external
def run(self):
        global strategyExecutor

        taskTimer = timer.Timer()
        stratExecConfig = persistence.StratExecConfig.getByKey(self.__stratExecConfigKey)
        self.__logger.info("WorkerTask for '%s' starting from %s" % (stratExecConfig.className, str(self.__paramsIt.getCurrent())))

        maxTaskRunTime = 9 * 60  # Stop the task after 9 minutes to avoid getting interrupted after 10 minutes.
        bestResult = 0.0
        bestResultParams = []
        errors = 0
        executions = 0
        maxStratTime = 0

        while self.__chunkSize > 0:
            stratExecTimer = timer.Timer()
            try:
                paramValues = self.__paramsIt.getCurrent()
                # self.__logger.info("WorkerTask running '%s' with parameters: %s" % (stratExecConfig.className, paramValues))

                # If there are no more parameters, just stop.