timothyyu / gdax-orderbook-ml

Application of machine learning to the Coinbase (GDAX) orderbook
BSD 3-Clause "New" or "Revised" License
94 stars 28 forks source link

refactor/rewrite autoSR() function #55

Open timothyyu opened 6 years ago

timothyyu commented 6 years ago

rewrite autoSR() function to be more accurate to S/R done by hand

timothyyu commented 6 years ago

in progress/start as of https://github.com/timothyyu/gdax-orderbook-ml/commit/a738775ed1e02938dd6ae017f726cc24b315ab01

timothyyu commented 6 years ago

autoSR_alt_experimental() start: https://github.com/timothyyu/gdax-orderbook-ml/commit/e04d0e82c91cc25de49f63b625eb45da4bc14778

timothyyu commented 6 years ago

e04d0e8 is start of update/rewrite of this function (python 2) to python 3: https://kite.trade/forum/discussion/1047/a-simple-python-function-to-detect-support-resistance-levels

def supres(ltp, n):
    """
    This function takes a numpy array of last traded price
    and returns a list of support and resistance levels 
    respectively. n is the number of entries to be scanned.
    """
    from scipy.signal import savgol_filter as smooth

    #converting n to a nearest even number
    if n%2 != 0:
        n += 1

    n_ltp = ltp.shape[0]

    # smoothening the curve
    ltp_s = smooth(ltp, (n+1), 3) 

    #taking a simple derivative
    ltp_d = np.zeros(n_ltp)
    ltp_d[1:] = np.subtract(ltp_s[1:], ltp_s[:-1])

    resistance = []
    support = []

    for i in xrange(n_ltp - n):
        arr_sl = ltp_d[i:(i+n)]
        first = arr_sl[:(n/2)] #first half
        last = arr_sl[(n/2):] #second half

        r_1 = np.sum(first > 0)
        r_2 = np.sum(last < 0)

        s_1 = np.sum(first < 0)
        s_2 = np.sum(last > 0)

        #local maxima detection
        if (r_1 == (n/2)) and (r_2 == (n/2)): 
            resistance.append(ltp[i+((n/2)-1)])

        #local minima detection
        if (s_1 == (n/2)) and (s_2 == (n/2)): 
            support.append(ltp[i+((n/2)-1)])

    return support, resistance
timothyyu commented 6 years ago

scipy argrelmin/max, scipy argrelextrema, np.greater, scipy find_peaks

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelmin.html https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelmax.html https://scipy.github.io/devdocs/generated/scipy.signal.find_peaks.html https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelextrema.html

https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.greater.html

timothyyu commented 6 years ago

argrelextrema for local minima/maxima as of 861a2ae61cb4fc7cc0c13b032d0560e2b68349e5

timothyyu commented 6 years ago

autoSR() results saved to disk as of fd72791d52893d310b1c4e944e934ca83926a0f3

timothyyu commented 5 years ago

mpl_finance as deprecated import placeholder as of https://github.com/timothyyu/gdax-orderbook-ml/commit/c741744b022999fc9d576b7dc05790a9497f1d41