Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                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%;
}

              
            
!

JS

              
                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);

              
            
!
999px

Console