def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=ticker, buy=MagicMock(return_value='mocked_limit_buy')) # Create trade and sell it init(default_conf, create_engine('sqlite://')) trade = create_trade(15.0) Trade.session.add(trade) trade.update(limit_buy_order) trade = Trade.query.filter(Trade.is_open.is_(True)).first() assert trade trade.update(limit_sell_order) trade = Trade.query.filter(Trade.is_open.is_(False)).first() assert trade with pytest.raises(ValueError, match=r'.*closed trade.*'): handle_trade(trade)
def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=ticker, buy=MagicMock(return_value='mocked_limit_buy')) # Create trade and sell it init(default_conf, create_engine('sqlite://')) trade = create_trade(15.0) trade.update(limit_buy_order) trade.update(limit_sell_order) Trade.session.add(trade) Trade.session.flush() trade = Trade.query.filter(Trade.is_open.is_(True)).first() assert trade # Simulate that there is no open order trade.open_order_id = None closed = close_trade_if_fulfilled(trade) assert closed assert not trade.is_open with pytest.raises(ValueError, match=r'.*closed trade.*'): handle_trade(trade)
def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, mocker): mocker.patch.dict('freqtrade.main._CONF', default_conf) mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: True) mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple( 'freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=MagicMock(return_value={ 'bid': 0.17256061, 'ask': 0.172661, 'last': 0.17256061 }), buy=MagicMock(return_value='mocked_limit_buy'), sell=MagicMock(return_value='mocked_limit_sell')) init(default_conf, create_engine('sqlite://')) trade = create_trade(15.0) trade.update(limit_buy_order) Trade.session.add(trade) Trade.session.flush() trade = Trade.query.filter(Trade.is_open.is_(True)).first() assert trade handle_trade(trade) assert trade.open_order_id == 'mocked_limit_sell' # Simulate fulfilled LIMIT_SELL order for trade trade.update(limit_sell_order) assert trade.close_rate == 0.0802134 assert trade.close_profit == 0.10046755 assert trade.close_date is not None
def test_handle_trade(conf, mocker): mocker.patch.dict('freqtrade.main._CONF', conf) mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()) mocker.patch.multiple('freqtrade.main.exchange', validate_pairs=MagicMock(), get_ticker=MagicMock(return_value={ 'bid': 0.17256061, 'ask': 0.172661, 'last': 0.17256061 }), buy=MagicMock(return_value='mocked_order_id')) trade = Trade.query.filter(Trade.is_open.is_(True)).first() assert trade handle_trade(trade) assert trade.close_rate == 0.17256061 assert trade.close_profit == 137.4872490056564 assert trade.close_date is not None assert trade.open_order_id == 'dry_run'