Regarding afl execution time

Consider this. An afl when run on one 1 single stock takes 1 minute to execute fully and produce one trade in in analysis window trade list. This takes up 1 threads. Suppose the CPU concerned has 10 cores and can run 20 threads in parallel such as the i9 10900k.
So my question is how much time should it take to run this afl on 20 stocks? Should it not take same amount of time i.e. 1 minute. Because the CPU as mentioned earlier can run 20 threads in parallel and since analysis window allocates 1 threads per instrument logically the execution time should be 1 minute. BUT THIS IS NOT THE CASE. It is taking much longer. It takes about 4 minutes. Why?

The answer is: IT DEPENDS. First your CPU has only 10 cores, not 20 cores. So it can't run 20 threads independently. 2 threads share same CPU core. So at start it is not 20x faster as you think. But there are dozens of other things to consider.
The world is not as simple as elementary school math.

Please read this:

3 Likes

I didn't know that 2 threads cannot run parallel on one cpu. It seems little deceitful of the processor companies if the 2 threads must share the cpu. Because I always thought they could run parallel. Anyways Thanks for clearing this up Tomasz.

One problem I face is that when live algo trading on a watchlist of 100 stocks on a base time interval of 1 minute if 99 stocks complete execution under 1 minute but that one stock takes up 10 minutes then the queue is on hold and the the cycle has to wait for that 1 stock to complete. This is very unfair to the 99 stocks that completed on time. My afl is very complicated and does not use moving average etc. it needs sub-leg verification which involves me to iterate lower time frames one by one sometimes as many as 100 timeframes. So that one culprit stock may need more time as it's sub-leg was verified in a timeframe that was way down the line. I wonder how you professionals handle such scenarios. A timer that quits execution for the culprit stock comes to mind as a solution. Like a timeout at 1 minute. That way the cycle completes at 1 minute and the trade list is refreshed at every 1 minute no matter what. Please share your insight.

You are mixing up the terms. 2 threads can run on one CPU. What I wrote was that "2 threads share same CPU CORE". A CORE is a part of CPU. CPU is entire processor, it consists typically of MANY CORES.
In so called "hyper-threading" CORE you have ONE CORE that can execute 2 threads but NOT entirely parallel. It depends on construction of specific CORE because they differ from family to another. Some resources (like registers) might be duplicated, but some resources (like floating point execution units) may be shared. The CORE might process some instructions in parallel and some not. This is complex stuff and definitely not as easy as 2+2=4.

https://www.liquidweb.com/blog/difference-cpu-cores-thread/

As to your formula - it is your code and it is up to you to write it efficiently. Typically proper coding leads to much higher speedups than any hardware upgrade. If you are struggling with proper coding hire someone who is more proficient Third party services, blogs, courses, books, add-ons

3 Likes

Agreed. Nothing compares to well written code. Also minimising the bars in a chart and setting the highest possible base interval helps. Thanks Tomasz. I am going through each and every article/threads you linked. Very helpful. Thanks again.