Real Time Stock Market Data Using PHP, Zerodha

How to fetch real time stock market data using php and update the price every second using Ajax.

CREATE LAYOUT

Copy the below code and paste into index.php file in out htdocs folder.

<!DOCTYPE html>
<html lang="en">
<head>
<title>MK Codez</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background-color:black;
  color:white;
}

/* Style the header */
.header {
  background-color: gray;
  
  text-align: center;
  height:20vh;
}
.sections
{
	border: 2px solid black;
	float:left;
	width:16.65%;
	background-color:black;
	height:20vh;
	color:black;
	overflow:scroll;
	overflow-x: hidden;
	text-align:left;
	color:white
}

/* Style the top navigation bar */
.topnav {
  overflow: hidden;
  background-color: #333;
  height:5vh;
}

/* Style the topnav links */
.topnav span.tradingsymbol {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 0px 14px 70px;
  text-decoration: none;
}
.topnav span.lastprice {
  float: left;
  display: block;
  color: #62FA1B;
  text-align: center;
  padding: 14px 0px 14px 4px;
  text-decoration: none;
}

/* Change color on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* Create three unequal columns that floats next to each other */
.column {
  float: left;
  padding: 10px;
  height:60vh;
  
}

/* Left and right column */
.column.left {
  width: 20%;
  height:60vh;
  background-color: #201F1F;
  border:1px solid white;
}
.column.right {
  width: 20%;
  height:60vh;
  background-color: #201F1F;
  border:1px solid white;
}

/* Middle column */
.column.middle {
  width: 60%;
  height:60vh;
  overflow:scroll;
  border:1px solid white;
  overflow-x: hidden;
  
}
::-webkit-scrollbar {
  width: 15px;
}
::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px grey; 
  border-radius: 2px;
}
 
/* Handle */
::-webkit-scrollbar-thumb {
  background: #2E2C2C; 
  border-radius: 10px;
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
  background: #b30000; 
}

table {
  border-collapse: collapse;
  width: 100%;
}

tr {
  border-bottom: 1px solid #ddd;
}
td{
	text-align:center;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
  }
}
.footer {
  background-color: black;
  color: #CBC3C3;
  padding: 10px;
  text-align: center;
  height:20vh;
}
.weights
{
	width:100%;
	background-color:#2E2C2C;
	padding:5px;
}
.position
{
	float:left;
	width:50%;
	height:100%;
	border:1px solid white;
}
.order
{float:left;
	width:50%;
	height:100%;
	border:1px solid white;
}
.indices {
  background-color: #555555; /* Green */
  border: none;
  color: white;
  padding: 10px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
button
{
	background-color: #555555; /* Green */
  border: none;
  color: white;
  padding: 10px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

</style>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<body>

<div class="header">
<div class="sections">
</div>
<div class="sections">
</div>
<div class="sections">
</div>
<div class="sections">
</div>
<div class="sections">
</div>
<div class="sections">
</div>
</div>

<div class="topnav">
  <span class="tradingsymbol">NIFTY BANK</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol">NIFTY IT</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol">NIFTY ENERGY</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol">NIFTY FMCG</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol">NIFTY AUTO</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol">NIFTY METAL</span>
  <span class="lastprice"></span>

</div>

<div class="row">
  <div class="column left">
   <h3 style="background-color:black;text-align:center">ORDER PLACEMENT</h3> 
  </div>
  
  <div class="column middle">
    <select onchange="changeIndex()", class="indices">
	<option value="NIFTY 50">NIFTY 50</option>
	<option value="NIFTY BANK">NIFTY BANK</option>
	<option value="NIFTY IT">NIFTY IT</option>
	<option value="NIFTY ENERGY">NIFTY ENERGY</option>
	<option value="NIFTY AUTO">NIFTY AUTO</option>
	</select>
	<button onclick="addStocks()">UPDATE DB</button><br>
	<table class="stockList">
  <tr>
    <th>NAME</th>
	<th>HIGH</th>
	<th>LOW</th>
	<th>VOLUME</th>
	<th>LTP</th>
	<th>CHANGE</th>
  </tr>
</table>
  
  </div>
  
  <div class="column right">
   <h3 style="background-color:black;text-align:center">STRATEGY CREATION</h3> 
  </div>
</div>
<div class="footer">
<div class="weights">
  <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">RELIANCE</span>
  <span class="lastprice"></span>
  <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">INFY</span>
  <span class="lastprice"></span>
    <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">HDFCBANK</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">ICICIBANK</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">HDFC</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">TCS</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">KOTAKBANK</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">ITC</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">LT</span>
  <span class="lastprice"></span>
      <span class="tradingsymbol", style="padding: 14px 0px 14px 30px;">AXISBANK</span>
  <span class="lastprice"></span>
  </div>
  <div class="position">
  <h3>POSITION</h3>
  </div>
  <div class="order">
  <h3>ORDER</h3>
  </div>
</div>
<script>

</script>
</body>
</html>

CREATE DATABASE

Goto : http://localhost/phpmyadmin

Create New Database and create table with name and indices attributes.

create table stocks(name varchar(20) not null, indices varchar(20) not null, primary key(name,indices))

The above query creates a new table.

INSERT INTO DATABASE

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password,"mysql");
// Check connection
if ($conn->connect_error) {
  echo("Connection failed: " . $conn->connect_error);
}
$indices=$_GET['index'];
$myfile = fopen("stocks.txt", "r") ;
$str=fread($myfile,filesize("stocks.txt"));
fclose($myfile);
$stocks=explode(",",$str);
for($i=0;$i<count($stocks);$i++)
{
$sql = "INSERT INTO stocks (name, indices)VALUES ('".$stocks[$i]."', '".$indices."')";
if ($conn->query($sql) === TRUE) {
  
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

}
echo "SUCCESS";
$conn->close();

?>

The above code helps to insert all stocks inside the text file into stocks table.

DISPLAY STOCKS

$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password,"mysql");

if ($conn->connect_error) {
  echo("Connection failed: " . $conn->connect_error);
}

$sql = "select name from stocks where indices='".$indices."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
 while($row = $result->fetch_assoc()) {
echo $row["name"];
  }
}
$conn->close();
?>

GET LTP OF MULTIPLE STOCKS

<?php

    require_once __DIR__ . '/vendor/autoload.php';
    session_start();
    use KiteConnect\KiteConnect;
	$instrument = json_decode(stripslashes($_GET['data']));
	//$instrument=array("NSE:".$data);
  
	$kite=$_SESSION["kite"] ;
	try {
        //$user = $kite->generateSession("W6CPlOg6UGxbaDiApm01m5PAWX8kf4oM", "n2smdr84oskoe11v9oox5wi2otvgzrly");
        //$kite->setAccessToken($user->access_token);
		echo json_encode($kite->getQuote($instrument));
    } catch(Exception $e) {
        
    }
 
?>

Sending Ajax Request by passing array of instruments.

That’s it

Please watch the video to learn step by step process.



Thank you,

Mukesh mohanan.

Comments

Post a Comment

Popular posts from this blog

Zerodha Algo Trading Python, How to connect to kiteconnect?

How to Place Order in Zerodha using Python

How to place order in zerodha when RSI crosses below 30? Algo Trading Python