class testPyConfig(unittest.TestCase):
    def setUp(self):
        self.config = PyConfig()
        self.config.setSource("test.ini")

    def tearDown(self):
        pass

    def testGetSession(self):
        keyValues = self.config.getSection("app_main")
        self.assertNotEqual(0, len(keyValues))

    def testGetOption(self):
        option = self.config.getOption("log", "file")
        self.assertEqual("test.log", option)
Example #2
0
class testPyConfig(unittest.TestCase):
    def setUp(self):
        self.config = PyConfig()
        self.config.setSource("test.ini")

    def tearDown(self):
        pass

    def testGetSession(self):
        keyValues = self.config.getSection("app_main")
        self.assertNotEqual(0, len(keyValues))

    def testGetOption(self):
        option = self.config.getOption("log", "file")
        self.assertEqual("test.log", option)
Example #3
0
    def __init__(self,
                 configFile,
                 startTickDate=0,
                 startTradeDate=0,
                 endTradeDate=None,
                 cash=150000,
                 symbolLists=None):
        LOG.debug("Loading config from %s" % configFile)
        self.__config = PyConfig()
        self.__config.setSource(configFile)

        self.__cash = cash
        self.__mCalculator = MetricManager()
        self.__symbolLists = symbolLists
        self.__accounts = []
        self.__startTickDate = startTickDate
        self.__startTradeDate = startTradeDate
        self.__endTradeDate = endTradeDate
        self.__firstSaver = None
Example #4
0
    def __init__(self, configFile, startTickDate = 0, startTradeDate = 0, endTradeDate = None, cash = 150000, symbolLists = None):
        LOG.debug("Loading config from %s" % configFile)
        self.__config = PyConfig()
        self.__config.setSource(configFile)

        self.__cash = cash
        self.__mCalculator = MetricManager()
        self.__symbolLists = symbolLists
        self.__accounts = []
        self.__startTickDate = startTickDate
        self.__startTradeDate = startTradeDate
        self.__endTradeDate = endTradeDate
        self.__firstSaver = None
Example #5
0
class BackTester(object):
    """ back testing """

    CASH = 1000000  #  0.1 million to start

    def __init__(self):
        self.__config = PyConfig()
        self.__mCalculator = MetricCalculator()
        self.__symbols = []

    def setup(self):
        """ setup """
        LOG.debug("Loading config from %s" % CONFIG_FILE)
        self.__config.setSource(CONFIG_FILE)
        appGlobal[TRADE_TYPE] = self.__config.getOption(CONF_APP_MAIN, CONF_TRADE_TYPE)
        self._setupLog()
        self._loadSymbols()

    def _setupLog(self):
        """ setup logging """
        logging.config.fileConfig(self.__config.getFullPath())

    def _runOneTest(self, symbol):
        """ run one test """
        LOG.debug("Running backtest for %s" % symbol)
        runner = TestRunner(self.__config, self.__mCalculator, symbol)
        runner.runTest()

    def _loadSymbols(self):
        """ find symbols"""
        symbolFile = self.__config.getOption(CONF_APP_MAIN, CONF_SYMBOL_FILE)
        assert symbolFile is not None, "%s is required in config file" % CONF_SYMBOL_FILE

        with open(os.path.join(self.__config.getDir(), symbolFile), "r") as f:
            for symbol in f:
                if symbol not in self.__symbols:
                    self.__symbols.append(symbol.strip())

        assert self.__symbols, "None symbol provided"

    def runTests(self):
        """ run tests """
        for symbol in self.__symbols:
            try:
                self._runOneTest(symbol)
            except BaseException as excp:
                LOG.error(
                    "Unexpected error when backtesting %s -- except %s, traceback %s"
                    % (symbol, excp, traceback.format_exc(8))
                )
Example #6
0
class BackTester(object):
    ''' back testing '''
    CASH = 1000000  #  0.1 million to start

    def __init__(self):
        self.__config = PyConfig()
        self.__mCalculator = MetricCalculator()
        self.__symbols = []

    def setup(self):
        ''' setup '''
        LOG.debug("Loading config from %s" % CONFIG_FILE)
        self.__config.setSource(CONFIG_FILE)
        appGlobal[TRADE_TYPE] = self.__config.getOption(
            CONF_APP_MAIN, CONF_TRADE_TYPE)
        self._setupLog()
        self._loadSymbols()

    def _setupLog(self):
        ''' setup logging '''
        logging.config.fileConfig(self.__config.getFullPath())

    def _runOneTest(self, symbol):
        ''' run one test '''
        LOG.debug("Running backtest for %s" % symbol)
        runner = TestRunner(self.__config, self.__mCalculator, symbol)
        runner.runTest()

    def _loadSymbols(self):
        ''' find symbols'''
        symbolFile = self.__config.getOption(CONF_APP_MAIN, CONF_SYMBOL_FILE)
        assert symbolFile is not None, "%s is required in config file" % CONF_SYMBOL_FILE

        with open(os.path.join(self.__config.getDir(), symbolFile), "r") as f:
            for symbol in f:
                if symbol not in self.__symbols:
                    self.__symbols.append(symbol.strip())

        assert self.__symbols, "None symbol provided"

    def runTests(self):
        ''' run tests '''
        for symbol in self.__symbols:
            try:
                self._runOneTest(symbol)
            except BaseException as excp:
                LOG.error("Unexpected error when backtesting %s -- except %s, traceback %s" \
                          % (symbol, excp, traceback.format_exc(8)))
 def setUp(self):
     self.config = PyConfig()
     self.config.setSource("test.ini")
Example #8
0
class BackTester(object):
    ''' back testing '''

    def __init__(self, configFile, startTickDate=0, startTradeDate=0, endTradeDate=None, cash=150000, symbolLists=None):
        LOG.debug("Loading config from %s" % configFile)
        self.__config = PyConfig()
        self.__config.setSource(configFile)

        self.__cash = cash
        self.__mCalculator = MetricCalculator()
        self.__symbolLists = symbolLists
        LOG.debug(self.__symbolLists)
        self.__accounts = []
        self.__startTickDate = startTickDate
        self.__startTradeDate = startTradeDate
        self.__endTradeDate = endTradeDate
        self.__firstSaver = None

    def setup(self):
        ''' setup '''
        appGlobal[TRADE_TYPE] = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_TRADE_TYPE)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_INIT_CASH, self.__cash)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_START_TRADE_DATE, self.__startTradeDate)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_END_TRADE_DATE, self.__endTradeDate)
        self._setupLog()
        LOG.debug(self.__symbolLists)
        if not self.__symbolLists:
            self._loadSymbols()

    def __getFirstSaver(self):
        ''' get first saver, create it if not exist'''
        saverName = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_SAVER)
        outputDb = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_OUTPUT_DB)
        if saverName and len(self.__symbolLists) > 0:
            self.__firstSaver = StateSaverFactory.createStateSaver(saverName,
                                                                   {'db': outputDb},
                                                                   getBackTestTableName(self.__symbolLists[0],
                                                                                        self.__config.getOption(
                                                                                            CONF_ULTRAFINANCE_SECTION,
                                                                                            CONF_STRATEGY_NAME)))

        return self.__firstSaver


    def _setupLog(self):
        ''' setup logging '''
        if self.__config.getSection("loggers"):
            logging.config.fileConfig(self.__config.getFullPath())

    def _runOneTest(self, symbols):
        ''' run one test '''
        LOG.debug("Running backtest for %s" % symbols)
        runner = TestRunner(self.__config, self.__mCalculator, self.__accounts, symbols, self.__startTickDate,
                            self.__endTradeDate, self.__cash)
        runner.runTest()

    def _loadSymbols(self):
        ''' find symbols'''
        symbolFile = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_SYMBOL_FILE)
        assert symbolFile is not None, "%s is required in config file" % CONF_SYMBOL_FILE

        LOG.info("loading symbols from %s" % os.path.join(self.__config.getDir(), symbolFile))
        if not self.__symbolLists:
            self.__symbolLists = []

        with open(os.path.join(self.__config.getDir(), symbolFile), "r") as f:
            for symbols in f:
                if symbols not in self.__symbolLists:
                    self.__symbolLists.append([symbol.strip() for symbol in symbols.split()])

        assert self.__symbolLists, "None symbol provided"

    def runTests(self):
        ''' run tests '''
        for symbols in self.__symbolLists:
            try:
                self._runOneTest(symbols)
            except KeyboardInterrupt:
                LOG.error("User Interrupted")
                sys.exit("User Interrupted")
            except BaseException as excp:
                LOG.error("Unexpected error when backtesting %s -- except %s, traceback %s" \
                          % (symbols, excp, traceback.format_exc(8)))

    def getLatestStates(self):
        ''' get latest state'''
        return [json.loads(str(result)) for result in self.__getFirstSaver().getStates(0, None)]

    def getLatestPlacedOrders(self, num=20):
        ''' get latest placed orders of first symbol list '''
        orders = []
        for account in self.__accounts:
            orders.extend(account.orderHistory[-num:])

        return orders

    def getHoldings(self):
        ''' get holdings '''
        holdings = []
        for account in self.__accounts:
            holdings.append(account.holdings)

        return holdings

    def getMetrics(self):
        ''' get all metrics '''
        return self.__mCalculator.getMetrics()


    def getOpenOrders(self):
        ''' get open orders '''
        return self.__

    def printMetrics(self):
        ''' print metrics '''
        LOG.info(self.getMetrics())
Example #9
0
 def setUp(self):
     self.config = PyConfig()
     self.config.setSource("test.ini")
Example #10
0
 def __init__(self):
     self.__config = PyConfig()
     self.__mCalculator = MetricCalculator()
     self.__symbols = []
Example #11
0
class BackTester(object):
    ''' back testing '''
    def __init__(self,
                 configFile,
                 startTickDate=0,
                 startTradeDate=0,
                 endTradeDate=None,
                 cash=150000,
                 symbolLists=None):
        LOG.debug("Loading config from %s" % configFile)
        self.__config = PyConfig()
        self.__config.setSource(configFile)

        self.__cash = cash
        self.__mCalculator = MetricManager()
        self.__symbolLists = symbolLists
        self.__accounts = []
        self.__startTickDate = startTickDate
        self.__startTradeDate = startTradeDate
        self.__endTradeDate = endTradeDate
        self.__firstSaver = None

    def setup(self):
        ''' setup '''
        appGlobal[TRADE_TYPE] = self.__config.getOption(
            CONF_ULTRAFINANCE_SECTION, CONF_TRADE_TYPE)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_INIT_CASH,
                               self.__cash)
        self.__config.override(CONF_ULTRAFINANCE_SECTION,
                               CONF_START_TRADE_DATE, self.__startTradeDate)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_END_TRADE_DATE,
                               self.__endTradeDate)
        self._setupLog()
        LOG.debug(self.__symbolLists)
        if not self.__symbolLists:
            self._loadSymbols()

    def _setupLog(self):
        ''' setup logging '''
        if self.__config.getSection("loggers"):
            logging.config.fileConfig(self.__config.getFullPath())

    def _runOneTest(self, symbols):
        ''' run one test '''
        LOG.debug("Running backtest for %s" % symbols)
        runner = TestRunner(self.__config, self.__mCalculator, self.__accounts,
                            symbols, self.__startTickDate, self.__endTradeDate,
                            self.__cash)
        runner.runTest()

    def _loadSymbols(self):
        ''' find symbols'''
        symbolFile = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                             CONF_SYMBOL_FILE)
        assert symbolFile is not None, "%s is required in config file" % CONF_SYMBOL_FILE

        LOG.info("loading symbols from %s" %
                 os.path.join(self.__config.getDir(), symbolFile))
        if not self.__symbolLists:
            self.__symbolLists = []

        with open(os.path.join(self.__config.getDir(), symbolFile), "r") as f:
            for symbols in f:
                if symbols not in self.__symbolLists:
                    self.__symbolLists.append(
                        [symbol.strip() for symbol in symbols.split()])

        assert self.__symbolLists, "None symbol provided"

    def runTests(self):
        ''' run tests '''
        for symbols in self.__symbolLists:
            try:
                self._runOneTest(symbols)
            except KeyboardInterrupt:
                LOG.error("User Interrupted")
                sys.exit("User Interrupted")
            except BaseException as excp:
                LOG.error("Unexpected error when backtesting %s -- except %s, traceback %s" \
                          % (symbols, excp, traceback.format_exc(8)))

    def getMetrics(self):
        ''' get all metrics '''
        return self.__mCalculator.getMetrics()

    def printMetrics(self):
        ''' print metrics '''
        LOG.info(self.getMetrics())
Example #12
0
 def __init__(self):
     self.__config = PyConfig()
     self.__mCalculator = MetricCalculator()
     self.__symbols = []
Example #13
0
class BackTester(object):
    ''' back testing '''
    def __init__(self, configFile, startTickDate = 0, startTradeDate = 0, endTradeDate = None, cash = 150000, symbolLists = None):
        LOG.debug("Loading config from %s" % configFile)
        self.__config = PyConfig()
        self.__config.setSource(configFile)

        self.__cash = cash
        self.__mCalculator = MetricManager()
        self.__symbolLists = symbolLists
        self.__accounts = []
        self.__startTickDate = startTickDate
        self.__startTradeDate = startTradeDate
        self.__endTradeDate = endTradeDate
        self.__firstSaver = None

    def setup(self):
        ''' setup '''
        appGlobal[TRADE_TYPE] = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_TRADE_TYPE)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_INIT_CASH, self.__cash)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_START_TRADE_DATE, self.__startTradeDate)
        self.__config.override(CONF_ULTRAFINANCE_SECTION, CONF_END_TRADE_DATE, self.__endTradeDate)
        self._setupLog()
        LOG.debug(self.__symbolLists)
        if not self.__symbolLists:
            self._loadSymbols()

    def _setupLog(self):
        ''' setup logging '''
        if self.__config.getSection("loggers"):
            logging.config.fileConfig(self.__config.getFullPath())

    def _runOneTest(self, symbols):
        ''' run one test '''
        LOG.debug("Running backtest for %s" % symbols)
        runner = TestRunner(self.__config, self.__mCalculator, self.__accounts, symbols, self.__startTickDate, self.__endTradeDate, self.__cash)
        runner.runTest()

    def _loadSymbols(self):
        ''' find symbols'''
        symbolFile = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_SYMBOL_FILE)
        assert symbolFile is not None, "%s is required in config file" % CONF_SYMBOL_FILE

        LOG.info("loading symbols from %s" % os.path.join(self.__config.getDir(), symbolFile))
        if not self.__symbolLists:
            self.__symbolLists = []

        with open(os.path.join(self.__config.getDir(), symbolFile), "r") as f:
            for symbols in f:
                if symbols not in self.__symbolLists:
                    self.__symbolLists.append([symbol.strip() for symbol in symbols.split()])

        assert self.__symbolLists, "None symbol provided"

    def runTests(self):
        ''' run tests '''
        for symbols in self.__symbolLists:
            try:
                self._runOneTest(symbols)
            except KeyboardInterrupt:
                LOG.error("User Interrupted")
                sys.exit("User Interrupted")
            except BaseException as excp:
                LOG.error("Unexpected error when backtesting %s -- except %s, traceback %s" \
                          % (symbols, excp, traceback.format_exc(8)))

    def getMetrics(self):
        ''' get all metrics '''
        return self.__mCalculator.getMetrics()

    def printMetrics(self):
        ''' print metrics '''
        LOG.info(self.getMetrics())