Package: espressoconnect
Espresso-API-PyhtonEspresso-API-Python is a Python library that provides a simple interface for accessing the Espresso API,which is a RESTful API for real-time streaming of financial market data.
With Espresso-API-Python, developers and traders can easily retrieve real-time market data such as stock prices, market trends, historical data, stream live market data (WebSockets), and more.InstallationUse the package managerpipto install espressoconnectpythonpipinstallespressoconnectpipinstallwebsocketUsage# package import statementfromespressoApi.espressoConnectimportEspressoConnect# Make a object callapi_key="Your API KEY"""" vendor_key="Vendor Key" """espressoApi=EspressoConnect(api_key=api_key)# Print the login url""" Pass vendor_key if it is needed """print(espressoApi.login_url())# Pass the required parameters for decryption and encryption""" After Successfully Login You will receive the request token valuethen you have to decrypt the token value by using secret key andthen swap the request token which is a combination of RequestId and CustomerIdThen after that decrypt the request token value   """request_token="Your request token"secret_key="Your secret key""""Use generate session method for decrypt and re-encrypt the request token value """session=espressoApi.generate_session(request_token,secret_key)"""Use get_access_token for generating access token """access_token=espressoApi.get_access_token(api_key,session,vendor_key,state)print(access_token)access_token='Your access token value'espressoApi=EspressoConnect(api_key=api_key,access_token=access_token)print(espressoApi.requestHeaders())# for printing request headers# place order historyorderparams={"customerId":XXXXXX,"scripCode":2475,"tradingSymbol":"ONGC",-->(AllowedparametersNC/BC/NF/RN/MX)"exchange":"NC","transactionType":"B",-->(Allowedparameters(B,S,BM,SM,SAM))"quantity":1,"disclosedQty":0,"price":"145.85","triggerPrice":"0","rmsCode":"ANY","afterHour":"N","orderType":"NORMAL","channelUser":"XXXXXX",-->(UseLoginIdasChannelUser)"validity":"GFD",-->(Validityofanorder(GFD/MyGTD/IOC))"requestType":"NEW","productType":"CNC""""Note: For Equity Exchange – CNC (Normal), For F&O – CNF(Normal), MIS or MIS+)""""instrumentType":"FUTCUR",-->((FutureStocks(FS)/FutureIndex(FI)/OptionIndex(OI)/OptionStocks(OS)/FutureCurrency(FUTCUR)/OptionCurrency(OPTCUR)))"strikePrice":"-1","""For optiontype""""optionType":"XX","expiry":"31/03/2023"}order=espressoApi.placeOrder(orderparams)print("PlaceOrder:{}".format(order))# modify orderorderparams={"orderId":"XXXXXXX","customerId":XXXXXX,"scripCode":2475,"tradingSymbol":"ONGC",-->(AllowedparametersNC/BC/NF/RN/MX)"exchange":"NC","transactionType":"B",-->(Allowedparameters(B,S,BM,SM,SAM))"quantity":1,"disclosedQty":0,"price":"145.85","triggerPrice":"0","rmsCode":"ANY","afterHour":"N","orderType":"NORMAL","channelUser":"XXXXXX",-->(UseLoginIdasChannelUser)"validity":"GFD",-->(Validityofanorder(GFD/MyGTD/IOC))"requestType":"MODIFY",-->(requestTypeshouldbeMODIFYformodifyingtheorder)"productType":"CNC""""Note: For Equity Exchange – CNC (Normal), For F&O – CNF(Normal), MIS or MIS+)""""instrumentType":"FUTCUR",-->((FutureStocks(FS)/FutureIndex(FI)/OptionIndex(OI)/OptionStocks(OS)/FutureCurrency(FUTCUR)/OptionCurrency(OPTCUR)))"strikePrice":"-1","""For optiontype""""optionType":"XX","expiry":"31/03/2023"}order=espressoApi.modifyOrder(orderparams)print("ModifyOrder:{}".format(order))# cancel orderorderparams={"orderId":"XXXXXX","customerId":XXXXXX,"scripCode":2475,"tradingSymbol":"ONGC",-->(AllowedparametersNC/BC/NF/RN/MX)"exchange":"NC","transactionType":"B",-->(Allowedparameters(B,S,BM,SM,SAM))"quantity":1,"disclosedQty":0,"price":"145.85","triggerPrice":"0","rmsCode":"ANY","afterHour":"N","orderType":"NORMAL","channelUser":"XXXXXX",-->(UseLoginIdasChannelUser)"validity":"GFD",-->(Validityofanorder(GFD/MyGTD/IOC))"requestType":"CANCEL",-->(requestTypeshouldbeCANCELforcanceltheorder)"productType":"CNC""""Note: For Equity Exchange – CNC (Normal), For F&O – CNF(Normal), MIS or MIS+)""""instrumentType":"FUTCUR",-->((FutureStocks(FS)/FutureIndex(FI)/OptionIndex(OI)/OptionStocks(OS)/FutureCurrency(FUTCUR)/OptionCurrency(OPTCUR)))"strikePrice":"-1","""For optiontype""""optionType":"XX","expiry":"31/03/2023"}order=espressoApi.cancelOrder(orderparams)print("CancelOrder:{}".format(order))# Retrieves all positionscustomerId="customerId < int data type>"order=espressoApi.trades(customerId)print("Retrieves All Position:{}".format(order))# Retrieve history of an given orderexchange="exchange value <string>"customerId="customerId <int data type>"orderId="orderId <int data type>"order=espressoApi.exchange(exchange,customerId,orderId)print("History of an give order:{}".format(order))# Retrieves the trade  generated by an orderexchange="exchange value <string>"customerId="customerId <int data type>"orderId="orderId <int data type>"order=espressoApi.exchangetrades(exchange,customerId,orderId)print("Trade Generated By an Order :{}".format(order))# services HoldingscustomerId="customerId <int data type>"order=espressoApi.holdings(customerId)print("Holdings :{}".format(order))# Script Masterexchange="exchange value <string>"order=espressoApi.master(exchange)print("Script Master :{}".format(order))# Scrip Master data by CSV-Without Access-Tokenexchange="exchange value <string>"order=espressoApi.mastercsv(exchange)print("Script Master CSV :{}".format(order))# Historical Dataexchange="exchange value <string>"scripcode="Unique scripcode provided by the broker <int>"interval="Available Intervals <string>"order=espressoApi.historicaldata(exchange,scripcode,interval)print("Holdings Data:{}".format(order))websocket Programming TestingfromespressoApi.espressoApiWebsocketimportEspressoWebSocketparams={"access_token":access_token,"api_key":api_key}token_list={"action":"subscribe","key":["feed"],"value":[""]}feed={"action":"feed","key":["depth"],"value":["MX250715"]}unsubscribefeed={"action":"unsubscribe","key":["feed"],"value":["NC22,NF37833,NF37834,MX253461,RN7719"]}sws=EspressoWebSocket(access_token,api_key)defon_data(wsapp,message):print("Ticks:{}".format(message))defon_open(wsapp):print("on open")sws.subscribe(token_list)sws.fetchData(feed)# sws.unsubscribe(feed)# sws.close_connection()defon_error(wsapp,error):print(error)defon_close(wsapp):print("Close")# Assign the callbacks.sws.on_open=on_opensws.on_data=on_datasws.on_error=on_errorsws.on_close=on_closesws.connect()