white07S / Fibonacci-Retracement

Machine learning model for finding Retracement,Support and Resistance
BSD 3-Clause "New" or "Revised" License
7 stars 1 forks source link

Sup-Res-Fib preliminary preparations #12

Closed bujowskis closed 1 year ago

bujowskis commented 1 year ago

Notes:

  1. Global maxima and minima
  2. max and min acceleration
    • absolute value - no neg/pos
  3. Calculations
    • Linear Regression (fast BUT decimal error => could cause issues with big money)
    • Hauge Transformation (optimal BUT slow => C?)
    • Naive Math (kind of in between the two above BUT complicated code)

TODO:

white07S commented 1 year ago

To draw a line we need at least two points and to draw a support or resistance is really important to verify if at least it goes through three points so rather can calculating global maxima and minima, I have calculated all the possible minima and maxima and later if we need global then we can find easily from this list of minima and maxima's.

bujowskis commented 1 year ago

moving to in-progress, this one's preliminary to all the Review Calculations code

bujowskis commented 1 year ago

since it's added and in review, will try to run it and get to cleaning up soon

bujowskis commented 1 year ago

We need a test run function to try and run, make sure it works for us

white07S commented 1 year ago

I have added the test function at the bottom of the sup_res_preliminary file.

bujowskis commented 1 year ago

reopened, need to run it

bujowskis commented 1 year ago

I've done some visual reformatting and added ___main__ that runs the test function, but the third assertion doesn't hold

    # test_code()
    assert result == calc_support_resistance(data, extmethod=METHOD_NAIVE)
    assert result == calc_support_resistance(data, extmethod=METHOD_NAIVECONSEC)
    assert result == calc_support_resistance(data)  # THIS

I get the following error:

ValueError: Accuracy order acc must be positive EVEN integer

In the code, it seems @white07S you're mentioning acc-3

        dx = 1  # 1 day interval
        d_dx = FinDiff(0, dx, 1, acc=accuracy)  # acc=3 #for 5-point stencil, currenly uses +/-1 day only
        d2_dx2 = FinDiff(0, dx, 2, acc=accuracy)  # acc=3 #for 5-point stencil, currenly uses +/-1 day only

        def get_minmax(h):
            clarr = np.asarray(h, dtype=np.float64)
            mom, momacc = d_dx(clarr), d2_dx2(clarr)

So it would seem there may be some issue with the code logic? Let us know.

PS - I've added a FIXME - odd accuracy to easily navigate to the code that causes the issue.

Full Traceback, as of the current version (commit add odd accuracy fixme):

Traceback (most recent call last):
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 452, in <module>
    test_code()
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 447, in test_code
    assert result == calc_support_resistance(data)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 416, in calc_support_resistance
    extremaIdxs = get_extrema(h, extmethod, accuracy)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 86, in get_extrema
    minFunc, maxFunc, numdiff_extrema = get_minmax(h)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 63, in get_minmax
    mom, momacc = d_dx(clarr), d2_dx2(clarr)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\operators.py", line 79, in __call__
    return self.apply(rhs, *args, **kwargs)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\operators.py", line 94, in apply
    return self.pds(rhs, *args, **kwargs)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\diff.py", line 216, in __call__
    return self.apply(rhs, *args, **kwargs)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\diff.py", line 275, in apply
    return self.diff(u, h, acc)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\diff.py", line 287, in diff
    coefs = coefficients(self.order, acc)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\coefs.py", line 55, in coefficients
    _validate_acc(acc)
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\venv\lib\site-packages\findiff\coefs.py", line 232, in _validate_acc
    raise ValueError('Accuracy order acc must be positive EVEN integer')
ValueError: Accuracy order acc must be positive EVEN integer

Process finished with exit code 1
white07S commented 1 year ago

Pass accuracy parameter in calc function as accuracy=8

bujowskis commented 1 year ago

running accuracy=8 doesn't produce the given result

I've altered the temporary testing code to print out the info for more insight and it seems METHOD_NUMDIFF only produces the right result for accuracy=2

Output with traceback: (sorry it's long, will alter if needed)

Result:
(([6, 12, 18], [0.0, 0.0], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))

METHOD_NAIVE:
(([6, 12, 18], [0.0, 0.0], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))

METHOD_NAIVECONSEC:
(([6, 12, 18], [0.0, 0.0], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))

METHOD_NUMDIFF (default):
(([1, 3, 6, 12, 18, 21, 23], [-2.0354795560799914e-17, 1.1428571428571435], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([9, 15], [0.0, 3.0], [], [[]]))

METHOD_NUMDIFF accuracy=2:
(([6, 12, 18], [0.0, 0.0], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))
True

METHOD_NUMDIFF accuracy=4:
(([1, 6, 12, 18, 23], [-6.265350993385214e-18, 0.39999999999999997], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))
False

METHOD_NUMDIFF accuracy=6:
(([1, 6, 12, 18, 23], [-6.265350993385214e-18, 0.39999999999999997], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([3, 9, 15, 21], [0.0, 3.0], [([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))], [[([3, 9, 15, 21], (0.0, 3.0, 0.0, 0.0, 0.0, 0.0))]]))
False

METHOD_NUMDIFF accuracy=8:
(([1, 3, 6, 12, 18, 21, 23], [-2.0354795560799914e-17, 1.1428571428571435], [([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))], [[([6, 12, 18], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))]]), ([9, 15], [0.0, 3.0], [], [[]]))
False

Traceback (most recent call last):
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 480, in <module>
    test_code()
  File "C:\Users\ADMIN\PycharmProjects\Fibonacci-Retracement\src\codeReview\sup_res_preliminary.py", line 474, in test_code
    assert result == method_numdiff  # FIXME - default accuracy + documentation
AssertionError

Process finished with exit code 1
white07S commented 1 year ago

So overall the issue is it's hard to find a single result that satisfies all the functionality and it becomes even harder when there are decimals because Python has a hard time comparing floating points (C our beloved). Indeed we can round the numbers but now it's really hard because the final return is a tuple and rounding each numerical is really hard from scratch.

So, rather than finding one common result function, we can leave numerical differentiation with one test case but I have added a plot function with optimized params.

bujowskis commented 1 year ago

I believe this covers it