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

              
                <div id="ticker">

  <div class="ticker">

    <div class="labels">
      <div class="label" v-for="label in labels" v-bind:style="{ 'top': getCandleOffset(label) }">
        <div class="line"></div>
        <span>{{label | price(product)}}</span>
      </div>
      <div class="price" v-bind:style="{ 'top': getCandleOffset(current.close) }">
        <span>{{current.close | price(product) }}</span>
      </div>
    </div>

    <ol>
      <li v-for="candle in displayCandles" v-bind:class="{ 'up': candle.open <= candle.close }">
        <div class="min-max" v-bind:style="{ 
          'height': getCandleHeight(candle.min, candle.max), 
          'top': getCandleOffset(candle.min, candle.max) }">
        </div>
        <div class="candle" 
           v-bind:style="{ 
            'height': getCandleHeight(candle.open, candle.close), 
            'top': getCandleOffset(candle.open, candle.close) }">
        </div>        
      </li>
      
      <li v-bind:class="{ 'up': current.open <= current.close }">
        <div class="min-max" 
          v-bind:style="{ 
            'height': getCandleHeight(current.min, current.max), 
            'top': getCandleOffset(current.min, current.max) }">
        </div>
        <div class="candle current" 
             v-bind:data-open="open" 
             v-bind:style="{
              'height': getCandleHeight(current.open, current.close), 
              'top': getCandleOffset(current.open, current.close) }">
        </div>
      </li>
    </ol>

    <div class="max">${{displayMax | price(product) }}</div>
    <div class="min">${{displayMin | price(product) }}</div>

    <div class="controls">

      <select v-model="product">
        <option>BTC-USD</option>
        <option>LTC-USD</option>
        <option>LTC-BTC</option>
        <option>ETH-USD</option>
      </select>

      <select v-model="candleCount">
        <option value="20">20 candles</option>
        <option value="40">40 candles</option>
        <option value="80">80 candles</option>
      </select>

      <select v-model="interval">
        <option value="1000">1s</option>
        <option value="5000">5s</option>
        <option value="10000">10s</option>
        <option value="30000">30s</option>
        <option value="60000">1min</option>
        <option value="300000">5min</option>
        <option value="900000">15min</option>
        <option value="3600000">1hr</option>
        <option value="21600000">6hr</option>
        <option value="86400000">1day</option>
      </select>

      <span class="price">Price: <u>{{current.close | price(product) }}</></span>

    </div>
  </div>

</div>
              
            
!

CSS

              
                $col1: darken(#1e2b34, 2%);
$col2: darken(#1e2b34, 3%);

body,
html {
  -webkit-user-select: none;
  width: 100%;
  height: 100%;
  background-color: #111;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

#ticker {
  height: 100%;
  width: 100%;
  position: absolute;
  box-sizing: border-box;
}

.ticker {
  position: relative;
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3),
    inset 0px 0px 35px 10px (rgba(20, 20, 20, 1));
  background-color: #1e2b34;
  background-image: -webkit-linear-gradient(
      45deg,
      $col1 25%,
      transparent 25%,
      transparent 75%,
      $col1 75%,
      $col1 100%
    ),
    -webkit-linear-gradient(45deg,
      $col1 25%, 
      $col2 25%, 
      $col2 75%, 
      $col1 75%, 
      $col1 100%);
  background-size: 100px 99px;
  background-position: 0 0, 50px 50px;
  height: 100%;
  width: 100%;
  color: white;
  padding: 30px 100px 45px 10px;
  display: table;
  table-layout: fixed;
  box-sizing: border-box;

  ol {
    margin: 0;
    padding: 0;
    height: 100%;
    display: table-row;
  }

  li {
    list-style-type: none;
    display: table-cell;
    position: relative;
    height: 100%;
    width: 100%;

    .candle {
      position: absolute;
      width: 90%;
      border: solid 1px #eb6e40;
      background-color: #ff7541;
      min-height: 1px;
      box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.3);
      background: repeating-linear-gradient(
        45deg,
        #ff7541,
        #ff7541 40px,
        darken(#ff7541, 3%) 40px,
        darken(#ff7541, 3%) 80px
      );
      cursor: pointer;
      
      &:hover {
        border: solid 1px #ccc !important;
      }
    }

    .min-max {
      width: 1px;
      position: absolute;
      left: 45%;
      background-color: #ff7541;
    }

    &.up {
      .min-max,
      .candle {
        background: none;
        background-color: #477749;
        border-color: #569253;
      }

      .candle {
        background-color: #21313b;
        _background: repeating-linear-gradient(
          -45deg,
          #21313b,
          #21313b 40px,
          lighten(#21313b, 1%) 40px,
          lighten(#21313b, 1%) 80px
        );
      }
    }
  }

  .max,
  .min {
    width: 100px;
    right: -25px;
    position: absolute;
    text-align: left;
    font-size: 11px;
    font-family: Courier;
    text-decoration: underline;
    color: #999;
    display: none;
  }

  .max {
    top: 24px;
  }

  .min {
    bottom: 40px;
  }
}

.labels {
  position: absolute;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: table;

  .label {
    position: absolute;
    width: 100%;
    .line {
      position: absolute;
      border-bottom: rgba(255, 255, 255, 0.09) 1px dashed;
      width: 100%;
      height: 2px;
      top: -3px;
    }
    span {
      position: absolute;
      right: 30px;
      top: -15px;
      font-size: 11px;
      color: #999;
      font-family: Courier;
    }
  }

  .price {
    position: absolute;
    z-index: 2;
    right: 21px;

    span {
      position: relative;
      top: -13px;
      text-decoration: underline;
      display: block;
      color: black;
      background-color: #aaa;
      font-size: 11px;
      padding: 5px 9px 5px 9px;
      height: 16px;
      font-family: Courier;
      line-height: 16px;
      border-radius: 4px;

      &:before {
        position: absolute;
        display: block;
        content: "";
        left: -9px;
        top: 11px;
        width: 11px;
        height: 4px;
        background-color: #aaa;
      }
    }
  }
}

.controls {
  position: absolute;
  left: 10px;
  bottom: 11px;
  select {
    display: inline-block;
    -webkit-appearance: none;
    box-shadow: 0 0 10px 3px rgba(0, 0, 0, 0.5);
    border: solid 1px white;
    padding: 3px 8px;
    cursor: pointer;
    margin: 0px;
    opacity: 0.7;
    font-size: 10px;
    background-color: #141414;
    color: #ccc;
    border-radius: 0;
    text-align-last: center;
    font-size: 11px;
    &:hover {
      color: white;
      border-color: white;
    }
  }
  
  .price {
    color: #c8c8c8;
    font-family: Courier;
    position: relative;
    top: 1px;
    left: 6px;
    font-size: 14px;
  }
}

              
            
!

JS

              
                const DEFAULT_CANDLE_COUNT = 80;
const DEFAULT_CANDLE_INTERVAL = 300000;
const DEFAULT_PRODUCT = "LTC-USD";

const FEED_URL = "wss://ws-feed.gdax.com";
const HISTORY_API = "https://api.gdax.com/products/";

let _ws = null;
let _interval = null;

Vue.filter("price", function (value, product) {
  let unit = "usd";

  if (product) {
    unit = product.split("-")[1].toLowerCase();
  }

  if (unit === "usd") {
    return `$${parseFloat(Math.round(value * 100) / 100).toFixed(2)}`;
  } else {
    return `${value}`;
  }
});

async function getOldCandles(product, interval) {
  return new Promise((resolve, reject) => {
    $.getJSON(
      `${HISTORY_API}/${product}/candles?granularity=${interval / 1000}`,
      function (response) {
        response = response.sort((a, b) => a[0] - b[0]);
        for (var i = 1; i < response.length; i++) {
          response[i][3] = response[i - 1][4];
        }
        const candles = response.map((candle) => ({
          open: candle[3],
          close: candle[4],
          min: candle[1],
          max: candle[2]
        }));
        resolve(candles);
      }
    );
  });
}

const ticker = new Vue({
  el: "#ticker",
  data: {
    product: DEFAULT_PRODUCT,
    candles: [],
    candleCount: DEFAULT_CANDLE_COUNT,
    current: {
      open: 0,
      close: 0,
      min: Infinity,
      max: 0
    },
    interval: DEFAULT_CANDLE_INTERVAL
  },
  computed: {
    displayCandles: function () {
      if (this.candles.length > this.candleCount) {
        return this.candles.slice(
          Math.max(this.candles.length - this.candleCount, 1)
        );
      } else {
        return this.candles;
      }
    },
    displayBoundsCandles: function () {
      if (this.candles.length > this.candleCount * 2) {
        return this.candles.slice(
          Math.max(this.candles.length - this.candleCount * 2, 1)
        );
      } else {
        return this.candles;
      }
    },
    displayMax: function () {
      return Math.max.apply(null, [
        ...this.displayBoundsCandles.map((v) => v.max),
        this.current.max
      ]);
    },
    displayMin: function () {
      return Math.min.apply(null, [
        ...this.displayBoundsCandles.map((v) => v.min),
        this.current.min
      ]);
    },
    labels: function () {
      const min = this.displayMin;
      const max = this.displayMax;
      const diff = max - min;

      if (diff === 0) {
        return [];
      }

      const step = diff / 5;

      var arr = [];

      for (var i = 0; i <= 5 + 1; i++) {
        let val = min + i * step;
        val = parseFloat(Math.round(val * 100) / 100).toFixed(2);
        arr.push(val);
      }

      arr = arr.filter((item, i) => arr.indexOf(item) === i);

      return arr;
    }
  },
  mounted: function () {
    this.init();
  },
  watch: {
    product: function () {
      this.init();
    },
    interval: function () {
      this.init();
    }
  },
  methods: {
    init: async function () {
      let product = this.product;

      this.candles.splice(0);
      this.current.open = 0;
      this.current.close = 0;
      this.current.min = Infinity;
      this.current.max = 0;

      let candles;

      if (this.interval >= 10000) {
        candles = await getOldCandles(this.product, this.interval);
        this.current.open = candles[candles.length - 1].close;
        this.current.close = candles[candles.length - 1].close;
      } else {
        candles = [];
      }

      this.candles = candles;

      if (_ws) {
        _ws.close();
      }

      _ws = new WebSocket(FEED_URL);
      _ws.onopen = () => {
        _ws.send(
          JSON.stringify({
            type: "subscribe",
            product_ids: [this.product],
            channels: [
              "ticker",
              {
                name: "ticker",
                product_ids: [this.product]
              }
            ]
          })
        );
      };

      _ws.onmessage = (evt) => {
        const response = JSON.parse(evt.data);
        const price = {
          // sequence: response.sequence,
          // time: new Date(response.time),
          value: parseFloat(response.price)
        };
        if (price.value) ticker.add(price, product);
      };

      if (_interval) {
        clearInterval(_interval);
      }

      _interval = setInterval(() => {
        const historicVal = {
          open: this.current.open,
          close: this.current.close,
          min: this.current.min,
          max: this.current.max
        };

        this.candles.push(historicVal);
        this.current.open = historicVal.close;
        this.current.close = historicVal.close;
        this.current.min = Infinity;
        this.current.max = 0;
      }, this.interval);
    },
    add: function (price, product) {
      if (product !== this.product) {
        return;
      }

      this.current.close = price.value;
      this.current.open = this.current.open || price.value;
      if (price.value > this.current.max) {
        this.current.max = price.value;
      }
      if (price.value < this.current.min) {
        this.current.min = price.value;
      }
    },
    getCandleHeight: function (open, close) {
      if (open === Infinity || close === Infinity) {
        return 0;
      }
      const diff = Math.abs(open - close);
      const window = this.displayMax - this.displayMin;
      const heightPerc = (diff / window) * 100;
      return `${heightPerc}%`;
    },
    getCandleOffset: function (open, close) {
      if (open === Infinity || close === Infinity) {
        return 0;
      }

      let top = Math.max(open, close);

      if (!close) {
        top = open;
      }

      const window = this.displayMax - this.displayMin;
      const positionPerc = 100 - ((top - this.displayMin) / window) * 100;
      return `${positionPerc}%`;
    }
  }
});

              
            
!
999px

Console