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

$listOfCurrencies = "CAD_CHF%2CCAD_JPY%2CCAD_HKD%2CCAD_SGD";
$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();
$returnArray = array();

foreach($results->prices as $result){


			$instrument = $result->instrument;

			$bidPrice = $result->bids[0]->price;

			$time = date('H:i');

			/*echo $instrument." ".$bidPrice." ".$time."<br>";*/

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

			$lastDay = date('Y-m-d', strtotime('-1 day', strtotime($date)));

			$getOldRate = $db->db->prepare("SELECT * FROM heatmap WHERE date = '$date' AND symbol = '$instrument'");
			$getOldRate->execute();
			$fetchRate = $getOldRate->fetch();


			if (empty($fetchRate)) {
				# code...
				if ($time > "16:00") {
						
						$stmt = $db->db->prepare("INSERT INTO heatmap(symbol, price, date) VALUES ('$instrument', '$bidPrice', '$date')");

						$stmt->execute();

				}
			}


			$getLastRate = $db->db->prepare("SELECT * FROM heatmap WHERE date = '$lastDay' AND symbol = '$instrument'");
			$getLastRate->execute();
			$lastRate = $getLastRate->fetch();

			$lastPrice = $lastRate['price'];

			$Difference = $bidPrice - $lastPrice;

			$ResultDifference = $Difference / $bidPrice * 100;

			$per = number_format((float)$ResultDifference, 2, '.', '');

			$array = array('sym'=>$instrument, 'bid'=>$bidPrice, 'per'=>$per);

			$array = json_encode($array);

			array_push($returnArray, $array);

}

print_r(json_encode($returnArray));


?>

