A library for interacting with SmartAPI for historical data, trading sessions, and more.
Project description
SmartAPI - Python Library for Angel Broking API Integration Overview This Python library (SmartAPI) is designed to interact with the Angel Broking API for fetching financial data such as instrument details, live market data, historical data, and more. The library makes use of several popular Python libraries, including pandas, requests, and pyotp, to handle data fetching, processing, and API authentication.
The provided script includes functions for logging in, fetching market data, handling sessions, and calculating live market prices and historical data for specific instruments.
Features Login: Allows users to log in with their credentials in an easy way to interact with their API. Instrument Data: Fetches instrument details such as tokens, expiry dates, and more. Live Market Data: Retrieves the last traded price (LTP) for instruments. Historical Data: Fetches OHLC (Open, High, Low, Close) data for instruments. Session Handling: Initializes and manages separate sessions for historical and trading APIs. Time Zone Management: Provides functionality to fetch the current time in IST (Indian Standard Time). Installation This library requires several Python packages. You can install the required dependencies using pip.
Step 1: Install Required Libraries To ensure that all necessary libraries are installed, the following Python script checks for and installs the missing libraries:
$pip install pandas requests pytz pyotp
$pip install smartapi-login
Usage Here’s how you can use the SmartAPI class in your script:
Import the library
from smartapi_login import SmartAPIHelper
- Initialize the SmartAPI class
api = SmartAPIHelper()
-
Login with Your Credentials
You need to provide your credentials for logging into the API: api.login(api_key_hist='YOUR_HISTORICAL_API_KEY', api_key_trading='YOUR_TRADING_API_KEY', uid='YOUR_USER_ID', mpin='YOUR_MPIN', totp='YOUR_TOTP_SECRET') -
Fetch Instrument Data To fetch the instrument details (e.g., symbol, token, exchange segment): instrument_df = api.fetch_instrument_df() print(instrument_df.head())
-
Fetch the Last Traded Price (LTP) To get the LTP for a specific instrument (e.g., 'WIPRO'): ltp = api.get_ltp('WIPRO') print(f"Last Traded Price: {ltp}")
-
Fetch Trading Symbols To fetch the trading symbols based on the current spot price: symbols_df = api.get_tradingsymbols() print(symbols_df.head())
-
Fetch Historical Data (OHLC) To fetch OHLC data for a specific symbol: ohlc_data = api.get_ohlc('BANKNIFTY', '5minute', 30) # 30 days of data print(ohlc_data.head())
Note:- Time interval format that supported by angelone smartapi
ONE_MINUTE, THREE_MINUTE, FIVE_MINUTE, TEN_MINUTE, FIFTEEN_MINUTE, THIRTY_MINUTE, ONE_HOUR, ONE_DAY ,
- Get Current Time in IST To get the current time in IST: current_time = api.get_ist_now() print(f"Current IST Time: {current_time}") Methods: login(api_key_hist, api_key_trading, uid, mpin, totp) Logs in with the provided credentials (API keys, user ID, MPIN, and TOTP secret).
fetch_instrument_df() Fetches the instrument details (tokens, expiry dates, etc.) and returns it as a pandas DataFrame.
get_historical_data_session() Generates a session for accessing historical market data.
get_trading_api_session() Generates a session for trading-related API interactions.
initialize_sessions() Initializes both historical and trading API sessions.
get_ltp(symbol) Fetches the Last Traded Price (LTP) for a given symbol.
get_tradingsymbols('Nifty Bank') Fetches the trading symbols for Nifty or BankNifty options, with the strike prices ranging from 1500 points above to 1500 points below the spot price of Nifty or BankNifty.
get_ohlc(symbol, interval, n) Fetches the OHLC data for the given symbol over the specified interval and number of days (n).
n: The number of days for which you want to extract data. interval: The time interval for the OHLC data. It should be one of the following: ONE_MINUTE: 1 Minute THREE_MINUTE: 3 Minutes FIVE_MINUTE: 5 Minutes TEN_MINUTE: 10 Minutes FIFTEEN_MINUTE: 15 Minutes THIRTY_MINUTE: 30 Minutes Note: For timeframes shorter than 1 day, the maximum limit is 60 days, as imposed by the broker.
get_ist_now() Fetches the current time in IST (Indian Standard Time).
Example Here’s an example of how to use the entire class:
Initialize the API
api = SmartAPI()
Login
api.login(api_key_hist='your_api_key_hist', api_key_trading='your_api_key_trading', uid='your_user_id', mpin='your_mpin', totp='your_totp_secret')
Fetch instrument data
instrument_df = api.fetch_instrument_df() print(instrument_df.head())
Fetch LTP
ltp = api.get_ltp('Nifty Bank') print(f"LTP: {ltp}")
Fetch OHLC data
ohlc_data = api.get_ohlc('BANKNIFTY', '5minute', 30) print(ohlc_data.head())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smartapi_login-1.0.0.7.tar.gz.
File metadata
- Download URL: smartapi_login-1.0.0.7.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60c45f2297ce4f6206bd964fb705f4991e06c6b26e811412bc52d33f70c813fe
|
|
| MD5 |
755597cf5bb7932f37027df196a3e1e6
|
|
| BLAKE2b-256 |
654a55f150aa6f7ced8bf7e557a5ab12c9e537d30d8ea42b1041ae34a6b62032
|
File details
Details for the file smartapi_login-1.0.0.7-py3-none-any.whl.
File metadata
- Download URL: smartapi_login-1.0.0.7-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6c9b4c4f3603e53e6006d8aad14555e5f26289af3a37e2a9f7bac025586f252
|
|
| MD5 |
49d1a03f6fc02d7c1a7b9a31abf27426
|
|
| BLAKE2b-256 |
2ad4b5dd0247309c3322387c185bd77a4f3e1b324432857369ef3e1df62629cc
|