### Install Development Requirements Source: https://github.com/angel-one/smartapi-python/blob/main/README.md Installs the necessary packages for development by referencing the requirements_dev.txt file. ```bash pip install -r requirements_dev.txt ``` -------------------------------- ### Install pycryptodome Source: https://github.com/angel-one/smartapi-python/blob/main/README.md Ensures the pycryptodome package is installed by first uninstalling the older pycrypto package and then installing pycryptodome. ```bash pip uninstall pycrypto pip install pycryptodome ``` -------------------------------- ### SmartAPI WebSocket V2 Subscription and Unsubscription Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This example demonstrates how to use the SmartWebSocketV2 class to subscribe to and unsubscribe from real-time market data. It includes setting up callbacks for data reception, connection opening, errors, and closing. ```python from SmartApi.smartWebSocketV2 import SmartWebSocketV2 from logzero import logger AUTH_TOKEN = "authToken" API_KEY = "api_key" CLIENT_CODE = "client code" FEED_TOKEN = "feedToken" correlation_id = "abc123" action = 1 mode = 1 token_list = [ { "exchangeType": 1, "tokens": ["26009"] } ] token_list1 = [ { "action": 0, "exchangeType": 1, "tokens": ["26009"] } ] sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN) def on_data(wsapp, message): logger.info("Ticks: {}".format(message)) # close_connection() def on_open(wsapp): logger.info("on open") sws.subscribe(correlation_id, mode, token_list) # sws.unsubscribe(correlation_id, mode, token_list1) def on_error(wsapp, error): logger.error(error) def on_close(wsapp): logger.info("Close") def close_connection(): sws.close_connection() # Assign the callbacks. sws.on_open = on_open sws.on_data = on_data sws.on_error = on_error sws.on_close = on_close sws.connect() ``` -------------------------------- ### Download Core Packages Source: https://github.com/angel-one/smartapi-python/blob/main/README.md Downloads essential Python packages including pyotp for authentication, logzero for logging, and websocket-client for real-time communication. ```bash pip install pyotp pip install logzero pip install websocket-client ``` -------------------------------- ### Placing a Limit Order with SmartAPI Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This code demonstrates how to place a limit order for a specific trading symbol on a given exchange. It shows how to structure the order parameters and includes two methods for order placement: one returning the order ID and another returning the full response. ```python orderparams = { "variety": "NORMAL", "tradingsymbol": "SBIN-EQ", "symboltoken": "3045", "transactiontype": "BUY", "exchange": "NSE", "ordertype": "LIMIT", "producttype": "INTRADAY", "duration": "DAY", "price": "19500", "squareoff": "0", "stoploss": "0", "quantity": "1" } # Method 1: Place an order and return the order ID orderid = smartApi.placeOrder(orderparams) logger.info(f"PlaceOrder : {orderid}") # Method 2: Place an order and return the full response response = smartApi.placeOrderFullResponse(orderparams) logger.info(f"PlaceOrder : {response}") ``` -------------------------------- ### Creating a GTT Rule with SmartAPI Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This snippet illustrates the process of creating a Good Till Triggered (GTT) rule for a trading instrument. It defines the necessary parameters such as trading symbol, trigger price, quantity, and validity period. ```python gttCreateParams={ "tradingsymbol" : "SBIN-EQ", "symboltoken" : "3045", "exchange" : "NSE", "producttype" : "MARGIN", "transactiontype" : "BUY", "price" : 100000, "qty" : 10, "disclosedqty": 10, "triggerprice" : 200000, "timeperiod" : 365 } rule_id=smartApi.gttCreateRule(gttCreateParams) logger.info(f"The GTT rule id is: {rule_id}") ``` -------------------------------- ### SmartAPI Authentication and Session Generation Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This snippet demonstrates how to authenticate with SmartAPI using your API key, username, and PIN, and generate a session token. It includes error handling for invalid tokens and session generation failures. ```python from SmartApi import SmartConnect import pyotp from logzero import logger api_key = 'Your Api Key' username = 'Your client code' pwd = 'Your pin' smartApi = SmartConnect(api_key) try: token = "Your QR value" totp = pyotp.TOTP(token).now() except Exception as e: logger.error("Invalid Token: The provided token is not valid.") raise e correlation_id = "abcde" data = smartApi.generateSession(username, pwd, totp) if data['status'] == False: logger.error(data) else: authToken = data['data']['jwtToken'] refreshToken = data['data']['refreshToken'] feedToken = smartApi.getfeedToken() res = smartApi.getProfile(refreshToken) smartApi.generateToken(refreshToken) res=res['data']['exchanges'] ``` -------------------------------- ### Project Dependencies Source: https://github.com/angel-one/smartapi-python/blob/main/requirements_dev.txt This snippet lists the Python packages and their specific versions that are required to run the Angel One SmartAPI project. These dependencies ensure compatibility and proper functionality of the project's components. ```python attrs==23.1.0 autobahn==23.6.2 Automat==22.10.0 certifi==2023.5.7 cffi==1.15.1 charset-normalizer==3.1.0 constantly==15.1.0 cryptography==43.0.1 hyperlink==21.0.0 idna==3.4 incremental==22.10.0 isodate==0.6.1 logzero==1.7.0 pycparser==2.21 pyotp==2.8.0 pyparsing==3.1.0 python-dateutil==2.8.2 pytz==2023.3 rdflib==6.3.2 rdflib-jsonld==0.6.2 requests==2.31.0 simplejson==3.19.1 six==1.16.0 smartapi-python==1.4.8 Twisted==22.10.0 txaio==23.1.1 typing_extensions==4.6.3 urllib3==2.0.3 websocket-client==1.8.0 zope.interface==6.0 ``` -------------------------------- ### SmartWebSocket Order Update Connection Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This snippet shows how to establish a connection for receiving real-time order updates using the SmartWebSocketOrderUpdate class. It requires authentication tokens and client code. ```python from SmartApi.smartWebSocketOrderUpdate import SmartWebSocketOrderUpdate client = SmartWebSocketOrderUpdate(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN) client.connect() ``` -------------------------------- ### Listing GTT Rules with SmartAPI Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This code shows how to retrieve a list of existing GTT rules. You can filter the rules by their status and paginate the results using page number and count parameters. ```python status=["FORALL"] #should be a list page=1 count=10 lists=smartApi.gttLists(status,page,count) ``` -------------------------------- ### Logging out from SmartAPI Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This code snippet shows how to terminate the current session with SmartAPI by calling the `terminateSession` method with your client ID. ```python logout=smartApi.terminateSession('Your Client Id') logger.info("Logout Successfull") ``` -------------------------------- ### Fetching Historical Candle Data with SmartAPI Source: https://github.com/angel-one/smartapi-python/blob/main/README.md This snippet demonstrates how to fetch historical candlestick data for a given symbol and interval. You need to specify the exchange, symbol token, interval, and the date range (from and to dates). ```python historicParam={ "exchange": "NSE", "symboltoken": "3045", "interval": "ONE_MINUTE", "fromdate": "2021-02-08 09:00", "todate": "2021-02-08 09:16" } smartApi.getCandleData(historicParam) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.