Merge pull request #1348 from mishaker/walletizer
Getting available balance from wallet instead of API call.
This commit is contained in:
commit
0aa74b8d72
@ -337,9 +337,7 @@ class FreqtradeBot(object):
|
|||||||
else:
|
else:
|
||||||
stake_amount = self.config['stake_amount']
|
stake_amount = self.config['stake_amount']
|
||||||
|
|
||||||
# TODO: should come from the wallet
|
avaliable_amount = self.wallets.get_free(self.config['stake_currency'])
|
||||||
avaliable_amount = self.exchange.get_balance(self.config['stake_currency'])
|
|
||||||
# avaliable_amount = self.wallets.wallets[self.config['stake_currency']].free
|
|
||||||
|
|
||||||
if stake_amount == constants.UNLIMITED_STAKE_AMOUNT:
|
if stake_amount == constants.UNLIMITED_STAKE_AMOUNT:
|
||||||
open_trades = len(Trade.query.filter(Trade.is_open.is_(True)).all())
|
open_trades = len(Trade.query.filter(Trade.is_open.is_(True)).all())
|
||||||
|
@ -45,6 +45,12 @@ def get_patched_exchange(mocker, config, api_mock=None) -> Exchange:
|
|||||||
return exchange
|
return exchange
|
||||||
|
|
||||||
|
|
||||||
|
def patch_wallet(mocker, free=999.9) -> None:
|
||||||
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(
|
||||||
|
return_value=free
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
def patch_edge(mocker) -> None:
|
def patch_edge(mocker) -> None:
|
||||||
# "ETH/BTC",
|
# "ETH/BTC",
|
||||||
# "LTC/BTC",
|
# "LTC/BTC",
|
||||||
|
@ -18,7 +18,7 @@ from freqtrade.persistence import Trade
|
|||||||
from freqtrade.rpc import RPCMessageType
|
from freqtrade.rpc import RPCMessageType
|
||||||
from freqtrade.state import State
|
from freqtrade.state import State
|
||||||
from freqtrade.strategy.interface import SellType, SellCheckTuple
|
from freqtrade.strategy.interface import SellType, SellCheckTuple
|
||||||
from freqtrade.tests.conftest import log_has, patch_exchange, patch_edge
|
from freqtrade.tests.conftest import log_has, patch_exchange, patch_edge, patch_wallet
|
||||||
|
|
||||||
|
|
||||||
# Functions for recurrent object patching
|
# Functions for recurrent object patching
|
||||||
@ -181,17 +181,10 @@ def test_get_trade_stake_amount(default_conf, ticker, limit_buy_order, fee, mock
|
|||||||
assert result == default_conf['stake_amount']
|
assert result == default_conf['stake_amount']
|
||||||
|
|
||||||
|
|
||||||
def test_get_trade_stake_amount_no_stake_amount(default_conf,
|
def test_get_trade_stake_amount_no_stake_amount(default_conf, mocker) -> None:
|
||||||
ticker,
|
|
||||||
limit_buy_order,
|
|
||||||
fee,
|
|
||||||
mocker) -> None:
|
|
||||||
patch_RPCManager(mocker)
|
patch_RPCManager(mocker)
|
||||||
patch_exchange(mocker)
|
patch_exchange(mocker)
|
||||||
mocker.patch.multiple(
|
patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5)
|
||||||
'freqtrade.exchange.Exchange',
|
|
||||||
get_balance=MagicMock(return_value=default_conf['stake_amount'] * 0.5)
|
|
||||||
)
|
|
||||||
freqtrade = FreqtradeBot(default_conf)
|
freqtrade = FreqtradeBot(default_conf)
|
||||||
|
|
||||||
with pytest.raises(DependencyException, match=r'.*stake amount.*'):
|
with pytest.raises(DependencyException, match=r'.*stake amount.*'):
|
||||||
@ -206,12 +199,12 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf,
|
|||||||
mocker) -> None:
|
mocker) -> None:
|
||||||
patch_RPCManager(mocker)
|
patch_RPCManager(mocker)
|
||||||
patch_exchange(mocker)
|
patch_exchange(mocker)
|
||||||
|
patch_wallet(mocker, free=default_conf['stake_amount'])
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker,
|
get_ticker=ticker,
|
||||||
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
||||||
get_balance=MagicMock(return_value=default_conf['stake_amount']),
|
|
||||||
get_fee=fee,
|
get_fee=fee,
|
||||||
get_markets=markets
|
get_markets=markets
|
||||||
)
|
)
|
||||||
@ -521,11 +514,11 @@ def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order,
|
|||||||
fee, markets, mocker) -> None:
|
fee, markets, mocker) -> None:
|
||||||
patch_RPCManager(mocker)
|
patch_RPCManager(mocker)
|
||||||
patch_exchange(mocker)
|
patch_exchange(mocker)
|
||||||
|
patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5)
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
get_ticker=ticker,
|
get_ticker=ticker,
|
||||||
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
||||||
get_balance=MagicMock(return_value=default_conf['stake_amount'] * 0.5),
|
|
||||||
get_fee=fee,
|
get_fee=fee,
|
||||||
get_markets=markets
|
get_markets=markets
|
||||||
)
|
)
|
||||||
|
@ -4,6 +4,7 @@ from unittest.mock import MagicMock
|
|||||||
|
|
||||||
|
|
||||||
def test_sync_wallet_at_boot(mocker, default_conf):
|
def test_sync_wallet_at_boot(mocker, default_conf):
|
||||||
|
default_conf['dry_run'] = False
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
get_balances=MagicMock(return_value={
|
get_balances=MagicMock(return_value={
|
||||||
@ -58,6 +59,7 @@ def test_sync_wallet_at_boot(mocker, default_conf):
|
|||||||
|
|
||||||
|
|
||||||
def test_sync_wallet_missing_data(mocker, default_conf):
|
def test_sync_wallet_missing_data(mocker, default_conf):
|
||||||
|
default_conf['dry_run'] = False
|
||||||
mocker.patch.multiple(
|
mocker.patch.multiple(
|
||||||
'freqtrade.exchange.Exchange',
|
'freqtrade.exchange.Exchange',
|
||||||
get_balances=MagicMock(return_value={
|
get_balances=MagicMock(return_value={
|
||||||
|
@ -29,6 +29,17 @@ class Wallets(object):
|
|||||||
self.wallets: Dict[str, Any] = {}
|
self.wallets: Dict[str, Any] = {}
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
def get_free(self, currency) -> float:
|
||||||
|
|
||||||
|
if self.exchange._conf['dry_run']:
|
||||||
|
return 999.9
|
||||||
|
|
||||||
|
balance = self.wallets.get(currency)
|
||||||
|
if balance and balance['free']:
|
||||||
|
return balance['free']
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
balances = self.exchange.get_balances()
|
balances = self.exchange.get_balances()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user