Esempio n. 1
0
class Trader(object):
    def __init__(self, exchangeClient):
        self.exchangeClient = exchangeClient

        self.marketDataManager = MarketDataManager(self.exchangeClient)
        self.portfolioManager = PortfolioManager(self.exchangeClient)

        self.exchangeDataListener = BinanceDataListener(
            self.exchangeClient, self.marketDataManager, self.portfolioManager)

        self.bigThree = BigThree('XLM', 'ETH', self.exchangeClient,
                                 self.marketDataManager, self.portfolioManager)

        self.creten = StrategyManager()
        self.creten.addStrategy(self.bigThree)

        # initialize data
        self.marketDataManager.init('XLMETH', CretenInterval.INTERVAL_1MINUTE)
        self.portfolioManager.init()

        self.exchangeDataListener.registerPortfolioListener()
        self.exchangeDataListener.registerKlineListener(
            "XLMETH", CretenInterval.INTERVAL_1MINUTE,
            self.creten.processKline)

    def run(self):
        self.exchangeDataListener.start()
Esempio n. 2
0
class RealTimeSimulator(CretenEngine):
	def __init__(self, exchangeClient, inputConfRaw, cretenExecId):
		super(RealTimeSimulator, self).__init__()

		self.exchangeClient = exchangeClient
		self.inputConfRaw = inputConfRaw
		self.cretenExecId = cretenExecId

		self.marketDataManager = MarketDataManager(self.exchangeClient)
		self.marketRulesManager = MarketRulesManager(self.exchangeClient)
		self.portfolioManager = PortfolioManager(self.exchangeClient)
		self.strategyManager = StrategyManager()
		self.orderManager = OrderManager(self.exchangeClient, self.marketRulesManager, self.strategyManager)

		self.exchangeDataListener = BinanceSimulationDataListener(self.exchangeClient, self.marketDataManager, self.marketRulesManager, self.portfolioManager, self.orderManager)
		self.exchangeEventSimulator = ExchangeEventSimulator(self.marketDataManager, self.orderManager, self.portfolioManager, self.exchangeDataListener)

	def run(self):
		self.log.info('')
		self.log.info('Starting real time simulation')

		inputConf = json.loads(self.inputConfRaw)
		jsonschema.validate(inputConf, RealtimetestSchema.schema)

		with Db.session_scope():
			ced = ExecManager.createCretenExecDetl(self.cretenExecId, inputConf['realtimetest'])
			cretenExecDetlId = ced.creten_exec_detl_id

		self.exchangeDataListener.setCretenExecDetlId(cretenExecDetlId)
		self.exchangeEventSimulator.setCretenExecDetlId(cretenExecDetlId)

		cretenInterval = getattr(CretenInterval, "INTERVAL_" + inputConf['realtimetest']['interval'])

		for pairConf in inputConf['realtimetest']['pairs']:
			pair = Pair(baseAsset = pairConf[0], quoteAsset = pairConf[1])

			self.marketDataManager.init(pair, cretenInterval)
			self.marketRulesManager.init(pair.getSymbol())

			strategyExecutor = StrategyExecutor()
			self.strategyManager.addStrategyExecutor(strategyExecutor)

			for strategyConf in inputConf['realtimetest']['strategies']:
				strategy = StrategyFactory.getStrategy(strategyConf, pair, cretenExecDetlId,
				                                       self.exchangeClient, self.marketDataManager,
				                                       self.marketRulesManager, self.portfolioManager,
				                                       self.orderManager)
				strategyExecutor.addStrategy(strategy)

			self.exchangeDataListener.registerCandleListener(pair, cretenInterval, [self.exchangeEventSimulator.simulateEvent, strategyExecutor.execute])

		self.portfolioManager.init()
		self.exchangeDataListener.registerUserDataListener()

		self.exchangeDataListener.start()

		while True:
			time.sleep(1)
Esempio n. 3
0
	def __init__(self, exchangeClient, inputConfRaw, cretenExecId):
		super(BackTester, self).__init__()

		self.exchangeClient = exchangeClient
		self.inputConfRaw = inputConfRaw
		self.cretenExecId = cretenExecId

		self.marketDataManager = MarketDataManager(self.exchangeClient)
		self.marketRulesManager = MarketRulesManager(self.exchangeClient)
		self.portfolioManager = PortfolioManager(self.exchangeClient)
		self.strategyManager = StrategyManager()
		self.orderManager = OrderManager(self.exchangeClient, self.marketRulesManager, self.strategyManager)

		self.exchangeDataListener = BacktestDataListener(self.exchangeClient, self.marketDataManager, self.marketRulesManager, self.portfolioManager, self.orderManager)
		self.exchangeEventSimulator = ExchangeEventSimulator(self.marketDataManager, self.orderManager, self.portfolioManager, self.exchangeDataListener)
Esempio n. 4
0
    def test_macd_rsi_adx_atr_obv_bb(self):
        tickers = ["TSLA", "SNAP"]

        strategies = [StrategyCII()]

        manager = \
            StrategyManager(
                strategies=strategies,
                tickers=tickers
            )

        stocks_per_strategy = manager.stocks_per_strategy
        for stock_per_strategy in stocks_per_strategy:
            for stock in stocks_per_strategy[stock_per_strategy]:
                print(stock.price_info)
                plotter = Plotter(period=1000)
                plotter.plot_stock(stock, collapse_indicators=False)

        print("Analysis has been run")

        if DEVELOPMENT == True:
            plt.show()

        test_date = "14/07/2020"
        test_df = stocks_per_strategy[StrategyCII.name][0].price_info
        ticker = "TSLA"
        values = [318.0, 286.2, 311.2, 303.359, 117090500.0]

        i = 0
        for metric in ["High", "Low", "Open", "Close", "Volume"]:
            value = TestIndicators.truncate(test_df[ticker][metric].loc[
                test_df[ticker][metric].index == datetime.datetime.strptime(
                    test_date, "%d/%m/%Y")].iloc[0])
            assert value == TestIndicators.truncate(values[i]), value
            i += 1
Esempio n. 5
0
class BackTester(CretenEngine):
	def __init__(self, exchangeClient, inputConfRaw, cretenExecId):
		super(BackTester, self).__init__()

		self.exchangeClient = exchangeClient
		self.inputConfRaw = inputConfRaw
		self.cretenExecId = cretenExecId

		self.marketDataManager = MarketDataManager(self.exchangeClient)
		self.marketRulesManager = MarketRulesManager(self.exchangeClient)
		self.portfolioManager = PortfolioManager(self.exchangeClient)
		self.strategyManager = StrategyManager()
		self.orderManager = OrderManager(self.exchangeClient, self.marketRulesManager, self.strategyManager)

		self.exchangeDataListener = BacktestDataListener(self.exchangeClient, self.marketDataManager, self.marketRulesManager, self.portfolioManager, self.orderManager)
		self.exchangeEventSimulator = ExchangeEventSimulator(self.marketDataManager, self.orderManager, self.portfolioManager, self.exchangeDataListener)

	def initPortfolio(self, portfolio):
		self.portfolioManager.clear()

		for position in portfolio:
			self.portfolioManager.addPosition(Position(position['asset'], position['quantity']))

	def _revertOpenTrades(self, strategyExecId):
		trades = TradeManager.getAllTrades(strategyExecId = strategyExecId, tradeState = REF_TRADE_STATE.OPENED)
		self.log.debug('')
		self.log.debug('Open trades to be reversed: ' + str(len(trades)))

		for trade in trades:
			self.log.debug('Reverting open trade ' + str(trade.trade_id))
			orders = TradeManager.getAllOrders(tradeId = trade.trade_id, orderState = [REF_ORDER_STATE.FILLED, REF_ORDER_STATE.PARTIALLY_FILLED])

			for dbOrder in orders:
				order = Order()
				order.setFromEntity(dbOrder)

				self.log.debug('Reverting order ' + str(order))

				basePosition = self.portfolioManager.getPosition(trade.base_asset)
				quotePosition = self.portfolioManager.getPosition(trade.quote_asset)

				if order.getOrderSide() == OrderSide.BUY:
					basePosition.setFree(basePosition.getFree() - float(order.getQty()))
					quotePosition.setFree(quotePosition.getFree() + float(order.getQty() * order.getPrice()))
				else:
					basePosition.setFree(basePosition.getFree() + float(order.getQty()))
					quotePosition.setFree(quotePosition.getFree() - float(order.getQty() * order.getPrice()))

				# transaction nesting is required to ensure that session invalidation in below method does not
				# detach objects (trades and orders) loaded and iterated within this method
				Db.Session().begin_nested()
				self.exchangeDataListener.processPortfolioUpdate(basePosition)
				Db.Session().begin_nested()
				self.exchangeDataListener.processPortfolioUpdate(quotePosition)

	def _showPerformance(self, strategyExecId):
		self.log.info('')
		self.log.info(Style.BRIGHT + Back.BLUE + Fore.LIGHTWHITE_EX + 'PERFORMANCE' + Style.RESET_ALL)

		perf = StrategyPerformance(strategyExecId)
		perf.printStats()

	def run(self):
		with Timer("BackTester::run", endDebugLevel = 'INFO'):
			inputConf = json.loads(self.inputConfRaw)
			jsonschema.validate(inputConf, BacktestSchema.schema)

			# generate backtest configuration based on the input file
			if 'backtestConf' in inputConf:
				for conf in inputConf['backtestConf']:
					backtestConf = BacktestGenerator.getBacktest(conf)

					self.log.debug("Generated backtest config: " + str(backtestConf))
					self.log.debug('')

					if 'backtest' not in inputConf:
						inputConf['backtest'] = []
					inputConf['backtest'] += backtestConf

			for conf in inputConf['backtest']:
				with Db.session_scope():
					ced = ExecManager.createCretenExecDetl(self.cretenExecId, conf)
					cretenExecDetlId = ced.creten_exec_detl_id

				self.exchangeDataListener.setCretenExecDetlId(cretenExecDetlId)
				self.exchangeEventSimulator.setCretenExecDetlId(cretenExecDetlId)

				for pairConf in conf['pairs']:
					pair = Pair(baseAsset = pairConf[0], quoteAsset = pairConf[1])

					self.log.info('')
					self.log.info('Loading market rules')
					self.marketRulesManager.init([pair.getSymbol()])
					self.log.info('Loading market rules completed')
					self.log.info('')

					for strategyConf in conf['strategies']:
						strategy = StrategyFactory.getStrategy(strategyConf, pair, cretenExecDetlId,
						                                       self.exchangeClient, self.marketDataManager,
						                                       self.marketRulesManager, self.portfolioManager, self.orderManager)

						self.strategyManager.reset()
						strategyExecutor = StrategyExecutor()
						strategyExecutor.addStrategy(strategy)
						self.strategyManager.addStrategyExecutor(strategyExecutor)

						startTimestamp = datetime.strptime(conf['start_tmstmp'], BACKTEST_TMSTMP_FORMAT)
						endTimestamp = datetime.strptime(conf['end_tmstmp'], BACKTEST_TMSTMP_FORMAT)

						self.log.info('Backtesting period: ' + str(startTimestamp) + ' - ' + str(endTimestamp))
						self.log.info('')

						self.exchangeDataListener.resetCandleListener()
						self.exchangeDataListener.resetUserDataListener()
						self.marketDataManager.removeAllCandles()
						self.orderManager.reset()

						self.log.info('Initialize portfolio:')
						for position in conf['portfolio']:
							self.log.info('\t' + position['asset'] + ': ' + str(position['quantity']))
						self.log.info('')
						self.initPortfolio(conf['portfolio'])

						interval = getattr(CretenInterval, "INTERVAL_" + conf['interval'])
						self.exchangeDataListener.init(startTimestamp, endTimestamp)
						self.exchangeDataListener.registerCandleListener(pair, interval, [self.exchangeEventSimulator.simulateEvent, strategyExecutor.execute])

						self.exchangeDataListener.start()

						# revert open trades in order not to interfere with overall statistics
						self._revertOpenTrades(strategy.getStrategyExecId())

						# evaluate results
						pos = self.portfolioManager.getPosition(pair.getQuoteAsset())
						self.log.info('')
						self.log.info('Final balance for ' + pair.getQuoteAsset() + ': ' + str(pos.getFree()))

						# calculate metrics
						self._showPerformance(strategy.getStrategyExecId())

				ced.exec_state = REF_EXEC_STATE.FINISHED
				with Db.session_scope():
					Db.Session().add(ced)