How to use the freqtrade.persistence.Trade.session.flush function in freqtrade

To help you get started, we’ve selected a few freqtrade examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github freqtrade / freqtrade / tests / rpc / test_rpc_apiserver.py View on Github external
trade = Trade(
        pair='XRP/ETH',
        amount=5,
        stake_amount=1,
        exchange='binance',
        open_rate=0.412,
        open_order_id="123456",
        is_open=False,
        fee_close=fee.return_value,
        fee_open=fee.return_value,
        close_rate=0.391
    )
    trade.close_profit = trade.calc_profit_ratio()
    Trade.session.add(trade)
    Trade.session.flush()

    rc = client_get(client, f"{BASE_URI}/performance")
    assert_response(rc)
    assert len(rc.json) == 2
    assert rc.json == [{'count': 1, 'pair': 'LTC/ETH', 'profit': 7.61},
                       {'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57}]
github freqtrade / freqtrade / freqtrade / persistence.py View on Github external
def cleanup() -> None:
    """
    Flushes all pending operations to disk.
    :return: None
    """
    Trade.session.flush()
github freqtrade / freqtrade / freqtrade / freqtradebot.py View on Github external
amount = self._safe_sell_amount(trade.pair, trade.amount)

        # Execute sell and update trade record
        order = self.exchange.sell(pair=str(trade.pair),
                                   ordertype=ordertype,
                                   amount=amount, rate=limit,
                                   time_in_force=self.strategy.order_time_in_force['sell']
                                   )

        trade.open_order_id = order['id']
        trade.close_rate_requested = limit
        trade.sell_reason = sell_reason.value
        # In case of market sell orders the order can be closed immediately
        if order.get('status', 'unknown') == 'closed':
            trade.update(order)
        Trade.session.flush()

        # Lock pair for one candle to prevent immediate rebuys
        self.strategy.lock_pair(trade.pair, timeframe_to_next_date(self.config['ticker_interval']))

        self._notify_sell(trade, ordertype)
github freqtrade / freqtrade / freqtrade / freqtradebot.py View on Github external
self.active_pair_whitelist = self._refresh_whitelist(trades)

        # Refreshing candles
        self.dataprovider.refresh(self._create_pair_whitelist(self.active_pair_whitelist),
                                  self.strategy.informative_pairs())

        # First process current opened trades
        self.process_maybe_execute_sells(trades)

        # Then looking for buy opportunities
        if len(trades) < self.config['max_open_trades']:
            self.process_maybe_execute_buys()

        # Check and handle any timed out open orders
        self.check_handle_timedout()
        Trade.session.flush()

        if (self.heartbeat_interval
                and (arrow.utcnow().timestamp - self._heartbeat_msg > self.heartbeat_interval)):
            logger.info(f"Bot heartbeat. PID={getpid()}")
            self._heartbeat_msg = arrow.utcnow().timestamp
github freqtrade / freqtrade / freqtrade / persistence.py View on Github external
def cleanup() -> None:
    """
    Flushes all pending operations to disk.
    :return: None
    """
    Trade.session.flush()
github freqtrade / freqtrade / freqtrade / freqtradebot.py View on Github external
fee_close=fee,
            open_rate=buy_limit_filled_price,
            open_rate_requested=buy_limit_requested,
            open_date=datetime.utcnow(),
            exchange=self.exchange.id,
            open_order_id=order_id,
            strategy=self.strategy.get_strategy_name(),
            ticker_interval=timeframe_to_minutes(self.config['ticker_interval'])
        )

        # Update fees if order is closed
        if order_status == 'closed':
            self.update_trade_state(trade, order)

        Trade.session.add(trade)
        Trade.session.flush()

        # Updating wallets
        self.wallets.update()

        return True
github freqtrade / freqtrade / freqtrade / rpc / rpc.py View on Github external
# Execute sell for all open orders
            for trade in Trade.get_open_trades():
                _exec_forcesell(trade)
            Trade.session.flush()
            return {'result': 'Created sell orders for all open trades.'}

        # Query for trade
        trade = Trade.get_trades(
            trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True), ]
        ).first()
        if not trade:
            logger.warning('forcesell: Invalid argument received')
            raise RPCException('invalid argument')

        _exec_forcesell(trade)
        Trade.session.flush()
        return {'result': f'Created sell order for trade {trade_id}.'}