hal9000cc / live_trading_indicators

MIT License
26 stars 7 forks source link

Zig-zag missing pivots #3

Open lapphan opened 1 year ago

lapphan commented 1 year ago

Compare to Zig Zag indicator on TradingView, live_trading_indicators's ZigZag is missing 4 pivots:

What's showing on TradingView:

image

What I've done:

#lti.py
import live_trading_indicators as lti

indicators = lti.Indicators('ccxt.binanceusdm', '2019-09-08', '2023-05-08')
ohlcv = indicators.OHLCV('BTCUSDT', '1d')
print(ohlcv)
dataframe = ohlcv.pandas()
print(dataframe.head())
zigzag = indicators.ZigZag('BTCUSDT', '1d', delta=0.5, end_points=True)
print(zigzag)
dataframe = zigzag.pandas()
dataframe = dataframe[dataframe['pivot_types'].values != 0]
print(dataframe)
#console ouput
~/live_trading_indicators$ python3 lti.py
2023-05-09 20:12:08:root:725:102 INFO Download using xxct.binanceusdm symbol BTCUSDT timeframe 1d from 2019-09-08T00:00:00.000, bars: 1339
(OHLCV data) source: ccxt.binanceusdm, symbol: BTCUSDT, timeframe: 1d
date: 2019-09-08T00:00 - 2023-05-08T00:00 (length: 1339)
empty bars: count 0 (0.00 %), max consecutive 0
Values: time, open, high, low, close, volume
        time      open      high       low     close     volume
0 2019-09-08  10000.00  10412.65  10000.00  10391.63   3096.291
1 2019-09-09  10316.62  10475.54  10077.22  10307.00  14824.373
2 2019-09-10  10307.00  10382.97   9940.87  10102.02   9068.955
3 2019-09-11  10094.27  10293.11   9884.31  10159.55  10897.922
4 2019-09-12  10163.06  10450.13  10042.12  10415.13  15609.634
(IndicatorData) source: ccxt.binanceusdm, name: ZigZag, symbol: BTCUSDT, timeframe: 1d, allowed nan
date: 2019-09-08T00:00 - 2023-05-08T00:00 (length: 1339)
Values: time, pivots, pivot_types
           time    pivots  pivot_types
1    2019-09-09       NaN            1
187  2020-03-13   3621.81           -1
584  2021-04-14  64986.11            1
619  2021-05-19  28688.00           -1
794  2021-11-10  69198.70            1
1170 2022-11-21  15443.20           -1

Please help me to get all missing pivots.

Thank you.

hal9000cc commented 1 year ago

I fix some bugs in ZigZag, thanks. However, I'm not sure that's all. You can take the fixes from the master branch.

hal9000cc commented 1 year ago

And I also don't understand where the price value of 15443.20 comes from. I don't find that.

lapphan commented 1 year ago

And I also don't understand where the price value of 15443.20 comes from. I don't find that.

The pivot 15443.20 is a correct pivot that already showed in v0.7.5 (please check #console ouput in my initial comment). You can see it at highlighted text below:

image

I also tested v0.7.5.1 and now all missing pivots are showed:

(IndicatorData) source: ccxt.binanceusdm, name: ZigZag, symbol: BTCUSDT, timeframe: 1d, allowed nan
date: 2019-09-08T00:00 - 2023-05-08T00:00 (length: 1339)
Values: time, pivots, pivot_types
           time    pivots  pivot_types
101  2019-12-18   6427.00           -1
158  2020-02-13  10540.00            1
187  2020-03-13   3621.81           -1
584  2021-04-14  64986.11            1
619  2021-05-19  28688.00           -1
794  2021-11-10  69198.70            1
1170 2022-11-21  15443.20           -1
1314 2023-04-14  31059.00            1
1323 2023-04-23  27279.70           -1

But the incomplete pivot is not correct in my opinion. Perhaps we can take the logic in TradingView's Zig Zag indicator:

My pseudocode for more clarity:

last_bar_candle = get_last_bar_candle()

if last_pivot.type = 1 # High
    incomplete_pivot.time = last_bar_candle.time
    incomplete_pivot.type = -1 # Low
    incomplete_pivot.price = last_bar_candle.low
else if last_pivot.type = -1 # Low
    incomplete_pivot.time = last_bar_candle.time
    incomplete_pivot.type = 1 # High
    incomplete_pivot.price = last_bar_candle.high

I hope you can understand my idea. Feel free to give any feedback to me. Thank you.