Trade Calculator
Trade Calculator
by Hifni Nazeer
HTML
<div>
<select class="form-control" id="cmbTxnType">
<option value="B" selected>BUY</option>
<option value="S">SELL</option>
</select>
</div>
<div>
<input type="text" class="form-control" placeholder="Price" id="txtPrice">
</div>
<div>
<input type="text" class="form-control" placeholder="Quantity" id="txtQty">
</div>
<div>
<button class="btn btn-default" id="btnCalculate">Calculate</button>
</div>
<div>
<label id="bsCaption"></label>
<input type="text" class="form-control" placeholder="Best Even Price" id="txtBestEvenPrice" value="0" readonly>
</div>
JavaScript
$(document).ready(function() {
alert('ready');
$('#bsCaption').html("You have to Sell at LKR");
$('#cmbTxnType').on('change', function() {
var bs = $(this).val();
var bsText = "You have to Buy at LKR";
if (bs == 'B')
bsText = "You have to Sell at LKR";
$('#bsCaption').html(bsText);
});
$('#btnCalculate').click(function() {
var feeRate = (1.12 / 100);
var bs = $('#cmbTxnType :selected').val();
var inputPrice = $('#txtPrice').val();
//validation
if (!$.isNumeric(inputPrice)){
alert("Price is not a valid Number");
return;
}
var inputQty = $('#txtQty').val();
//validation
if (!$.isNumeric(inputQty)){
alert("Quantity is not a valid Number");
return;
}
var principalVal = inputPrice * inputQty;
if (bs !== 'B')
feeRate = feeRate * (-1);
var netVal = principalVal * (1 + (feeRate));
var bestPrice = (principalVal * (1 + feeRate)) / (inputQty * (1 - feeRate));
alert(bestPrice);
$('#txtBestEvenPrice').val(bestPrice.toFixed(2) );
});
});