I have encountered a new issue today. Before placing an order, my code checks for available funds and does some risk management calculation. Today at 13:30 and exception was thrown up and it got disconnected. Needless to say that it was a momentum signal and I lost out on an important money making opportunity.
My code :
def getDeployableFunds(u, capAllocated):
funds = u.getFunds()
print(funds)
funds = funds['data']['equity']['available_margin']
print(f' Available margin : {funds}')
funds_deployable_ = int(funds * capAllocated / 100) # % of funds to be deployed
print(f' Funds deployable : {funds_deployable_}')
return funds_deployable_
Code of u.getfunds()
def getFunds(self):
self.base_url = "https://api.upstox.com/v2"
endpoint = '/user/get-funds-and-margin'
uri = f"{self.base_url}{endpoint}"
params = {'segment': 'SEC'}
response = requests.get(uri, headers=self.headers, params=params)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Access the response data
funds = response.json()
# print(data)
return funds
else:
# Handle any errors
print('Request failed with status code:', response.status_code)
print(response.json()['errors'][0]['message'])
return None
Following exception was thrown :
File "/media/Ubuntu Storage/Upstox/Order_Placement.py", line 27, in place_Order
funds_deployable = getDeployableFunds(u, capAllocated=capAllocated)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/Funds_Deployable.py", line 4, in getDeployableFunds
funds = u.getFunds()
^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/Upstox_API/Upstox_Helper.py", line 345, in getFunds
response = requests.get(uri, headers=self.headers, params=params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/venv/lib/python3.12/site-packages/requests/api.py", line 73, in get
return request("get", url, params=params, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/venv/lib/python3.12/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/venv/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/venv/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/Ubuntu Storage/Upstox/venv/lib/python3.12/site-packages/requests/adapters.py", line 682, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')