0

I am using pandas_ta to calculate supertrend its fine for one stock but when I am trying to calculate it for like 100 stocks using a simple for loop, it takes more than 6 secs, how can I optimize it? or is there any different way to achieve it?

for column in close.columns:
    supertrend[column] = ta.supertrend(high=high[column], low=low[column],close=close[column], length=period, multiplier=multiplier)[props]
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Asif Khan
  • 159
  • 1
  • 1
  • 13
  • Why do you think that's a problem? That's 60 milliseconds per stock, which is pretty good. – Tim Roberts Oct 18 '21 at 06:15
  • @TimRoberts I have seen sites like chartink.com, they calculate for more than 100 stocks in 800 milliseconds – Asif Khan Oct 18 '21 at 06:19
  • Can you show the complete code? Using `ta.` instead of `df.ta.` and multiple dataframes to store the data seems unusual. Without further information, it's hard to say how this affects the runtime and if the loop could be avoided. – Michael Szczesny Oct 18 '21 at 07:21
  • Maybe you can distribute or [parallelize](https://stackoverflow.com/a/45545111/14277722) the loop with [dask](https://docs.dask.org/en/stable/) – Michael Szczesny Oct 18 '21 at 07:31
  • @MichaelSzczesny here is my code ```high = self.give_base_value('high', inetrval, stockindex, period, offset=offset) low = self.give_base_value('low', inetrval, stockindex, period, offset=offset) close = self.give_base_value('close', inetrval, stockindex, period, offset=offset) for column in close.columns: supertrend[column] = ta.supertrend(high=high[column], low=low[column],close=close[column], length=period, multiplier=multiplier)[props]``` here high is dataframe of all the scripts with script names as columns, similarly for close and low. – Asif Khan Oct 18 '21 at 07:59
  • @MichaelSzczesny I have tried dask but it seem multiprocessing is not working in python due to GIL, could you suggest any other method to achieve parallelism?? – Asif Khan Oct 22 '21 at 14:20

0 Answers0