peerchemist / finta

Common financial technical indicators implemented in Pandas.
GNU Lesser General Public License v3.0
2.13k stars 688 forks source link

Poor performance of some indicators #98

Closed giollord closed 3 years ago

giollord commented 4 years ago

Some functions (especially ones which use Pandas.Series and DataFrame instead on numpy arrays) are very slow. I have OHLCV dataset with 26k records and I'm having such execution times with default parameters: WMA - 25.37s HMA - 75.044s SAR - 14.03s CCI - 64.758s SQZMI - 6.877s MFI - 7.054s IFT_RSI - 25.08s

Could be that there are other slow functions, but I tested the same dataset on just these ones and performance was pretty good: SMA, EMA, DEMA, TEMA, ZLEMA, VWAP, SMMA, BBANDS, KC, DO, TP, CHANDELIER, APZ, TRIX, KST, TSI, ER, MACD, VW_MACD, EV_MACD, MOM, AO, CHAIKIN, RSI, STOCHRSI, STC, CMO, QSTICK, WTO, ATR, VORTEX, COPP are under 0.1s; KAMA and UO just a bit more than 1s.

I did some experiments and at least in a case of WMA and IFT_RSI I've got time about 0.3s for a really small adjustment in code: weights = pd.Series(np.arange(1, period + 1)) replace with weights = np.arange(1, period + 1) For CCI adding raw=True increases speed drastically (about x60-x70): mad = tp_rolling.apply(lambda s: abs(s - s.mean()).mean(), raw=True)

peerchemist commented 4 years ago

I'll dig into this next week.

giollord commented 4 years ago

I really like your project, but I'm too lazy to contribute normally by making pull request, sorry for that... So, just as an idea for optimizing MFI, SQZMI and SAR: MFI:

_mf["neg"] = _mf.apply(neg, axis=1)
_mf["pos"] = _mf.apply(pos, axis=1)

replace with (unfortunately, doesn't give very big boost)

_mf["neg"] = 0
_mf.loc[_mf["delta"] < 0, "neg"] = _mf["rmf"]
_mf["pos"] = 0
_mf.loc[_mf["delta"] > 0, "pos"] = _mf["rmf"]

SQZMI:

comb["SQZ"] = (comb["BB_LOWER"] > comb["KC_LOWER"]) & (comb["BB_UPPER"] < comb["KC_UPPER"])

SAR:

high, low = ohlc.high.to_numpy(), ohlc.low.to_numpy()
peerchemist commented 4 years ago

I've adopted https://github.com/peerchemist/finta/commit/e65409fba4cc81dfef57bd20402ceeaa2d747526, but that is as far as I would go at this point.