Back to Freqtrade

Trade Object

docs/trade-object.md

2026.411.0 KB
Original Source

Trade Object

Trade

A position freqtrade enters is stored in a Trade object - which is persisted to the database. It's a core concept of freqtrade - and something you'll come across in many sections of the documentation, which will most likely point you to this location.

It will be passed to the strategy in many strategy callbacks. The object passed to the strategy cannot be modified directly. Indirect modifications may occur based on callback results.

Trade - Available attributes

The following attributes / properties are available for each individual trade - and can be used with trade.<property> (e.g. trade.pair).

AttributeDataTypeDescription
pairstringPair of this trade.
safe_base_currencystringCompatibility layer for base currency .
safe_quote_currencystringCompatibility layer for quote currency.
is_openbooleanIs the trade currently open, or has it been concluded.
exchangestringExchange where this trade was executed.
open_ratefloatRate this trade was entered at (Avg. entry rate in case of trade-adjustments).
open_rate_requestedfloatThe rate that was requested when the trade was opened.
open_trade_valuefloatValue of the open trade including fees.
close_ratefloatClose rate - only set when is_open = False.
close_rate_requestedfloatThe close rate that was requested.
safe_close_ratefloatClose rate or close_rate_requested or 0.0 if neither is available. Only makes sense once the trade is closed.
stake_amountfloatAmount in Stake (or Quote) currency.
max_stake_amountfloatMaximum stake amount that was used in this trade (sum of all filled Entry orders).
amountfloatAmount in Asset / Base currency that is currently owned. Will be 0.0 until the initial order fills.
amount_requestedfloatAmount that was originally requested for this trade as part of the first entry order.
open_datedatetimeTimestamp when trade was opened use open_date_utc instead
open_date_utcdatetimeTimestamp when trade was opened - in UTC.
close_datedatetimeTimestamp when trade was closed use close_date_utc instead
close_date_utcdatetimeTimestamp when trade was closed - in UTC.
close_profitfloatRelative profit at the time of trade closure. 0.01 == 1%
close_profit_absfloatAbsolute profit (in stake currency) at the time of trade closure.
realized_profitfloatAbsolute already realized profit (in stake currency) while the trade is still open.
leveragefloatLeverage used for this trade - defaults to 1.0 in spot markets.
enter_tagstringTag provided on entry via the enter_tag column in the dataframe.
exit_reasonstringReason why the trade was exited.
exit_order_statusstringStatus of the exit order.
strategystringStrategy name that was used for this trade.
timeframeintTimeframe used for this trade.
is_shortbooleanTrue for short trades, False otherwise.
ordersOrder[]List of order objects attached to this trade (includes both filled and cancelled orders).
date_last_filled_utcdatetimeTime of the last filled order.
date_entry_fill_utcdatetimeDate of the first filled entry order.
entry_side"buy" / "sell"Order Side the trade was entered.
exit_side"buy" / "sell"Order Side that will result in a trade exit / position reduction.
trade_direction"long" / "short"Trade direction in text - long or short.
max_ratefloatHighest price reached during this trade. Not 100% accurate.
min_ratefloatLowest price reached during this trade. Not 100% accurate.
nr_of_successful_entriesintNumber of successful (filled) entry orders.
nr_of_successful_exitsintNumber of successful (filled) exit orders.
has_open_positionbooleanTrue if there is an open position (amount > 0) for this trade. Only false while the initial entry order is unfilled.
has_open_ordersbooleanHas the trade open orders (excluding stoploss orders).
has_open_sl_ordersbooleanTrue if there are open stoploss orders for this trade.
open_ordersOrder[]All open orders for this trade excluding stoploss orders.
open_sl_ordersOrder[]All open stoploss orders for this trade.
fully_canceled_entry_order_countintNumber of fully canceled entry orders.
canceled_exit_order_countintNumber of canceled exit orders.
AttributeDataTypeDescription
stop_lossfloatAbsolute value of the stop loss.
stop_loss_pctfloatRelative value of the stop loss.
initial_stop_lossfloatAbsolute value of the initial stop loss.
initial_stop_loss_pctfloatRelative value of the initial stop loss.
stoploss_last_update_utcdatetimeTimestamp of the last stoploss on exchange order update.
stoploss_or_liquidationfloatReturns the more restrictive of stoploss or liquidation price and corresponds to the price a stoploss would trigger at.

Futures/Margin trading attributes

AttributeDataTypeDescription
liquidation_pricefloatLiquidation price for leveraged trades.
interest_ratefloatInterest rate for margin trades.
funding_feesfloatTotal funding fees for futures trades.

Class methods

The following are class methods - which return generic information, and usually result in an explicit query against the database. They can be used as Trade.<method> - e.g. open_trades = Trade.get_open_trade_count()

!!! Warning "Backtesting/hyperopt" Most methods will work in both backtesting / hyperopt and live/dry modes. During backtesting, it's limited to usage in strategy callbacks. Usage in populate_*() methods is not supported and will result in wrong results.

get_trades_proxy

When your strategy needs some information on existing (open or close) trades - it's best to use Trade.get_trades_proxy().

Usage:

python
from freqtrade.persistence import Trade
from datetime import timedelta

# ...
trade_hist = Trade.get_trades_proxy(pair='ETH/USDT', is_open=False, open_date=current_date - timedelta(days=2))

get_trades_proxy() supports the following keyword arguments. All arguments are optional - calling get_trades_proxy() without arguments will return a list of all trades in the database.

  • pair e.g. pair='ETH/USDT'
  • is_open e.g. is_open=False
  • open_date e.g. open_date=current_date - timedelta(days=2)
  • close_date e.g. close_date=current_date - timedelta(days=5)

get_open_trade_count

Get the number of currently open trades

python
from freqtrade.persistence import Trade
# ...
open_trades = Trade.get_open_trade_count()

get_total_closed_profit

Retrieve the total profit the bot has generated so far. Aggregates close_profit_abs for all closed trades.

python
from freqtrade.persistence import Trade

# ...
profit = Trade.get_total_closed_profit()

total_open_trades_stakes

Retrieve the total stake_amount that's currently in trades.

python
from freqtrade.persistence import Trade

# ...
profit = Trade.total_open_trades_stakes()

Class methods not supported in backtesting/hyperopt

The following class methods are not supported in backtesting/hyperopt mode.

get_overall_performance

Retrieve the overall performance - similar to the /performance telegram command.

python
from freqtrade.persistence import Trade

# ...
if self.config['runmode'].value in ('live', 'dry_run'):
    performance = Trade.get_overall_performance()

Sample return value: ETH/BTC had 5 trades, with a total profit of 1.5% (ratio of 0.015).

json
{"pair": "ETH/BTC", "profit": 0.015, "count": 5}

get_trading_volume

Get total trading volume based on orders.

python
from freqtrade.persistence import Trade

# ...
volume = Trade.get_trading_volume()

Order Object

An Order object represents an order on the exchange (or a simulated order in dry-run mode). An Order object will always be tied to it's corresponding Trade, and only really makes sense in the context of a trade.

Order - Available attributes

an Order object is typically attached to a trade. Most properties here can be None as they are dependent on the exchange response.

AttributeDataTypeDescription
tradeTradeTrade object this order is attached to
ft_pairstringPair this order is for
ft_is_openbooleanis the order still open?
ft_order_sidestringOrder side ('buy', 'sell', or 'stoploss')
ft_cancel_reasonstringReason why the order was canceled
ft_order_tagstringCustom order tag
order_idstringExchange order ID
order_typestringOrder type as defined on the exchange - usually market, limit or stoploss
statusstringStatus as defined by ccxt's order structure. Usually open, closed, expired, canceled or rejected
sidestringbuy or sell
pricefloatPrice the order was placed at
averagefloatAverage price the order filled at
amountfloatAmount in base currency
filledfloatFilled amount (in base currency) (use safe_filled instead)
safe_filledfloatFilled amount (in base currency) - guaranteed to not be None
safe_amountfloatAmount - falls back to ft_amount if None
safe_pricefloatPrice - falls back through average, price, stop_price, ft_price
safe_placement_pricefloatPrice at which the order was placed
remainingfloatRemaining amount (use safe_remaining instead)
safe_remainingfloatRemaining amount - either taken from the exchange or calculated.
safe_costfloatCost of the order - guaranteed to not be None
safe_fee_basefloatFee in base currency - guaranteed to not be None
safe_amount_after_feefloatAmount after deducting fees
costfloatCost of the order - usually average * filled (Exchange dependent on futures trading, may contain the cost with or without leverage and may be in contracts.)
stop_pricefloatStop price for stop orders. Empty for non-stoploss orders.
stake_amountfloatStake amount used for this order.
stake_amount_filledfloatFilled Stake amount used for this order.
order_datedatetimeOrder creation date use order_date_utc instead
order_date_utcdatetimeOrder creation date (in UTC)
order_filled_datedatetimeOrder fill date use order_filled_utc instead
order_filled_utcdatetimeOrder fill date
order_update_datedatetimeLast order update date