how to access margin calculator api using python ?
import requests
url = ‘https://api.fyers.in/api/v2/span_margin’
headers = {
'Authorization': 'UC0KMO****-102:eyJ0eX*****',
'Content-Type': 'application/json'
}
data = {
"data": [
{
"symbol": "NSE:BANKNIFTY23NOV44400CE",
"qty": 50,
"side": -1,
"type": 2,
"productType": "INTRADAY",
"limitPrice": 0.0,
"stopLoss": 0.0
},
{
"symbol": "NSE:BANKNIFTY23NOVFUT",
"qty": 50,
"side": -1,
"type": 2,
"productType": "INTRADAY",
"limitPrice": 0.0,
"stopLoss": 0.0
}
]
}
response = requests.post(url, headers=headers, json=data)
print(r
try this i got this fromm chatgpt
We currently proving CURL request for pan margin .please use https://myapi.fyers.in/docsv3#tag/Margin-Calculator
Any update on the margin calculator in python? is there any plan to release it?
Thank you.
def get_margin(self, symbol, lot_size):
try:
market_data = app.get_quote(exchange="NSE", symbols=[symbol])
if (
market_data["s"] == "ok"
and "d" in market_data
and len(market_data["d"]) > 0
):
instrument_data = market_data["d"][0]
price = instrument_data["v"].get("lp", 0) # Last traded price (LTP)
if price <= 0 or lot_size <= 0:
raise ValueError("Invalid price or lot size received from API.")
# Calculate the margin required per lot
margin_per_lot = (
price * lot_size
) # Adjust for any specific margin rules
# Fetch available funds using your app.get_fund() method
# funds = app.get_fund()
funds = 10000
if funds <= 0:
raise ValueError("Insufficient funds or fund retrieval error.")
# Calculate the number of lots
num_lots = funds // margin_per_lot # Integer division
return int(num_lots)
except:
traceback.print_exc()
Thank you @105900790867029970635 , this is very crude method to calculate the margin, I do have my own but wanted to check on Python API.
def checkMarginFyers(marginsData,broker_handle):
marginURL = 'https://api-t1.fyers.in/api/v3/multiorder/margin'
auth_token = f"{broker_handle.client_id}:{broker_handle.token}"
#print(auth_token)
headers = {
'Authorization': auth_token,
'Content-Type': 'application/json'
}
data = {
"data":marginsData
}
response = requests.post(marginURL, headers=headers, json=data)
margin_total = 0
margin_avail = 0
if response.status_code == 200:
marginJson = response.json()
if len(marginJson) > 0:
margin = response.json().get('data')
margin_total = margin.get('margin_total')
margin_avail = margin.get('margin_avail')
print(f"Margin Required: {margin_total} and Available Margin is : {margin_avail}")
if margin_avail > margin_total:
print('Margin Available to enter Trade')
return True
else:
return False
Hi, Nagesh Rudhra Yellapu thanks for your reponse but my method will get the exact quantity we can able to buy for each stock based on the balance available in the account so it will calculate the margin required for no of qty’s that can be bought for a particular stock
Note:- This method not uses seperate request it fetches ltp and checks the margin