The official Python client for communicating with PaytmMoney Equity API.
PMClient is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.
PaytmMoney Technology Pvt Ltd (c) 2022. Licensed under the MIT License.
pip3 install -r requirements.txt
from pyPMClient import PMClient
# Initialize PMClient using apiKey and apiSecret.
pm = PMClient(api_secret="your_api_secret", api_key="your_api_key")
# Initialize PMClient using apiKey, apiSecret & jwt tokens if user has already generated.
pm = PMClient(api_secret="your_api_secret", api_key="your_api_key", access_token="access_token", public_access_token="public_access_token", read_access_token="read_access_token")
# state_key : Variable key which merchant/fintech company expects Paytm Money to return with Request Token. This can be string.
pm.login(state_key)
User manually executes a login url in the browser and fetches requestToken after validating username, password, OTP and passcode.
- User manually executes a login url in the browser and fetches requestToken after validating username, password, OTP and passcode.
- After a successful login user will be provided the request_token in the URL.
- Once the request_token is obtained you can generate access_token by calling generate_session.
pm.generate_session(request_token="your_request_token")
pm.set_access_token("your_access_token")
pm.set_public_access_token("your_public_access_token")
pm.set_read_access_token("your_read_access_token")
- Here you can place regular, cover and bracket order.
- For cover order in argument user has to add trigger_price.
- For bracket order in argument user has to add stoploss_value & profit_value.
# Regular Order
order = pm.place_order(txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, source, off_mkt_flag)
# Cover Order
order = pm.place_order(txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, source, trigger_price)
# Bracket Order
order = pm.place_order(txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, source, stoploss_value, profit_value)
- Here you can modify orders.
- For cover order in argument user has to add leg_no.
- For bracket order in argument user has to add leg_no & algo_order_no.
# Regular Order
order = pm.modify_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id)
# Cover Order
order = pm.modify_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id, leg_no)
# Bracket Order
order = pm.modify_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id, leg_no, algo_order_no)
- Here you can Cancel Orders.
- For cover order in argument user has to add leg_no.
- For bracket order in argument user has to add leg_no & algo_order_no.
# Regular Order
order = pm.cancel_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id)
# Cover Order
order = pm.cancel_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id, leg_no)
# Bracket Order
order = pm.cancel_order(source, txn_type, exchange, segment, product, security_id, quantity, validity, order_type, price, mkt_type, order_no, serial_no, group_id, leg_no, algo_order_no)
- For converting orders.
# Regular Order
order = pm.convert_regular(source, txn_type, exchange, mkt_type, segment, product_from, product_to, quantity, security_id)
- Fetch details of all the order.
pm.order_book()
- Fetch Trade Details.
pm.trade_details(order_no, leg_no, segment)
- Get all the positions.
pm.position()
- Get position detail of specific stock.
pm.position_details(security_id, product, exchange)
- Get the funds history.
pm.funds_summary(config)
- Calculate Scrip Margin.
pm.scrips_margin(source, margin_list=[
"exchange":"exchange",
"segment":"segment",
"security_id":"security_id",
"txn_type":"txn_type",
"quantity":"quantity",
"strike_price":"strike_price",
"trigger_price":"trigger_price",
"instrument":"instrument"
])
- Calculate Order Margin.
pm.order_margin(source, exchange, segment, security_id, txn_type, quantity, price, product, trigger_price)
- Get value of the holdings.
pm.holdings_value()
- Get holdings data of User.
pm.user_holdings_data()
- User can filter by file_name.
- To get the supported fileName API Doc for fileNames
pm.security_master(file_name)
- Fetch user details.
pm.get_user_details()
pm.generate_tpin()
pm.validate_tpin(trade_type, isin_list=[])
- user can get the edis_request_id from the response of validate TPIN API.
pm.status(edis_request_id)
pm.logout()
- To create a GTT order.
pm.create_gtt(segment, exchange, pml_id, security_id, product_type, set_price, transaction_type, order_type, trigger_type, quantity, trigger_price, limit_price)
- To get all GTT or get by pml_id or status.
pm.get_gtt_by_pml_id_and_status(status, pml_id)
- To get GTT by Id.
pm.get_gtt(id)
- To update GTT by Id.
pm.update_gtt(id, quantity, trigger_price, limit_price, set_price, transaction_type, order_type, trigger_type)
- To Delete GTT by Id.
pm.delete_gtt(id)
- To get expiry of the GTT.
pm.get_gtt_expiry_date(pml_id)
- To get the aggregate of the GTTs.
pm.get_gtt_aggregate()
- To GTT by InstructionId.
pm.get_gtt_by_instruction_id(id)
- To create a GTT order.
- Note : transaction_details is a list of dictionary(key-value pair).
- Refer below sample requestBody
pm.create_gtt_v2(segment, exchange, security_id, product_type, set_price, transaction_type, trigger_type, transaction_details)
# Sample requestBody for OCO trigger_type
pm.create_gtt_v2(
segment = "E",
exchange = "BSE",
security_id = 500570,
product_type = "C",
set_price = "702.65",
transaction_type = "S",
trigger_type = "OCO",
transaction_details = [
{
"sub_type": "STOPLOSS",
"trigger_price": "695.60",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
},
{
"sub_type": "TARGET",
"trigger_price": "709.70",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
}
]
)
# Sample requestBody for SINGLE trigger_type
pm.create_gtt_v2(
segment = "E",
exchange = "BSE",
security_id = 500570,
product_type = "C",
set_price = "709.35",
transaction_type = "B",
trigger_type = "SINGLE",
transaction_details = [
{
"trigger_price": "702.25",
"order_type": "MKT",
"limit_price": 0,
"quantity": 1
}
]
)
- To get all GTT or get by pml_id or status.
pm.get_gtt_by_pml_id_and_status_v2(status, pml_id)
- To get GTT by Id.
pm.get_gtt_v2(id)
- To update GTT by Id.
- Note : transaction_details is a list of dictionary(key-value pair).
- Refer below sample requestBody
pm.update_gtt_v2(id, set_price, transaction_type, trigger_type, transaction_details)
pm.update_gtt_v2(
id=217,
set_price = "8.40",
transaction_type = "S",
trigger_type = "OCO",
transaction_details = [
{
"id": 218, #For OCO only
"sub_type": "STOPLOSS", #For OCO only
"quantity": "2",
"trigger_price": "9.0",
"limit_price": "15.0",
"order_type": "LMT"
},
{
"id": 219, #For OCO only
"sub_type": "TARGET", #For OCO only
"quantity": "2",
"trigger_price": "15.0",
"limit_price": "20",
"order_type": "LMT"
}
]
)
- To GTT by InstructionId.
pm.get_gtt_by_instruction_id_v2(id)
- To Get Live Price Data via API
pm.get_live_market_data("mode", preferences)
- To Get Option Chain using type, symbol and expiry (in DD-MM-YYYY format)
pm.get_option_chain("type", "symbol", "expiry")
- To Get Option Chain Config using symbol
pm.get_option_chain_config("symbol")
- Get all orders without apiKey filter
pm.orders()
- Get Charges Info
pm.charges_info("brokerage_profile_code", "transaction_type", "product_type", "instrument_type", "exchange", qty, price)
- To use websocket client in your project, add below code in a python file -
from pmClient.WebSocketClient import WebSocketClient
webSocketClient = WebSocketClient("your_public_access_token") # pass your public access token here
customerPreferences = []
preference = {
"actionType": 'ADD', # 'ADD', 'REMOVE'
"modeType": 'LTP', # 'LTP', 'FULL', 'QUOTE'
"scripType": 'INDEX', # 'ETF', 'FUTURE', 'INDEX', 'OPTION', 'EQUITY'
"exchangeType": 'NSE', # 'BSE', 'NSE'
"scripId": '13'
}
# create as many preferences as you like as shown above and append them in customerPreferences array
customerPreferences.append(preference)
def on_open():
# send preferences via websocket once connection is open
webSocketClient.subscribe(customerPreferences)
def on_close(code, reason):
# this event gets triggered when connection is closed
print(code, reason)
def on_error(error_message):
# this event gets triggered when error occurs
print(error_message)
def on_message(arr):
# this event gets triggered when response is received
print(arr)
webSocketClient.set_on_open_listener(on_open)
webSocketClient.set_on_close_listener(on_close)
webSocketClient.set_on_error_listener(on_error)
webSocketClient.set_on_message_listener(on_message)
"""
set below reconnect config if reconnect feature is desired
Set first param as true and second param, the no. of times retry to connect to server shall be made
"""
webSocketClient.set_reconnect_config(True, 5)
# this method is called to create a websocket connection with broadcast server
webSocketClient.connect()
# To explicitly close websocket connection with server, call this method
webSocketClient.disconnect()