0% found this document useful (0 votes)
42 views

Step-by-Step Process to Set Up Redis with Python and Zerodha API

Uploaded by

Sai Rajeev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Step-by-Step Process to Set Up Redis with Python and Zerodha API

Uploaded by

Sai Rajeev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Step-by-Step Process to Set Up Redis with Python and Zerodha API

Step 1: Install Redis on Windows

Since you’ve already downloaded Redis for Windows, follow these steps
to set it up:

1. Extract the Redis ZIP file:


○ If you haven’t done so already, extract the downloaded Redis
ZIP file to a folder, e.g., C:\redis.
2. Run Redis Server:
○ Open a command prompt in Administrator mode (right-click
on Command Prompt > "Run as Administrator").

Navigate to the folder where you extracted Redis:

We have at C:\Program Files\Redis

Run the Redis server:


3. This will start the Redis server. You should see logs indicating that
Redis is running.
4. Test Redis:
○ In the same Command Prompt window, open another
command prompt (you can keep the Redis server running in
one window).

Type:
bash
Copy code
redis-cli.exe

This will open the Redis command-line interface. To test Redis, type the
following command:
bash
Copy code
ping


○ Redis should respond with PONG, indicating that it’s working.

Step-by-Step Process:

Step 1: Set Up Zerodha API Access

1. Create a Zerodha account (if you haven’t already).


2. Go to the Zerodha Kite API page: Zerodha Kite API.
3. Register for API access:
○ Create an API key and secret after logging into your Zerodha account.
4. Obtain Access Token:
○ Once you have the API key and secret, follow Zerodha’s instructions to
generate an access token. You'll use this token to access real-time data via
their WebSocket API.

Step 2: Set Up Python on Your Local Machine

1. Install Python:
○ If you don’t have Python installed, download it from python.org.

Install Required Libraries: Open a command prompt or terminal, and run the following
commands to install the necessary libraries:

pip install kiteconnect websocket-client google-api-python-client


oauth2client pandas

2.

Step 3: Clone the Kite Candles Repository

1. Go to the GitHub repository you provided: kite_candles.


2. Clone or download the repository to your local machine.
○ You can simply download the ZIP file and extract it, or use the following
command if you have Git installed:

https://github.com/tahseenjamal/kite_candles.git
Step 4: Configure Zerodha API in the Code

1. Open the cloned repository in your code editor.


2. Find the API key section in the code and replace it with your API key, secret, and
access token from Step 1.
○ This will connect your script to Zerodha’s API.

Step 5: Set Up Google Sheets API Access

1. Create a Google Cloud Project:


○ Go to Google Cloud Console.
○ Create a new project.
2. Enable the Google Sheets API:
○ In the Google Cloud Console, search for "Google Sheets API" and enable it
for your project.
3. Create Credentials:
○ Under APIs & Services > Credentials, create OAuth 2.0 credentials and
download the credentials.json file.
4. Store the credentials file in your project folder (where your script is).

Step 6: Authenticate Google Sheets API

1. In your Python script, authenticate the Google Sheets API:


○ The first time you run the script, it will open a browser window asking for
permission to access your Google Sheets. Grant it.
○ After authentication, a token.json file will be created, which stores your
credentials securely.

Step 7: Modify the Script to Stream Data and Update Google Sheets

1. WebSocket Streaming:
○ Modify the code (or use the kite_candles repository) to subscribe to the
tokens you want to track (for example, Nifty options or stocks).
2. Process Data:
○ In the WebSocket on_ticks function (which handles incoming data),
process and store the real-time data for each token.
○ Format the data (e.g., latest price or candlestick data).
3. Update Google Sheets:
○ Modify the code to update Google Sheets in real-time.
○ Use the update_sheet() function (mentioned earlier) to push data to
Google Sheets.
○ You can update Google Sheets every minute or with each new WebSocket
tick, depending on your preference.
Step 8: Run the Script Locally

1. Run the script on your local machine.


○ This will open WebSocket connections to Zerodha’s API and start streaming
data for the tokens.
○ As the data comes in, it will be processed and stored.
○ Every minute (or as configured), the data will be written to Google Sheets.

Step 9: Monitor the Script

● Keep an eye on the terminal or console to ensure that the script is running properly
and that data is updating in Google Sheets.

Set up Zerodha API: Get your API key, secret, and access token.

Install Python and dependencies: Install necessary libraries on your local machine.

Clone kite_candles repo: Use it to fetch real-time market data.

Set up Google Sheets API: Create a Google Cloud project and authenticate access.

Update the script: Modify the script to process the data and send updates to Google
Sheets.

Run the script: Keep the script running on your machine to stream data and update
Google Sheets.

------------------------------- Above is local --------------------------------

Below is cloud

Google does not have a direct service or API specifically designed to provide live options
data like the one you're asking for (live 1-minute data of ATM and +/- 5 strikes for Nifty
options). However, you can use Google Cloud's serverless offerings (such as Cloud
Functions) in combination with external data providers, like Zerodha (or another broker that
offers live options data), to fetch and process the live data.

Here’s how you can set up a solution using Google Cloud to get live options data for Nifty
at 1-minute intervals for ATM and +/- 5 strikes using Zerodha's WebSocket API:

Overview of the Process

1. Zerodha WebSocket API:


○ Use Zerodha’s WebSocket API to get live market data for Nifty options at
1-minute intervals.
○ You'll need to track the ATM (At-The-Money) options and the +/- 5 strike
prices around ATM for Nifty.
2. Google Cloud Functions:

○ Use Google Cloud Functions to process the data fetched from the WebSocket
and append it to a Google Sheet or store it in a Cloud Database like
Firestore or BigQuery.
3. Google Sheets API:

○ If you want to store the data in Google Sheets, use the Google Sheets API to
automatically append the live data to a sheet.

Detailed Setup and Architecture

Step 1: Set Up Zerodha WebSocket for Market Data

1. Zerodha Kite WebSocket API provides real-time market data, including options
data.
○ First, you need to get API keys from Zerodha (if you haven’t already).
○ Then, connect to the WebSocket API using Zerodha's token for Nifty
options.
○ Fetch live market data for ATM and +/- 5 strikes for Nifty options.

Step 2: Identifying ATM and +/- 5 Strikes

1. ATM Calculation:

○ The ATM option strike is the one closest to the current market price of Nifty.
○ For example, if Nifty is at 18,000, the ATM strike could be 18,000.
2. Find +/- 5 Strikes:

○ You can calculate the strikes surrounding the ATM option using a simple
formula. If ATM is 18,000, the surrounding strikes could be 17,950, 18,050,
17,900, 18,100, etc.
3. Monitor Data for These Strikes:

○ After calculating the ATM and surrounding strikes, subscribe to these strike
prices using the Zerodha WebSocket API.
○ The WebSocket will send data about the Nifty options you’ve subscribed to,
including live prices, volume, and other market data.

Step 3: Set Up Google Cloud Functions


1. Create a Google Cloud Project:

○ If you don’t have a Google Cloud Project, create one via the Google Cloud
Console.
2. Enable APIs:

○ Enable the Google Sheets API.


○ Enable Google Cloud Functions API.
3. Set Up Authentication for Google Sheets:

○ Create a Service Account to authenticate with the Google Sheets API and
give it appropriate permissions (like “Editor”).
○ Share the Google Sheet with the service account email address.
4. Create a Google Cloud Function:

○ This function will be triggered by the WebSocket data (e.g., by publishing a


message to a Google Cloud Pub/Sub topic).
○ The Cloud Function will then process this data and store it in a Google Sheet
or a Database like Firestore.

Step 4: Use WebSocket to Fetch Data and Trigger Google Cloud Functions

1. Connect to the Zerodha WebSocket API:

○ Once connected to the WebSocket, you can fetch real-time data.


○ The WebSocket will send a continuous stream of updates, but you’ll filter out
the data for the ATM and surrounding strikes.
2. Process Data:

○ Parse the incoming data to get relevant details such as price, open interest,
volume, etc.
○ Aggregate this data into a format that can be easily appended to your Google
Sheet.
3. Publish Data to Pub/Sub (Optional for Scaling):

○If you want to make the solution more robust and scalable, you can push
each update from the WebSocket into a Google Cloud Pub/Sub topic.
○ Google Cloud Functions can subscribe to this Pub/Sub topic and process
incoming data for appending to the Google Sheet.
4. Append Data to Google Sheets:

○ Using the Google Sheets API, the Cloud Function will automatically append
the processed data into your Google Sheet. This could include columns like:
■ Timestamp
■ Option Strike Price
■ Option Type (Call/Put)
■ Last Traded Price
■ Volume
■ Open Interest

Step 5: Monitor and Update Data in Google Sheets

● The Google Sheets API will handle the actual process of adding the real-time
market data into the Google Sheet.
● The structure of your Google Sheet might look something like this:

Timestamp Strike Option Last Traded Volum Open


Price Type Price e Interest

2024-12-12 17950 Call 35.00 1000 5000


10:00:00

2024-12-12 17950 Put 40.00 1200 5300


10:01:00

... ... ... ... ... ...

● Every minute, data for the ATM and +/- 5 strikes will be updated in the sheet.

Step 6: Cost Estimate

Here’s a rough estimate of the cost:

1. Google Cloud Functions:

○ Free Tier: 2 million invocations/month.


○ Cost after Free Tier: $0.40 per million invocations.
2. Google Sheets API:

○ Free Tier: 100,000 requests/day.


○ Cost after Free Tier: Minimal, as you're likely under the limits.
3. Google Cloud Pub/Sub (if used for scalability):

○ Free Tier: 10GB data + 100,000 operations/month.


○ After Free Tier: $0.40 per GB and $0.40 per million operations.
4. Google Cloud Scheduler (if scheduling the function to trigger every minute):

○ Free Tier: 3 jobs/month.


○ Cost after Free Tier: $0.10 per job/month.
Conclusion

This solution involves the use of:

● Zerodha WebSocket API to fetch real-time options data.


● Google Cloud Functions to process this data and update it in a Google Sheet.
● Google Sheets API for seamless interaction with the sheet.
● Optionally, Google Cloud Pub/Sub and Cloud Scheduler to manage and schedule
tasks.

Although Google doesn’t directly provide live options data, combining Zerodha's WebSocket
API with Google Cloud's serverless architecture provides a scalable and cost-effective
solution. You can monitor the live options data for ATM and surrounding strikes for Nifty
on a real-time basis.

Let me know if you need more clarification!

Total Estimated Monthly Cost:

Total Monthly Cost Estimate for 20 Tokens:

● Zerodha API: ₹2,000.


● Google Cloud Functions: ₹142 (extra compute time).
● Google Cloud Scheduler: ₹360.
● Google Sheets API and Pub/Sub: Free.

Updated Summary of Estimated Costs (For 20 Tokens):

1. Zerodha API (WebSocket):


○ ₹2,000/month (fixed cost for 20 tokens).
2. Google Cloud Functions:
○ Invocations: Free (under the free tier).
○ Compute time: ₹142/month (for extra GB-seconds beyond the free tier).
3. Google Sheets API:
○ Free (under 100,000 requests/day).
4. Google Cloud Pub/Sub (Optional):
○ Free (under 10 GB data and 100,000 operations).
5. Google Cloud Scheduler (Optional):
○ ₹360/month (for 43,200 jobs per month).

Project: Real-Time Options Data Dashboard


Objective

To create a pipeline that fetches real-time options data from Zerodha's WebSocket API,
processes and stores it in a Google Sheet for monitoring, and optionally integrates Pub/Sub
and Cloud Scheduler for advanced task management.

Components and Workflow

1. Zerodha WebSocket API


○ Establish a WebSocket connection to fetch real-time options data such as
price, volume, and other market metrics.
○ Stream the data in JSON format.
2. Google Cloud Functions
○ Deploy a Cloud Function to act as a receiver for the WebSocket data.
○ Process the incoming data (e.g., filtering relevant options, extracting useful
fields).
○ Update the Google Sheet via the Sheets API.
3. Google Sheets API
○ Authenticate and connect to a specific Google Sheet.
○ Write the processed data into rows or update existing rows based on logic
(e.g., option symbol as key).
4. Optional: Google Cloud Pub/Sub
○ Use Pub/Sub to decouple the WebSocket data ingestion and processing
logic.
○ Publish real-time data to a Pub/Sub topic.
○ Trigger Cloud Functions to process the published data.
5. Optional: Google Cloud Scheduler
○ Schedule periodic tasks like cleaning up old data from the Google Sheet or
running analytics functions.

Project Flow

1. WebSocket Data Ingestion


○ Use Python or Node.js to establish a WebSocket connection with Zerodha's
API.
○ Stream data and push it to a Pub/Sub topic or directly to a Cloud Function.
2. Data Processing (Google Cloud Functions)
○ Extract relevant fields (e.g., option name, LTP, OI).
○ Perform any calculations or filtering.
3. Update Google Sheet
○ Authenticate with the Google Sheets API using OAuth credentials.
○ Update the sheet with real-time data in a tabular format.
4. Task Scheduling (Optional)
○ Use Cloud Scheduler to trigger functions for periodic cleanups or
aggregations.

Technology Stack

● Zerodha Kite Connect API (WebSocket for real-time data)


● Google Cloud Platform (GCP):
○ Cloud Functions (serverless compute)
○ Pub/Sub (messaging service)
○ Cloud Scheduler (task scheduling)
● Google Sheets API (data storage and visualization)
● Python/Node.js (programming language for Cloud Functions)

You might also like