Api for live MTM positions

HI everyone, can someone please help with any api to get live mtm positions using dhan api?

Hi @Rajnish_aryan
There is no direct api call to fetch live MTM, you can use below function.

def get_live_pnl():
try:
	time.sleep(1)
	pos_book = Dhan.get_positions()
	pos_book_dict = pos_book['data']
	pos_book = pd.DataFrame(pos_book_dict)
	live_pnl = []

	if pos_book.empty:
		return 0

	for pos_ in pos_book_dict:
		security_id = int(pos_['securityId'])
		underlying = instrument_df[((instrument_df['SEM_SMST_SECURITY_ID']==security_id))].iloc[-1]['SEM_CUSTOM_SYMBOL']
		closePrice = get_ltp(underlying)
		Total_MTM = (float(pos_['daySellValue']) - float(pos_['dayBuyValue'])) + (int(pos_['netQty']) *closePrice * float(pos_['multiplier']))
		live_pnl.append(Total_MTM)
	
	return sum(live_pnl)
except Exception as e:
	self.logger.exception(f'got exception in pnl as {e} ')
	traceback.print_exc()
	return 0

or you can use it through codebase, as described in session 3

https://www.youtube.com/watch?v=DPWU5wLuYcQ

1 Like