Ejemplo n.º 1
0
 def __init__(self, config, mCalculator, symbol):
     self.__accountManager = AccountManager()
     self.__tickFeeder = TickFeeder()
     self.__tradingCenter = TradingCenter()
     self.__tradingEngine = TradingEngine()
     self.__indexHelper = IndexHelper()
     self.__history = History()
     self.__saver = None
     self.__symbol = symbol
     self.__config = config
     self.__mCalculator = mCalculator
Ejemplo n.º 2
0
 def __init__(self, config, metricManager, accounts, symbols, startTickDate,
              endTradeDate, cash):
     self.__accountManager = AccountManager()
     self.__accountId = None
     self.__startTickDate = startTickDate
     self.__endTradeDate = endTradeDate
     self.__tickFeeder = TickFeeder(start=startTickDate, end=endTradeDate)
     self.__tradingCenter = TradingCenter()
     self.__tradingEngine = TradingEngine()
     self.__indexHelper = IndexHelper()
     self.__accounts = accounts
     self.__history = History()
     self.__saver = None
     self.__symbols = symbols
     self.__config = config
     self.__metricManager = metricManager
     self.__cash = cash
Ejemplo n.º 3
0
 def __init__(self, config, mCalculator, symbol):
     self.__accountManager = AccountManager()
     self.__tickFeeder = TickFeeder()
     self.__tradingCenter = TradingCenter()
     self.__tradingEngine = TradingEngine()
     self.__indexHelper = IndexHelper()
     self.__history = History()
     self.__saver = None
     self.__symbol = symbol
     self.__config = config
     self.__mCalculator = mCalculator
Ejemplo n.º 4
0
 def __init__(self, config, mCalculator, accounts, symbols, startTickDate, endTradeDate, cash):
     self.__accountManager = AccountManager()
     self.__accountId = None
     self.__tickFeeder = TickFeeder(start=startTickDate, end=endTradeDate)
     self.__tradingCenter = TradingCenter()
     self.__tradingEngine = TradingEngine()
     self.__indexHelper = IndexHelper()
     self.__accounts = accounts
     self.__history = History()
     self.__saver = None
     self.__symbols = symbols
     self.__config = config
     self.__mCalculator = mCalculator
     self.__cash = cash
Ejemplo n.º 5
0
class TestRunner(object):
    ''' back testing '''

    def __init__(self, config, mCalculator, accounts, symbols, startTickDate, endTradeDate, cash):
        self.__accountManager = AccountManager()
        self.__accountId = None
        self.__tickFeeder = TickFeeder(start=startTickDate, end=endTradeDate)
        self.__tradingCenter = TradingCenter()
        self.__tradingEngine = TradingEngine()
        self.__indexHelper = IndexHelper()
        self.__accounts = accounts
        self.__history = History()
        self.__saver = None
        self.__symbols = symbols
        self.__config = config
        self.__mCalculator = mCalculator
        self.__cash = cash

    def _setup(self):
        ''' setup '''
        self._setupTradingCenter()
        self._setupTickFeeder()
        self._setupSaver()

        #wire things together
        self._setupStrategy()
        self.__tickFeeder.tradingCenter = self.__tradingCenter
        self.__tradingEngine.tickProxy = self.__tickFeeder
        self.__tradingEngine.orderProxy = self.__tradingCenter
        self.__tradingCenter.accountManager = self.__accountManager
        self.__tradingEngine.saver = self.__saver
        self.__tickFeeder.saver = self.__saver
        self.__accountManager.saver = self.__saver

    def _setupTradingCenter(self):
        self.__tradingCenter.start = 0
        self.__tradingCenter.end = None

    def _setupTickFeeder(self):
        ''' setup tickFeeder'''
        self.__tickFeeder.indexHelper = self.__indexHelper
        self.__tickFeeder.setSymbols(self.__symbols)
        self.__tickFeeder.setDam(self._createDam("")) # no need to set symbol because it's batch operation

        iSymbol = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_INDEX)
        self.__tickFeeder.setIndexSymbol(iSymbol)

    def _createDam(self, symbol):
        ''' setup Dam'''
        damName = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_INPUT_DAM)
        inputDb = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_INPUT_DB)
        dam = DAMFactory.createDAM(damName, {'db': inputDb})
        dam.setSymbol(symbol)

        return dam

    def _setupSaver(self):
        ''' setup Saver '''
        saverName = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_SAVER)
        outputDb = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_OUTPUT_DB)
        if saverName:
            self.__saver = StateSaverFactory.createStateSaver(saverName,
                                                              {'db': outputDb},
                                                              getBackTestTableName(self.__symbols,
                                                                                   self.__config.getOption(
                                                                                       CONF_ULTRAFINANCE_SECTION,
                                                                                       CONF_STRATEGY_NAME)))

    def _setupStrategy(self):
        ''' setup tradingEngine'''
        strategy = StrategyFactory.createStrategy(
            self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_STRATEGY_NAME),
            self.__config.getSection(CONF_ULTRAFINANCE_SECTION))
        strategy.setSymbols(self.__symbols)
        strategy.history = self.__history

        #associate account
        self.__accountId = self.__accountManager.createAccount(self.__cash)
        strategy.accountId = self.__accountId
        strategy.accountManager = self.__accountManager

        #register on trading engine
        strategy.tradingEngine = self.__tradingEngine
        self.__tradingEngine.register(strategy)

    def _execute(self):
        ''' run backtest '''
        LOG.info("Running backtest for %s" % self.__symbols)

        #start trading engine
        thread = Thread(target=self.__tradingEngine.runListener, args=())
        thread.setDaemon(False)
        thread.start()

        #start tickFeeder
        self.__tickFeeder.execute()
        self.__tickFeeder.complete()

        timePositions = self.__accountManager.getAccountPostions(self.__accountId)
        startTradeDate = self.__config.getOption(CONF_ULTRAFINANCE_SECTION, CONF_START_TRADE_DATE)
        if startTradeDate:
            startTradeDate = int(startTradeDate)
            timePositions = [tp for tp in timePositions if tp[0] >= startTradeDate]

        self.__mCalculator.calculate(self.__symbols, timePositions, self.__tickFeeder.iTimePositionDict)

        self.__tradingEngine.stop()
        thread.join(timeout=240)
        LOG.info("Stop backtest for %s" % self.__symbols)


    def _printResult(self):
        ''' print result'''
        account = self.__accountManager.getAccount(self.__accountId)
        self.__accounts.append(account)
        LOG.info("account %s" % account)
        LOG.debug([str(order) for order in account.orderHistory])
        LOG.debug("account position %s" % self.__accountManager.getAccountPostions(self.__accountId))


    def runTest(self):
        ''' run one test '''
        self._setup()
        self._execute()
        self._printResult()
Ejemplo n.º 6
0
class TestRunner(object):
    """ back testing """

    CASH = 100000  #  0.1 million to start

    def __init__(self, config, mCalculator, symbol):
        self.__accountManager = AccountManager()
        self.__tickFeeder = TickFeeder()
        self.__tradingCenter = TradingCenter()
        self.__tradingEngine = TradingEngine()
        self.__indexHelper = IndexHelper()
        self.__history = History()
        self.__saver = None
        self.__symbol = symbol
        self.__config = config
        self.__mCalculator = mCalculator

    def _setup(self):
        """ setup """
        self._setupTradingCenter()
        self._setupTickFeeder()
        self._setupSaver()

        # wire things together
        self._setupStrategy()
        self.__tickFeeder.tradingCenter = self.__tradingCenter
        self.__tradingEngine.tickProxy = self.__tickFeeder
        self.__tradingEngine.orderProxy = self.__tradingCenter
        self.__tradingCenter.accountManager = self.__accountManager
        self.__tradingEngine.saver = self.__saver
        self.__accountManager.saver = self.__saver

    def _setupTradingCenter(self):
        self.__tradingCenter.start = 0
        self.__tradingCenter.end = None

    def _setupTickFeeder(self):
        """ setup tickFeeder"""
        self.__tickFeeder.indexHelper = self.__indexHelper
        self.__tickFeeder.hisotry = self.__history
        # set source dam
        sDam = self._createDam(self.__symbol)
        self.__tickFeeder.addSource(sDam)

        # set index dam
        iSymbol = self.__config.getOption(CONF_APP_MAIN, CONF_INDEX)
        iDam = self._createDam(iSymbol)
        self.__tickFeeder.setIndexDam(iDam)

    def _createDam(self, symbol):
        """ setup Dam"""
        damName = self.__config.getOption(CONF_INPUT_SECTION, CONF_DAM)
        setting = self.__config.getSection(CONF_INPUT_SECTION)
        dam = DAMFactory.createDAM(damName, setting)
        dam.setSymbol(symbol)

        return dam

    def _setupSaver(self):
        """ setup Saver """
        saverName = self.__config.getOption(CONF_OUTPUT_SECTION, CONF_SAVER)
        setting = self.__config.getSection(CONF_OUTPUT_SECTION)
        if saverName:
            self.__saver = StateSaverFactory.createStateSaver(
                saverName,
                setting,
                "%s_%s" % (self.__symbol, self.__config.getOption(CONF_STRATEGY, CONF_STRATEGY_NAME)),
            )

    def _setupStrategy(self):
        """ setup tradingEngine"""
        strategy = StrategyFactory.createStrategy(
            self.__config.getOption(CONF_STRATEGY, CONF_STRATEGY_NAME), self.__config.getSection(CONF_STRATEGY)
        )
        strategy.setSymbols([self.__symbol])
        strategy.indexHelper = self.__indexHelper
        strategy.history = self.__history

        # associate account
        accountId = self.__accountManager.addAccount(Account(BackTester.CASH, 0))
        strategy.accountId = accountId

        # register on trading engine
        strategy.tradingEngine = self.__tradingEngine
        self.__tradingEngine.register(strategy)

    def _execute(self):
        """ run backtest """
        LOG.info("Running backtest for %s" % self.__symbol)
        # start trading engine
        thread = Thread(target=self.__tradingEngine.runListener, args=())
        thread.setDaemon(True)
        thread.start()

        # start tickFeeder
        self.__tickFeeder.execute()
        self.__tradingEngine.stop()
        thread.join(timeout=60)

    def _printResult(self):
        """ print result"""
        for account in self.__accountManager.getAccounts():
            accountId = account.accountId
            LOG.info("account %s" % account)
            LOG.debug([str(order) for order in account.orderHistory])
            LOG.debug("account position %s" % self.__accountManager.getAccountPostions(accountId))
            LOG.info(self.__mCalculator.formatMetrics(self.__accountManager.getAccountPostions(accountId)))

    def runTest(self):
        """ run one test """
        self._setup()
        self._execute()
        self._printResult()
Ejemplo n.º 7
0
class TestRunner(object):
    ''' back testing '''
    def __init__(self, config, metricManager, accounts, symbols, startTickDate,
                 endTradeDate, cash):
        self.__accountManager = AccountManager()
        self.__accountId = None
        self.__startTickDate = startTickDate
        self.__endTradeDate = endTradeDate
        self.__tickFeeder = TickFeeder(start=startTickDate, end=endTradeDate)
        self.__tradingCenter = TradingCenter()
        self.__tradingEngine = TradingEngine()
        self.__indexHelper = IndexHelper()
        self.__accounts = accounts
        self.__history = History()
        self.__saver = None
        self.__symbols = symbols
        self.__config = config
        self.__metricManager = metricManager
        self.__cash = cash

    def _setup(self):
        ''' setup '''
        self._setupTradingCenter()
        self._setupTickFeeder()
        self._setupSaver()

        #wire things together
        self._setupStrategy()
        self.__tickFeeder.tradingCenter = self.__tradingCenter
        self.__tradingEngine.tickProxy = self.__tickFeeder
        self.__tradingEngine.orderProxy = self.__tradingCenter
        self.__tradingCenter.accountManager = self.__accountManager
        self.__tradingEngine.saver = self.__saver
        self.__tickFeeder.saver = self.__saver
        self.__accountManager.saver = self.__saver

    def _setupTradingCenter(self):
        self.__tradingCenter.start = 0
        self.__tradingCenter.end = None

    def _setupTickFeeder(self):
        ''' setup tickFeeder'''
        self.__tickFeeder.indexHelper = self.__indexHelper
        self.__tickFeeder.setSymbols(self.__symbols)
        self.__tickFeeder.setDam(self._createDam(
            ""))  # no need to set symbol because it's batch operation

        iSymbol = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                          CONF_INDEX)
        self.__tickFeeder.setIndexSymbol(iSymbol)

    def _createDam(self, symbol):
        ''' setup Dam'''
        damName = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                          CONF_INPUT_DAM)
        inputDb = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                          CONF_INPUT_DB)
        dam = DAMFactory.createDAM(damName, {'db': inputDb})
        dam.setSymbol(symbol)

        return dam

    def _setupSaver(self):
        ''' setup Saver '''
        saverName = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                            CONF_SAVER)
        outputDbPrefix = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                                 CONF_OUTPUT_DB_PREFIX)
        if saverName:
            self.__saver = StateSaverFactory.createStateSaver(
                saverName, {
                    'db':
                    outputDbPrefix + getBackTestResultDbName(
                        self.__symbols,
                        self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                                CONF_STRATEGY_NAME),
                        self.__startTickDate, self.__endTradeDate)
                })

    def _setupStrategy(self):
        ''' setup tradingEngine'''
        strategy = StrategyFactory.createStrategy(
            self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                    CONF_STRATEGY_NAME),
            self.__config.getSection(CONF_ULTRAFINANCE_SECTION))
        strategy.setSymbols(self.__symbols)
        strategy.history = self.__history

        #associate account
        self.__accountId = self.__accountManager.createAccount(self.__cash)
        strategy.accountId = self.__accountId
        strategy.accountManager = self.__accountManager

        #register on trading engine
        strategy.tradingEngine = self.__tradingEngine
        self.__tradingEngine.register(strategy)

    def _execute(self):
        ''' run backtest '''
        LOG.info("Running backtest for %s" % self.__symbols)
        #start trading engine
        thread = Thread(target=self.__tradingEngine.runListener, args=())
        thread.setDaemon(False)
        thread.start()

        #start tickFeeder
        self.__tickFeeder.execute()
        self.__tickFeeder.complete()

        timePositions = self.__accountManager.getAccountPostions(
            self.__accountId)
        startTradeDate = self.__config.getOption(CONF_ULTRAFINANCE_SECTION,
                                                 CONF_START_TRADE_DATE)
        if startTradeDate:
            startTradeDate = int(startTradeDate)
            timePositions = [
                tp for tp in timePositions if tp[0] >= startTradeDate
            ]

        #get and save metrics
        result = self.__metricManager.calculate(
            self.__symbols, timePositions, self.__tickFeeder.iTimePositionDict)
        account = self.__accountManager.getAccount(self.__accountId)
        self.__saver.writeMetrics(result[BasicMetric.START_TIME],
                                  result[BasicMetric.END_TIME],
                                  result[BasicMetric.MIN_TIME_VALUE][1],
                                  result[BasicMetric.MAX_TIME_VALUE][1],
                                  result[BasicMetric.SRATIO],
                                  result[BasicMetric.MAX_DRAW_DOWN][1],
                                  result[BasicMetric.R_SQUARED],
                                  account.getTotalValue(), account.holdings)

        #write to saver
        LOG.debug("Writing state to saver")
        self.__saver.commit()

        self.__tradingEngine.stop()
        thread.join(timeout=240)

    def _printResult(self):
        ''' print result'''
        account = self.__accountManager.getAccount(self.__accountId)
        self.__accounts.append(account)
        LOG.info("account %s" % account)
        LOG.debug([str(order) for order in account.orderHistory])
        LOG.debug("account position %s" %
                  self.__accountManager.getAccountPostions(self.__accountId))

    def runTest(self):
        ''' run one test '''
        self._setup()
        self._execute()
        self._printResult()
Ejemplo n.º 8
0
class TestRunner(object):
    ''' back testing '''
    CASH = 100000  #  0.1 million to start

    def __init__(self, config, mCalculator, symbol):
        self.__accountManager = AccountManager()
        self.__tickFeeder = TickFeeder()
        self.__tradingCenter = TradingCenter()
        self.__tradingEngine = TradingEngine()
        self.__indexHelper = IndexHelper()
        self.__history = History()
        self.__saver = None
        self.__symbol = symbol
        self.__config = config
        self.__mCalculator = mCalculator

    def _setup(self):
        ''' setup '''
        self._setupTradingCenter()
        self._setupTickFeeder()
        self._setupSaver()

        #wire things together
        self._setupStrategy()
        self.__tickFeeder.tradingCenter = self.__tradingCenter
        self.__tradingEngine.tickProxy = self.__tickFeeder
        self.__tradingEngine.orderProxy = self.__tradingCenter
        self.__tradingCenter.accountManager = self.__accountManager
        self.__tradingEngine.saver = self.__saver
        self.__accountManager.saver = self.__saver

    def _setupTradingCenter(self):
        self.__tradingCenter.start = 0
        self.__tradingCenter.end = None

    def _setupTickFeeder(self):
        ''' setup tickFeeder'''
        self.__tickFeeder.indexHelper = self.__indexHelper
        self.__tickFeeder.hisotry = self.__history
        #set source dam
        sDam = self._createDam(self.__symbol)
        self.__tickFeeder.addSource(sDam)

        #set index dam
        iSymbol = self.__config.getOption(CONF_APP_MAIN, CONF_INDEX)
        iDam = self._createDam(iSymbol)
        self.__tickFeeder.setIndexDam(iDam)

    def _createDam(self, symbol):
        ''' setup Dam'''
        damName = self.__config.getOption(CONF_INPUT_SECTION, CONF_DAM)
        setting = self.__config.getSection(CONF_INPUT_SECTION)
        dam = DAMFactory.createDAM(damName, setting)
        dam.setSymbol(symbol)

        return dam

    def _setupSaver(self):
        ''' setup Saver '''
        saverName = self.__config.getOption(CONF_OUTPUT_SECTION, CONF_SAVER)
        setting = self.__config.getSection(CONF_OUTPUT_SECTION)
        if saverName:
            self.__saver = StateSaverFactory.createStateSaver(
                saverName, setting, "%s_%s" %
                (self.__symbol,
                 self.__config.getOption(CONF_STRATEGY, CONF_STRATEGY_NAME)))

    def _setupStrategy(self):
        ''' setup tradingEngine'''
        strategy = StrategyFactory.createStrategy(
            self.__config.getOption(CONF_STRATEGY, CONF_STRATEGY_NAME),
            self.__config.getSection(CONF_STRATEGY))
        strategy.setSymbols([self.__symbol])
        strategy.indexHelper = self.__indexHelper
        strategy.history = self.__history

        #associate account
        accountId = self.__accountManager.createAccount(BackTester.CASH)
        strategy.accountId = accountId

        #register on trading engine
        strategy.tradingEngine = self.__tradingEngine
        self.__tradingEngine.register(strategy)

    def _execute(self):
        ''' run backtest '''
        LOG.info("Running backtest for %s" % self.__symbol)
        #start trading engine
        thread = Thread(target=self.__tradingEngine.runListener, args=())
        thread.setDaemon(True)
        thread.start()

        #start tickFeeder
        self.__tickFeeder.execute()
        self.__tradingEngine.stop()
        thread.join(timeout=60)

    def _printResult(self):
        ''' print result'''
        for account in self.__accountManager.getAccounts():
            accountId = account.accountId
            LOG.info("account %s" % account)
            LOG.debug([str(order) for order in account.orderHistory])
            LOG.debug("account position %s" %
                      self.__accountManager.getAccountPostions(accountId))
            LOG.info(
                self.__mCalculator.formatMetrics(
                    self.__accountManager.getAccountPostions(accountId)))

    def runTest(self):
        ''' run one test '''
        self._setup()
        self._execute()
        self._printResult()