# Common Broker API issues

This article describes common issues that you might face when implementing the [Broker API].

## Timeout issue

You may encounter one of the following timeout issues:

- *Failed to close position: Position closing timeout*.
- *Failed to modify order: timeout waiting for new order*.
- *Failed to reverse position: Position reversing timeout*.

These issues happen because the library either received incorrect information or failed to receive timely updates for an order
or a position from your Broker API implementation.
To avoid these issues, ensure that:

- Your Broker API implementation calls [`orderUpdate`]/[`positionUpdate`] within 10 seconds
after the library sends a request to place/modify/cancel order or close/reverse position.
    This update is confirmation to the library that your backend server received the request.
- Your backend server and Broker API implementation provide the correct information to the library.

Consider the following example: a user closes a position of 10 AAPL shares.
In this case, the library calls the [`closePosition`] method to notify your backend server about the user's intent.
As a parameter, it provides your server with `positionId`.
After that, the library expects your backend server to close the position and provide an update on its new state within 10 seconds.

Your Broker API implementation should call the [`positionUpdate`] method on the Trading Host to provide updates.
As a parameter, it should send the correct [`Position`] object to the library, which means that:

- the `id` property should match `positionId`
- the `qty` property should be `0`
- other required properties are specified
- all properties correspond to the declared types

When the library gets a position update, for example with a non-zero quantity or a different ID,
it assumes the data is for a different position and waits for the correct data.
If the library waits more than 10 seconds, it returns *Failed to close position: Position closing timeout*.

## Buy/Sell buttons don't work

The *Buy/Sell* buttons may appear inactive or non-working for several reasons.

- **Buttons are not visible at all.** Make sure you are using the [Trading Platform], as the *Buy/Sell* buttons are only available there. Additionally, check the UI setting under *Settings → Trading → Buy/Sell buttons* and ensure the checkbox is enabled. If this setting is turned off, the buttons will not appear on the chart.
- **Buttons are disabled.** Ensure that the [`isTradable`] method returns `true` for tradable symbols. Without this method, the buttons remain disabled.
- **Buttons are enabled but show no prices.** Implement the [`symbolInfo`] method to provide the required symbol data. Without it, the buttons will not display bid/ask prices.
- **Clicking buttons doesn't place orders**. Implement [`placeOrder`] and other required methods of the Broker API.
- **Prices in buttons are not updating.** To show real-time bid/ask prices in the buttons, implement the [`getQuotes`] method.

## Order and position IDs mismatch

In some backend implementations, you might change the ID of an [order] or [position] between the time it’s submitted and the time it’s processed by your backend.
When this happens, the library may still reference the original ID, which no longer matches what's on your backend.
This mismatch can lead to issues in the UI.
For example, incorrect data may be displayed, and users may be unable to modify or cancel orders/positions because the library is referencing an invalid or unknown ID.

To recover from this mismatch, you can call the [`ordersFullUpdate`] and [`positionsFullUpdate`] methods.
These methods force the library to clear existing data and refetch the full list of orders or positions as if it was the first-time load.
Use these methods when you're unable to maintain a stable mapping between frontend and backend IDs.

:::warning

`ordersFullUpdate` and `positionsFullUpdate` are intended only for rare edge cases, such as dynamic ID changes.
These methods should not be used as part of your regular update flow.

If you need to notify the chart about a new or updated order/position, use [`orderUpdate`] or [`positionUpdate`] instead.
These methods are designed to update the UI without clearing and reloading all data.
They should be your **default approach** for keeping the chart in sync with your backend events.

:::

## Empty fields in bracket controls

You may encounter an issue when the input fields for bracket controls are empty.
Besides, users cannot enter values when accessing the *Edit position brackets* dialog.

This issue occurs because the library has not received all the necessary values for these fields.
To solve this issue, ensure that your integration meets the following requirements.

- The [`resolveSymbol`] method returns all required fields within the `LibrarySymbolInfo` object.
- The [`symbolInfo`] method returns all required fields within the `InstrumentInfo` object.
- You provide the library with the following updates:
  - Quote values via the [`getQuotes`]/[`subscribeQuotes`] method of the Datafeed API.
  - Pip values via the [`pipValueUpdate`] method of the Trading Host if [`subscribePipValue`] is implemented.
    Ignore this update if `subscribePipValue` is not implemented.
  - Equity values via the [`equityUpdate`] method of the Trading Host.
    :::tip
    It is advisable to send the equity updates once the broker has been created.
    If you want to provide updates while constructing the broker, encapsulate the updates within the [`setTimeout`] method.
    ```js
    setTimeout(() => {
      this._host.equityUpdate(12345678);
    }, 5);
    ```
    :::

## Symbol quantity is overridden

The default symbol quantity should be provided in the [`qty`] field of [`InstrumentInfo`] when the library calls [`symbolInfo`]. When a user modifies the quantity in the [Order Ticket](https://charting-library-docs.xstaging.tv/latest/trading_terminal/order-ticket.md), the library saves the new value using either local storage or [`settings_adapter`]. This user-defined quantity is then used instead of the default one for the next orders. This is intended behavior.

You can use the following methods to control quantity:

- [`getQty`]
- [`setQty`]
- [`subscribeSuggestedQtyChange`]
- [`unsubscribeSuggestedQtyChange`]

If you want to override the user-defined quantity, call the [`subscribeSuggestedQtyChange`] method to track quantity changes and then reset its value using [`setQty`].

```javascript
var widget = (window.tvWidget = new TradingView.widget({
    // Other widget properties

    broker_factory: function (host) {
        window.host = host;
        return new Brokers.BrokerDemo(host, datafeed);
    },
}));

// Declare a callback
const cb = (qty) => {
    console.log(`Quantity changed to ${qty}`);
};
// Subscribe to quantity changes for BTCUSD using the callback
host.subscribeSuggestedQtyChange('COINBASE:BTCUSD', cb);
// Change the quantity
host.setQty('COINBASE:BTCUSD', 24);
// Unsubscribe from quantity changes for BTCUSD
host.unsubscribeSuggestedQtyChange('COINBASE:BTCUSD', cb);
```

## P&L in bracket orders shows 0

If [profit and loss] (P&L) in [bracket orders] displays 0, check the [`pipValue`] property in the `InstrumentInfo` object returned by your [`symbolInfo`] method.
This issue occurs when `pipValue` is set to `0`.
Set it to a non-zero value to display P&L correctly.

Note that this behavior affects only instruments with P&L displayed in *Money* mode.
For *Ticks* and *Percentage* modes, P&L values appear as expected.

## Individual positions are not displayed on the chart

If you have enabled [position netting] but individual position lines are not displayed on the chart, check the [`supportPositionBrackets`] flag.
If `supportPositionBrackets` is set to `true`, the library prioritizes net positions and hides individual positions on the chart.

To display individual positions on the chart with [bracket functionality]:

1. Set [`supportPositionBrackets`] to `false`.
2. Set [`supportIndividualPositionBrackets`] to `true`.

You cannot enable both flags simultaneously if you wish to see individual positions on the chart.

[bracket functionality]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/brackets.md#position-brackets
[bracket orders]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/brackets.md
[Broker API]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts.md#broker-api
[`closePosition`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#closeposition
[`equityUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#equityupdate
[`getQuotes`]: https://charting-library-docs.xstaging.tv/latest/connecting_data/datafeed-api/trading-platform-methods.md#getquotes
[`getQty`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#getqty
[`InstrumentInfo`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.InstrumentInfo
[`isTradable`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#istradable
[order]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/orders.md
[`ordersFullUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#ordersfullupdate
[`orderUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#orderupdate
[`pipValue`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.InstrumentInfo#pipvalue
[`pipValueUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#pipvalueupdate
[`placeOrder`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#placeorder
[position]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/positions.md
[`Position`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.Position
[position netting]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/positions.md#position-netting
[`positionsFullUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#positionsfullupdate
[`positionUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#positionupdate
[profit and loss]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/profit-and-loss.md
[`qty`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.InstrumentInfo#qty
[`resolveSymbol`]: https://charting-library-docs.xstaging.tv/latest/connecting_data/datafeed-api/required-methods.md#resolvesymbol
[`setQty`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#setqty
[`setTimeout`]: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout
[`settings_adapter`]: https://charting-library-docs.xstaging.tv/latest/saving_loading/user-settings.md#settings-adapter
[`subscribePipValue`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#subscribepipvalue
[`subscribeQuotes`]: https://charting-library-docs.xstaging.tv/latest/connecting_data/datafeed-api/trading-platform-methods.md#subscribequotes
[`subscribeSuggestedQtyChange`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#subscribesuggestedqtychange
[`supportIndividualPositionBrackets`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.BrokerConfigFlags#supportindividualpositionbrackets
[`supportPositionBrackets`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.BrokerConfigFlags#supportpositionbrackets
[`symbolInfo`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#symbolinfo
[Trading Platform]: https://charting-library-docs.xstaging.tv/latest/trading_terminal.md
[`unsubscribeSuggestedQtyChange`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#unsubscribesuggestedqtychange

---

## Sitemap

- [All documentation pages](https://charting-library-docs.xstaging.tv/llms.txt)
- [Full page map with headings](https://charting-library-docs.xstaging.tv/docs_map.md)
