Allow passing price to buy function

This commit is contained in:
Matthias 2018-10-09 07:06:11 +02:00
parent d23dc3ec41
commit 5029003957
2 changed files with 54 additions and 3 deletions

View File

@ -425,7 +425,7 @@ class FreqtradeBot(object):
return True return True
return False return False
def execute_buy(self, pair: str, stake_amount: float) -> bool: def execute_buy(self, pair: str, stake_amount: float, price: Optional[float] = None) -> bool:
""" """
Executes a limit buy for the given pair Executes a limit buy for the given pair
:param pair: pair for which we want to create a LIMIT_BUY :param pair: pair for which we want to create a LIMIT_BUY
@ -436,6 +436,9 @@ class FreqtradeBot(object):
stake_currency = self.config['stake_currency'] stake_currency = self.config['stake_currency']
fiat_currency = self.config.get('fiat_display_currency', None) fiat_currency = self.config.get('fiat_display_currency', None)
if price:
buy_limit = price
else:
# Calculate amount # Calculate amount
buy_limit = self.get_target_bid(pair, self.exchange.get_ticker(pair)) buy_limit = self.get_target_bid(pair, self.exchange.get_ticker(pair))

View File

@ -689,6 +689,54 @@ def test_balance_bigger_last_ask(mocker, default_conf) -> None:
assert freqtrade.get_target_bid('ETH/BTC', {'ask': 5, 'last': 10}) == 5 assert freqtrade.get_target_bid('ETH/BTC', {'ask': 5, 'last': 10}) == 5
def test_execute_buy(mocker, default_conf, fee, markets, limit_buy_order) -> None:
patch_RPCManager(mocker)
patch_exchange(mocker)
freqtrade = FreqtradeBot(default_conf)
stake_amount = 2
bid = 0.11
get_bid = MagicMock(return_value=bid)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
get_target_bid=get_bid,
_get_min_pair_stake_amount=MagicMock(return_value=1)
)
buy_mm = MagicMock(return_value={'id': limit_buy_order['id']})
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock(return_value={
'bid': 0.00001172,
'ask': 0.00001173,
'last': 0.00001172
}),
buy=buy_mm,
get_fee=fee,
get_markets=markets
)
pair = 'ETH/BTC'
print(buy_mm.call_args_list)
assert freqtrade.execute_buy(pair, stake_amount)
assert get_bid.call_count == 1
assert buy_mm.call_count == 1
call_args = buy_mm.call_args_list[0][0]
assert call_args[0] == pair
assert call_args[1] == bid
assert call_args[2] == stake_amount / bid
# Test calling with price
fix_price = 0.06
assert freqtrade.execute_buy(pair, stake_amount, fix_price)
# Make sure get_target_bid wasn't called again
assert get_bid.call_count == 1
assert buy_mm.call_count == 2
call_args = buy_mm.call_args_list[1][0]
assert call_args[0] == pair
assert call_args[1] == fix_price
assert call_args[2] == stake_amount / fix_price
def test_process_maybe_execute_buy(mocker, default_conf) -> None: def test_process_maybe_execute_buy(mocker, default_conf) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)