forex pip calculator

by trisna dev

HTML

<h2>Kalkulator Margin XAUUSD</h2>

  <label for="modal">Modal ($):</label>
  <input type="number" id="modal" value="60" step="0.01">

  <label for="leverage">Leverage:</label>
  <input type="number" id="leverage" value="100">

  <label for="lot">Lot (misal 0.01):</label>
  <input type="number" id="lot" value="0.01" step="0.01">

  <label for="harga">Harga XAUUSD (misal 3327):</label>
  <input type="number" id="harga" value="3327" step="0.01">

  <label for="stopOut">Stop-out level (%):</label>
  <select id="stopOut">
    <option value="0">0%</option>
    <option value="0.2">20%</option>
    <option value="0.5" selected>50%</option>
    <option value="0.6">60%</option>
  </select>

  <button onclick="hitung()">Hitung</button>

  <div class="result" id="hasil"></div>

CSS

body {
      font-family: Arial, sans-serif;
      padding: 20px;
      max-width: 400px;
      margin: auto;
      background: #f9f9f9;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    label {
      display: block;
      margin-top: 10px;
    }
    input, select {
      width: 100%;
      padding: 8px;
      margin-top: 4px;
    }
    button {
      margin-top: 15px;
      padding: 10px;
      width: 100%;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 5px;
    }
    .result {
      margin-top: 20px;
      background: #fff;
      padding: 10px;
      border-radius: 5px;
    }

JavaScript

function hitungMarginDanPip({ modal, leverage, lot, hargaXAUUSD, stopOutLevel }) {
      const ouncePerLot = 100;
      const ounce = lot * ouncePerLot;
      const margin = (hargaXAUUSD * ounce) / leverage;

      const equityAtStopOut = modal * (1 - stopOutLevel);
      const freeMargin = equityAtStopOut - margin;

      const nilaiPerPip = ounce * 0.01; // pip = 0.01 dollar per ounce
      const pipYangBisaDitahan = freeMargin > 0 ? freeMargin / nilaiPerPip : 0;

      return {
        margin: margin.toFixed(2),
        freeMargin: freeMargin.toFixed(2),
        nilaiPerPip: nilaiPerPip.toFixed(2),
        pipYangBisaDitahan: Math.floor(pipYangBisaDitahan)
      };
    }

    function hitung() {
      const modal = parseFloat(document.getElementById("modal").value);
      const leverage = parseFloat(document.getElementById("leverage").value);
      const lot = parseFloat(document.getElementById("lot").value);
      const hargaXAUUSD = parseFloat(document.getElementById("harga").value);
      const stopOutLevel = parseFloat(document.getElementById("stopOut").value);

      const hasil = hitungMarginDanPip({
        modal,
        leverage,
        lot,
        hargaXAUUSD,
        stopOutLevel
      });

      document.getElementById("hasil").innerHTML = `
        <strong>Hasil Perhitungan:</strong><br>
        Margin: $${hasil.margin}<br>
        Free Margin: $${hasil.freeMargin}<br>
        Nilai Per Pip: $${hasil.nilaiPerPip}<br>
        Pip yang Bisa Ditahan: ${hasil.pipYangBisaDitahan} pip
      `;
    }