Get the Trade Balance for a variety of countries over time from the Global Macro Database (GMDB). The Trade Balance is the difference between the total value of goods and services a country exports and the total value of goods and services it imports. A positive trade balance (a “trade surplus”) means a country exports more than it imports, while a negative trade balance (a “trade deficit”) means a country imports more than it exports.

Formula:

Trade Balance = Exports - Imports

The balance is annual and expressed in millions of national currency, with a negative value marking a trade deficit and a positive value a surplus.

Data comes from the Global Macro Database (GMDB), further information about the variable can be found within https://www.globalmacrodata.com/documentation.html

Also known as: net exports, balance of trade.

No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Trade Balance for you. Just ask in plain English.

Calculate the Trade Balance in Python

The Trade Balance is available in the Economics module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_trade_balance as shown below.

from financetoolkit import Economics

economics = Economics(start_date='2020-01-01', end_date='2023-01-01')

economics.get_trade_balance(countries=['United States', 'Germany', 'China'])

Which returns:

  Germany China United States
2020 184386 2.45079e+06 -626202
2021 189652 2.97188e+06 -860029
2022 98724 3.89305e+06 -958935
2023 167656 2.73467e+06 -797342

Parameters

get_trade_balance accepts the following parameters:

  • countries (list[str] | str | None, optional): A list of countries or a single country to include in the results. Defaults to None.
  • rolling (int, optional): The rolling window size to use for smoothing the data (simple moving average). Defaults to None.
  • trailing (int, optional): The trailing window size to use for summing the data over trailing periods (e.g. a trailing-4-quarter sum). Defaults to None.
  • growth (bool, optional): Whether to return the growth data or the actual data. Defaults to False.
  • lag (int, optional): The number of periods to lag the growth data. Defaults to 1.
  • standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.

The Economics module page introduces the module, and the sidebar lists all of its functions.

Share