Explanation of the Issue with Our Algo Trading System

Dear Alice Blue Support Team,

I wanted to provide you with a clear overview of the problem we’re encountering in our algo trading system, as discussed earlier with Venkatesh from your support team.

In our daily algo trading activities, we manage multiple accounts with various trading strategies and logics. To execute our algorithms effectively, we rely on certain checks related to order statuses during the trading session.

For instance, we monitor the completion of Stoploss orders so that we can cancel Target orders accordingly.

Additionally, we also need to continuously track the Last Traded Price (LTP) of various indices throughout the trading day to inform our algorithmic decisions. This continuous process requires us to make frequent API calls to Alice Blue throughout the trading session, which typically spans from 9:00 a.m. to 3:00 p.m.

To ensure we do not overload the APIs with requests, we have implemented a minimum sleep duration of 3 to 5 seconds within each function. This helps distribute our API requests more evenly over time, reducing the strain on the system.

Furthermore, we have implemented a retry mechanism in our functions, even in cases where we receive an error from the Alice Blue API. We utilize a loop structure to retry the function, and only when we receive the correct output, do we exit the loop. This simple yet effective logic is crucial in preventing our algorithms from crashing during the market hours. Once an algorithm stops prematurely, it becomes impossible for us to execute it for that particular account on that specific day.

Errors we are facing:

1st Error:

Function : order_status= alice.get_order_history(str(oid))[‘Status’}
Error: {TypeError(‘string indices must be integers’)}

2nd Error:

Code: ltp = alice.get_scrip_info(instrument)[‘Ltp’]

Error: {KeyError(‘Ltp’)}

3rd Error:

Code: WEBSOCKET (we use same websocket code as mentioned in pya3 documentation)

Error: Error : Connection to remote host was lost.

4th Error:

Code: alice.place_order()

Error: {‘stat’: ‘Not_Ok’, ‘Emsg’: ‘Session Expired’}
session shows Expired even after Login

Please note that the errors mentioned above occur randomly; there is no specific timing for these errors. Some days we do not encounter them at all, while on other days, they occur frequently and can potentially disrupt our algorithmic trading operations.

We have attached all the logs and screenshots of our code for your review and analysis to help improve our system. Please take a moment to go through them.

We hope this explanation clarifies our situation and helps in identifying any potential issues or optimizations that can be made to ensure the stability and efficiency of our algo trading operations.

NOTE: To gain a clearer understanding of the challenges we are encountering, you can run a while loop continuously throughout the trading session, calling the function concurrently. This will help you observe the issues with the API in real-time.

Code:
(as we can only upload one Media here , so I am attaching Error logs as image and writing the code here)


def get_ltp_info(alice,instrument):
	for i in range(30):
		try:
			ltp = alice.get_scrip_info(instrument)['Ltp']
			return ltp
		except Exception as e:
			print("ERROR IN LTP FETCH-> ",{e})
			sleep(2)
			pass
          

def get_order_status(alice,oid):
    order_status= None
    sleep(1)
    for i in range(11):
        try:
            order_details = alice.get_order_history(str(oid))
            order_status = order_details['Status']
            return order_status
        except Exception as e:
            print("ORDER STATUS ERROR:",{e})
            sleep(10)
            pass

We are checking with developer team we will come back to you soon.