JS-Api

What it is ?: A set of JS methods (specific public interface).

What should I do to use it ?: You should create a JS object which will receive the data by some way and respond to Charting Library requests.

Data caching (history & symbol info) is implemented in Charting Library. When you create an object implementing described interface, just pass it to Library widget constructor through [[datafeed argument|Widget-Constructor#datafeed-mandatory]].

Methods

  1. [[onReady|JS-Api#onreadycallback]]
  2. [[searchSymbols|JS-Api#searchsymbolsuserinput-exchange-symboltype-onresultreadycallback]]
  3. [[resolveSymbol|JS-Api#resolvesymbolsymbolname-onsymbolresolvedcallback-onresolveerrorcallback]]
  4. [[getBars|JS-Api#getbarssymbolinfo-resolution-from-to-onhistorycallback-onerrorcallback-firstdatarequest]]
  5. [[subscribeBars|JS-Api#subscribebarssymbolinfo-resolution-onrealtimecallback-subscriberuid-onresetcacheneededcallback]]
  6. [[unsubscribeBars|JS-Api#unsubscribebarssubscriberuid]]
  7. [[calculateHistoryDepth|JS-Api#calculatehistorydepthresolution-resolutionback-intervalback]]
  8. [[getMarks|JS-Api#getmarkssymbolinfo-startdate-enddate-ondatacallback-resolution-optional]]
  9. [[getTimescaleMarks|JS-Api#gettimescalemarkssymbolinfo-startdate-enddate-ondatacallback-resolution]]
  10. [[getServerTime|JS-Api#getservertimecallback]]

:chart: [[Trading Terminal]] specific:

  1. [[getQuotes|JS-Api#chart-getquotessymbols-ondatacallback-onerrorcallback]]
  2. [[subscribeQuotes|JS-Api#chart-subscribequotessymbols-fastsymbols-onrealtimecallback-listenerguid]]
  3. [[unsubscribeQuotes|JS-Api#chart-unsubscribequoteslistenerguid]]
  4. :chart: [[subscribeDepth|JS-Api#chart-subscribedepthsymbolinfo-callback-string]]
  5. :chart: [[unsubscribeDepth|JS-Api#chart-unsubscribedepthsubscriberuid]]

onReady(callback)

  1. callback: function(configurationData)
    1. configurationData: object (see below)

This call is intended to provide the object filled with configuration data.
This data affects some of chart behavior aspects so it is called [[server-side customization|Customization Overview#customization-done-through-data-stream]].
Charting Library expects you will call callback and pass your datafeed configurationData as an argument.
Configuration data is an object; for now, following properties are supported:

exchanges

An array of exchange descriptors. Exchange descriptor is an object {value, name, desc}. value will be passed as exchange argument to searchSymbols (see below).

exchanges = [] leads to exchanges filter absence in Symbol Search list. Use value = "" if you want to create wildcard filter (all exchanges).

symbols_types

An array of filter descriptors. Filter descriptor is an object {name, value}. value will be passed as symbolType argument to searchSymbols.

symbolsTypes = [] leads to types filter absence in Symbol Search list. Use value = "" if you want to create wildcard filter (all types)

supported_resolutions

An array of supported resolutions. Resolution must be a string. Format is described in another [[article|Resolution]].

'resolutions'=undefined or [] leads to resolutons widget having its default content (see http://tradingview.com/e/ ). Example: [1, 15, 240, "D", "6M"] will give you "1 minute, 15 minutes, 4 hours, 1 day, 6 months" in resolution widget.

supports_marks

Boolean showing whether your datafeed supports marks on bars or not.

supports_timescale_marks

Boolean showing whether your datafeed supports timescale marks or not.

supports_time

Set this one to true if your datafeed provides server time (unix time). It is used to adjust Countdown on the Price scale.

futures_regex

Set it if you want to group futures in the symbol search. This REGEX should divide an instrument name in 2 parts: a root and an expiration.
Sample regex: : /^(.+)([12]!|[FGHJKMNQUVXZ]\d{1,2})$/. It will be applied to the instruments whose type is futures.

searchSymbols(userInput, exchange, symbolType, onResultReadyCallback)

  1. userInput: string. It is text entered by user in symbol search field
  2. exchange: string. The requested exchange (chosen by user). Empty value means no filter was specified.
  3. symbolType: string. The requested symbol type: index, stock, forex e.t.c. (chosen by user). Empty value means no filter was specified.
  4. onResultReadyCallback: function(result)
    1. result: array (see below)

This call is intended to provide the list of symbols matching to user's search query. result is expected to be smth like this:

[
    {
        "symbol": <short symbol name>,
        "full_name": <full symbol name> // e.g., BTCE:BTCUSD
        "description": <symbol description>,
        "exchange": <symbol exchange name>,
        "ticker": <symbol ticker name, optional>,
        "type": "stock" | "futures" | "bitcoin" | "forex" | "index"
    }, {
        //    .....
    }
]

If no symbols are found, then callback should be called with an empty array. See more details about ticker value [[here|Symbology#ticker]]

resolveSymbol(symbolName, onSymbolResolvedCallback, onResolveErrorCallback)

  1. symbolName: string. Symbol name or ticker if provided.
  2. onSymbolResolvedCallback: function([[SymbolInfo|Symbology#symbolinfo-structure]])
  3. onResolveErrorCallback: function(reason)

Charting Library will call this function when it need to get [[SymbolInfo|Symbology#symbolinfo-structure]] by symbol's name.

getBars(symbolInfo, resolution, from, to, onHistoryCallback, onErrorCallback, firstDataRequest)

  1. symbolInfo: [[SymbolInfo|Symbology#symbolinfo-structure]] object
  2. resolution: string
  3. from: unix timestamp, leftmost required bar time
  4. to: unix timestamp, rightmost required bar time
  5. onHistoryCallback: function(array of bars, meta = { noData = false })
    1. bar: object {time, close, open, high, low, volume}
    2. meta: object {noData = true | false, nextTime - unix time}
  6. onErrorCallback: function(reason)
  7. firstDataRequest: boolean to identify the first history call for this symbol/resulution. When it is true you can ignore to (which depends on browser's Date.now()) and return bars up to current bar (including it).

This function is called when chart needs a history fragment defined by dates range. The charting library expects onHistoryCallback to be called just once after receiving all the requesting history. No further calls are expected.

Important: nextTime is a time of the next bar in the history. It should be set when there is no data in the requested period only.

Important: noData should be set when there is no data in the requested period and earlier only.

Remark: bar.time is expected to be the amount of milliseconds since Unix epoch start in UTC timezone.

Remark: bar.time for daily bars is expected to be a trading day (not session start day) at 00:00 UTC. Charting Library aligns time according to Session from SymbolInfo

Remark: bar.time for monthly bars is the first trading day of the month without the time part

subscribeBars(symbolInfo, resolution, onRealtimeCallback, subscriberUID, onResetCacheNeededCallback)

  1. symbolInfo: [[SymbolInfo|Symbology#symbolinfo-structure]] object
  2. resolution: string
  3. onRealtimeCallback: function(bar)
    1. bar: object {time, close, open, high, low, volume}
  4. subscriberUID: object
  5. onResetCacheNeededCallback (since 1.7): function() to be executed when bars data has changed

Charting Library calls this function when it wants to receive realtime updates for a symbol. Chart expects you will call onRealtimeCallback every time you want to update the most recent bar or to append a new one.

Remark: When you call onRealtimeCallback with bar having time equal to most recent bar's time, the whole last bar is replaced with the bar object you've passed into the call. Example:

  1. The most recent bar is {1419411578413, 10, 12, 9, 11}
  2. You call onRealtimeCallback({1419411578413, 10, 14, 9, 14})
  3. Library finds out that bar with time 1419411578413 already exists and is the most recent one
  4. Library replaces the whole bar so now the most recent bar is {1419411578413, 10, 14, 9, 14}

Remark 2: Is it possible either to update the most recent bar or to append a new one with onRealtimeCallback. You've get an error if you call this function trying to update a bar in history.

Remark 3: For now, there is no way to change bars in history after the chart received it.

unsubscribeBars(subscriberUID)

  1. subscriberUID: object

Library calls this function when is doesn't want to receive updates for this subscriber any more. subscriberUID will be the same object which Library passed to subscribeBars before.

calculateHistoryDepth(resolution, resolutionBack, intervalBack). Optional.

  1. resolution: requested symbol's resolution
  2. resolutionBack: desired history period dimension. Supported values: D | M
  3. intervalBack: amount or resolutionBack periods which Library is going to request

Charting Library calls this function when it is going to request some history data to give you an ability to override required history depth. It passes some arguments so you could know how much bars is it going to get. Here are a few examples:

  • calculateHistoryDepth("D", "M", 12) called: the Library is going to request 12 months of daily bars
  • calculateHistoryDepth("60", "D", 15) called: the Library is going to request 15 days of hourly bars

This function should return undefined if you do not want to override anything. If you do, it should return an object {resolutionBack, intervalBack}. Properties meaning is similar to respective arguments' one.

Example:

Assume the implementation is

Datafeed.prototype.calculateHistoryDepth = function(resolution, resolutionBack, intervalBack) {
    if (period == "1D") {
        return {
            resolutionBack: 'M',
            intervalBack: 6
        };
    }
}

This means when Charting Library will request the data for '1D' resolution, the history will be 6 months in depth. In all other cases the history depth will have the default value.

getMarks(symbolInfo, startDate, endDate, onDataCallback, resolution). Optional.

  1. symbolInfo: [[SymbolInfo|Symbology#symbolinfo-structure]] object
  2. startDate: unix timestamp (UTC). Leftmost visible bar's time.
  3. endDate: unix timestamp (UTC). Rightmost visible bar's time.
  4. onDataCallback: function(array of marks)
  5. resolution: string

Library calls this function to get [[marks|Marks-On-Bars]] for visible bars range. Chart expects you to call onDataCallback only once per each getMarks call. mark is an object having following properties:

  • id: unique mark id. Will be passed to a [[respective callback|Widget-Methods#onbarmarkclickedcallback]] when user clicks on a mark
  • time: unix time, UTC
  • color: red | green | blue | yellow | { border: '#ff0000', background: '#00ff00' }
  • text: mark popup text. HTML supported
  • label: a letter to be printed on a mark. Single character
  • labelFontColor: color of a letter on a mark
  • minSize: minimal size of mark (diameter, pixels)

A few marks per bar are allowed (for now, maximum is 10). Marks out of bars are not allowed.

Remark: This function will be called only if you declared your back-end is [[supporting marks|JS-Api#supports_marks]].

getTimescaleMarks(symbolInfo, startDate, endDate, onDataCallback, resolution). Optional.

  1. symbolInfo: [[SymbolInfo|Symbology#symbolinfo-structure]] object
  2. startDate: unix timestamp (UTC). Leftmost visible bar's time.
  3. endDate: unix timestamp (UTC). Rightmost visible bar's time.
  4. onDataCallback: function(array of marks)
  5. resolution: string

Library calls this function to get timescale marks for visible bars range. Chart expects you to call onDataCallback only once per each getTimescaleMarks call. mark is an object having following properties:

  • id: unique mark id. Will be passed to a [[respective callback|Widget-Methods#ontimescalemarkclickedcallback]] when user clicks on a mark
  • time: unix time, UTC
  • color: red | green | blue | yellow | ... | #000000
  • label: a letter to be printed on a mark. Single character
  • tooltip: array of text strings. Each element of the array is a new text line of a tooltip.

Only one mark per bar is allowed. Marks out of bars are not allowed.

Remark: This function will be called only if you declared your back-end is [[supporting marks|JS-Api#supports_timescale_marks]].

getServerTime(callback)

  1. callback: function(unixTime)

This function is called if configuration flag supports_time is set to true when chart needs to know the server time. The charting library expects callback to be called once. The time is provided without milliseconds. Example: 1445324591. It is used to display Countdown on the price scale.

:chart: [[Trading Terminal]] specific

:chart: getQuotes(symbols, onDataCallback, onErrorCallback)

  1. symbols: array of symbols names
  2. onDataCallback: function(array of data)
    1. data: [[symbol quote data|Quotes#symbol-quote-data]]
  3. onErrorCallback: function(reason)

This function is called when chart needs quotes data. The charting library expects onDataCallback to be called once when all requesting data received. No further calls are expected.

:chart: subscribeQuotes(symbols, fastSymbols, onRealtimeCallback, listenerGUID)

  1. symbols: array of symbols to be updated rarely (suggested frequency is once per minute). These symbols are in the watch list but they are not visible at the moment.
  2. fastSymbols: array of symbols to be updated quite frequently (once in 10 seconds or more often)
  3. onRealtimeCallback: function(array of data)
    1. data: [[symbol quote data|Quotes#symbol-quote-data]]
  4. listenerGUID: unique identifier of the listener

Trading Terminal calls this function when it wants to receive realtime quotes for a symbol. Chart expects you will call onRealtimeCallback every time you want to update quotes.

:chart: unsubscribeQuotes(listenerGUID)

  1. listenerGUID: unique identifier of the listener

Trading Terminal calls this function when is doesn't want to receive updates for this listener any more. listenerGUID will be the same object which Library passed to subscribeQuotes before.

:chart: subscribeDepth(symbolInfo, callback): String

  1. symbolInfo: [[SymbolInfo|Symbology#symbolinfo-structure]] object
  2. callback: function(depth)
    1. depth: object {snapshot, asks, bids}
      1. snapshot: Boolean - if true asks and bids have full set of depth, otherwise they contain only updated levels.
      2. asks: Array of {price, volume}
      3. bids: Array of {price, volume}

Trading Terminal calls this function when it wants to receive realtime level 2 (DOM) for a symbol. Chart expects you will call callback every time you want to update depth data.
This method should return unique identified (subscriberUID) that will be used to unsubscribe data.

:chart: unsubscribeDepth(subscriberUID)

  1. subscriberUID: String

Trading Terminal calls this function when is doesn't want to receive updates for this listener any more. subscriberUID will be the same object which you have returned from subscribeDepth.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350

推荐阅读更多精彩内容

  • 虽然画的不好,身体也没端正,不过,我会努力,一定会一天比一天好的。
    TSM丶猫儿阅读 327评论 0 1
  • 那天做完报告其实心情并不是特别好。我们小组忙前忙后,披星戴月了一个礼拜,最后却交不出一份能够让领导满意的答卷,即便...
    周启帆阅读 487评论 0 0
  • 花自飘零水自流 一种相思 两处闲愁 经典的婉约 让人叹为观止 但 至今思项羽 不肯过江东 豪放的诗句 不禁想起 诗...
    莫邪干剑阅读 209评论 0 0