To fetch the Last Traded Price (LTP) using the ICICI Breeze API, you would generally follow these steps:
1. Set Up Your Environment
Ensure you have Python installed and that you're ready to use the requests library for HTTP requests.
2. Obtain API Credentials
You'll need an API key and secret from ICICI Breeze. These are necessary to authenticate your requests.
3. Authentication
Authenticate your requests with the API key and secret.
4. Define the API Endpoint and Request Parameters
You will typically use an endpoint provided by the Breeze API to fetch market data like LTP.
import requests
# Replace with your API credentials
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
# Base URL for Breeze API
base_url = 'https://api.icicidirect.com/breezeapi'
# Headers with API credentials
headers = {
'Content-Type': 'application/json',
'api-key': api_key,
'api-secret': api_secret
}
# Define the symbol you want to fetch the LTP for
symbol = 'RELIANCE' # Example: Reliance Industries
# Endpoint to fetch LTP (adjust the endpoint based on API documentation)
ltp_url = f'{base_url}/market/ltp'
# Parameters for the request
params = {
'symbol': symbol,
'exchange': 'NSE' # Specify the exchange, e.g., NSE or BSE
}
5. Fetch the LTP
Make a GET request to fetch the LTP.
python
# Fetch the LTP
response = requests.get(ltp_url, headers=headers, params=params)
# Check the response
if response.status_code == 200:
ltp_data = response.json()
print("LTP:", ltp_data.get('ltp'))
else:
print("Failed to fetch LTP:", response.text)
6. Handle the Response
Parse the response to extract the LTP.
python
if response.status_code == 200:
ltp_data = response.json()
ltp = ltp_data['ltp'] # Adjust the key according to the actual response structure
print(f"LTP of {symbol}: {ltp}")
else:
print("Error fetching LTP:", response.text)
Important Notes:
API Documentation: Always refer to the latest ICICI Breeze API documentation to ensure you're using the correct endpoints and parameters.
Error Handling: Implement error handling to manage potential issues, such as invalid symbols or network errors.
Rate Limits: Be aware of any rate limits imposed by the API to avoid throttling.