def __processJob(self, job, barsFreq, instruments, bars): bestResult = None parameters = job.getNextParameters() bestParams = parameters while parameters is not None: # Wrap the bars into a feed. feed = barfeed.OptimizerBarFeed(barsFreq, instruments, bars) # Run the strategy. self.getLogger().info("Running strategy with parameters %s" % (str(parameters))) result = None try: result = self.runStrategy(feed, *parameters) except Exception as e: self.getLogger().exception( "Error running strategy with parameters %s: %s" % (str(parameters), e)) self.getLogger().info("Result %s" % result) if bestResult is None or result > bestResult: bestResult = result bestParams = parameters # Run with the next set of parameters. parameters = job.getNextParameters() assert (bestParams is not None) self.pushJobResults(job.getId(), bestResult, bestParams)
def testBaseBarFeedNoAdjClose(self): bars = [ bar.Bars({"orcl": bar.BasicBar(datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, None, bar.Frequency.DAY)}), bar.Bars({"orcl": bar.BasicBar(datetime.datetime(2001, 1, 2), 1, 1, 1, 1, 1, None, bar.Frequency.DAY)}), ] barFeed = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], bars) check_base_barfeed(self, barFeed, False)
def testDateTimesNotInOrder(self): bars = [ bar.Bars({"orcl": bar.BasicBar(datetime.datetime(2001, 1, 2), 1, 1, 1, 1, 1, 1, bar.Frequency.DAY)}), bar.Bars({"orcl": bar.BasicBar(datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1, bar.Frequency.DAY)}), ] f = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], bars) with self.assertRaisesRegexp(Exception, "Bar date times are not in order.*"): for dt, b in f: pass
def resultFunction2(self, parametres): assert(len(parametres) == len(self.lowerBound)) feedToBuildOnEveryLoop = barfeed.OptimizerBarFeed(self.feed.getFrequency(), self.feed.getRegisteredInstruments(), self.bars) myStrategy = UnionStrategy(feedToBuildOnEveryLoop, "btc", parametres) myStrategy.run() return myStrategy.getResult()
def runStrategy(self, stratExecConfig, paramValues): bars = self.__loadBars(stratExecConfig) barFeed = barfeed.OptimizerBarFeed(barfeed.Frequency.DAY, [stratExecConfig.instrument], bars) # Evaluate the strategy with the feed bars. params = [barFeed] params.extend(paramValues) myStrategy = cls.Class(stratExecConfig.className).getClass()(*params) myStrategy.run() return myStrategy.getResult()
def testBaseBarFeedNoAdjClose(self): bars = [ bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, None, bar.Frequency.DAY) ]), bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 2), 1, 1, 1, 1, 1, None, bar.Frequency.DAY) ]), ] barFeed = barfeed.OptimizerBarFeed(bar.Frequency.DAY, [INSTRUMENT], bars) check_base_barfeed(self, barFeed, False)
def testDupliateDateTimesForTradeBars(self): bars = [ bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1, bar.Frequency.TRADE) ]), bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 2, 1, bar.Frequency.TRADE) ]), ] f = barfeed.OptimizerBarFeed(bar.Frequency.TRADE, [INSTRUMENT], bars) expected_volume = 1 for dt, b in f: assert b[INSTRUMENT].getVolume() == expected_volume expected_volume += 1
def testDupliateDateTimesForDailyBars(self): bars = [ bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1, bar.Frequency.DAY) ]), bar.Bars([ bar.BasicBar(INSTRUMENT, datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1, bar.Frequency.DAY) ]), ] f = barfeed.OptimizerBarFeed(bar.Frequency.DAY, [INSTRUMENT], bars) with self.assertRaisesRegexp(Exception, "%s bars are not in order.*" % INSTRUMENT): for dt, b in f: pass
def __processJob(self, job, instruments, bars): bestResult = 0 parameters = job.getNextParameters() bestParams = parameters while parameters != None: # Wrap the bars into a feed. feed = barfeed.OptimizerBarFeed(instruments, bars) # Run the strategy. self.getLogger().info("Running strategy with parameters %s" % (str(parameters))) result = self.runStrategy(feed, *parameters) self.getLogger().info("Result %s" % result) if result > bestResult: bestResult = result bestParams = parameters # Run with the next set of parameters. parameters = job.getNextParameters() assert(bestParams != None) self.pushJobResults(job.getId(), bestResult, bestParams)
def testEmtpy(self): barFeed = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], []) self.assertEquals(barFeed.barsHaveAdjClose(), False)