<html>
<head>
<title>TradingView Charting Library demo</title>
<!-- Fix for iOS Safari zooming bug -->
<meta
name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"
/>
<script
type="text/javascript"
src="https://trading-terminal.tradingview-widget.com/charting_library/charting_library.standalone.js"
></script>
<script
type="text/javascript"
src="https://trading-terminal.tradingview-widget.com/datafeeds/udf/dist/bundle.js"
></script>
<script type="text/javascript" src="https://trading-terminal.tradingview-widget.com/broker-sample/dist/bundle.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body style="margin: 0px">
<div id="tv_chart_container"></div>
</body>
</html>
xxxxxxxxxx
function generateDOMData(start, end, step, amount) {
const answer = [];
const steps = Math.abs((end - start) / step);
let count = 0;
for (let i = start; step < 0 ? i >= end : i <= end; i += step) {
count += 1;
answer.push({
price: i,
volume: (0.8 + 0.1 * Math.random()) * amount * ((steps - count) / steps)
});
}
return answer;
}
function initOnReady() {
class TestDatafeed extends Datafeeds.UDFCompatibleDatafeed {
constructor(datafeedURL, updateFrequency, limitedServerResponse) {
super(datafeedURL, updateFrequency, limitedServerResponse);
this._depthSubscriptions = {};
}
subscribeDepth(symbol, callback) {
const subscriptionUniqueId = this._createDepthSubscription(
'https://myserver.com',
symbol,
callback
);
return subscriptionUniqueId;
}
unsubscribeDepth(listenerID) {
this._removeDepthSubscription(listenerID);
}
_createDepthSubscription(server, symbol, callback) {
// Create an uniqueID
const uniqueId = Math.round(Math.random() * 10000000).toString(36);
const latestPrice = 173;
// Mocked data, using setInterval to fake data updates
const intervalId = setInterval(() => {
const data = {
snapshot: true,
bids: generateDOMData(
latestPrice + 0.05,
latestPrice + 10,
0.01,
10000
),
asks: generateDOMData(
latestPrice - 0.05,
latestPrice - 10,
-0.01,
10000
)
};
callback(data);
}, 1000);
this._depthSubscriptions[uniqueId] = intervalId;
return uniqueId;
}
_removeDepthSubscription(listenerID) {
const intervalId = this._depthSubscriptions[listenerID];
clearInterval(intervalId);
}
}
class CustomBroker extends Brokers.BrokerSample {
isTradable() {
/* If this property returns false, then when trying to view the DOM widget
nothing will be displayed, and after a minute (or a quite a few seconds)
console errors will start to appear.
*/
return Promise.resolve(true);
}
}
var datafeed = new TestDatafeed("https://demo-feed-data.tradingview.com");
var widget = (window.tvWidget = new TradingView.widget({
library_path:
"https://trading-terminal.tradingview-widget.com/charting_library/",
// debug: true, // uncomment this line to see Library errors and warnings in the console
fullscreen: true,
symbol: "AAPL",
interval: "1D",
container: "tv_chart_container",
datafeed: datafeed,
locale: "en",
disabled_features: ["order_panel"],
enabled_features: [
"dom_widget",
"hide_right_toolbar",
"hide_right_toolbar_tabs"
],
custom_formatters: {
studyFormatterFactory: (format, symbolInfo) => {
if (format.type === "volume") {
return {
format: (value) => value.toPrecision(8)
};
}
return null;
}
},
settings_adapter: {
initialSettings: {
"trading.chart.proterty": '{"showSellBuyButtons":0}'
},
setValue: function (key, value) {
console.log(key, value);
}
},
broker_factory: function (host) {
return new CustomBroker(host, datafeed);
},
broker_config: {
configFlags: {
supportLevel2Data: true,
supportClosePosition: true,
showQuantityInsteadOfAmount: true,
supportEditAmount: false,
supportOrderBrackets: true,
supportPositionBrackets: true
}
}
}));
}
window.addEventListener("DOMContentLoaded", initOnReady, false);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.