	<?php 
	include('../classes/database.class.php');

	$listOfCurrencies = $_POST['sym'];

	/*$listOfCurrencies = "SGD_EUR%2CSGD_USD%2CSGD_AUD%2CSGD_GBP%2CSGD_NZD%2CSGD_CAD%2CSGD_CHF%2CSGD_JPY%2CSGD_HKD";*/

	$ch = curl_init("https://api-fxtrade.oanda.com/v3/accounts/001-001-414778-001/pricing?instruments=" . $listOfCurrencies);
	$header = array();
	$header[] = 'Content-type: application/json';
	$header[] = 'Authorization: Bearer c143ece96a9cb2ce9dc6ecdbaf844523-77cf55b79018d06cf3396c90855d06be';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$resultsj = curl_exec($ch);
	$results = json_decode($resultsj);
	$db = new database();
	$getrate = "";
	$symbolshow = "";

	if (!isset($results->errorMessage)) {
		# code...
		$getrate = $results->prices[0]->bids[0]->price;

		$symbolshow = $listOfCurrencies;

	}else{

		$firstsymbol = substr($listOfCurrencies, 0, 3);
		$secondsymbol = substr($listOfCurrencies, 4, 6);
		$fullSymbol = $secondsymbol."_".$firstsymbol;
		

		$newch = curl_init("https://api-fxtrade.oanda.com/v3/accounts/001-001-414778-001/pricing?instruments=" . $fullSymbol);
		curl_setopt($newch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($newch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($newch, CURLOPT_SSL_VERIFYPEER, false);
		$newresultsj = curl_exec($newch);
		$newresults = json_decode($newresultsj);
		$rate = $newresults->prices[0]->bids[0]->price;
		$newrate = 1 / $rate;

		$getrate = number_format((float)$newrate, 5, '.', '');

		$symbolshow = $listOfCurrencies;

	}

	$oldrate = 0.000;
	$color = "";

	$getoldrate = $db->db->prepare("SELECT * FROM tickercompany WHERE sym = '$symbolshow' ORDER By id DESC");
	$getoldrate->execute();
	$oldratearray = $getoldrate->fetch();

	$oldid = $oldratearray['id'];

	if (!empty($oldratearray)) {
		# code...
		$oldrate = $oldratearray['rate'];
	}

	if ($getrate > $oldrate) {
		# code..
		$color = "lime";
	}else{
		$color = "red";
	}

	$date = date('Y-m-d');

	$insertNewRate = $db->db->prepare("INSERT INTO tickercompany(sym, rate, date) VALUES('$symbolshow', '$getrate', '$date')");

	$insertNewRate->execute();

	$deleteOld = $db->db->prepare("DELETE FROM tickercompany WHERE id = '$oldid'");

	$deleteOld->execute();

	$reusltarray = array('result'=>'suc', 'rate'=>$getrate, 'sym'=>$symbolshow, 'color'=>$color);

	print_r(json_encode($reusltarray));

	?>

