From 27a6dcf3fc13821006903573d330c06cecf9b169 Mon Sep 17 00:00:00 2001 From: misagh Date: Thu, 22 Nov 2018 21:23:35 +0100 Subject: [PATCH 1/4] getting available balance from wallet instead of API call. --- freqtrade/freqtradebot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 8a2db84a9..49a4959c1 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -338,8 +338,8 @@ class FreqtradeBot(object): stake_amount = self.config['stake_amount'] # TODO: should come from the wallet - avaliable_amount = self.exchange.get_balance(self.config['stake_currency']) - # avaliable_amount = self.wallets.wallets[self.config['stake_currency']].free + #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: open_trades = len(Trade.query.filter(Trade.is_open.is_(True)).all()) From a9f04609d3c2cf2ed623c15ab223bc5f912a7b1e Mon Sep 17 00:00:00 2001 From: misagh Date: Fri, 23 Nov 2018 10:17:10 +0100 Subject: [PATCH 2/4] tests fixed --- freqtrade/freqtradebot.py | 2 -- freqtrade/tests/conftest.py | 7 +++++++ freqtrade/tests/test_freqtradebot.py | 11 ++++------- freqtrade/tests/test_wallets.py | 2 ++ freqtrade/wallets.py | 5 ++++- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 49a4959c1..c93811cae 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -337,8 +337,6 @@ class FreqtradeBot(object): else: stake_amount = self.config['stake_amount'] - # TODO: should come from the wallet - #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: diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index b6c022b45..cdd71fc9a 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -14,6 +14,7 @@ from telegram import Chat, Message, Update from freqtrade.exchange.exchange_helpers import parse_ticker_dataframe from freqtrade.exchange import Exchange from freqtrade.edge import Edge +from freqtrade.wallets import Wallet from freqtrade.freqtradebot import FreqtradeBot logging.getLogger('').setLevel(logging.INFO) @@ -45,6 +46,12 @@ def get_patched_exchange(mocker, config, api_mock=None) -> Exchange: return exchange +def patch_wallet(mocker, currency='BTC', free=999.9) -> None: + mocker.patch('freqtrade.wallets.Wallet', MagicMock( + return_value=Wallet('bittrex', currency, free, 100, 1000) + )) + + def patch_edge(mocker) -> None: # "ETH/BTC", # "LTC/BTC", diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index cef89c250..bcd914d0d 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -18,7 +18,7 @@ from freqtrade.persistence import Trade from freqtrade.rpc import RPCMessageType from freqtrade.state import State 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 @@ -188,10 +188,7 @@ def test_get_trade_stake_amount_no_stake_amount(default_conf, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - get_balance=MagicMock(return_value=default_conf['stake_amount'] * 0.5) - ) + patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5) freqtrade = FreqtradeBot(default_conf) with pytest.raises(DependencyException, match=r'.*stake amount.*'): @@ -206,12 +203,12 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) + patch_wallet(mocker, free=default_conf['stake_amount']) mocker.patch.multiple( 'freqtrade.exchange.Exchange', validate_pairs=MagicMock(), get_ticker=ticker, buy=MagicMock(return_value={'id': limit_buy_order['id']}), - get_balance=MagicMock(return_value=default_conf['stake_amount']), get_fee=fee, get_markets=markets ) @@ -521,11 +518,11 @@ def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order, fee, markets, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) + patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5) mocker.patch.multiple( 'freqtrade.exchange.Exchange', get_ticker=ticker, buy=MagicMock(return_value={'id': limit_buy_order['id']}), - get_balance=MagicMock(return_value=default_conf['stake_amount'] * 0.5), get_fee=fee, get_markets=markets ) diff --git a/freqtrade/tests/test_wallets.py b/freqtrade/tests/test_wallets.py index cc10d665c..e6a17ecbf 100644 --- a/freqtrade/tests/test_wallets.py +++ b/freqtrade/tests/test_wallets.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock def test_sync_wallet_at_boot(mocker, default_conf): + default_conf['dry_run'] = False mocker.patch.multiple( 'freqtrade.exchange.Exchange', 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): + default_conf['dry_run'] = False mocker.patch.multiple( 'freqtrade.exchange.Exchange', get_balances=MagicMock(return_value={ diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index 82f527d2c..4415478d3 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -26,7 +26,10 @@ class Wallets(object): def __init__(self, exchange: Exchange) -> None: self.exchange = exchange - self.wallets: Dict[str, Any] = {} + if self.exchange._conf['dry_run']: + self.wallets: Dict[str, Any] = {'BTC': Wallet('Bittrex', 'BTC', 999.99, 100, 1000)} + else: + self.wallets: Dict[str, Any] = {} self.update() def update(self) -> None: From 29347a693187cbce26f2d75babca002dfec9ddda Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 24 Nov 2018 16:37:28 +0100 Subject: [PATCH 3/4] adding get_free to wallet --- freqtrade/freqtradebot.py | 2 +- freqtrade/tests/conftest.py | 7 +++---- freqtrade/wallets.py | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index c93811cae..0e77a31c0 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -337,7 +337,7 @@ class FreqtradeBot(object): else: stake_amount = self.config['stake_amount'] - avaliable_amount = self.wallets.wallets[self.config['stake_currency']].free + avaliable_amount = self.wallets.get_free(self.config['stake_currency']) if stake_amount == constants.UNLIMITED_STAKE_AMOUNT: open_trades = len(Trade.query.filter(Trade.is_open.is_(True)).all()) diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index cdd71fc9a..63655126c 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -14,7 +14,6 @@ from telegram import Chat, Message, Update from freqtrade.exchange.exchange_helpers import parse_ticker_dataframe from freqtrade.exchange import Exchange from freqtrade.edge import Edge -from freqtrade.wallets import Wallet from freqtrade.freqtradebot import FreqtradeBot logging.getLogger('').setLevel(logging.INFO) @@ -46,9 +45,9 @@ def get_patched_exchange(mocker, config, api_mock=None) -> Exchange: return exchange -def patch_wallet(mocker, currency='BTC', free=999.9) -> None: - mocker.patch('freqtrade.wallets.Wallet', MagicMock( - return_value=Wallet('bittrex', currency, free, 100, 1000) +def patch_wallet(mocker, free=999.9) -> None: + mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock( + return_value=free )) diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index 4415478d3..b8b37907d 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -26,12 +26,20 @@ class Wallets(object): def __init__(self, exchange: Exchange) -> None: self.exchange = exchange - if self.exchange._conf['dry_run']: - self.wallets: Dict[str, Any] = {'BTC': Wallet('Bittrex', 'BTC', 999.99, 100, 1000)} - else: - self.wallets: Dict[str, Any] = {} + self.wallets: Dict[str, Any] = {} 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: balances = self.exchange.get_balances() From 63c2ea110acb9fe04338be56055a824566de4bee Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 24 Nov 2018 16:41:17 +0100 Subject: [PATCH 4/4] Not sure why those arguments were there ! --- freqtrade/tests/test_freqtradebot.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index bcd914d0d..a36ae2cd8 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -181,11 +181,7 @@ def test_get_trade_stake_amount(default_conf, ticker, limit_buy_order, fee, mock assert result == default_conf['stake_amount'] -def test_get_trade_stake_amount_no_stake_amount(default_conf, - ticker, - limit_buy_order, - fee, - mocker) -> None: +def test_get_trade_stake_amount_no_stake_amount(default_conf, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker) patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5)