HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<head>
<title>TradingView Trading Platform 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://charting-library.tradingview-widget.com/charting_library/charting_library.standalone.js"></script>
<script type="text/javascript" src="https://charting-library.tradingview-widget.com/datafeeds/udf/dist/bundle.js"></script>
<script type="text/javascript" src="https://charting-library.tradingview-widget.com/broker-sample/dist/bundle.js"></script>
</head>
<body style="margin:0px;">
<main>
<div id="color_inputs">
<solid-color-input id="black" value="#000000"></solid-color-input>
<solid-color-input id="white" value="#ffffff"></solid-color-input>
<color-range-input id="color1" mid="#2962FF"></color-range-input>
<color-range-input id="color2" mid="#787B86"></color-range-input>
<color-range-input id="color3" mid="#F23645"></color-range-input>
<color-range-input id="color4" mid="#089981"></color-range-input>
<color-range-input id="color5" mid="#FF9800"></color-range-input>
<color-range-input id="color6" mid="#9c27b0"></color-range-input>
<color-range-input id="color7" mid="#ffeb3b"></color-range-input>
</div>
</div>
<div id="tv_chart_container"></div>
</div>
</main>
<div>
<p>Below is the JSON representation of the currently applied theme colors:</p>
<code><pre></pre></code>
</div>
</body>
</html>
body {
height: 100vh;
width: 100vw;
}
main {
display: flex;
flex-direction: row;
max-width: 500px;
}
#color_inputs {
width: 478px;
min-width: 478px;
}
#tv_chart_container {
flex: 1 0 auto;
height: 500px;
width: 478px;
}
.row, .column {
display: flex;
flex: 1;
flex-direction: column;
}
.column {
height: 100%;
}
.row {
width: 100%;
}
button {
flex: 1;
height: 40px;
font-size: 150%;
}
class ColorRangeInputElement extends HTMLElement {
static template = `
<style>
.container {
display: flex;
min-height: 1px;
min-width: 1px;
}
.color-block {
aspect-ratio: 1;
flex: 1 0 auto;
}
.color-input {
aspect-ratio: 1;
flex: 1;
height: 100%;
opacity: 0;
}
</style>
<label class="container">
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<div class="color-block" data-type="lighter"></div>
<input class="color-input" type="color" />
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
<div class="color-block" data-type="darker"></div>
</label>
`;
constructor() {
super();
this.attachShadow({ mode: "open" });
}
connectedCallback() {
this.shadowRoot.innerHTML = ColorRangeInputElement.template;
this.setAttribute("aria-label", "Color picker");
this.setAttribute("role", "button");
this.colorInputId = this.getAttribute("id") + "colorInput";
const colorInputElement = this.shadowRoot.querySelector("input");
colorInputElement.setAttribute("id", this.colorInputId);
colorInputElement.setAttribute("value", this.getAttribute("mid"));
colorInputElement.addEventListener("input", () => this._update());
this._update();
}
_interpolate(fromHexString, toHexString) {
const fromRgba = this._rgbaFromHex(fromHexString);
const toRgba = this._rgbaFromHex(toHexString);
const numberOfSteps = 12;
const results = [];
const step = 1 / numberOfSteps;
for (let t = step; t < 1 - step; t += step) {
const r = Math.round(fromRgba.r + (toRgba.r - fromRgba.r) * t)
.toString(16)
.padStart(2, "0");
const g = Math.round(fromRgba.g + (toRgba.g - fromRgba.g) * t)
.toString(16)
.padStart(2, "0");
const b = Math.round(fromRgba.b + (toRgba.b - fromRgba.b) * t)
.toString(16)
.padStart(2, "0");
results.push("#" + r + g + b);
}
return results;
}
_update() {
const colorInputElement = this.shadowRoot.getElementById(
this.getAttribute("id") + "colorInput"
);
const midColor = colorInputElement.value;
const lighterColors = this._interpolate("#ffffff", midColor);
const darkerColors = this._interpolate(midColor, "#000000");
this.setAttribute("mid", midColor);
this.setAttribute(
"value",
JSON.stringify([...lighterColors, midColor, ...darkerColors.slice(0, -1)])
);
window.requestAnimationFrame(() => {
this.shadowRoot.querySelector("label").style.backgroundColor = midColor;
const lighterDivs = this.shadowRoot.querySelectorAll(
'div[data-type="lighter"]'
);
lighterDivs.forEach((el, i) => {
el.style.backgroundColor = lighterColors[i];
});
const darkerDivs = this.shadowRoot.querySelectorAll(
'div[data-type="darker"]'
);
darkerDivs.forEach((el, i) => {
el.style.backgroundColor = darkerColors[i];
});
});
this.dispatchEvent(new Event("input"));
}
_rgbaFromHex(value) {
const rgba = {
r: parseInt(value.slice(1, 3), 16),
g: parseInt(value.slice(3, 5), 16),
b: parseInt(value.slice(5, 8), 16),
a: 1,
};
return rgba;
}
}
class SolidColorInputElement extends HTMLElement {
static template = `
<style>
.container {
display: flex;
min-height: 1px;
min-width: 1px;
}
.color-block {
aspect-ratio: 1;
flex: 1 0 auto;
}
.color-input {
aspect-ratio: 1;
flex: 1;
height: 100%;
opacity: 0;
}
</style>
<label class="container">
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<input class="color-input" type="color" />
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
<div class="color-block"></div>
</label>
`
constructor() {
super();
this.attachShadow({ mode: "open" });
}
connectedCallback() {
this.shadowRoot.innerHTML = SolidColorInputElement.template;
this.setAttribute("aria-label", "Color picker");
this.setAttribute("role", "button");
this.colorInputId = this.getAttribute("id") + "colorInput";
const colorInputElement = this.shadowRoot.querySelector("input");
colorInputElement.setAttribute("id", this.colorInputId);
colorInputElement.setAttribute("value", this.getAttribute("value"));
colorInputElement.addEventListener("input", () => this._update());
this._update();
}
_update() {
const colorInputElement = this.shadowRoot.getElementById(
this.getAttribute("id") + "colorInput"
);
const value = colorInputElement.value;
this.setAttribute("value", value);
window.requestAnimationFrame(() => {
this.shadowRoot.querySelector("label").style.backgroundColor = value;
const divs = this.shadowRoot.querySelectorAll(
'div'
);
divs.forEach((el, i) => {
el.style.backgroundColor = value;
});
});
this.dispatchEvent(new Event("input"));
}
}
window.customElements.define("color-range-input", ColorRangeInputElement);
window.customElements.define("solid-color-input", SolidColorInputElement);
window.customThemes = {
light: {
"color1": [],
"color2": [],
"color3": [],
"color4": [],
"color5": [],
"color6": [],
"color7": [],
"white": "#ffffff",
"black": "#000000"
},
dark: {
"color1": [],
"color2": [],
"color3": [],
"color4": [],
"color5": [],
"color6": [],
"color7": [],
"white": "#ffffff",
"black": "#000000"
}
};
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var theme = getParameterByName('theme') || 'light';
function initOnReady() {
var datafeedUrl = "https://demo-feed-data.tradingview.com";
var customDataUrl = getParameterByName('dataUrl');
if (customDataUrl !== "") {
datafeedUrl = customDataUrl.startsWith('https://') ? customDataUrl : `https://${customDataUrl}`;
}
var datafeed = new Datafeeds.UDFCompatibleDatafeed(datafeedUrl, undefined, {
maxResponseLength: 1000,
expectedOrder: 'latestFirst',
});
var widget = window.tvWidget = new TradingView.widget({
// debug: true, // uncomment this line to see Library errors and warnings in the console
autosize: true,
symbol: 'AAPL',
interval: '1D',
container: "tv_chart_container",
// BEWARE: no trailing slash is expected in feed URL
datafeed: datafeed,
library_path: "https://charting-library.tradingview-widget.com/charting_library/",
locale: getParameterByName('lang') || "en",
disabled_features: ["use_localstorage_for_settings"],
enabled_features: ["study_templates", 'dom_widget'],
charts_storage_url: 'https://saveload.tradingview.com',
charts_storage_api_version: "1.1",
client_id: 'trading_platform_demo',
user_id: 'public_user',
theme,
widgetbar: {
details: true,
news: true,
watchlist: true,
datawindow: true,
watchlist_settings: {
default_symbols: ["MSFT", "IBM", "AAPL"]
}
},
rss_news_feed: {
"default": [{
url: "https://demo-feed-data.tradingview.com/news?symbol={SYMBOL}",
name: "Yahoo Finance"
}]
},
broker_factory: function (host) { return new Brokers.BrokerSample(host, datafeed); },
broker_config: {
configFlags: {
supportNativeReversePosition: true,
supportClosePosition: true,
supportPLUpdate: true,
supportLevel2Data: false,
showQuantityInsteadOfAmount: true,
supportEditAmount: false,
supportOrderBrackets: true,
supportMarketBrackets: true,
supportPositionBrackets: true,
},
durations: [
{ name: 'DAY', value: 'DAY' },
{ name: 'GTC', value: 'GTC' },
],
},
});
function refreshCustomThemes() {
if (!window.tvWidget) {
return;
}
window.tvWidget.customThemes().then((api) => {
api.resetCustomThemes();
document.querySelectorAll('color-range-input').forEach((el) => {
window.customThemes[theme][el.id] = JSON.parse(el.getAttribute('value'));
});
document.querySelectorAll('solid-color-input').forEach((el) => {
window.customThemes[theme][el.id] = el.getAttribute('value');
});
api.applyCustomThemes(window.customThemes);
const prettyJson = JSON.stringify(window.customThemes.light).replaceAll('],', '],\n\t').replaceAll('{', '{\n\t').replaceAll(',"black"', ',\n\t"black"').replaceAll('}', '\n}')
document.querySelector('pre').textContent = prettyJson;
});
}
document.querySelectorAll('color-range-input').forEach((el) => {
el.addEventListener('input', refreshCustomThemes);
});
document.querySelectorAll('solid-color-input').forEach((el) => {
el.addEventListener('input', refreshCustomThemes);
});
// document.querySelector('button').addEventListener('click', () => {
// navigator.clipboard.writeText(JSON.stringify(window.customThemes.light));
// });
tvWidget.onChartReady(() => refreshCustomThemes());
};
window.addEventListener('DOMContentLoaded', initOnReady, false);
Also see: Tab Triggers