facioquo/stock-indicators-python

`decimal.InvalidOperation` with small numbers

youngpake opened this issue · 12 comments

Trying to use small numbers like 1.09701 doesnt work, I tried scaling and everything but I always get a decimal conversion error no matter what I try

Error message(s):

Traceback (most recent call last):
File "c:/Spagetti Code Collection/Ultimate-Market-Oracle/process_trading_zones.py", line 45, in detect_patterns
coordinates_list, new_dataframe = strategy.detect_pattern(ohlc_data=self.ohlc_data)
File "c:\Spagetti Code Collection\Ultimate-Market-Oracle\patterns\zigzag_2_0.py", line 56, in detect_pattern
zigzag_data = [{'date': x.date, 'zig_zag': x.zig_zag, 'point_type': x.point_type} for x in result]
File "c:\Spagetti Code Collection\Ultimate-Market-Oracle\patterns\zigzag_2_0.py", line 56, in
zigzag_data = [{'date': x.date, 'zig_zag': x.zig_zag, 'point_type': x.point_type} for x in result]
File "C:\Spagetti Code Collection\Ultimate-Market-Oracle\venv\lib\site-packages\stock_indicators\indicators\zig_zag.py", line 51, in zig_zag
return to_pydecimal(self._csdata.ZigZag)
File "C:\Spagetti Code Collection\Ultimate-Market-Oracle\venv\lib\site-packages\stock_indicators_cstypes\decimal.py", line 36, in to_pydecimal
return PyDecimal(str(cs_decimal))
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

Possibly related to #382 (comment)

@youngpake
Sorry for inconvenient. It looks like locale setting does not match with your Python environment.
To investigate it, could you give me the results after running the below?

On Python:

from decimal import Decimal
value = Decimal(12.345)
print(value) # case 1
print('{:n}'.format(value)) # case 2
print(format(value, 'n')) # case 3

import locale
print(locale.getlocale(locale.LC_ALL))
print(locale.localeconv())

On your terminal:

locale

12.3450000000000006394884621840901672840118408203125
12.3450000000000006394884621840901672840118408203125
12.3450000000000006394884621840901672840118408203125
Traceback (most recent call last):
File "test.py", line 8, in
print(locale.getlocale(locale.LC_ALL))
File "C:\Users\wytse\AppData\Local\Programs\Python\Python38\lib\locale.py", line 590, in getlocale
raise TypeError('category LC_ALL is not supported')
TypeError: category LC_ALL is not supported

im using python 3.8, could that be the problem?

@youngpake
I don't think it's a Python version problem.
oh.. you are using Windows system. I'm sorry but could you print the zigzag results using the code below?
I'm not using Windows system now, so It's hard to figure out.

for r in results:
      print(str(r.date) + ", " + str(r._csdata.ZigZag))

@youngpake
I've just made a quick patch, could you try with this one?
You can try with following commands:

pip uninstall stock-indicators
pip install git+https://github.com/LeeDongGeon1996/Stock.Indicators.Python.git@fix/apply-locale

its running without errors but only setting a single zigzag point on 2500 rows.

def calculate(self, data: pd.DataFrame) -> pd.DataFrame:
    quotes = [Quote(date=row['date'], open=row['open'], high=row['high'], low=row['low'], close=row['close'], volume=row['volume'])
              for index, row in data.iterrows()]
    
    zigzag_results = indicators.get_zig_zag(quotes, EndType.CLOSE, self.depth)
    
    for result in zigzag_results:
        data.loc[data['date'] == result.date, 'zigzag'] = result.zig_zag
        data.loc[data['date'] == result.date, 'point_type'] = result.point_type

Detecting pattern for ZigZagStrategy
symbol timeframe open high low close volume spread real_volume date zigzag point_type
0 EURUSD H1 1.10437 1.10451 1.10411 1.10439 375.0 11.0 0.0 2024-01-02 00:00:00 NaN None
1 EURUSD H1 1.10441 1.10447 1.10354 1.10364 2207.0 3.0 0.0 2024-01-02 01:00:00 NaN None
2 EURUSD H1 1.10364 1.10384 1.10340 1.10381 2376.0 1.0 0.0 2024-01-02 02:00:00 NaN None
Detected 1 zigzag points
Applied zigzag strategy
symbol timeframe open high low close volume spread real_volume date zigzag point_type
2506 EURUSD H1 1.08506 1.08535 1.08451 1.08494 1160.0 0.0 0.0 2024-05-27 11:00:00 NaN None
2507 EURUSD H1 1.08494 1.08672 1.08489 1.08672 1192.0 0.0 0.0 2024-05-27 12:00:00 NaN None
2508 EURUSD H1 1.08672 1.08674 1.08538 1.08629 972.0 0.0 0.0 2024-05-27 13:00:00 NaN None
2509 EURUSD H1 1.08626 1.08642 1.08565 1.08573 960.0 0.0 0.0 2024-05-27 14:00:00 NaN None
2510 EURUSD H1 1.08573 1.08595 1.08409 1.08490 1834.0 0.0 0.0 2024-05-27 15:00:00 NaN None
2511 EURUSD H1 1.08492 1.08589 1.08445 1.08551 1679.0 0.0 0.0 2024-05-27 16:00:00 NaN None
2512 EURUSD H1 1.08552 1.08596 1.08489 1.08584 1509.0 0.0 0.0 2024-05-27 17:00:00 NaN None
2513 EURUSD H1 1.08585 1.08599 1.08564 1.08596 833.0 0.0 0.0 2024-05-27 18:00:00 NaN None
2514 EURUSD H1 1.08596 1.08624 1.08595 1.08599 463.0 0.0 0.0 2024-05-27 19:00:00 NaN None
2515 EURUSD H1 1.08599 1.08609 1.08571 1.08574 251.0 0.0 0.0 2024-05-27 20:00:00 NaN None
2516 EURUSD H1 1.08574 1.08587 1.08572 1.08574 222.0 0.0 0.0 2024-05-27 21:00:00 NaN None
2517 EURUSD H1 1.08575 1.08601 1.08571 1.08598 191.0 0.0 0.0 2024-05-27 22:00:00 NaN None
2518 EURUSD H1 1.08599 1.08604 1.08559 1.08581 298.0 0.0 0.0 2024-05-27 23:00:00 NaN None
2519 EURUSD H1 1.08564 1.08594 1.08506 1.08566 3017.0 10.0 0.0 2024-05-28 00:00:00 NaN None
2520 EURUSD H1 1.08564 1.08602 1.08564 1.08594 312.0 7.0 0.0 2024-05-28 01:00:00 NaN None
2521 EURUSD H1 1.08594 1.08619 1.08589 1.08616 374.0 0.0 0.0 2024-05-28 02:00:00 NaN None
2522 EURUSD H1 1.08617 1.08684 1.08608 1.08652 1080.0 0.0 0.0 2024-05-28 03:00:00 NaN None
2523 EURUSD H1 1.08652 1.08736 1.08644 1.08724 1155.0 0.0 0.0 2024-05-28 04:00:00 NaN None
2524 EURUSD H1 1.08723 1.08768 1.08716 1.08758 922.0 0.0 0.0 2024-05-28 05:00:00 NaN None
2525 EURUSD H1 1.08759 1.08786 1.08737 1.08783 728.0 0.0 0.0 2024-05-28 06:00:00 NaN None
2526 EURUSD H1 1.08784 1.08801 1.08757 1.08760 842.0 0.0 0.0 2024-05-28 07:00:00 NaN None
2527 EURUSD H1 1.08761 1.08779 1.08727 1.08751 751.0 0.0 0.0 2024-05-28 08:00:00 NaN None
2528 EURUSD H1 1.08752 1.08799 1.08737 1.08754 1423.0 0.0 0.0 2024-05-28 09:00:00 NaN None
2529 EURUSD H1 1.08753 1.08790 1.08716 1.08774 1502.0 0.0 0.0 2024-05-28 10:00:00 NaN None
2530 EURUSD H1 1.08774 1.08797 1.08671 1.08700 1545.0 0.0 0.0 2024-05-28 11:00:00 NaN None
2531 EURUSD H1 1.08700 1.08776 1.08672 1.08769 1249.0 0.0 0.0 2024-05-28 12:00:00 NaN None
2532 EURUSD H1 1.08769 1.08799 1.08754 1.08782 1064.0 0.0 0.0 2024-05-28 13:00:00 NaN None
2533 EURUSD H1 1.08782 1.08890 1.08782 1.08841 1476.0 0.0 0.0 2024-05-28 14:00:00 NaN None
2534 EURUSD H1 1.08838 1.08847 1.08746 1.08765 2146.0 0.0 0.0 2024-05-28 15:00:00 NaN None
2535 EURUSD H1 1.08764 1.08802 1.08640 1.08648 2702.0 0.0 0.0 2024-05-28 16:00:00 1.086480000000000000000000030 None

@youngpake is that result from patched version?

but only setting a single zigzag point on 2500 rows

Try using a smaller percent_change value when using smaller candle periods; it defaults to 5%, which you'd not see often in hourly Forex.

Patched version released on test PyPI. Please have a test and any feedback appreciated.

pip install -i https://test.pypi.org/simple/ stock-indicators==1.3.1.dev9

The patch finally has been released in v1.3.1.