Speeding up Your Upstox Algorithms by 50% with a minor Tweak


The Flaw in WSS system of Upstox is it will give you null data sometimes when you ask for LTP. You need to ask it multiple times.

Today a minor issue clicked on my head increasing the execution speed to the tune of 400%. Without Further, Let me explain the schema -

Instead of doing a database gimmick, I did a variable uptoken = dict()
It will store all the tokens with their LTP. That’s it.

Past Schema

So, every time, I am scanning for positions. (read - Open Positions.) I scan if the symbol name is there in the dict.

If it is there, then see if the LTP is 0 or NA. If its is 0 or NA, I search for LTP.

Now, once I seek the LTP function, I will see if it is coming blank. If it comes blank, I keep pinning the WSS and stall the rest of the program.

New Schema

Now, the cool part is if you throw a string of symbols, it will try to fetch for all of them at once. So, instead of filing them one by one.

I scan if the symbol name is there in the dict. If not, I keep adding them.

Now, I will scan the dict and check if there is any of its LTP is 0 or NA. Now, instead of throwing one by one symbol, I will throw the entire dict and seek the latest LTPs filling the previous 0 or NA thing.

This is a minor change. Very trivial but will increase the speed by a huge tune.

Now, another minor change here is polling the LTP system while adding the symbols. This gives a gap between the ask and “weirdly” (because it shouldn’t actually) increase the speed even further because the 0 or NA thing is rarely there in the second iteration (i.e when scanning the dict)

You can also use recursion backwards in the middle of the function. It is like reducing the “forward steps” based on current data.


Quick Note - We are subbing to the newfound symbols as We find them, We are asking for LTP for all of them while asking one of them’s LTP. Very minor issue.

If You’re fetching datastream all the time, this issue will not be a bellwether.