facioquo / stock-indicators-python

Stock Indicators for Python. Maintained by @LeeDongGeon1996
https://python.StockIndicators.dev
Apache License 2.0
208 stars 35 forks source link

Error getting all is None when using ZigZag Indicator #294

Closed dev-iCoderX closed 1 year ago

dev-iCoderX commented 1 year ago

Hi.

I have been using your libraries for a while and they are working fine until today.

Everything is still the same and I have not edited a single piece of code. 2-3 days ago it was working fine but now it returns all None when using: indicators.get_zig_zag.

Looking forward to your support.

Thanks very much.

DaveSkender commented 1 year ago

This can happen in certain combinations of quotes and parameters. Can you provide those so we can review?

Also, have you upgraded the package version lately?

dev-iCoderX commented 1 year ago
from stock_indicators import indicators
from stock_indicators import EndType
from datetime import datetime
import pandas as pd
from stock_indicators import Quote
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager
from config import apiKey, secretKey
#pip install python-binance
#pip install stock-indicators

def _calc_dev(base_price, price):
    return 100 * (price - base_price) / base_price

def zigzag(quotes_list, df, dev_threshold=1):
    results = indicators.get_zig_zag(quotes_list, EndType.HIGH_LOW, dev_threshold)
    rs = list(results)
    lastPoint = 0
    dataOut={
        "Type":[],
        "Time":[],
        "Price":[],
    }
    countLow = 0
    countHigh = 0
    for r in rs:
        print(r.point_type, r.zig_zag)
        if r.point_type is not None:
            if r.point_type == "L":
                index = [i for i, x in enumerate(df['low']) if x == float(r.zig_zag)]
                countLow += 1
                if countHigh == 0:
                    countHigh += 1
            else:
                index = [i for i, x in enumerate(df['high']) if x == float(r.zig_zag)]
                countHigh += 1

            lastPoint = [x for x in index if x >= lastPoint][0]

            dataOut["Type"].append(r.point_type)
            dataOut["Time"].append(df['date'][lastPoint])
            dataOut["Price"].append(float(r.zig_zag))

    if countHigh > countLow:
        #lowest_l = sorted(i, x for i, x in enumerate(df['low'][lastPoint:], key=lambda x: x[1])[0]
        listLow = df['low'][lastPoint:]
        print(listLow.index.tolist())
        print(listLow[1498])
        lowest_l = sorted([(ind_l, listLow[ind_l]) for ind_l in listLow.index.tolist()], key=lambda x: x[1])[0]
        print(lowest_l)
        dev = abs(_calc_dev(lowest_l[1], dataOut["Price"][-1]))
        print(dev)
        if dev >= dev_threshold:
            dataOut["Time"].append(df['date'][lastPoint:][lowest_l[0]])
            dataOut["Type"].append("L")
            print(df['date'][lastPoint:])
            print(lowest_l[0])

            dataOut["Price"].append(float(lowest_l[1]))

    return dataOut

def GetLastZigZag(apiKey, apiSecret, symbol="BTCUSDT", interval=Client.KLINE_INTERVAL_1MINUTE, testnet=False):
    #client = Client("ad56fcd63ac989685da6bf14b2be2e9367ccbdd453c5b981644c5025e61de795", "7bfe2d101299e79cf6dd64be9c65764e89c58f5cee910839b10932d9fa172b5c", testnet=True)
    client = Client(apiKey, apiSecret, testnet=testnet)
    quotes_list=[]
    df = {
        'date':[],
        'open':[],
        'high':[],
        'low':[],
        'close':[],
        'volume':[],
    }
    raw = client.futures_klines(symbol=symbol,interval=interval,limit=1500)
    allHistory = raw[:-1]
    print(raw[-1])
    for history in allHistory:
        date = datetime.fromtimestamp(history[0]/1000)
        open = float(history[1])
        high = float(history[2])
        low = float(history[3])
        close = float(history[4])
        volume = float(history[5])
        quotes_list.append(Quote(date,open,high,low,close,volume))
        df['date'].append(date)
        df['open'].append(open)
        df['high'].append(high)
        df['low'].append(low)
        df['close'].append(close)
        df['volume'].append(volume)
    # Calculate 3% change ZIGZAG
    df = pd.DataFrame(df)
    df.to_csv('data.csv', index=False)
    dataOut = zigzag(quotes_list,df,1)

    df = pd.DataFrame(dataOut)
    print("===========")
    last1=df.iloc[len(df.index)-1]
    last2=df.iloc[len(df.index)-2]
    df.to_csv('result.csv', index=False)
    return last1, last2

last1, last2 = GetLastZigZag("cWVOtwoHC0OBbyGC5iyRJNYd2RXbM2k06zgSosE4iplLT6NkRLHrWFjFJEr6M1BA", "0wwbUJXpnSAJ80U0Pq2rnBaVnTQEGXA1KM4bNAVZJh9uEAMuL3cmepqGn5YhKnl0", symbol="BTCUSDT", testnet=False)

Here is my entire code and stock-indicators version is 1.0.0

Thank you

DaveSkender commented 1 year ago

Do you see anything odd in quotes_list or data.csv to indicate some bad data going into the zigzag analysis? I’m also seeing results being transformed multiple times, does the original output have this problem?

dev-iCoderX commented 1 year ago

image

The data is complete and nothing is missing

DaveSkender commented 1 year ago

It’s hard to say what’s happening if it were working, then suddenly stopped. If you did not change the library version in that time, it’s not likely the library itself that is the problem. It’s static code.

Since you’re using smaller time periods, try reducing the percent_change parameter value. If it’s too large, you’ll never get any initialization or triggered reversals.

DaveSkender commented 1 year ago

@haluu0902 are you still seeking support on this issue? Let us know if you need further assistance.