Repository navigation
Widget Methods
Here is a list of supported widget's methods. You can call them using widget object returned to you by widget's constructor.
Remark: Please note that it's safe to call any method only after onChartReady callback is fired. So the common practice is to do smth like
widget.onChartReady(function() {
// now it's safe to call any other widget's methods
});#Methods
- Subscribing To Chart Events
- Chart Actions
- Studies And Shapes
- Saving/Loading Charts
- Custom UI Controls
- Trading Primitives
- Getters
#Subscribing To Chart Events
####onChartReady(callback)
-
callback: function()
The Charting Library will call the callback provided once when chart is initialized and ready. You can safely call all other methods from this moment.
####onSymbolChange(callback)
-
callback: function(symbolData) -
symbolData: object{name, exchange, description, type, interval}
The Charting Library will call the callback provided every time the main series symbol changes. New symbol info will be passed as argument.
####onIntervalChange(callback)
-
callback: function(interval) -
interval: string
The Charting Library will call the callback provided every time the main series interval changes. New interval will be passed as argument.
####onRealtimeTick(callback)
-
callback: function(barData) -
barData: object{time, open, high, low, close, volume}
The Charting Library will call the callback provided every time the new bar update comes.
####onAutoSaveNeeded(callback)
-
callback: function()
The Library will call the callback provided every time when user changes the chart. Chart change means any user action that can be undone. The callback will not be called more than once in five seconds.
####onMarkClick(callback)
-
callback: function(markId)
The Library will call the callback provided every time when user clicks a mark on bar. Mark ID will be passed as an argument.
####onGrayedObjectClicked(callback)
-
callback: function(subject) -
subject: object{type, name}1.type:drawing|study2.name: string, name of a subject which was clicked
The Library will call the callback provided every time when user clicks on grayed out object. Example:
new TradingView.widget({
drawings_access: {
type: "black",
tools: [
{ name: "Regression Trend" },
{ name: "Trend Angle", grayed: true },
]
},
studies_access: {
type: "black",
tools: [
{ name: "Aroon" },
{ name: "Balance of Power", grayed: true },
]
},
<...> // other widget settings
});
widget.onChartReady(function() {
widget.onGrayedObjectClicked(function(data) {
// this function will be called when one tries to
// create Balance Of Power study or Trend Angle shape
alert(data.name + " is grayed out!");
})
});####onScreenshotReady(callback)
-
callback: function(imageName)
The Library will call the callback provided every time when user creates a screenshot and server returns the created image name.
####onTick(callback)
-
callback: function(data)
The Library will call the callback provided every time when recent bar updates.
#Chart Actions
####setVisibleRange(range, callback)
-
range: object,{from to} -
from,to: unix timestamps, UTC -
callback:function(). The Library will call it after it's done with the viewport setup.
Forces the chart to adjust its parameters (scroll, scale) to make the selected time period fit the view port.
Neither from, nor to must not be in future. This method was introduced in version 1.2.
####setLanguage(locale)
-
locale: language code
Sets the Widget's language. For now, this call reloads the chart. Please avoid using it.
####setSymbol(symbol, interval, callback)
-
symbol: string -
interval: string -
callback: function()
Makes the chart to change its symbol and resolution. Callback is called after new symbol's data arrived.
####remove() Removes chart widget from your page.
####executeAction(action)
-
action: string
Executes any action from chart's context menu (the menu which is popped up when one right-clicks the empty space on a main pane) by its name. Use names as you see them in English localization. Examples:
// < ... >
widget.executeAction("Insert Indicator..."); // calling this will show `Insert Study` dialog
// < ... >
widget.executeAction("Hide All Drawing Tools"); // this will toggle all shapes visibility
// < ... >####refreshMarks()
Calling this method makes the Library to request visible marks once again.
#Studies And Shapes
####createStudy(name, forceOverlay, lock, inputs, callback, overrides)
-
name: string, a name of an indicator as you can see it inIndicatorswidget -
forceOverlay: forces the Charting Library to place the created study on main pane -
lock: boolean, shows whether a user will be able to remove/change/hide your study or not -
inputs: (since version1.2) an array of study inputs. This array is expected to contain just inputs values in the same order they are printed in study's properties page. -
callback: function(entityId) - 'overrides': (since version
1.2) an object containing properties you'd like to set for your new study.
Creates the study on a main symbol. Examples:
createStudy('MACD', false, false, [14, 30, 9])createStudy('Moving Average Exponential', false, false, [26])createStudy('Moving Average', false, false, [26], null, {"plot.color.0" : "#FF0000"})
Remark: Compare study has 2 inputs: [dataSource, symbol]. Supported dataSource values: ["close", "high", "low", "open"].
Remark 2: You actually use Overlay study when choose to Add a series on the chart. This study has the single input -- symbol.
####createShape(point, options, callback)
-
point: object{time, [price], [channel]} -
time: unix time. The only mandatory argument. -
price: If you specifyprice, then your icon will be placed on its level. If you do not, then the icon sticks to bar at respective time. -
channel: The price level to stick to is specified bychannelargument (open,high,low,close). If no channel is specified, 'open' is a default. -
options: object{shape, [text], [lock], [overrides]} -
shapemay be one of the ['arrow_up', 'arrow_down', 'flag', 'vertical_line', 'horizontal_line']. 'flag' is the default value. -
textis an optional argument. It's the text that will be assigned to shape if it can contain a text (for now, it's only 'arrow_up' and 'arrow_down'). -
lockshows whether a user will be able to remove/change/hide your shape or not. -
overrides(since version1.2). It is an object containing properties you'd like to set for your new shape. -
callback: function(entityId)
Supported overrides:
- Shape: vertical_line,
-
linecolor[#80CCDB] -
linewidth: [1] -
linestyle[0] -
showTime[true]
-
- Shape: horizontal_line
-
linecolor[#80CCDB] -
linewidth[1] -
linestyle[0] -
showPrice[true], -
showLabel[false], -
text[""], -
textcolor["#157760"], -
fontsize[12], -
bold[false], -
italic[false], -
horzLabelsAlign["center"], -
vertLabelsAlign["top"]
-
- Shape: arrow_up, arrow_down
-
color["#787878"] -
text[""] -
fontsize[20] -
font["Verdana"]
-
- Shape: flag
-
color[#FF0000]
-
Supported values for particular overrides:
-
linestyle:[0 (solid), 1 (dotted), 2 (dashed), 3 (large dashed)] -
linewidth:[1, 2, 3, 4] -
horzLabelsAlign:["center", "left", "right"] -
vertLabelsAlign:["top", "middle", "bottom"]
This call creates a shape at specified point on main series.
####removeEntity(entityId)
-
entityId: object. Value which was passed to your callback after the entity (shape of study) was created.
Removes the specified entity.
####createVerticalLine(point, options)
-
point: object{time} -
options: obejct{lock}
This function is a synonym for createShape with shape = 'vertical_line'. It is treated as obsolete.
####removeAllShapes() Removes all shapes (drawings) from the chart.
####removeAllStudies() Removed all studies from the chart.
#Saving/Loading Charts
####save(callback)
-
callback: function(object)
Saves the chart state to JS object. Charting Library will call your callback and pass the state object as argument. This call is a part of low-level save/load API.
####load(state)
-
state: object
Loads the chart from state object. This call is a part of low-level save/load API.
#Custom UI Controls
####onContextMenu(callback)
-
callback: function(unixtime, price). This callback is expected to return a value (see below).
The Library will call the callback provided every time when user opens context menu on the chart. Unix time and price of context menu point will be provided as arguments. To customize context menu items you have to return array of items descriptors. Item descriptor has following structure:
{
position: 'top' | 'bottom',
text: 'Menu item text',
click: <onItemClicked callback>
}
-
position: position of item in context menu -
text: text of menu item -
click: callback which will be called when user select your menu item
Example:
widget.onChartReady(function() {
widget.onContextMenu(function(unixtime, price) {
return [{
position: "top",
text: "First top menu item, time: " + unixtime + ", price: " + price,
click: function() { alert("First clicked."); }
}, {
position: "top",
text: "Second top menu item 2",
click: function() { alert("Second clicked."); }
}, {
position: "bottom",
text: "Bottom menu item",
click: function() { alert("Third clicked."); }
}];
});####createButton()
Creates a new DOM element in chart top toolbar and returns jQuery object for this button. You can use it to append custom controls right on the chart. Example:
widget.onChartReady(function() {
widget.createButton()
.attr('title', "My custom button tooltip")
.on('click', function (e) { alert("My custom button pressed!"); })
.append($('<span>My custom button caption</span>'));
});#Trading Primitives
####createOrderLine() Creates a new order on the chart and returns an API-object which you can use to control the order properties and behavior. It's strongly recommended to read this article before using this call.
API object methods:
-
remove(): Removes the position from the chart. You can’t use API-object after this call. -
onModify(callback)/onModify(data, callback) -
onMove(callback)/onMove(data, callback)
API object has a set of properties listed below. Each property should be used through respective accessors. I.e., if you want to work with Extend Left property, use getExtendLeft() of setExtendLeft() methods.
General properties:
| Property | Type | Supported Values | Default Value |
|---|---|---|---|
| Price | Double | Double | 0.0 |
| Text | String | String | "" |
| Tooltip | String | String | "" |
| Quantity | String | String | "" |
Connection line properties:
| Property | Type | Supported Values | Default Value |
|---|---|---|---|
| Extend Left | Boolean | "inherit" or Boolean | True |
| Line Length | Integer | "inherit" or 0 .. 100 | 0 |
| Line Style | Integer | "inherit" or 0 .. 2 | 2 |
| Line Width | Integer | "inherit" or 1 .. 4 | 1 |
Fonts:
| Property | Type | Default Value |
|---|---|---|
| Body Font | String | "bold 7pt Verdana" |
| Quantity Font | String | "bold 7pt Verdana" |
Colors:
| Property | Type | Default Value |
|---|---|---|
| Line Color | String | "rgb(255, 0, 0)" |
| Body Border Color | String | "rgb(255, 0, 0)" |
| Body Background Color | String | "rgba(255, 255, 255, 0.75)" |
| Body Text Color | String | "rgb(255, 0, 0)" |
| Quantity Border Color | String | "rgb(255, 0, 0)" |
| Quantity Background Color | String | "rgba(255, 0, 0, 0.75)" |
| Quantity Text Color | String | "rgb(255, 255, 255)" |
| Cancel Button Border Color | String | "rgb(255, 0, 0)" |
| Cancel Button Background Color | String | "rgba(255, 255, 255, 0.75)" |
| Cancel Button Icon Color | String | "rgb(255, 0, 0)" |
Example:
widget.createOrderLine()
.onMove(function() {
this.setText("onMove called");
})
.onModify("onModify called", function(text) {
this.setText(text);
})
.onCancel("onCancel called", function(text) {
this.setText(text);
})
.setText("STOP: 73.5 (5,64%)")
.setQuantity("2");####createPositionLine() Creates a new position on the chart and returns an API-object which you can use to control the position properties and behavior. It's strongly recommended to read this article before using this call.
API object methods:
-
remove(): Removes the position from the chart. You can’t use API-object after this call. -
onClose(callback)/onClose(data, callback) -
onModify(callback)/onModify(data, callback) -
onReverse(callback)/onReverse(data, callback)
API object has a set of properties listed below. Each property should be used through respective accessors. I.e., if you want to work with Extend Left property, use getExtendLeft() of setExtendLeft() methods.
General properties:
| Property | Type | Supported Values | Default Value |
|---|---|---|---|
| Price | Double | Double | 0.0 |
| Text | String | String | "" |
| Tooltip | String | String | "" |
| Quantity | String | String | "" |
Connection line properties:
| Property | Type | Supported Values | Default Value |
|---|---|---|---|
| Extend Left | Boolean | "inherit" or Boolean | True |
| Line Length | Integer | "inherit" or 0 .. 100 | 0 |
| Line Style | Integer | "inherit" or 0 .. 2 | 2 |
| Line Width | Integer | "inherit" or 1 .. 4 | 1 |
Fonts:
| Property | Type | Default Value |
|---|---|---|
| Body Font | String | "bold 7pt Verdana" |
| Quantity Font | String | "bold 7pt Verdana" |
Colors:
| Property | Type | Default Value |
|---|---|---|
| Line Color | String | "rgb(0, 113, 224)" |
| Body Border Color | String | "rgb(0, 113, 224)" |
| Body Background Color | String | "rgba(255, 255, 255, 0.75)" |
| Body Text Color | String | "rgb(0, 113, 224)" |
| Quantity Border Color | String | "rgb(0, 113, 224)" |
| Quantity Background Color | String | "rgba(0, 113, 224, 0.75)" |
| Quantity Text Color | String | "rgb(255, 255, 255)" |
| Reverse Button Border Color | String | "rgb(0, 113, 224)" |
| Reverse Button Background Color | String | "rgba(255, 255, 255, 0.75)" |
| Reverse Button Icon Color | String | "rgb(0, 113, 224)" |
| Close Button Border Color | String | "rgb(0, 113, 224)" |
| Close Button Background Color | String | "rgba(255, 255, 255, 0.75)" |
| Close Button Icon Color | String | "rgb(0, 113, 224)" |
Example:
widget.createPositionLine()
.onModify(function() {
this.setText("onModify called");
})
.onReverse("onReverse called", function(text) {
this.setText(text);
})
.onClose("onClose called", function(text) {
this.setText(text);
})
.setText("PROFIT: 71.1 (3.31%)")
.setQuantity("8.235")
.setPrice(15.5)
.setExtendLeft(false)
.setLineStyle(0)
.setLineLength(25);####createExecutionShape() Creates a new execution on the chart and returns an API-object which you can use to control the execution properties. It's strongly recommended to read this article before using this call.
API object has a set of properties listed below. Each property should be used through respective accessors. I.e., if you want to work with Extend Left property, use getExtendLeft() of setExtendLeft() methods.
General properties:
| Property | Type | Supported Values | Default Value |
|---|---|---|---|
| Price | Double | Double | 0.0 |
| Time | Integer | Unix time | 0 |
| Direction | String | "buy" or "sell" | "buy" |
| Text | String | String | "execution" |
| Tooltip | String | String | |
| Arrow Height | Integer | Integer | 8 |
| Arrow Spacing | Integer | Integer | 1 |
Fonts:
| Property | Type | Default Value |
|---|---|---|
| Font | String | "8pt Verdana" |
Colors:
| Property | Type | Default Value |
|---|---|---|
| Text Color | String | "rgb(0, 0, 0)"" |
| Arrow Color | String | "rgba(0, 0, 255)" |
Example:
widget.createExecutionShape()
.setText("@1,320.75 Limit Buy 1")
.setTooltip("@1,320.75 Limit Buy 1")
.setTextColor("rgba(0,255,0,0.5)")
.setArrowColor("#0F0")
.setDirection("buy")
.setTime(1413559061758)
.setPrice(15.5);#Getters
####symbolInterval(callback)
-
callback: function(result) -
result: object{symbol, interval}
Charting Library will call your callback with an object containing chart's symbol and interval.
####getVisibleRange(callback)
-
callback: function(range) -
range: object{from, to}.fromandtoare Unit timestamps in the timezone of chart.
The Library will call your callback and pass the current visible time range. This method was introduced in version 1.2.
#See Also
- What is Charting Library
- Getting Started
- Data Binding
- Chart Customization
- Customization Overview
- Client-Side Customization
- Server-Side Customization
- Customization Use Cases
- Trading Terminal
- Saving/Loading Charts
- Creating Custom Studies
- Breaking changes in versions