Skip to content

Commit

Permalink
fix divizion by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
hal9000cc committed Jan 15, 2023
1 parent 8c0bab8 commit ceb0c82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 44 deletions.
78 changes: 35 additions & 43 deletions benchmarks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"empty bars: count 443925 (1.41 %), max consecutive 79200\n",
"Values: time, open, high, low, close, volume\n",
"Load OHLCV: 1.835846876958385 seconds\n",
"Load OHLCV: 1.8683229759335518 seconds\n",
"\n"
]
}
Expand Down Expand Up @@ -58,143 +58,135 @@
"(IndicatorData) source: binance, name: ADL, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, adl\n",
"ADL: 0.6411716479342431 seconds\n",
"ADL: 0.6332008140161633 seconds\n",
"\n",
"(IndicatorData) source: binance, name: ADX, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, adx, p_di, m_di\n",
"ADX: 2.8882603840902448 seconds\n",
"ADX: 2.6621625241823494 seconds\n",
"\n",
"(IndicatorData) source: binance, name: ATR, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, tr, atr, atrp\n",
"ATR: 1.0308619779534638 seconds\n",
"ATR: 1.0195300120394677 seconds\n",
"\n",
"(IndicatorData) source: binance, name: Aroon, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, up, down, oscillator\n",
"Aroon: 2.0204839049838483 seconds\n",
"Aroon: 1.6156933240126818 seconds\n",
"\n",
"(IndicatorData) source: binance, name: awesome, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, awesome\n",
"Awesome: 0.7379472709726542 seconds\n",
"Awesome: 0.7299590001348406 seconds\n",
"\n",
"(IndicatorData) source: binance, name: BollingerBands, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, mid_line, up_line, down_line, z_score\n",
"BollingerBands: 2.6290360738057643 seconds\n",
"BollingerBands: 2.2623401798773557 seconds\n",
"\n",
"(IndicatorData) source: binance, name: CCI, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, cci\n",
"CCI: 3.4095026631839573 seconds\n",
"CCI: 3.167852855985984 seconds\n",
"\n",
"(IndicatorData) source: binance, name: EMA, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, ema\n",
"EMA: 0.14985381299629807 seconds\n",
"EMA: 0.15534969489090145 seconds\n",
"\n",
"(IndicatorData) source: binance, name: Ichimoku, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, tenkan, kijun, senkou_a, senkou_b, chikou\n",
"Ichimoku: 6.559336323989555 seconds\n",
"Ichimoku: 6.103666668059304 seconds\n",
"\n",
"(IndicatorData) source: binance, name: Keltner, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, mid_line, up_line, down_line, width\n",
"Keltner: 1.538262422895059 seconds\n",
"Keltner: 1.5321263989899307 seconds\n",
"\n",
"(IndicatorData) source: binance, name: SMA, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, move_average\n",
"MA: 0.5325861210003495 seconds\n",
"MA: 0.535043149953708 seconds\n",
"\n",
"(IndicatorData) source: binance, name: MACD, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, macd, signal, hist\n",
"MACD: 0.6857965900562704 seconds\n",
"MACD: 0.5411174290347844 seconds\n",
"\n",
"(IndicatorData) source: binance, name: OBV, symbol: ethusdt, timeframe: 1s, not allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, OBV\n",
"OBV: 0.5684860511682928 seconds\n",
"OBV: 0.4656117339618504 seconds\n",
"\n",
"(OHLCV data) source: binance, symbol: ethusdt, timeframe: 1s\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"empty bars: count 443925 (1.41 %), max consecutive 79200\n",
"Values: time, open, high, low, close, volume\n",
"OHLCV: 0.000193373067304492 seconds\n",
"OHLCV: 0.00020547187887132168 seconds\n",
"\n",
"(IndicatorData) source: binance, name: ParabolicSAR, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, sar, signal\n",
"ParabolicSAR: 0.5469749132171273 seconds\n",
"ParabolicSAR: 0.2457318021915853 seconds\n",
"\n",
"(IndicatorData) source: binance, name: ROC, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, roc, smooth_roc\n",
"ROC: 1.066619403893128 seconds\n",
"ROC: 0.7587062558159232 seconds\n",
"\n",
"(IndicatorData) source: binance, name: RSI, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, rsi\n",
"RSI: 1.5385335159953684 seconds\n",
"RSI: 0.8928227289579809 seconds\n",
"\n",
"(IndicatorData) source: binance, name: SMA, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, sma\n",
"SMA: 0.5395441809669137 seconds\n",
"SMA: 0.5386050960514694 seconds\n",
"\n",
"(IndicatorData) source: binance, name: Stochastic, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, value_d, value_k, oscillator\n",
"Stochastic: 1.5798970749601722 seconds\n",
"Stochastic: 1.4135339320637286 seconds\n",
"\n",
"(IndicatorData) source: binance, name: Supertrend, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, supertrend, sumpertrend_mid\n",
"Supertrend: 0.6728542840573937 seconds\n",
"Supertrend: 0.4586079840082675 seconds\n",
"\n",
"(IndicatorData) source: binance, name: TEMA, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, tema\n",
"TEMA: 0.9964027211535722 seconds\n",
"TEMA: 0.7878364420030266 seconds\n",
"\n",
"(IndicatorData) source: binance, name: TRIX, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, trix\n",
"TRIX: 0.7344676740467548 seconds\n",
"TRIX: 0.5999367560725659 seconds\n",
"\n",
"(IndicatorData) source: binance, name: VWAP, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, vwap\n",
"VWAP: 1.149828225839883 seconds\n",
"VWAP: 0.4509703479707241 seconds\n",
"\n",
"(IndicatorData) source: binance, name: VWMA, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, vwma\n",
"VWMA: 0.4383336380124092 seconds\n",
"VWMA: 0.20009861490689218 seconds\n",
"\n",
"(IndicatorData) source: binance, name: WilliamsR, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, williams_r\n",
"WilliamsR: 1.8968893920537084 seconds\n",
"\n",
"(IndicatorData) source: binance, name: ZigZag, symbol: ethusdt, timeframe: 1s, allowed nan\n",
"date: 2021-01-01T00:00 - 2021-12-31T23:59 (length: 31536000) \n",
"Values: time, pivots, pivot_types\n",
"ZigZag: 0.174813395133242 seconds\n",
"\n"
]
},
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_657073/4245252041.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mtest_globals\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'indicator_name'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mindicator_name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m time = timeit.timeit(\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;34m'indicator = indicators.get_indicator(indicator_name)\\n'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;34m'params = indicator_params.get(indicator_name, {})\\n'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.10/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(stmt, setup, timer, number, globals)\u001b[0m\n\u001b[1;32m 232\u001b[0m number=default_number, globals=None):\n\u001b[1;32m 233\u001b[0m \u001b[0;34m\"\"\"Convenience function to create Timer object and call timeit method.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 234\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mTimer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstmt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msetup\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 235\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 236\u001b[0m def repeat(stmt=\"pass\", setup=\"pass\", timer=default_timer,\n",
"\u001b[0;32m/usr/lib/python3.10/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 176\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 177\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 178\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 179\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 180\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<timeit-src>\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer)\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.10/site-packages/live_trading_indicators/indicators_set/__init__.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, symbols, timeframe, time_begin, time_end, **kwargs)\u001b[0m\n\u001b[1;32m 442\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mLTIExceptionTimeBeginLaterTimeEnd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0muse_time_begin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muse_time_end\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 443\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 444\u001b[0;31m return self.indicators.get_indicator_out(self.indicator_name, self.indicator_module, symbols, use_timeframe,\n\u001b[0m\u001b[1;32m 445\u001b[0m kwargs, use_time_begin, use_time_end)\n\u001b[1;32m 446\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.10/site-packages/live_trading_indicators/indicators_set/__init__.py\u001b[0m in \u001b[0;36mget_indicator_out\u001b[0;34m(self, indicator_name, indicator_module, symbols, timeframe, indicator_kwargs, time_begin, time_end)\u001b[0m\n\u001b[1;32m 296\u001b[0m **indicator_kwargs)\n\u001b[1;32m 297\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 298\u001b[0;31m out = self.get_indicator_out_cached(indicator_name, indicator_module, symbols, timeframe, indicator_kwargs,\n\u001b[0m\u001b[1;32m 299\u001b[0m use_time_begin, use_time_end)\n\u001b[1;32m 300\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.10/site-packages/live_trading_indicators/indicators_set/__init__.py\u001b[0m in \u001b[0;36mget_indicator_out_cached\u001b[0;34m(self, indicator_name, indicator_module, symbols, timeframe, indicator_kwargs, time_begin, time_end)\u001b[0m\n\u001b[1;32m 276\u001b[0m indicator_kwargs, time_begin, time_end)\n\u001b[1;32m 277\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mout_valid\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 278\u001b[0;31m \u001b[0mout_valid\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mindicator_module\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indicator_out\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msymbols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeframe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout_for_grow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mindicator_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 279\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mput_out_to_cache\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindicator_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msymbols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeframe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindicator_kwargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout_valid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 280\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.10/site-packages/live_trading_indicators/indicators_set/WilliamsR.py\u001b[0m in \u001b[0;36mget_indicator_out\u001b[0;34m(indicators, symbol, timeframe, out_for_grow, period)\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mLTIExceptionTooLittleData\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'data length {data_len} < {period}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 33\u001b[0;31m \u001b[0mwilliams_r\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcalc_williams\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mohlcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhigh\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mohlcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mohlcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperiod\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m return IndicatorData({\n",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
Expand Down
2 changes: 1 addition & 1 deletion src/live_trading_indicators/indicators_set/WilliamsR.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def calc_williams(high, low, close, period):
for t in range(period - 1, n_bars):
high_max = high[t - period + 1: t + 1].max()
low_min = low[t - period + 1: t + 1].min()
williams_r[t] = (close[t] - high_max) / (high_max - low_min) * 100
williams_r[t] = 0 if high_max == low_min else (close[t] - high_max) / (high_max - low_min) * 100

return williams_r

Expand Down

0 comments on commit ceb0c82

Please sign in to comment.