예제 #1
0
def test_forcesell_all_handle(default_conf, update, ticker, mocker) -> None:
    """
    Test _forcesell() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
    rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
    mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    # Create some test data
    for _ in range(4):
        freqtradebot.create_trade()
    rpc_mock.reset_mock()

    update.message.text = '/forcesell all'
    telegram._forcesell(bot=MagicMock(), update=update)

    assert rpc_mock.call_count == 4
    for args in rpc_mock.call_args_list:
        assert '0.00001098' in args[0][0]
        assert 'loss: -0.59%, -0.00000591 BTC' in args[0][0]
        assert '-0.089 USD' in args[0][0]
예제 #2
0
def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
    """
    Test _daily() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch.multiple('freqtrade.exchange.Exchange',
                          validate_pairs=MagicMock(),
                          get_ticker=ticker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    freqtradebot = FreqtradeBot(default_conf)
    telegram = Telegram(freqtradebot)

    # Try invalid data
    msg_mock.reset_mock()
    freqtradebot.state = State.RUNNING
    update.message.text = '/daily -2'
    telegram._daily(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'must be an integer greater than 0' in msg_mock.call_args_list[0][
        0][0]

    # Try invalid data
    msg_mock.reset_mock()
    freqtradebot.state = State.RUNNING
    update.message.text = '/daily today'
    telegram._daily(bot=MagicMock(), update=update)
    assert str('Daily Profit over the last 7 days'
               ) in msg_mock.call_args_list[0][0][0]
예제 #3
0
def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:
    """
    Test rpc_status_table() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.exchange.Exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker,
        get_fee=fee,
        get_markets=markets
    )

    freqtradebot = FreqtradeBot(default_conf)
    rpc = RPC(freqtradebot)

    freqtradebot.state = State.STOPPED
    with pytest.raises(RPCException, match=r'.*\*Status:\* `trader is not running``*'):
        rpc._rpc_status_table()

    freqtradebot.state = State.RUNNING
    with pytest.raises(RPCException, match=r'.*\*Status:\* `no active order`*'):
        rpc._rpc_status_table()

    freqtradebot.create_trade()
    result = rpc._rpc_status_table()
    assert 'just now' in result['Since'].all()
    assert 'ETH/BTC' in result['Pair'].all()
    assert '-0.59%' in result['Profit'].all()
예제 #4
0
def test_rpc_stop(mocker, default_conf) -> None:
    """
    Test rpc_stop() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=MagicMock()
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    rpc = RPC(freqtradebot)
    freqtradebot.state = State.RUNNING

    (error, result) = rpc.rpc_stop()
    assert not error
    assert '`Stopping trader ...`' in result
    assert freqtradebot.state == State.STOPPED

    (error, result) = rpc.rpc_stop()
    assert error
    assert '*Status:* `already stopped`' in result
    assert freqtradebot.state == State.STOPPED
예제 #5
0
def test_authorized_only_exception(default_conf, mocker, caplog) -> None:
    """
    Test authorized_only() method when an exception is thrown
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())

    update = Update(randint(1, 100))
    update.message = Message(randint(1, 100), 0, datetime.utcnow(), Chat(0, 0))

    conf = deepcopy(default_conf)
    conf['telegram']['enabled'] = False
    dummy = DummyCls(FreqtradeBot(conf, create_engine('sqlite://')))
    dummy.dummy_exception(bot=MagicMock(), update=update)
    assert dummy.state['called'] is False
    assert not log_has(
        'Executing handler: dummy_handler for chat_id: 0',
        caplog.record_tuples
    )
    assert not log_has(
        'Rejected unauthorized message from: 0',
        caplog.record_tuples
    )
    assert log_has(
        'Exception occurred within Telegram module',
        caplog.record_tuples
    )
예제 #6
0
def test_rpc_count(mocker, default_conf, ticker) -> None:
    """
    Test rpc_count() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_balances=MagicMock(return_value=ticker),
        get_ticker=ticker
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    rpc = RPC(freqtradebot)

    (error, trades) = rpc.rpc_count()
    nb_trades = len(trades)
    assert not error
    assert nb_trades == 0

    # Create some test data
    freqtradebot.create_trade()
    (error, trades) = rpc.rpc_count()
    nb_trades = len(trades)
    assert not error
    assert nb_trades == 1
예제 #7
0
def test_rpc_count(mocker, default_conf, ticker, fee, markets) -> None:
    """
    Test rpc_count() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.exchange.Exchange',
        validate_pairs=MagicMock(),
        get_balances=MagicMock(return_value=ticker),
        get_ticker=ticker,
        get_fee=fee,
        get_markets=markets
    )

    freqtradebot = FreqtradeBot(default_conf)
    rpc = RPC(freqtradebot)

    trades = rpc._rpc_count()
    nb_trades = len(trades)
    assert nb_trades == 0

    # Create some test data
    freqtradebot.create_trade()
    trades = rpc._rpc_count()
    nb_trades = len(trades)
    assert nb_trades == 1
예제 #8
0
def test_rpc_status_table(default_conf, ticker, mocker) -> None:
    """
    Test rpc_status_table() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    rpc = RPC(freqtradebot)

    freqtradebot.state = State.STOPPED
    (error, result) = rpc.rpc_status_table()
    assert error
    assert '*Status:* `trader is not running`' in result

    freqtradebot.state = State.RUNNING
    (error, result) = rpc.rpc_status_table()
    assert error
    assert '*Status:* `no active order`' in result

    freqtradebot.create_trade()
    (error, result) = rpc.rpc_status_table()
    assert 'just now' in result['Since'].all()
    assert 'BTC_ETH' in result['Pair'].all()
    assert '-0.59%' in result['Profit'].all()
예제 #9
0
def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee,
                       limit_buy_order, limit_sell_order, markets,
                       mocker) -> None:
    """
    Test _profit() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price',
                 return_value=15000.0)
    mocker.patch.multiple('freqtrade.exchange.Exchange',
                          validate_pairs=MagicMock(),
                          get_ticker=ticker,
                          get_fee=fee,
                          get_markets=markets)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    freqtradebot = FreqtradeBot(default_conf)
    telegram = Telegram(freqtradebot)

    telegram._profit(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'no closed trade' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()

    # Create some test data
    freqtradebot.create_trade()
    trade = Trade.query.first()

    # Simulate fulfilled LIMIT_BUY order for trade
    trade.update(limit_buy_order)

    telegram._profit(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'no closed trade' in msg_mock.call_args_list[-1][0][0]
    msg_mock.reset_mock()

    # Update the ticker with a market going up
    mocker.patch('freqtrade.exchange.Exchange.get_ticker', ticker_sell_up)
    trade.update(limit_sell_order)

    trade.close_date = datetime.utcnow()
    trade.is_open = False

    telegram._profit(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert '*ROI:* Close trades' in msg_mock.call_args_list[-1][0][0]
    assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
    assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]
    assert '*ROI:* All trades' in msg_mock.call_args_list[-1][0][0]
    assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
    assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]

    assert '*Best Performing:* `ETH/BTC: 6.20%`' in msg_mock.call_args_list[
        -1][0][0]
예제 #10
0
def test_send_msg(default_conf, mocker) -> None:
    """
    Test send_msg() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
    conf = deepcopy(default_conf)
    bot = MagicMock()
    freqtradebot = get_patched_freqtradebot(mocker, conf)
    telegram = Telegram(freqtradebot)

    telegram._config['telegram']['enabled'] = True
    telegram._send_msg('test', bot)
    assert len(bot.method_calls) == 1
예제 #11
0
def test_rpc_daily_profit(default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker)\
        -> None:
    """
    Test rpc_daily_profit() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    stake_currency = default_conf['stake_currency']
    fiat_display_currency = default_conf['fiat_display_currency']

    rpc = RPC(freqtradebot)

    # Create some test data
    freqtradebot.create_trade()
    trade = Trade.query.first()
    assert trade

    # Simulate buy & sell
    trade.update(limit_buy_order)
    trade.update(limit_sell_order)
    trade.close_date = datetime.utcnow()
    trade.is_open = False

    # Try valid data
    update.message.text = '/daily 2'
    (error, days) = rpc.rpc_daily_profit(7, stake_currency, fiat_display_currency)
    assert not error
    assert len(days) == 7
    for day in days:
        # [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
        assert (day[1] == '0.00000000 BTC' or
                day[1] == '0.00006217 BTC')

        assert (day[2] == '0.000 USD' or
                day[2] == '0.933 USD')
    # ensure first day is current date
    assert str(days[0][0]) == str(datetime.utcnow().date())

    # Try invalid data
    (error, days) = rpc.rpc_daily_profit(0, stake_currency, fiat_display_currency)
    assert error
    assert days.find('must be an integer greater than 0') >= 0
예제 #12
0
def test_status_table_handle(default_conf, update, ticker, fee, markets,
                             mocker) -> None:
    """
    Test _status_table() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch.multiple(
        'freqtrade.exchange.Exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker,
        buy=MagicMock(return_value={'id': 'mocked_order_id'}),
        get_fee=fee,
        get_markets=markets)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    conf = deepcopy(default_conf)
    conf['stake_amount'] = 15.0
    freqtradebot = FreqtradeBot(conf)
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.STOPPED
    telegram._status_table(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'trader is not running' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()

    freqtradebot.state = State.RUNNING
    telegram._status_table(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'no active order' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()

    # Create some test data
    freqtradebot.create_trade()

    telegram._status_table(bot=MagicMock(), update=update)

    text = re.sub('</?pre>', '', msg_mock.call_args_list[-1][0][0])
    line = text.split("\n")
    fields = re.sub('[ ]+', ' ', line[2].strip()).split(' ')

    assert int(fields[0]) == 1
    assert fields[1] == 'ETH/BTC'
    assert msg_mock.call_count == 1
예제 #13
0
def test_version_handle(default_conf, update, mocker) -> None:
    """
    Test _version() method
    """
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    freqtradebot = get_patched_freqtradebot(mocker, default_conf)
    telegram = Telegram(freqtradebot)

    telegram._version(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert '*Version:* `{}`'.format(
        __version__) in msg_mock.call_args_list[0][0][0]
예제 #14
0
def test_help_handle(default_conf, update, mocker) -> None:
    """
    Test _help() method
    """
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    freqtradebot = get_patched_freqtradebot(mocker, default_conf)

    telegram = Telegram(freqtradebot)

    telegram._help(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert '*/help:* `This help message`' in msg_mock.call_args_list[0][0][0]
예제 #15
0
def test_reload_conf_handle(default_conf, update, mocker) -> None:
    """ Test _reload_conf() method """
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)

    freqtradebot = get_patched_freqtradebot(mocker, default_conf)
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.RUNNING
    assert freqtradebot.state == State.RUNNING
    telegram._reload_conf(bot=MagicMock(), update=update)
    assert freqtradebot.state == State.RELOAD_CONF
    assert msg_mock.call_count == 1
    assert 'Reloading config' in msg_mock.call_args_list[0][0][0]
예제 #16
0
def test_rpc_trade_status(default_conf, ticker, mocker) -> None:
    """
    Test rpc_trade_status() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.rpc_manager.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    rpc = RPC(freqtradebot)

    freqtradebot.state = State.STOPPED
    (error, result) = rpc.rpc_trade_status()
    assert error
    assert 'trader is not running' in result

    freqtradebot.state = State.RUNNING
    (error, result) = rpc.rpc_trade_status()
    assert error
    assert 'no active trade' in result

    freqtradebot.create_trade()
    (error, result) = rpc.rpc_trade_status()
    assert not error
    trade = result[0]

    result_message = [
        '*Trade ID:* `1`\n'
        '*Current Pair:* '
        '[BTC_ETH](https://www.bittrex.com/Market/Index?MarketName=BTC-ETH)\n'
        '*Open Since:* `just now`\n'
        '*Amount:* `90.99181074`\n'
        '*Open Rate:* `0.00001099`\n'
        '*Close Rate:* `None`\n'
        '*Current Rate:* `0.00001098`\n'
        '*Close Profit:* `None`\n'
        '*Current Profit:* `-0.59%`\n'
        '*Open Order:* `(LIMIT_BUY rem=0.00000000)`'
    ]
    assert result == result_message
    assert trade.find('[BTC_ETH]') >= 0
예제 #17
0
def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None:
    """
    Test rpc_trade_status() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
    mocker.patch.multiple(
        'freqtrade.exchange.Exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker,
        get_fee=fee,
        get_markets=markets
    )

    freqtradebot = FreqtradeBot(default_conf)
    rpc = RPC(freqtradebot)

    freqtradebot.state = State.STOPPED
    with pytest.raises(RPCException, match=r'.*trader is not running*'):
        rpc._rpc_trade_status()

    freqtradebot.state = State.RUNNING
    with pytest.raises(RPCException, match=r'.*no active trade*'):
        rpc._rpc_trade_status()

    freqtradebot.create_trade()
    trades = rpc._rpc_trade_status()
    trade = trades[0]

    result_message = [
        '*Trade ID:* `1`\n'
        '*Current Pair:* '
        '[ETH/BTC](https://bittrex.com/Market/Index?MarketName=BTC-ETH)\n'
        '*Open Since:* `just now`\n'
        '*Amount:* `90.99181074`\n'
        '*Open Rate:* `0.00001099`\n'
        '*Close Rate:* `None`\n'
        '*Current Rate:* `0.00001098`\n'
        '*Close Profit:* `None`\n'
        '*Current Profit:* `-0.59%`\n'
        '*Open Order:* `(limit buy rem=0.00000000)`'
    ]
    assert trades == result_message
    assert trade.find('[ETH/BTC]') >= 0
예제 #18
0
def test_version_handle(default_conf, update, mocker) -> None:
    """
    Test _version() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())
    msg_mock = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        send_msg=msg_mock
    )
    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    telegram._version(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0]
예제 #19
0
def test_help_handle(default_conf, update, mocker) -> None:
    """
    Test _help() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())
    msg_mock = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        send_msg=msg_mock
    )
    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    telegram._help(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert '*/help:* `This help message`' in msg_mock.call_args_list[0][0][0]
예제 #20
0
def test_stop_handle_already_stopped(default_conf, update, mocker) -> None:
    """
    Test _stop() method
    """
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)

    freqtradebot = get_patched_freqtradebot(mocker, default_conf)
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.STOPPED
    assert freqtradebot.state == State.STOPPED
    telegram._stop(bot=MagicMock(), update=update)
    assert freqtradebot.state == State.STOPPED
    assert msg_mock.call_count == 1
    assert 'already stopped' in msg_mock.call_args_list[0][0][0]
예제 #21
0
def test_status_handle(default_conf, update, ticker, mocker) -> None:
    """
    Test _status() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )
    msg_mock = MagicMock()
    status_table = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        _status_table=status_table,
        send_msg=msg_mock
    )
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.STOPPED
    telegram._status(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'trader is not running' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()

    freqtradebot.state = State.RUNNING
    telegram._status(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'no active trade' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()

    # Create some test data
    freqtradebot.create_trade()
    # Trigger status while we have a fulfilled order for the open trade
    telegram._status(bot=MagicMock(), update=update)

    assert msg_mock.call_count == 1
    assert '[BTC_ETH]' in msg_mock.call_args_list[0][0][0]
예제 #22
0
def test_performance_handle_invalid(default_conf, update, mocker) -> None:
    """
    Test _performance() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
    freqtradebot = FreqtradeBot(default_conf)
    telegram = Telegram(freqtradebot)

    # Trader is not running
    freqtradebot.state = State.STOPPED
    telegram._performance(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'not running' in msg_mock.call_args_list[0][0][0]
예제 #23
0
def test_send_msg_network_error(default_conf, mocker, caplog) -> None:
    """
    Test send_msg() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
    conf = deepcopy(default_conf)
    bot = MagicMock()
    bot.send_message = MagicMock(side_effect=NetworkError('Oh snap'))
    freqtradebot = get_patched_freqtradebot(mocker, conf)
    telegram = Telegram(freqtradebot)

    telegram._config['telegram']['enabled'] = True
    telegram._send_msg('test', bot)

    # Bot should've tried to send it twice
    assert len(bot.method_calls) == 2
    assert log_has('Telegram NetworkError: Oh snap! Trying one more time.',
                   caplog.record_tuples)
예제 #24
0
def test_forcesell_down_handle(default_conf, update, ticker, fee,
                               ticker_sell_down, markets, mocker) -> None:
    """
    Test _forcesell() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price',
                 return_value=15000.0)
    rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg',
                            MagicMock())
    mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
    mocker.patch.multiple('freqtrade.exchange.Exchange',
                          validate_pairs=MagicMock(),
                          get_ticker=ticker,
                          get_fee=fee,
                          get_markets=markets)

    freqtradebot = FreqtradeBot(default_conf)
    telegram = Telegram(freqtradebot)

    # Create some test data
    freqtradebot.create_trade()

    # Decrease the price and sell it
    mocker.patch.multiple('freqtrade.exchange.Exchange',
                          validate_pairs=MagicMock(),
                          get_ticker=ticker_sell_down)

    trade = Trade.query.first()
    assert trade

    update.message.text = '/forcesell 1'
    telegram._forcesell(bot=MagicMock(), update=update)

    assert rpc_mock.call_count == 2
    assert 'Selling' in rpc_mock.call_args_list[-1][0][0]
    assert '[ETH/BTC]' in rpc_mock.call_args_list[-1][0][0]
    assert 'Amount' in rpc_mock.call_args_list[-1][0][0]
    assert '0.00001044' in rpc_mock.call_args_list[-1][0][0]
    assert 'loss: -5.48%, -0.00005492' in rpc_mock.call_args_list[-1][0][0]
    assert '-0.824 USD' in rpc_mock.call_args_list[-1][0][0]
예제 #25
0
def test_send_msg(default_conf, mocker) -> None:
    """
    Test send_msg() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())
    mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
    conf = deepcopy(default_conf)
    bot = MagicMock()
    freqtradebot = FreqtradeBot(conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    telegram._config['telegram']['enabled'] = False
    telegram.send_msg('test', bot)
    assert not bot.method_calls
    bot.reset_mock()

    telegram._config['telegram']['enabled'] = True
    telegram.send_msg('test', bot)
    assert len(bot.method_calls) == 1
예제 #26
0
def test_status(default_conf, update, mocker, ticker) -> None:
    """
    Test _status() method
    """
    update.message.chat.id = 123
    conf = deepcopy(default_conf)
    conf['telegram']['enabled'] = False
    conf['telegram']['chat_id'] = 123

    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )
    msg_mock = MagicMock()
    status_table = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        rpc_trade_status=MagicMock(return_value=(False, [1, 2, 3])),
        _status_table=status_table,
        send_msg=msg_mock
    )
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    freqtradebot = FreqtradeBot(conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    # Create some test data
    for _ in range(3):
        freqtradebot.create_trade()

    telegram._status(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 3

    update.message.text = MagicMock()
    update.message.text.replace = MagicMock(return_value='table 2 3')
    telegram._status(bot=MagicMock(), update=update)
    assert status_table.call_count == 1
예제 #27
0
def test_forcesell_handle_invalid(default_conf, update, mocker) -> None:
    """
    Test _forcesell() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
    mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
    msg_mock = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        send_msg=msg_mock
    )
    mocker.patch('freqtrade.freqtradebot.exchange.validate_pairs', MagicMock())

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    # Trader is not running
    freqtradebot.state = State.STOPPED
    update.message.text = '/forcesell 1'
    telegram._forcesell(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'not running' in msg_mock.call_args_list[0][0][0]

    # No argument
    msg_mock.reset_mock()
    freqtradebot.state = State.RUNNING
    update.message.text = '/forcesell'
    telegram._forcesell(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'Invalid argument' in msg_mock.call_args_list[0][0][0]

    # Invalid argument
    msg_mock.reset_mock()
    freqtradebot.state = State.RUNNING
    update.message.text = '/forcesell 123456'
    telegram._forcesell(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'Invalid argument.' in msg_mock.call_args_list[0][0][0]
예제 #28
0
def test_count_handle(default_conf, update, ticker, fee, markets,
                      mocker) -> None:
    """
    Test _count() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple('freqtrade.rpc.telegram.Telegram',
                          _init=MagicMock(),
                          _send_msg=msg_mock)
    mocker.patch.multiple(
        'freqtrade.exchange.Exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker,
        buy=MagicMock(return_value={'id': 'mocked_order_id'}),
        get_markets=markets)
    mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
    freqtradebot = FreqtradeBot(default_conf)
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.STOPPED
    telegram._count(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'not running' in msg_mock.call_args_list[0][0][0]
    msg_mock.reset_mock()
    freqtradebot.state = State.RUNNING

    # Create some test data
    freqtradebot.create_trade()
    msg_mock.reset_mock()
    telegram._count(bot=MagicMock(), update=update)

    msg = '<pre>  current    max    total stake\n---------  -----  -------------\n' \
          '        1      {}          {}</pre>'\
        .format(
            default_conf['max_open_trades'],
            default_conf['stake_amount']
        )
    assert msg in msg_mock.call_args_list[0][0][0]
예제 #29
0
def test_performance_handle(default_conf, update, ticker, limit_buy_order,
                            limit_sell_order, mocker) -> None:
    """
    Test _performance() method
    """
    patch_get_signal(mocker, (True, False))
    patch_coinmarketcap(mocker)
    msg_mock = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        send_msg=msg_mock
    )
    mocker.patch.multiple(
        'freqtrade.freqtradebot.exchange',
        validate_pairs=MagicMock(),
        get_ticker=ticker
    )
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    # Create some test data
    freqtradebot.create_trade()
    trade = Trade.query.first()
    assert trade

    # Simulate fulfilled LIMIT_BUY order for trade
    trade.update(limit_buy_order)

    # Simulate fulfilled LIMIT_SELL order for trade
    trade.update(limit_sell_order)

    trade.close_date = datetime.utcnow()
    trade.is_open = False
    telegram._performance(bot=MagicMock(), update=update)
    assert msg_mock.call_count == 1
    assert 'Performance' in msg_mock.call_args_list[0][0][0]
    assert '<code>BTC_ETH\t6.20% (1)</code>' in msg_mock.call_args_list[0][0][0]
예제 #30
0
def test_start_handle(default_conf, update, mocker) -> None:
    """
    Test _start() method
    """
    patch_coinmarketcap(mocker)
    mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock())
    msg_mock = MagicMock()
    mocker.patch.multiple(
        'freqtrade.rpc.telegram.Telegram',
        _init=MagicMock(),
        send_msg=msg_mock
    )
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    freqtradebot = FreqtradeBot(default_conf, create_engine('sqlite://'))
    telegram = Telegram(freqtradebot)

    freqtradebot.state = State.STOPPED
    assert freqtradebot.state == State.STOPPED
    telegram._start(bot=MagicMock(), update=update)
    assert freqtradebot.state == State.RUNNING
    assert msg_mock.call_count == 0