cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv
Planned migrations in progress! PRO features temporarily disabled.

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <div class="container login-container">
  <h1 class="login-title">Interactive code example</h1>
  <button type="button" class="login-btn">Click here to enable trading</button>
</div>
<div class="container loading-container">
  <div class="spinner">
    <div class="bounce1"></div>
    <div class="bounce2"></div>
    <div class="bounce3"></div>
  </div>
  <h1 id="loading-text"></h1>
</div>
<div class="container content-container">
  <form id="order-entry">
    <div class="input-container">
      <label class="input-label" for="instrument-input">Instrument</label>
      <input class="order-input typeahead" id="instrument-input" type="text" name="instrument" placeholder "Select instrument" value="EURUSD" />
    </div>
    <div class="input-container">
      <label class="input-label" for="amount-input">Amount</label>
      <input class="order-input" id="amount-input" type="number" name="amount" value="10000" />
    </div>
    <div class="buy-sell-container cf">
      <button class="buy-sell-btn sell-btn" value="SELL">
      <span class="buy-sell-label sell-label">Sell</span>
      <span class="buy-sell-label" id="sell-price"></span>
    </button>
      <button class="buy-sell-btn buy-btn" value="BUY">
      <span class="buy-sell-label buy-label">Buy</span>
      <span class="buy-sell-label" id="buy-price"></span>
    </button>
    </div>
  </form>
  <div class="position-container">
    <div class="net-position">
      <div class="net-position-left">
        <span class="net-position-item net-position-side net-position-buy">LONG</span>
        <span class="net-position-item net-position-amount">1000</span>
        <span class="net-position-item net-position-symbol">EURUSD</span>
      </div>
      <div class="net-position-right">
        <span class="net-position-item net-position-profit">0</span>
        <a class="net-position-item net-position-close square" href="" onclick="return closeSymbolPositions();"></a>
      </div>
    </div>
  </div>

  <button type="button" class="logout-btn" onclick='tradableEmbed.signOut()'>Disable trading</button>
  <pre id="test"></pre>
</div>

<div class="container success-container">
  <p>You <span id="success-side"></span></p>
  <p><span id="success-amount"></span> <span id="success-symbol"></span></p>
  <a href="" onclick="showOrderEntry(); return false;">New Trade</a>
</div>
            
          
!
            
              .cf:before,
.cf:after {
  content: " ";
  display: table;
}

.cf:after {
  clear: both;
}

body {
  font-size: 22px;
  margin: 20px;
  font-family: 'Roboto', sans-serif;
  font-weight: 300;
}

.loading-container, .content-container, .success-container{
  display: none;
}

h1{
  text-align: center;
  color: #999999;
  font-size: 28px;
  margin: 45px 0;
}

.login-btn, .logout-btn{
  margin: 0 auto;
  display: block;
  background: white;
  border: 1px solid #108993;
  color: #108993;
  text-transform: uppercase;
  font-size: 28px;
  height: 110px;
  max-width: 350px;
}

.logout-btn{
  height: 60px;
  line-height: 60px;
}

.spinner {
  margin: 100px auto 0;
  width: 70px;
  text-align: center;
}

.spinner > div {
  width: 18px;
  height: 18px;
  background-color: #d8d8d8;

  border-radius: 100%;
  display: inline-block;
  animation: sk-bouncedelay 1.4s infinite ease-in-out both;
}

.spinner .bounce1 {
  animation-delay: -0.32s;
}

.spinner .bounce2 {
  animation-delay: -0.16s;
}

@keyframes sk-bouncedelay {
  0%, 80%, 100% { 
    transform: scale(0);
  } 40% { 
    transform: scale(1.0);
  }
}

.order-input{
  display: block;
  text-align: right;
  width: 100%;
  color: #108993;
  border: none;
  border-bottom: 1px solid #c4cdcf;
  font-size: 28px;
  height: 40px;
  line-height: 40px;
  box-sizing: border-box;
  
  &[type="text"]{
    padding-right: 20px;
  }
}

.input-container, .buy-sell-container, .position-container, .success-container{
  max-width: 300px;
  margin: 30px auto;
  position: relative;
}

.input-label{
  position: absolute;
  left: 5px;
  font-size: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: #a2abad;
  text-transform: uppercase;
  z-index: 1;
}

.buy-sell-btn{
  height: 90px;
  border: none;
  display: block;
  width: 50%;
  float: left;
  background: none;
  
  &.sell-btn{
    text-align: right;
    border-right: 1px solid #c4cdcf;
    padding-right: 35px;
  }
  
  &.buy-btn{
    text-align: left;
    padding-left: 35px;
  }
}

.buy-sell-label{
  display: block;
  height: 50%;
  text-transform: uppercase;
  
  &.sell-label{
    color: #9d0049;
  }
  
  &.buy-label{
    color: #108993;
  }
}

.position-container{
  max-width: 400px;
  display: none;
  border-top: 1px solid #c4cdcf;
}

.net-position{
  font-size: 24px;
	color: #a2abad;
  height: 50px;
  line-height: 50px;
}

.net-position-right{
  float: right;
  text-align: right;
}

.net-position-left{
  float: left;
}

.net-position-buy{
  color: #108993;
}
  
.net-position-sell{
  color: #9d0049;
}

.net-position-item-mute{
  opacity: 0.5;
}

.tt-suggestion{
    padding: 0 10px;
    border-bottom: 1px solid lightgray;
    height: 40px;
    line-height: 40px;
  
  &.tt-cursor{
    background: lightblue;
  }
}

.tt-menu{
    width: 100%;
    background: white;
    border: 1px solid lightgray;
    border-top: none;
}

.success-container{
  text-align: center;
  
  p{
    font-size: 24px;
    line-height: 1.4;
    color: #424b55;
    margin: 0;
  }
  
  a{
    display: block;
    font-size: 24px;
  	color: #108993;
    margin-top: 20px;
  }
}

.square{
  width: 31px;
  height: 31px;
  position: relative;
  display: inline-block;
  top: 7px;
  
  &:before,&:after{
    content: '';
    position: absolute;
    width: 90%;
    height: 2px;
    background-color: #c4cdcf;
    top: 48%;
  }

  &:before{
    transform: rotate(45deg);
    left: 5%;
  }
  &:after{
    transform: rotate(-45deg);
    right: 5%;
  }
}
            
          
!
            
              var symbol = "EURUSD";
var instrumentId = "EURUSD";
var instrument = {};
var symbolPositions = [];
var globalOpenPositions = [];

function showLogin() {
  $(".content-container, .loading-container, .success-container").hide();
  $(".login-container").show();
}

function showLoading(loadingText) {
  $(".login-container, .content-container, .success-container").hide();
  $(".loading-container").show();
  $('#loading-text').html(loadingText);
}

function showOrderEntry() {
  $(".login-container, .loading-container, .success-container").hide();
  $(".content-container").show();
}

function showSuccess(side, amount, symbol) {
  $('#success-side').html(side === 'BUY' ? 'bought' : 'sold');
  $('#success-amount').html(amount);
  $('#success-symbol').html(symbol);
  $(".login-container, .loading-container, .content-container").hide();
  $(".success-container").show();
}

var instrumentBloodHound = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('symbol'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  identify: function(inst) {
    return inst.symbol;
  },
  local: []
});

function updateBloodhound(instruments) {
  instrumentBloodHound.clear();
  instrumentBloodHound.add(instruments);
  updateInstrument(instrumentBloodHound.get(symbol)[0]);
}

function updateMinMax(inst) {
  var $amountInput = $('#amount-input');
  $amountInput.attr('min', inst.minAmount);
  $amountInput.attr('max', inst.maxAmount);
}

function updateInstrument(inst) {
  tradable.removeInstrumentIdFromUpdates('myClientId', instrumentId);
  instrumentId = inst.instrumentId;
  symbol = inst.symbol;
  instrument = inst;
  tradable.addInstrumentIdToUpdates('myClientId', inst.instrumentId);
  updateMinMax(inst);
}

$('.typeahead').typeahead({
  hint: false,
  highlight: true,
  minLength: 1
}, {
  name: 'instrumentBloodHound',
  display: 'symbol',
  source: instrumentBloodHound
});

$('.typeahead').on('typeahead:select', function(event, suggestion) {
  updateInstrument(suggestion);
})

function convertAmountToCurrency(amount) {
  var roundedAmount = roundPrice(amount);
  if (roundedAmount < 0) {
    return '-' + tradableEmbed.selectedAccount.currencySign + Math.abs(roundedAmount);
  }
  return tradableEmbed.selectedAccount.currencySign + roundedAmount;
}

function roundPrice(price, symbol) {
  var precision = !!symbol ? instrumentBloodHound.get(symbol)[0].decimals : 2;
  var factor = Math.pow(10, precision);
  return Math.round(price * factor) / factor
}

function updateCurrentPositions(openPositions) {
  var totalAmount = 0;
  symbolPositions = [];
  globalOpenPositions = openPositions;
  for (var i = 0; i < globalOpenPositions.length; i++) {
    if (globalOpenPositions[i].symbol === symbol) {
      symbolPositions.push(globalOpenPositions[i]);
      totalAmount += Math.abs(globalOpenPositions[i].amount)
    }
  }
  if (symbolPositions.length === 0) {
    $('.position-container').hide();
    return;
  }

  var positionAmount = symbolPositions[0].side === 'BUY' ? symbolPositions[0].amount : -symbolPositions[0].amount;
  var openProfit = symbolPositions[0].openProfit;
  for (var j = 1; j < symbolPositions.length; j++) {
    positionAmount = symbolPositions[j].side === 'BUY' ? positionAmount + symbolPositions[j].amount : positionAmount - symbolPositions[j].amount;
    openProfit += symbolPositions[j].openProfit;
  }
  $('.net-position-side')
    .removeClass('net-position-buy net-position-sell')
    .addClass(positionAmount > 0 ? 'net-position-buy' : 'net-position-sell')
    .html(positionAmount > 0 ? 'LONG' : 'SHORT');
  $('.net-position-amount').html(positionAmount);
  $('.net-position-symbol').html(symbol);
  $('.net-position-profit')
    .removeClass('net-position-buy net-position-sell')
    .addClass(openProfit > 0 ? 'net-position-buy' : 'net-position-sell')
    .html(convertAmountToCurrency(openProfit));
  $('.position-container').show();
}

function closeSymbolPositions() {
  var positions = globalOpenPositions.slice(0)
  for (var i = 0; i < positions.length; i++) {
    if (positions[i].symbol === symbol) {
      tradableEmbed.closePosition(positions[i].id);
    }
  }
  return false;
}

tradableEmbed.onAccountUpdated(function(snapshot) {
  var price = null;
  for (var i = 0; i < snapshot.prices.length; i++) {
    if (snapshot.prices[i].symbol === symbol) {
      price = snapshot.prices[i];
      break;
    }
  }
  updateCurrentPositions(snapshot.positions.open);
  if (price === null) {
    return;
  }
  $('#sell-price').html(roundPrice(price.bid, price.symbol));
  $('#buy-price').html(roundPrice(price.ask, price.symbol));
});

$('.login-btn').on('click', function() {
  tradableEmbed.createForexDemoAccount();
  showLoading('Enabling trading');
});

tradableEmbed.onEmbedReady(function() {
  if (tradableEmbed.tradingEnabled) {
    showOrderEntry();
    updateBloodhound(tradableEmbed.availableInstruments);
  } else {
    showLogin();
  }
});

$('.buy-sell-btn').on('click', function(event) {
  event.preventDefault();
  if(!$('#order-entry').get(0).checkValidity()){
    return;
  }
  var side = $(this).val();
  var amount = $('#amount-input').val();
  var orderSymbol = $('#instrument-input').val();
  tradableEmbed.placeMarketOrder(amount, side, orderSymbol)
    .then(function(order) {
      showSuccess(order.side, order.amount, order.symbol)
    });
  var loadingText = (side === 'BUY' ? 'Buying ' : 'Selling ') + orderSymbol
  showLoading(loadingText);
});

$('.position-container-positions').on('click', '.position-close', function() {
  var id = $(this).data('position');
  tradableEmbed.closePosition(id);
})
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console