What you’ll need
- Access to trading view codebase repo (need manual approval from Trading View)
- An API key from the Dashboard (only for production use, you can use the API without an API key in development mode)
- A react app (or any other framework) to display the chart
Open Source Reference Implementation
Before diving into the code, check out the production-ready implementation in the MTT (Mobula Trader Terminal) codebase:| File | Description |
|---|---|
datafeed.tsx | Complete datafeed with REST + WebSocket OHLCV streaming |
index.tsx | TradingView chart component with theme support |
token/page.tsx | Token page with chart integration |
pair/page.tsx | Pair page with chart integration |
- Token page: mtt.gg/token/solana:solana/So11111111111111111111111111111111111111112
- Pair page: mtt.gg/pair/solana:solana/YOUR_PAIR_ADDRESS
Walkthrough
1
Identify your asset
Pick your asset symbol, name or address. If it is a symbol/name, make sure to check case sensitivity and to respect the asset name as listed on Mobula curated token list (explorable here). If it is an address, make sure to check the blockchain supported (check here for the full list) and to respect the blockchain ID format.You can also use pair address directly (if using asset, we will route to the largest liquidity pool for this asset).
2
Setup Chart Component
Let’s first setup the main chart component - you must host the trading view lib files inside the public folder of your app, in our
example, we host them at static/charting_library.Here’s a simplified version. For the full production implementation with theme support, loading states, and tool persistence, see the MTT chart component.
3
Setup the Datafeed with Token & Pair Support
The datafeed handles both token-based and pair-based charts. This is the key difference in the MTT implementation:
- Token mode (
isPair: false): Usesassetparameter to route to the highest liquidity pool - Pair mode (
isPair: true): Usesaddressparameter for a specific pool
4
Fetch Historical Data (getBars)
The
getBars function fetches historical OHLCV data. The key is handling both token and pair modes:5
Subscribe to Real-Time OHLCV Stream
The MTT implementation uses Mobula’s WebSocket
ohlcv stream for real-time candle updates:6
Resolution Normalization
The MTT datafeed supports sub-second resolutions. Here’s the normalization function:
Token Page vs Pair Page
The MTT terminal has two types of pages that use charts differently:Token Page (/token/[blockchain]/[address])
Uses the asset mode which automatically routes to the highest liquidity pool:
Pair Page (/pair/[blockchain]/[address])
Uses the pair mode for a specific pool/pair:
Supported Resolutions
The MTT implementation supports these resolutions:| Resolution | Period | Description |
|---|---|---|
1s | 1 second | Sub-second precision |
5s | 5 seconds | |
15s | 15 seconds | |
30s | 30 seconds | |
1 / 1m | 1 minute | |
5 / 5m | 5 minutes | |
15 / 15m | 15 minutes | |
60 / 1h | 1 hour | |
1D / 1d | 1 day | |
1W / 1w | 1 week | |
1M | 1 month |
Full Example: Complete Datafeed
For the complete production-ready datafeed with all edge cases handled, see: src/components/charts/datafeed.tsx Key features in the production implementation:- Request deduplication with
pendingRequestsmap - Last bar caching for smooth real-time updates
- First candle gap handling (fills gaps between last historical bar and first streaming candle)
- Proper subscription cleanup
- Support for both token and pair modes