Example #1
0
    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])
Example #2
0
class TestMysql(OrderedTest):
    """Test mysql class methods"""
    def setUp(self):
        """
        Define static instance variables and create redis/mysql objects as well as working test
        directory
        """
        LOGGER.info("Setting up environment")
        for container in ['mysql-unit', 'redis-unit']:
            command = "docker-compose -f install/docker-compose_unit.yml up -d " + container
        time.sleep(6)
        self.dbase = Mysql(test=True, interval="1h")

    def step_1(self):
        """Check insert and update trades"""

        self.date = '2018-05-07 22:44:59'
        self.sell_date = '2018-05-07 22:44:59'
        self.pair = 'XXXYYY'
        self.buy_price = 100
        self.sell_price = 500
        base_in = 20
        self.dbase.insert_trade(self.pair, self.date, self.buy_price, base_in,
                                30)
        sql = 'select buy_time, sell_time from trades'
        buy_time, sell_time = self.dbase.fetch_sql_data(sql)[-1]
        current_time = buy_time.strftime("%Y-%m-%d %H:%M:%S")
        assert current_time == self.date
        assert sell_time is None

        quote = self.dbase.get_quantity(self.pair)
        perc_inc = perc_diff(self.buy_price, self.sell_price)
        base_out = add_perc(perc_inc, base_in)
        self.dbase.update_trades(self.pair,
                                 self.sell_date,
                                 self.sell_price,
                                 quote=quote,
                                 base_out=base_out)
        sell_time = self.dbase.fetch_sql_data(
            'select sell_time from trades')[-1]
        assert sell_time is not None

    def tearDown(self):
        del self.dbase