omerbsezer / CNN-TA

Algorithmic Financial Trading with Deep Convolutional Neural Networks: Time Series to Image Conversion Approach: A novel algorithmic trading model CNN-TA using a 2-D convolutional neural network based on image processing properties.
113 stars 40 forks source link

Target Labelling Phase #6

Closed dalalkrish closed 3 years ago

dalalkrish commented 3 years ago

Hi Omer,

Congratulations on this paper. I thoroughly enjoyed reading it. I'm trying to implement it but I'm struggling with generating BUY, SELL, and HOLD labels. I tried reading through your JAVA code and tried to tweak my code as much as possible but I'm not a JAVA person. Can you please suggest what am I doing wrong? My implementation only produces the HOLD label. I very much appreciate your help.

My implementation:

window_size = 11
counter = 0
result = []

window_begin_idx=0; window_end_idx=0; window_middle_idx=0; min_idx=0; max_idx=0;

while counter < len(closing_price):
    if counter > window_size:

        window_begin_idx = counter - window_size
        window_end_idx = window_begin_idx + window_size - 1
        window_middle_idx = (window_begin_idx + window_end_idx)//2

        for i in range(window_begin_idx, window_end_idx+1):
            rng = closing_price[window_begin_idx:window_end_idx+1]
            number = closing_price[i]
            mins = rng.min()
            maxs = rng.max()
            if number < mins:
                mins=number
                min_idx = np.argmin(rng)
            if number > maxs:
                maxs=number
                max_idx = np.argmax(rng)

        if max_idx == window_middle_idx:
            result.append("SELL")
        elif min_idx == window_middle_idx:
            result.append("BUY")
        else:
            result.append("HOLD")

        mins = 0.0
        maxs = 10000.0
    counter+=1

len(result)
dalalkrish commented 3 years ago

I solved it. For anyone, who wants to do it in Python:

counter = 0
result = []

window_begin_idx=0; window_end_idx=0; window_middle_idx=0; min_idx=0; max_idx=0; 
number=0.0; mins=10000.0; maxs=0.0

while counter < len(closing_price):
    if counter > window_size:
        window_begin_idx = counter - window_size
        window_end_idx = window_begin_idx + window_size - 1
        window_middle_idx = (window_begin_idx + window_end_idx)//2

        for i in range(window_begin_idx, window_end_idx+1):

            number = closing_price[i]

            if number < mins:
                mins=number
                min_idx = np.where(closing_price==mins)[0][0]
            if number > maxs:
                maxs=number
                max_idx = np.where(closing_price==maxs)[0][0]

        if max_idx == window_middle_idx:
            result.append("SELL")
        elif min_idx == window_middle_idx:
            result.append("BUY")
        else:
            result.append("HOLD")

        mins = 10000.0
        maxs = 0.0

    counter+=1