![Publish Package](https://github.com/carkod/binbot-charts/actions/workflows/npm-publish.yml/badge.svg)


Binbot charts is default candlestick bars chart used in terminal.binbot.com to render bots graphically, packaged to be used in React.
Import it in your project as a React component

`import TVChartContainer from 'binbot-charts'`

## Multi-Exchange Support

This library now supports multiple cryptocurrency exchanges. You can configure which exchange to use and which exchanges to make available for symbol search.

### Supported Exchanges

- **Binance** - Default exchange
- **KuCoin** - Added in v0.7.3+

### Usage Example

```jsx
import TVChartContainer, { SUPPORTED_EXCHANGES } from "binbot-charts";

function MyChart() {
  return (
    <TVChartContainer
      symbol="BTCUSDT"
      interval="1h"
      exchange="binance" // or "kucoin"
      supportedExchanges={["binance", "kucoin"]}
    />
  );
}
```

### Props

- `exchange` (optional, default: `"binance"`): The exchange to use for fetching data. Options: `"binance"`, `"kucoin"`
- `supportedExchanges` (optional, default: `["binance", "kucoin"]`): Array of exchanges to include in symbol search
- `symbol` (optional, default: `"BTCUSDT"`): Trading pair symbol
- `interval` (optional, default: `"1h"`): Chart interval/timeframe
- `timescaleMarks` (optional): Array of custom marks to display on the timeline
- `orderLines` (optional): Array of order lines to display on the chart
- `onTick` (optional): Callback function for real-time tick updates
- `getLatestBar` (optional): Callback function to get the latest bar data
- `onOrderLineMove` (optional): Receives `(id, price)` after a user finishes dragging an editable order line. Update the matching line in `orderLines` to accept the price; otherwise the line returns to its controlled price.
- `onOrderLineEditCancel` (optional): Called when Escape is pressed inside the chart. Set the selected line's `editable` flag to `false` to lock it again.

Each order line supports `editable?: boolean` (default `false`). A form can set
this flag on the line associated with its focused input. Keep that selection when
focus transfers into the chart iframe, and clear it when another field is selected
or the user clicks outside the chart. Price callbacks report price units; the
consumer is responsible for converting prices to percentages and validating them.
Programmatic price updates do not emit drag callbacks.

### Editing an expiry timestamp

Native timescale marks are not draggable. Pass a `timeMarker` while the expiry
input is selected to show a draggable vertical line and an X handle on the time
axis. Omit it on blur outside the chart or Escape. Keep the selection when focus
moves into the chart so the user can drag.

```tsx
<TVChartContainer
  timeMarker={editingExpiry ? {
    id: "conditional_expiry",
    time: expiresAt / 1000,
    text: "Conditional entry expires",
    color: "#e056b5",
  } : undefined}
  onTimeMarkerMove={(_id, seconds) => setExpiresAt(seconds * 1000)}
  onOrderLineEditCancel={() => setEditingExpiry(false)}
/>
```

`onTimeMarkerMove(id, time)` reports Unix seconds continuously during dragging;
update `timeMarker.time` to accept the change. Dragging uses minute precision;
the focused handle also supports Left/Right for one minute and Shift+Left/Right
for one hour. Update any related `timescaleMarks` from the same timestamp.

The overlay supports fixed-duration crypto intervals (seconds, minutes, days,
weeks) and projects future timestamps using actual candle spacing. It does not
snap future expiry to the last candle. It appears once candles are loaded and
the timestamp is visible; pan the chart if needed. Monthly and non-time-based
intervals do not show this handle. Plot positioning measures the canvas in the
bundled same-origin TradingView iframe and follows chart scrolling, zooming,
and resizing.

### Exchange Configuration

You can also import and use the exchange configurations directly:

```jsx
import { SUPPORTED_EXCHANGES, ExchangeConfig } from "binbot-charts";

// Access exchange configuration
const binanceConfig = SUPPORTED_EXCHANGES.binance;
console.log(binanceConfig.restApiUrl); // "https://api.binance.com"
```

## How to start

1. Run `npm install && npm start`. It will build the project and open a default browser with the Charting Library.
2. Run `npm run build` if it's the first time building the library. The example src project will use the final build.
> `library_path` should be `node_modules/dist/charting_library`
> Write a script to copy `charting_library` to `public/charting_library` during build. E.g. `cp -r node_modules/dist/charting_library/ src/public/charting_library`
3. Run `npm run format` and `npm run test` before pushing to the PR to avoid failed Github checks
4. To publish, bump the version in your PR with `npm version patch --no-git-tag-version` (or `minor`/`major`), commit `package.json` and `package-lock.json`, and merge the PR into `main`. The Publish Package workflow publishes an unpublished version to npm and creates its GitHub release.
5. To retry publishing manually, run **Publish Package** from the Actions tab and select the `main` branch. A version already on npm is not republished; a missing GitHub release is still created.


## About This Project

This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app).

## Notes

The earliest supported version of Node of the charting library for these examples is `v20`.
