def setUp(self): """ Define static instance variables and create redis/mysql objects as well as working test directory """ self.pairs = [pairs] self.startdate = startdate self.sum = xsum self.max = xmax self.min = xmin self.drawup = drawup self.drawdown = drawdown os.system("configstore package process_templates {} /etc".format( config_env)) config.create_config() # reload config self.days = days self.outputdir = "/tmp/test_data" self.intervals = [interval] self.logger = get_logger(__name__) self.logger.info("Setting up environment") self.redis = Redis(interval=self.intervals[0], test=True, test_data=True) self.dbase = Mysql(test=True, interval=self.intervals[0]) if not os.path.exists(self.outputdir): os.mkdir(self.outputdir)
def setUp(self): """ Define static instance variables and create redis/mysql objects as well as working test directory """ self.pairs = [pairs] self.startdate = startdate self.sum = xsum self.max = xmax self.min = xmin self.days = 15 self.outputdir = "/tmp/test_data" self.intervals = ["1h"] self.logger = get_logger(__name__) self.logger.info("Setting up environment") self.redis = Redis(interval=self.intervals[0], test=True, db=0) self.dbase = Mysql(test=True, interval=self.intervals[0]) if not os.path.exists(self.outputdir): os.mkdir(self.outputdir)
def test_high_price(self): """ Test setting, retrieving, deleting high price in redis """ pair = 'ETHBTC' interval = '1m' price = 12 redis = Redis() result = redis.get_high_price(pair, interval) self.assertIsNone(result) redis.put_high_price(pair, interval, price) result = redis.get_high_price(pair, interval) self.assertEqual(result, price) redis.del_high_price(pair, interval) result = redis.get_high_price(pair, interval) self.assertIsNone(result) del redis
def test_long_take_profit(self): """ Test stop loss for long trades """ config_env = 'unit/scalp' os.system("configstore package process_templates {} /etc".format(config_env)) config.create_config() redis = Redis(test=True, test_data=True) # current_high > addperc(profit_perc, open_price) 10% result = redis._Redis__get_take_profit(current_price=200, current_high=500, open_price=20) self.assertTrue(result) # current_high > addperc(profit_perc, open_price) 10% result = redis._Redis__get_take_profit(current_price=10, current_high=500, open_price=20) self.assertTrue(result) # current_high > addperc(profit_perc, open_price) 10% result = redis._Redis__get_take_profit(current_price=10, current_high=1, open_price=20) self.assertFalse(result) config_env = 'unit/scalp/alt' os.system("configstore package process_templates {} /etc".format(config_env)) config.create_config() redis = Redis(test=True, test_data=False) # turn off immediate take profit- changes check to current_price # current_price > addperc(profit_perc, open_price) 10% result = redis._Redis__get_take_profit(current_price=10, current_high=1, open_price=20) self.assertFalse(result) # current_price > addperc(profit_perc, open_price) 10% result = redis._Redis__get_take_profit(current_price=100, current_high=1, open_price=20) self.assertTrue(result)
def test_long_stop_loss(self): """ Test stop loss for long trades """ config_env = 'unit/scalp' os.system("configstore package process_templates {} /etc".format(config_env)) config.create_config() redis = Redis(test=True, test_data=True) # current_low < open_price - stop_perc 10% result = redis._Redis__get_stop_loss(current_price=100, current_low=100, open_price=400) self.assertTrue(result) # current_low < open_price - stop_perc 10% result = redis._Redis__get_stop_loss(current_price=359, current_low=500, open_price=400) self.assertFalse(result) config_env = 'unit/scalp/alt' redis = Redis(test=True, test_data=False) # turn off immediate stop - changes check to current_price # current_price < open_price - stop_perc 10% result = redis._Redis__get_stop_loss(current_price=364, current_low=500, open_price=400) self.assertFalse(result) # current_price < open_price - stop_perc 10% result = redis._Redis__get_stop_loss(current_price=200, current_low=500, open_price=400) self.assertTrue(result) # no open price - return False result = redis._Redis__get_stop_loss(current_price=200, current_low=500, open_price='') self.assertFalse(result)
def test_get_items(self): """ Test putting/getting items to/from redis """ redis = Redis() items = redis.get_items('ABCDEF', '10m') self.assertEqual(len(items), 0) redis.redis_conn('ETHBTC', '1m', {"a": 1}, time.time()) items = redis.get_items('ABCDEF', '10m') self.assertEqual(len(items), 0) items = redis.get_items('ETHBTC', '1m') self.assertEqual(len(items), 1) redis.redis_conn('ETHBTC', '1m', {"a": 1}, time.time()) items = redis.get_items('ETHBTC', '1m') self.assertEqual(len(items), 2) for _ in range(10): redis.redis_conn('ETHBNB', '1m', {"a": 1}, time.time()) time.sleep(1) items = redis.get_items('ETHBNB', '1m') self.assertEqual(len(items), 10) redis.clear_all() del redis
def test_long_trailing_stop_loss(self): """ Test trailing stop loss for long trades """ config_env = 'unit/scalp' os.system("configstore package process_templates {} /etc".format(config_env)) config.create_config() redis = Redis(test=True, test_data=True) # current_high <= subperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=100, high_price=500, low_price=0, current_low=0, current_high=2, open_price=400) self.assertTrue(result) # current_price <= subperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=1000, high_price=500, low_price=0, current_low=0, current_high=2000, open_price=4000) self.assertFalse(result) # current_price <= subperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=90, high_price=500, low_price=0, current_low=0, current_high=2000, open_price=4000) self.assertTrue(result) # current_price <= subperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=90, high_price='', low_price=0, current_low=0, current_high=2000, open_price=4000) print(result) # False self.assertFalse(result) redis = Redis(test=True, test_data=False) # current_price <= subperc(trailing_perc, high_price) # and current_price > addperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=90, high_price=500, low_price=0, current_low=0, current_high=2000, open_price=4000) self.assertFalse(result) # current_price <= subperc(trailing_perc, high_price) # and current_price > addperc(trailing_perc, high_price) result = redis._Redis__get_trailing_stop(current_price=3000, high_price=500, low_price=0, current_low=0, current_high=2000, open_price=4000) self.assertFalse(result) # current_price <= subperc(trailing_perc, high_price) 498 <= (500-0.1%) ~499.5 # and current_price > addperc(trailing_start, open_price) result = redis._Redis__get_trailing_stop(current_price=498, high_price=500, low_price=0, current_low=0, current_high=4000, open_price=200) self.assertTrue(result) # current_price <= subperc(trailing_perc, high_price) 498 <= (500-0.1%) ~499.5 # and current_price > addperc(trailing_start, open_price) result = redis._Redis__get_trailing_stop(current_price=498, high_price=500, low_price=0, current_low=0, current_high=4000, open_price=300) self.assertTrue(result) config_env = 'unit/scalp/alt' os.system("configstore package process_templates {} /etc".format(config_env)) config.create_config() #changes check to current_high (from current_price) redis = Redis(test=True, test_data=True) # current_high <= subperc(trailing_perc, high_price) # and current_price > addperc(trailing_start, open_price) result = redis._Redis__get_trailing_stop(current_price=498, high_price=500, low_price=0, current_low=0, current_high=4000, open_price=300) self.assertFalse(result) # current_high <= subperc(trailing_perc, high_price) # and current_price > addperc(trailing_start, open_price) result = redis._Redis__get_trailing_stop(current_price=498, high_price=600, low_price=0, current_low=0, current_high=4000, open_price=300) self.assertFalse(result) ####### ####### # current_high <= subperc(trailing_perc, high_price) # and current_price > addperc(trailing_start, open_price) result = redis._Redis__get_trailing_stop(current_price=498, high_price=4000, low_price=0, current_low=0, current_high=600, open_price=300) self.assertTrue(result)
def test_long_drawdown(self): """ Test drawdown """ pair = 'BTCUSDT' redis = Redis(test=True, test_data=True) redis.clear_all() # Initial data - no intial drawdown data = { 'closeTime': [1], 'open': [100], 'high': [200], 'low': [300], 'close': [100] } redis.update_drawdown(pair, self.create_series(data), 'open') self.assertEqual(abs(redis.get_drawdown(pair)), 0) # Close price 90% lower data = { 'closeTime': [2], 'open': [3], 'high': [4], 'low': [10], 'close': [10] } redis.update_drawdown(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawdown(pair)), 90) # Close price higher - no change in drawdown data = { 'closeTime': [3], 'open': [100], 'high': [200], 'low': [300], 'close': [100] } redis.update_drawdown(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawdown(pair)), 90) # Close price higher - no change in drawdown data = { 'closeTime': [4], 'open': [200], 'high': [300], 'low': [500], 'close': [200] } redis.update_drawdown(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawdown(pair)), 90) # Close price 5% lower than previos low data = { 'closeTime': [5], 'open': [200], 'high': [300], 'low': [5], 'close': [2] } redis.update_drawdown(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawdown(pair)), 95) # End of trade - 0 drawdown redis.rm_drawdown(pair) self.assertEqual(abs(redis.get_drawdown(pair)), 0) # regarless of price, drawdown is 0 as open arg not specified data = { 'closeTime': [6], 'open': [200], 'high': [300], 'low': [5], 'close': [2] } redis.update_drawdown(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawdown(pair)), 0)
def test_long_drawup(self): """ Test drawup """ pair = 'BTCUSDT' redis = Redis(test=True, test_data=True) redis.clear_all() # Initial data - no intial drawup data = { 'closeTime': [1], 'open': [100], 'high': [200], 'low': [300], 'close': [100] } redis.update_drawup(pair, self.create_series(data), 'open') self.assertEqual(abs(redis.get_drawup(pair)['perc']), 0) # Close price 90% lower, no change in drawup data = { 'closeTime': [2], 'open': [3], 'high': [4], 'low': [10], 'close': [10] } redis.update_drawup(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawup(pair)['perc']), 0) # Close price equal to opening price, no change in drawup data = { 'closeTime': [3], 'open': [100], 'high': [200], 'low': [300], 'close': [100] } redis.update_drawup(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawup(pair)['perc']), 0) # Double initial price data = { 'closeTime': [4], 'open': [200], 'high': [300], 'low': [500], 'close': [200] } redis.update_drawup(pair, self.create_series(data)) self.assertEqual(float(redis.get_drawup(pair)['price']), 200) # Failing here self.assertEqual(abs(redis.get_drawup(pair)['perc']), 100) # ? data = { 'closeTime': [4], 'open': [800], 'high': [300], 'low': [500], 'close': [500] } redis.update_drawup(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawup(pair)['perc']), 400) # End of trade - 0 drawdup redis.rm_drawup(pair) self.assertEqual(abs(redis.get_drawup(pair)['perc']), 0) # regarless of price, drawdup is 0 as open arg not specified data = { 'closeTime': [6], 'open': [200], 'high': [300], 'low': [5], 'close': [2] } redis.update_drawup(pair, self.create_series(data)) self.assertEqual(abs(redis.get_drawup(pair)['perc']), 0)
class UnitRun(OrderedTest): """ Sequential unit tests which download data from binance, run process, and collect results Items checked: * pickle file * mysql/redis connection * results """ def setUp(self): """ Define static instance variables and create redis/mysql objects as well as working test directory """ self.pairs = [pairs] self.startdate = startdate self.sum = xsum self.max = xmax self.min = xmin self.days = 15 self.outputdir = "/tmp/test_data" self.intervals = ["1h"] self.logger = get_logger(__name__) self.logger.info("Setting up environment") self.redis = Redis(interval=self.intervals[0], test=True, db=0) self.dbase = Mysql(test=True, interval=self.intervals[0]) if not os.path.exists(self.outputdir): os.mkdir(self.outputdir) def step_1(self): """ Step 1 - get test data """ self.logger.info("Getting test data") get_data(self.startdate, self.intervals, self.pairs, self.days, self.outputdir, extra=200) filename = self.outputdir + '/' + self.pairs[ 0] + '_' + self.intervals[0] + '.p' self.logger.info("Filename: %s", filename) assert os.path.exists(filename) == 1 def step_2(self): """ Step 2 - execute test run """ self.logger.info("Executing serial test run") main_indicators = config.main.indicators.split() serial_test(self.pairs, self.intervals, self.outputdir, main_indicators) assert True def step_3(self): """ Step 3 - collect and compare data """ self.logger.info("Comparing mysql data") db_sum = self.dbase.fetch_sql_data("select sum(perc) from profit", header=False)[0][0] db_min = self.dbase.fetch_sql_data("select min(perc) from profit", header=False)[0][0] db_max = self.dbase.fetch_sql_data("select max(perc) from profit", header=False)[0][0] self.logger.info("DB_SUM: %s", db_sum) self.logger.info("DB_MAX: %s", db_max) self.logger.info("DB_MIN: %s", db_min) self.assertGreaterEqual(float(db_sum), self.sum) self.assertGreaterEqual(float(db_max), self.max) self.assertGreaterEqual(float(db_min), self.min) def tearDown(self): """Cleanup DB and files""" self.redis.clear_all() self.dbase.delete_data() del self.redis del self.dbase shutil.rmtree(self.outputdir)
def test_get_action(self): """ Test get action method """ redis = Redis(interval="4h", test_data=True, test=True) dbase = Mysql(test=True, interval="4h") redis.clear_all() dbase.delete_data() action = redis.get_action('BTCUSDT', '4h') self.assertEqual(action[0], 'HOLD') self.assertEqual(action[1], 'Not enough data') self.assertEqual(action[2], 0) self.assertEqual(action[4]['buy'], []) self.assertEqual(action[4]['sell'], []) redis.clear_all() dbase.delete_data() self.insert_data('buy', redis) action = redis.get_action('BTCUSDT', '4h') self.assertEqual(action[0], 'OPEN') self.assertEqual(action[1], 'long_spot_NormalOPEN') self.assertEqual(action[2], '2019-09-03 19:59:59') self.assertEqual(action[3], 10647.37) self.assertEqual(action[4]['buy'], [1]) self.assertEqual(action[4]['sell'], []) self.insert_data('sell', redis) action = redis.get_action('BTCUSDT', '4h') self.assertEqual(action[0], 'NOITEM') self.assertEqual(action[1], 'long_spot_NOITEM') self.assertEqual(action[2], '2019-09-06 23:59:59') self.assertEqual(action[3], 10298.73) self.assertEqual(action[4]['buy'], []) # Sell rule matched but no item to sell self.assertEqual(action[4]['sell'], [1]) dbase.insert_trade("BTCUSDT", "2019-09-06 23:59:59", "10647.37", "333", "0.03130663") action = redis.get_action('BTCUSDT', '4h') self.assertEqual(action[0], 'CLOSE') self.assertEqual(action[1], 'long_spot_NormalCLOSE') self.assertEqual(action[2], '2019-09-06 23:59:59') self.assertEqual(action[3], 10298.73) self.assertEqual(action[4]['buy'], []) self.assertEqual(action[4]['sell'], [1]) dbase.update_trades("BTCUSDT", "2019-09-07 23:59:59", "10999", "444", "0.0313066") self.insert_data('random', redis) action = redis.get_action('BTCUSDT', '4h') self.assertEqual(action[0], 'NOITEM') self.assertEqual(action[1], 'long_spot_NOITEM') self.assertEqual(action[2], '2019-09-16 19:59:59') self.assertEqual(action[3], 10121.39) self.assertEqual(action[4]['buy'], []) self.assertEqual(action[4]['sell'], [1])
def test_get_items(self): """ Test putting/getting items to/from redis """ redis = Redis(test_data=True, test=True) redis.clear_all() items = redis.get_items('ABCDEF', '10m') self.assertEqual(len(items), 0) redis.redis_conn('ETHBTC', '1m', {"a":1}, 1607896214) items = redis.get_items('ABCDEF', '10m') self.assertEqual(len(items), 0) items = redis.get_items('ETHBTC', '1m') self.assertEqual(len(items), 0) redis.redis_conn('ETHBTC', '1m', {"a":1}, 1609199999) items = redis.get_items('ETHBTC', '1m') self.assertEqual(len(items), 1) def random_mepoch(): start_time = int(time.time()) end_time = start_time + 10368000 # 120 days time_str = str(random.randint(start_time, end_time *1000)) return int(time_str[:-5] +'99999') redis.clear_all() for i in range(10): redis.redis_conn('ETHBNB', '1m', {i:i}, random_mepoch()) items = redis.get_items('ETHBNB', '1m') self.assertEqual(len(items), 10) redis.clear_all() del redis