var quoteData = [{"pair":"USD\/JPY","bid":"83.681","ask":"83.711"},{"pair":"EUR\/USD","bid":"1.26827","ask":"1.26847"},{"pair":"GBP\/USD","bid":"1.54231","ask":"1.54271"},{"pair":"USD\/CHF","bid":"1.01119","ask":"1.01149"},{"pair":"USD\/CAD","bid":"1.03781","ask":"1.03831"},{"pair":"AUD\/USD","bid":"0.92072","ask":"0.92102"},{"pair":"EUR\/GBP","bid":"0.82193","ask":"0.82243"},{"pair":"EUR\/JPY","bid":"106.142","ask":"106.182"},{"pair":"GBP\/JPY","bid":"129.048","ask":"129.118"},{"pair":"EUR\/CHF","bid":"1.28273","ask":"1.28313"},{"pair":"CHF\/JPY","bid":"82.728","ask":"82.768"},{"pair":"GBP\/CHF","bid":"1.55987","ask":"1.56037"},{"pair":"EUR\/AUD","bid":"1.37681","ask":"1.37761"},{"pair":"EUR\/CAD","bid":"1.31622","ask":"1.31712"},{"pair":"AUD\/CAD","bid":"0.95553","ask":"0.95633"},{"pair":"CAD\/JPY","bid":"80.574","ask":"80.644"},{"pair":"AUD\/JPY","bid":"77.036","ask":"77.086"},{"pair":"NZD\/USD","bid":"0.72127","ask":"0.72177"},{"pair":"AUD\/NZD","bid":"1.27479","ask":"1.27619"},{"pair":"USD\/TRY","bid":"1.51799","ask":"1.51899"}]; var dealingDeskId = "1090"; var clientTierId = "139"; var currencyPairId = "1"; var accessURL = "http://banktest1.fxdd.com:3872/c/QuoteRequestServlet?"; var arrPairs = new Array(); arrPairs[1] = 'CHF/JPY'; arrPairs[2] = 'USD/CAD'; arrPairs[3] = 'XAU/USD'; arrPairs[4] = 'GBP/CHF'; arrPairs[5] = 'GBP/JPY'; arrPairs[6] = 'AUD/NZD'; arrPairs[7] = 'AUD/USD'; arrPairs[8] = 'NZD/USD'; arrPairs[9] = 'EUR/JPY'; arrPairs[10] = 'USD/JPY'; arrPairs[11] = 'EUR/MXN'; arrPairs[12] = 'XAG/USD'; arrPairs[13] = 'GBP/USD'; arrPairs[14] = 'AUD/CAD'; arrPairs[15] = 'USD/PLN'; arrPairs[16] = 'EUR/CAD'; arrPairs[17] = 'EUR/USD'; arrPairs[18] = 'EUR/PLN'; arrPairs[19] = 'EUR/AUD'; arrPairs[20] = 'USD/CHF'; arrPairs[21] = 'USD/MXN'; arrPairs[22] = 'AUD/JPY'; arrPairs[23] = 'CAD/JPY'; arrPairs[24] = 'EUR/CHF'; arrPairs[25] = 'EUR/GBP'; // Rates ASK | BID var arrRates = new Array(); arrRates[1] = '86.0|86.0'; arrRates[2] = '1.475|1.475'; arrRates[3] = '426.61|426.61'; arrRates[4] = '2.2|2.2'; arrRates[5] = '198.0|198.0'; arrRates[6] = '1.08|1.08'; arrRates[7] = '0.56|0.56'; arrRates[8] = '0.71|0.71'; arrRates[9] = '128.0|128.0'; arrRates[10] = '120.0|120.0'; arrRates[11] = '13.9|13.9'; arrRates[12] = '6.83|6.83'; arrRates[13] = '1.576|1.576'; arrRates[14] = '0.94|0.94'; arrRates[15] = '3.6|3.6'; arrRates[16] = '1.6|1.6'; arrRates[17] = '1.069|1.069'; arrRates[18] = '4.5|4.5'; arrRates[19] = '1.7|1.7'; arrRates[20] = '1.376|1.376'; arrRates[21] = '10.8|10.8'; arrRates[22] = '79.0|79.0'; arrRates[23] = '99.36|99.36'; arrRates[24] = '1.5|1.5'; arrRates[25] = '0.679|0.679'; var lotSizeArray = new Array(); lotSizeArray[1] = '0.010000'; lotSizeArray[2] = '0.000100'; lotSizeArray[3] = '0.010000'; lotSizeArray[4] = '0.000100'; lotSizeArray[5] = '0.010000'; lotSizeArray[6] = '0.000100'; lotSizeArray[7] = '0.000100'; lotSizeArray[8] = '0.000100'; lotSizeArray[9] = '0.010000'; lotSizeArray[10] = '0.010000'; lotSizeArray[11] = '0.000100'; lotSizeArray[12] = '0.000100'; lotSizeArray[13] = '0.000100'; lotSizeArray[14] = '0.000100'; lotSizeArray[15] = '0.000100'; lotSizeArray[16] = '0.000100'; lotSizeArray[17] = '0.000100'; lotSizeArray[18] = '0.000100'; lotSizeArray[19] = '0.000100'; lotSizeArray[20] = '0.000100'; lotSizeArray[21] = '0.000100'; lotSizeArray[22] = '0.010000'; lotSizeArray[23] = '0.010000'; lotSizeArray[24] = '0.000100'; lotSizeArray[25] = '0.000100'; //this id numbers are mapped to the numbers in the FIP project... var currencyPairIdArray = new Array(); currencyPairIdArray[1] = '15'; currencyPairIdArray[2] = '8'; currencyPairIdArray[3] = '23'; currencyPairIdArray[4] = '16'; currencyPairIdArray[5] = '7'; currencyPairIdArray[6] = '22'; currencyPairIdArray[7] = '9'; currencyPairIdArray[8] = '21'; currencyPairIdArray[9] = '6'; currencyPairIdArray[10] = '1'; currencyPairIdArray[11] = '14'; currencyPairIdArray[12] = '24'; currencyPairIdArray[13] = '4'; currencyPairIdArray[14] = '19'; currencyPairIdArray[15] = '11'; currencyPairIdArray[16] = '18'; currencyPairIdArray[17] = '3'; currencyPairIdArray[18] = '12'; currencyPairIdArray[19] = '17'; currencyPairIdArray[20] = '2'; currencyPairIdArray[21] = '13'; currencyPairIdArray[22] = '20'; currencyPairIdArray[23] = '25'; currencyPairIdArray[24] = '10'; currencyPairIdArray[25] = '5'; // ************************** Functions for TAB - START ***********************************// function getRateForMarginCalc() { var currencyId = document.calculator.marginCurrencyPair.options[document.calculator.marginCurrencyPair.selectedIndex].value; currencyId = arrPairIds[currencyId]; var tabId = document.calculator.selectedTabId.value; document.calculator.action = "QuoteRequestServlet?dealingDeskId="+dealingDeskId+"&clientTierId="+clientTierId+"¤cyPairId="+currencyId+"&selectedTabId="+tabId; document.calculator.submit(); } function getRate(index) { var currencyIndex = index; var currencyId = currencyPairIdArray[currencyIndex]; //console.log("currencyIndex %d , currencyId %d",currencyIndex, currencyId); // This is the function which makes call to the server and get the fresh rate. var result; for (var i in quoteData){ if(quoteData[i].pair == arrPairs[currencyIndex]){ result = currencyIndex + "-" + currencyPairId + "-" + quoteData[i].ask + "|" + quoteData[i].bid; // console.log("Pair is %s and index is %s and bid is %s and ask is %s",quoteData[i].pair, currencyIndex, quoteData[i].bid, quoteData[i].ask); } } document.calculator.newRate.value = result; updateNewRate(currencyIndex); } function updateNewRate(index) { var currencyIndex = index; var result = document.calculator.newRate.value; var i = result.indexOf('-'); var curIndex = result.substr(0, i); if(curIndex == currencyIndex) { var secondToken = result.substr(i+1, result.length); var curIndex = result.substr(0, i); var currencyId = secondToken.substr(0, secondToken.indexOf('-')); var quote = secondToken.substr(secondToken.indexOf('-')+1, secondToken.length); arrRates[currencyIndex] = quote; return; } else { alert("Problem in getting the current rate. Going to apply old rate"); } } //********************** Function to calculate the PIP value. - START ***************************************// function checkCurrencyPairForPIPCalculation(){ var currencyPairId = document.calculator.currencyPair.options[document.calculator.currencyPair.selectedIndex].value; var currencyPair = document.calculator.currencyPair.options[document.calculator.currencyPair.selectedIndex].text; //console.log("id is %d and pair name is %s",currencyPairId,currencyPair); var intSlashIndex = currencyPair.indexOf('/'); //base currency var txtBase = currencyPair.substr(0, intSlashIndex); //counter currency var txtCounter = currencyPair.substr(intSlashIndex+1, 3); var rate; var askBidRate; var ask; var bid; if(txtCounter != null && txtCounter == "USD"){ getRate(currencyPairId); askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); ask = askBidRate.substr(0, index); bid = askBidRate.substr(index+1, askBidRate.length); rate = ( (ask/1) + (bid/1) ) / 2; document.calculator.Ask.value = prDec(rate, 4); document.calculator.pipBaseCurrency.value = txtBase; document.calculator.pipCounterCurrency.value = txtCounter; return; } if(txtBase != null && txtBase == "USD"){ getRate(currencyPairId); askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); ask = askBidRate.substr(0, index); bid = askBidRate.substr(index+1, askBidRate.length); rate = ( (ask/1) + (bid/1) ) / 2; document.calculator.Ask.value = prDec(rate, 4); document.calculator.pipBaseCurrency.value = txtBase; document.calculator.pipCounterCurrency.value = txtCounter; return; } if(txtBase != "USD" && txtCounter != "USD") { var index = checkCurrencyPairExists("USD", txtCounter); if(index != -1){ getRate(index); askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); ask = askBidRate.substr(0, indexCp); bid = askBidRate.substr(indexCp+1, askBidRate.length); rate = ( (ask/1) + (bid/1) ) / 2; document.calculator.Ask.value = prDec(rate, 4); document.calculator.pipBaseCurrency.value = "USD"; document.calculator.pipCounterCurrency.value = txtCounter; return; } if(index == -1) { var index = checkCurrencyPairExists(txtCounter, "USD"); if(index != -1) { getRate(index); askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); ask = askBidRate.substr(0, indexCp); bid = askBidRate.substr(indexCp+1, askBidRate.length); rate = ( (ask/1) + (bid/1) ) / 2; document.calculator.pipBaseCurrency.value = txtCounter; document.calculator.pipCounterCurrency.value = "USD"; document.calculator.Ask.value = prDec(rate, 2); return; } } } } var Volume; var Ask; function calculatePIPValue(){ var lot_size = document.calculator.Volume.value; var rate = document.calculator.Ask.value; if(rate == "") { alert("Rate can't be empty"); document.calculator.Ask.focus(); return false; } var currencyPairId = document.calculator.currencyPair.options[document.calculator.currencyPair.selectedIndex].value; var currencyPair = document.calculator.currencyPair.options[document.calculator.currencyPair.selectedIndex].text; var currencyRate = arrRates[currencyPairId]; var tick_ize = lotSizeArray[currencyPairId]; var intSlashIndex = currencyPair.indexOf('/'); var txtBase = currencyPair.substr(0, intSlashIndex); var txtCounter = currencyPair.substr(intSlashIndex+1, 3); var pipValue; if(txtCounter != null && txtCounter == "USD"){ pipValue = (lot_size/1) * (tick_ize);// * (rate/1); pipValue= prDec( pipValue, 2 ); document.calculator.PIP.value = pipValue; return; } if(txtBase != null && txtBase == "USD"){ pipValue = ( (lot_size/1) * (tick_ize) ) / (rate/1); pipValue= prDec( pipValue, 2 ); document.calculator.PIP.value = pipValue; return; } if(txtBase != "USD" && txtCounter != "USD") { var index = checkCurrencyPairExists("USD", txtCounter); if(index != -1){ pipValue = ( (lot_size/1) * (tick_ize) ) / (rate/1); pipValue= prDec( pipValue, 2 ); document.calculator.PIP.value = pipValue; return; } if(index == -1) { var index = checkCurrencyPairExists(txtCounter, "USD"); if(index != -1) { var currencyrate = arrRates[index]; pipValue = ( (lot_size/1) * (tick_ize) ) * (rate/1); pipValue= prDec( pipValue, 2 ); document.calculator.PIP.value = pipValue; return; } } } } function calculatePipPrice(currencyPairId, currencyPair, lot_size) { var intSlashIndex = currencyPair.indexOf('/'); var txtBase = currencyPair.substr(0, intSlashIndex); var txtCounter = currencyPair.substr(intSlashIndex+1, 3); var pipPrice; var tick_ize = lotSizeArray[currencyPairId]; if(txtCounter != null && txtCounter == "USD"){ var askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, index); var bid = askBidRate.substr(index+1, askBidRate.length); var rate = ( (ask/1) + (bid/1) ) / 2; pipPrice = (lot_size/1) * (tick_ize);// * (rate/1) return pipPrice; } if(txtBase != null && txtBase == "USD"){ var askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, index); var bid = askBidRate.substr(index+1, askBidRate.length); var rate = ( (ask/1) + (bid/1) ) / 2; pipPrice = ( (lot_size/1) * (tick_ize) ) / (rate/1); return pipPrice; } if(txtBase != "USD" && txtCounter != "USD") { var index = checkCurrencyPairExists("USD", txtCounter); if(index != -1){ var askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, indexCp); var bid = askBidRate.substr(indexCp+1, askBidRate.length); var rate = ( (ask/1) + (bid/1) ) / 2; pipPrice = ( (lot_size/1) * (tick_ize) ) / (rate/1); return pipPrice; } if(index == -1) { var index = checkCurrencyPairExists(txtCounter, "USD"); if(index != -1) { var askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, indexCp); var bid = askBidRate.substr(indexCp+1, askBidRate.length); var rate = ( (ask/1) + (bid/1) ) / 2; pipPrice = ( (lot_size/1) * (tick_ize) ) * (rate/1); return pipPrice; } } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// //********************** Function to calculate the PROFIT. ***************************************// function calculateProfit() { var openingRate = document.calculator.openingRate.value; var closingRate = document.calculator.closingRate.value; var rate = document.calculator.profitQuoteRate.value; var amountLots = document.calculator.profitAmountLots.value; if( openingRate == "" ) { alert("Opening Rate cant be empty"); document.calculator.openingRate.focus(); return false; } if( closingRate == "") { alert("Closing Rate can't be empty"); document.calculator.closingRate.focus(); return false; } if( rate == "") { alert("Rate can't be empty"); document.calculator.profitQuoteRate.focus(); return false; } if(amountLots == null || amountLots == ""){ document.calculator.profitAmountLots.value = 1; } var currencyPairId = document.calculator.profitCurrencyPair.options[document.calculator.profitCurrencyPair.selectedIndex].value; var currencyPair = document.calculator.profitCurrencyPair.options[document.calculator.profitCurrencyPair.selectedIndex].text; var intSlashIndex = currencyPair.indexOf('/'); var txtBase = currencyPair.substr(0, intSlashIndex); var txtCounter = currencyPair.substr(intSlashIndex+1, 3); var lotSize = document.calculator.lotSize.options[document.calculator.lotSize.selectedIndex].value; amountLots = document.calculator.profitAmountLots.value; var amount_bc = (amountLots/1) * (lotSize/1); var profitValue; if(txtCounter != null && txtCounter == "USD"){ profitValue = ( (closingRate/1)-(openingRate/1) ) * (amount_bc) * (rate/1); document.calculator.txtProfit.value = prDec(profitValue, 2); return; } if(txtBase != null && txtBase == "USD"){ profitValue = ( (closingRate/1)-(openingRate/1) ) * (amount_bc); document.calculator.txtProfit.value = prDec(profitValue, 2); return; } if(txtBase != "USD" && txtCounter != "USD") { var index = checkCurrencyPairExists(txtBase, "USD"); if(index != -1){ profitValue = ( (closingRate/1)-(openingRate/1) ) * (amount_bc) * (rate/1); document.calculator.txtProfit.value = prDec(profitValue, 2); return; } if(index == -1) { var index = checkCurrencyPairExists( "USD", txtBase); if(index != -1) { alert('counter in USD / base'); profitValue = (( (closingRate/1)-(openingRate/1) ) * (amount_bc) ) / (rate/1); document.calculator.txtProfit.value = prDec(profitValue, 2); return; } } } } function checkCurrencyPairForProfitCalculation() { var currencyPairId = document.calculator.profitCurrencyPair.options[document.calculator.profitCurrencyPair.selectedIndex].value; var currencyPair = document.calculator.profitCurrencyPair.options[document.calculator.profitCurrencyPair.selectedIndex].text; var intSlashIndex = currencyPair.indexOf('/'); var txtBase = currencyPair.substr(0, intSlashIndex); var txtCounter = currencyPair.substr(intSlashIndex+1, 3); var rate; if(txtCounter != null && txtCounter == "USD"){ getRate(currencyPairId); var askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, index); var bid = askBidRate.substr(index+1, askBidRate.length); rate = ask; document.calculator.profitQuoteRate.value = rate; document.calculator.profitBaseCurrency.value = txtBase; document.calculator.profitCounterCurrency.value = txtCounter; return; } if(txtBase != null && txtBase == "USD"){ getRate(currencyPairId); var askBidRate = arrRates[currencyPairId]; var index = askBidRate.indexOf('|'); var ask = askBidRate.substr(0, index); var bid = askBidRate.substr(index+1, askBidRate.length); rate = ask; document.calculator.profitQuoteRate.value = rate; document.calculator.profitBaseCurrency.value = txtBase; document.calculator.profitCounterCurrency.value = txtCounter; return; } if(txtBase != "USD" && txtCounter != "USD") { var index = checkCurrencyPairExists(txtBase, "USD"); if(index != -1){ getRate(index); var askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); ask = askBidRate.substr(0, indexCp); bid = askBidRate.substr(indexCp+1, askBidRate.length); rate = ask; document.calculator.profitQuoteRate.value = rate; document.calculator.profitBaseCurrency.value = txtBase; document.calculator.profitCounterCurrency.value = "USD"; return; } if(index == -1) { var index = checkCurrencyPairExists( "USD", txtBase); if(index != -1) { getRate(index); var askBidRate = arrRates[index]; var indexCp = askBidRate.indexOf('|'); ask = askBidRate.substr(0, indexCp); bid = askBidRate.substr(indexCp+1, askBidRate.length); rate = ask; document.calculator.profitQuoteRate.value = rate; document.calculator.profitBaseCurrency.value = "USD"; document.calculator.profitCounterCurrency.value = txtBase; return; } } } } function checkCurrencyPairExists(base, counter) { var index = -1; for(i=1;i 0) { reg0Str += '\.?[0-9]{0,' + decimalPlaces + '}'; } else if (decimalPlaces < 0) { reg0Str += '\.?[0-9]*'; } reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str; reg0Str = reg0Str + '$'; var reg0 = new RegExp(reg0Str); if (reg0.test(temp)) return true; // first replace all non numbers var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']'; var reg1 = new RegExp(reg1Str, 'g'); temp = temp.replace(reg1, ''); if (allowNegative) { // replace extra negative var hasNegative = temp.length > 0 && temp.charAt(0) == '-'; var reg2 = /-/g; temp = temp.replace(reg2, ''); if (hasNegative) temp = '-' + temp; } if (decimalPlaces != 0) { var reg3 = /\./g; var reg3Array = reg3.exec(temp); if (reg3Array != null) { // keep only first occurrence of . // and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0 var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length); reg3Right = reg3Right.replace(reg3, ''); reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right; temp = temp.substring(0,reg3Array.index) + '.' + reg3Right; } } obj.value = temp; } function blockNonNumbers(obj, e, allowDecimal, allowNegative) { var key; var isCtrl = false; var keychar; var reg; if(window.event) { key = e.keyCode; isCtrl = window.event.ctrlKey } else if(e.which) { key = e.which; isCtrl = e.ctrlKey; } if (isNaN(key)) return true; keychar = String.fromCharCode(key); // check for backspace or delete, or if Ctrl was pressed if (key == 8 || isCtrl) { return true; } reg = /\d/; var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false; var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false; return isFirstN || isFirstD || reg.test(keychar); } //**************** Common Validation Functions ************// window.addEvent('domready', function() { }); function checkIfQuoteExists(pair){ for (var i in quoteData){ if(quoteData[i].pair == pair) return true; } return false; } window.addEvent('domready', function(){ var calculationType = 0; var currencyOptions = document.getElementsByName("currencyPair"); if(currencyOptions.length == 0){ var currencyOptions = document.getElementsByName("marginCurrencyPair"); var calculationType = 1; if(currencyOptions.length == 0){ var currencyOptions = document.getElementsByName("profitCurrencyPair"); var calculationType = 2; } } if(currencyOptions.length > 0) { var currentSelectObject = currencyOptions[0]; switch(calculationType){ case 0: checkCurrencyPairForPIPCalculation(); break; case 1: checkCurrencyPairForMarginCalculation(); break; case 2: checkCurrencyPairForProfitCalculation(); break; } for(j=0;j