From fbe69cee3f040af8527b7793684bce39af9d6d20 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 9 Oct 2018 19:25:43 +0200 Subject: [PATCH] Add /forcebuy to telegram --- freqtrade/rpc/rpc.py | 34 ++++++++++++++++++++++++++++++++++ freqtrade/rpc/telegram.py | 19 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index dc3d9bd65..d33e4bdb1 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -379,6 +379,40 @@ class RPC(object): _exec_forcesell(trade) Trade.session.flush() + def _rpc_forcebuy(self, pair: str, price: Optional[float]) -> Optional[Trade]: + """ + Handler for forcebuy + Buys a pair trade at the given or current price + """ + + if not self._freqtrade.config.get('forcebuy_enable', False): + raise RPCException('Forcebuy not enabled.') + + if self._freqtrade.state != State.RUNNING: + raise RPCException('trader is not running') + + # Check pair is in stake currency + stake_currency = self._freqtrade.config.get('stake_currency') + if not pair.endswith(stake_currency): + raise RPCException( + f'Wrong pair selected. Please pairs with stake {stake_currency} pairs only') + # check if valid pair + + # check if pair already has an open pair + trade = Trade.query.filter(Trade.is_open.is_(True)).filter(Trade.pair.is_(pair)).first() + if trade: + raise RPCException(f'position for {pair} already open - id: {trade.id}') + + # gen stake amount + stakeamount = self._freqtrade._get_trade_stake_amount() + + # execute buy + if self._freqtrade.execute_buy(pair, stakeamount, price): + trade = Trade.query.filter(Trade.is_open.is_(True)).filter(Trade.pair.is_(pair)).first() + return trade + else: + return None + def _rpc_performance(self) -> List[Dict]: """ Handler for performance. diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 64708ef74..3354d6e6f 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -86,6 +86,7 @@ class Telegram(RPC): CommandHandler('start', self._start), CommandHandler('stop', self._stop), CommandHandler('forcesell', self._forcesell), + CommandHandler('forcebuy', self._forcebuy), CommandHandler('performance', self._performance), CommandHandler('daily', self._daily), CommandHandler('count', self._count), @@ -372,6 +373,24 @@ class Telegram(RPC): except RPCException as e: self._send_msg(str(e), bot=bot) + @authorized_only + def _forcebuy(self, bot: Bot, update: Update) -> None: + """ + Handler for /forcebuy . + Buys a pair trade at the given or current price + :param bot: telegram bot + :param update: message update + :return: None + """ + + message = update.message.text.replace('/forcebuy', '').strip().split() + pair = message[0] + price = float(message[1]) if len(message) > 1 else None + try: + self._rpc_forcebuy(pair, price) + except RPCException as e: + self._send_msg(str(e), bot=bot) + @authorized_only def _performance(self, bot: Bot, update: Update) -> None: """