This Python script provides functionalities to interact with Zerodha API for accessing stock market data, placing orders, and retrieving historical data.
Ensure you have the required Python libraries installed:
requestsdateutil
You can install these dependencies using pip:
pip install requests python-dateutil-
API Authentication:
- Obtain your Zerodha API credentials (user_id, password, twofa) to authenticate API requests.
-
Installation:
- Clone or download the script to your local machine.
-
Run the Script:
- Open the script in your Python environment and execute it.
The script provides the following functionalities:
-
Authentication:
- Authenticate user credentials and obtain the
enctokenfor subsequent API calls.
- Authenticate user credentials and obtain the
-
Zerodha API Wrapper:
- Implements a
KiteAppclass to interact with Zerodha API endpoints. - Provides methods for fetching instruments, quotes, historical data, margins, orders, and positions.
- Implements a
-
Data Retrieval:
- Retrieve historical market data for specified stock symbols within a date range.
- Supports multi-threading for efficient data retrieval.
-
Initialize Zerodha API:
- Obtain the
enctokenfrom Zerodha web and initialize theKiteAppinstance.
- Obtain the
-
Retrieve Historical Data:
- Modify the
symbol_listand date parameters to specify the stocks and date range for data retrieval. - Run the script to retrieve and store historical data in CSV format.
- Modify the
-
Customization:
- Customize the script by adjusting API endpoints, adding new functionalities, or modifying data processing logic.
zerodha_api_integration.py: Main script for Zerodha API integration.nifty.csv: CSV file containing stock symbols and IDs for data retrieval.Data/: Directory to store retrieved market data in CSV format.
- Ensure you have valid Zerodha API credentials and permissions to access the required endpoints.
- Customize the script as per your specific requirements and data retrieval preferences.
- Use multi-threading for efficient data retrieval across multiple stock symbols and date ranges.
This Python script allows you to resample stock market data stored in CSV files into 5-minute intervals (OHLCV format) and save the resampled data to separate CSV files.
Before using this script, ensure you have the following installed:
- Python (3.x recommended)
- Required Python libraries (
pandas,numpy,glob)
Install the required libraries using pip:
pip install pandas numpyThe script performs the following tasks:
-
Import Libraries:
- Import necessary libraries including
pandas,numpy,glob,datetime, andwarnings.
- Import necessary libraries including
-
Set Paths:
- Define the paths for reading original data (
path) and saving resampled data (path_to_save_data).
- Define the paths for reading original data (
-
Read Files:
- Use
glob.glob()to retrieve a list of file paths for the original data.
- Use
-
Resampling Process:
- Loop through each file path and read the CSV file into a pandas DataFrame.
- Convert the 'Date' and 'TIME1' columns to datetime format and set the 'Datetime' column as the index.
- Resample the data to 5-minute intervals (
"5T"), aggregating OHLCV values. - Filter the resampled data to include only market hours (between 09:15 AM and 03:25 PM) and remove any NaN values.
-
Save Resampled Data:
- Rename columns for consistency (e.g., 'CLOSE' to 'Close').
- Define the file save path for the resampled data.
- Save the resampled data to a CSV file without including the index.
-
Specify Paths:
- Modify
path_to_save_dataandpathvariables according to your directory structure.
- Modify
-
Run the Script:
- Execute the script in your Python environment.
-
Output:
- The resampled data will be saved as separate CSV files in the specified
path_to_save_datadirectory.
- The resampled data will be saved as separate CSV files in the specified
- Ensure that the original data files are in CSV format with columns for 'Date', 'TIME1', 'Open', 'High', 'Low', 'CLOSE', and 'VOLUME'.
- Customize the script as needed to handle different data formats or adjust the resampling parameters.
- Use this script to preprocess and organize stock market data for further analysis and modeling.