{"action":"opened","issue":{"url":"https://api.github.com/repos/yoniberger/tsne/issues/5","repository_url":"https://api.github.com/repos/yoniberger/tsne","labels_url":"https://api.github.com/repos/yoniberger/tsne/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/yoniberger/tsne/issues/5/comments","events_url":"https://api.github.com/repos/yoniberger/tsne/issues/5/events","html_url":"https://github.com/yoniberger/tsne/issues/5","id":176028407,"number":5,"title":"5","user":{"login":"yoniberger","id":10967624,"avatar_url":"https://avatars.githubusercontent.com/u/10967624?v=3","gravatar_id":"","url":"https://api.github.com/users/yoniberger","html_url":"https://github.com/yoniberger","followers_url":"https://api.github.com/users/yoniberger/followers","following_url":"https://api.github.com/users/yoniberger/following{/other_user}","gists_url":"https://api.github.com/users/yoniberger/gists{/gist_id}","starred_url":"https://api.github.com/users/yoniberger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yoniberger/subscriptions","organizations_url":"https://api.github.com/users/yoniberger/orgs","repos_url":"https://api.github.com/users/yoniberger/repos","events_url":"https://api.github.com/users/yoniberger/events{/privacy}","received_events_url":"https://api.github.com/users/yoniberger/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-09T14:50:04Z","updated_at":"2016-09-09T14:50:04Z","closed_at":null,"body":"import pandas as pd\r\nimport numpy as np\r\nimport pylab as plt\r\nimport matplotlib.finance as mpf\r\n\r\n\r\n\r\nA = pd.read_excel('exchange_rates.xlsx', index_col=0)\r\n\r\n\r\n\r\ndef get_data():\r\n    A = pd.read_excel('currencies.xlsx', skiprows=range(11))\r\n    B = A.dropna(axis=1, how='all')\r\n    data = []\r\n    for I in range(int(B.shape[1]/5)):\r\n        C = B.ix[:, 5*I:5*I+5]\r\n        C.columns = ['Date','Open','Close','High','Low']\r\n        C = C.set_index('Date')\r\n        data.append(C)\r\n    return data\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef moving_average(A, length=90):\r\n    X = A['Close'].values\r\n    mov_av = np.array([X[0] if I == 0 else X[int(max(I - int(length),0)) : I].mean() for I in range(X.shape[0])])\r\n    # mov_av = np.reshape(mov_av, (X.shape[0], 1))\r\n    difference = X - mov_av\r\n\r\n    plt.close()\r\n    plt.plot(A.index, X)\r\n    plt.plot(A.index, mov_av)\r\n    plt.plot(A.index, difference)\r\n    plt.plot(A.index, np.zeros(X.shape[0]))\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef plot_it(A,\r\n            dict_buy,\r\n            dict_sell,\r\n            bad_dict_buy,\r\n            bad_dict_sell,\r\n            recharges,\r\n            failed_buy_setup1,\r\n            failed_buy_setup2,\r\n            failed_sell_setup1,\r\n            failed_sell_setup2,\r\n            pbsu,\r\n            pssu,\r\n            what_to_plot,\r\n            setup_length):\r\n\r\n    plt.subplots(figsize=(20,10))\r\n    C = A[what_to_plot]\r\n    C.plot(color = 'orange')\r\n    dat_range = (C.max() - C.min())/10\r\n    ymin = C.min() - 5*dat_range\r\n    ymax = C.max()+5*dat_range\r\n    plt.ylim(ymin,ymax)\r\n    point_size = 20\r\n    dot = u'$\\u00B7$'\r\n\r\n\r\n    '''Plot setup and countdown phases'''\r\n    for K in dict_buy:\r\n        if K in bad_dict_buy:       #Plot bad setups with smaller font\r\n            fontsize = 10\r\n        else:\r\n            fontsize = 15\r\n\r\n        # for I in range(setup_length):     #Uncomment this line if want to print the full setup\r\n\r\n        for I in [0,setup_length-1]:\r\n            x = A.index[K+I]\r\n            y = C[K] - dat_range - I*dat_range/10\r\n            plt.text(x,y,'-'+str(I+1),color = 'blue',fontsize = fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n        '''If current setup is later recharged then print recharge point\r\n        A recharge that later failed is denoted by RF, a normal recharge is denoted R'''\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n            if recharge_point in (failed_buy_setup1 + failed_buy_setup2):\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point]+ dat_range/2\r\n            plt.text(x, y, 'R' + f, color='cyan', fontsize=15, va ='center' )\r\n\r\n\r\n\r\n        # for J in range(len(dict_buy[K])):     #Uncomment this line if want to print the full countdown\r\n        if K not in recharges:\r\n            for J in [0,len(dict_buy[K])-1]:\r\n                ind = dict_buy[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] - 2*dat_range - dat_range*J/40\r\n                plt.text(x,y,'-'+str(J+1),color = 'red',fontsize = fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x, y2, dot, color='black', fontsize=point_size,ha = 'center', va ='center')\r\n        elif (K in recharges and recharges[K] in (failed_buy_setup1 + failed_buy_setup2)):\r\n            for J in [0,len(dict_buy[K])-1]:\r\n                ind = dict_buy[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] - 2*dat_range - dat_range*J/40\r\n                plt.text(x,y,'(-'+str(J+1)+')',color = 'red',fontsize = fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x, y2, dot, color='black', fontsize=point_size,ha = 'center', va ='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''DO THE SAME FOR SELL SETUPS'''\r\n    for K in dict_sell:\r\n        if K in bad_dict_sell:\r\n            fontsize = 10\r\n        else:\r\n            fontsize = 15\r\n        # for I in range(setup_length):\r\n\r\n\r\n        for I in [0, setup_length - 1]:\r\n\r\n            x = A.index[K + I]\r\n            y = C[K] + dat_range + dat_range*I/10\r\n            plt.text(x, y, '+'+str(I + 1), color='green', fontsize=fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n\r\n            if recharge_point in (failed_sell_setup1 + failed_sell_setup2):\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point]- dat_range/2\r\n            plt.text(x, y, 'R'+f, color='cyan', fontsize=15, va ='center')\r\n\r\n\r\n        if K not in recharges:\r\n            for J in [0,len(dict_sell[K])-1]:\r\n            # for J in range(len(dict_sell[K])):\r\n                ind = dict_sell[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] + 2*dat_range + dat_range*J/40\r\n                plt.text(x, y, '+'+str(J + 1), color='purple', fontsize=fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n        elif  (K in recharges and recharges[K] in (failed_sell_setup1 + failed_sell_setup2)):\r\n\r\n            for J in [0,len(dict_sell[K])-1]:\r\n            # for J in range(len(dict_sell[K])):\r\n                ind = dict_sell[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] + 2*dat_range + dat_range*J/40\r\n                plt.text(x, y, '(+'+str(J + 1)+')', color='purple', fontsize=fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''Plot if Time Series ends in the middle of a setup'''\r\n\r\n    if pbsu:\r\n        ind = pbsu[0]\r\n        for I in [0,(A.shape[0] - 1 - ind)]:\r\n            x = A.index[ind+I]\r\n            y = C[ind + I] - dat_range\r\n            plt.text(x,y,'-'+str(I+1),color = 'blue',fontsize = fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n    elif pssu:\r\n        ind = pssu[0]\r\n        for I in [0, A.shape[0] - 1 - ind]:\r\n            x = A.index[ind + I]\r\n            y = C[ind+I] + dat_range\r\n            plt.text(x, y, '+' + str(I + 1), color='blue', fontsize=fontsize, ha='center', va='center')\r\n            y2 = C[K + I]\r\n            plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''PLOT SETUPS THAT LATER FAILED\r\n    We plot fails in different colours based on how they failed\r\n    Magenta means setup failed because the time series did not move enough in required direction\r\n    Black means setup failed because an opposite setup occurred before the countdown was completed\r\n    '''\r\n\r\n    down_arrow = u'$\\u2193$'\r\n    up_arrow = u'$\\u2191$'\r\n\r\n    y = ymax - dat_range/1.8\r\n\r\n    for K in failed_sell_setup1:             #Failed because it was a weak setup\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed sell', color='magenta', fontsize= 15, ha = 'center',)\r\n        plt.text(x, y - dat_range, down_arrow, color='magenta',  fontsize=20,ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    for K in failed_sell_setup2:         #Failed because it was pushed out by another setup\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed sell', color='black', fontsize=15, ha = 'center')\r\n        plt.text(x, y - dat_range, down_arrow, fontsize=20, color = 'black',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    y = ymin + dat_range/10\r\n\r\n    for K in failed_buy_setup1:\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed buy', color='magenta', fontsize=15, ha = 'center')\r\n        plt.text(x, y + dat_range, up_arrow, fontsize=20, color = 'magenta',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    for K in failed_buy_setup2:\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed buy ', color='black', fontsize=15, ha = 'center')\r\n        plt.text(x, y+dat_range, up_arrow, fontsize=20, color = 'black',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n\r\n    '''Sometimes a failed setup is recharged before it fails. Plot these'''\r\n    for K in failed_buy_setup1  + failed_buy_setup2 + failed_sell_setup1 + failed_buy_setup2:\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n\r\n            if recharge_point in failed_buy_setup1  + failed_buy_setup2 + failed_sell_setup1 + failed_buy_setup2:\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point] + dat_range/2\r\n            plt.text(x, y, 'R' + f, color='cyan', fontsize=fontsize, va='center')\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef simple_plot(A,\r\n            dict_buy,\r\n            dict_sell,\r\n            recharges,\r\n            setup_length,\r\n            countdown_length,\r\n            what_to_plot):\r\n\r\n    plt.subplots(figsize=(20,10))\r\n    C = A[what_to_plot]\r\n    C.plot(color = 'orange')\r\n    dat_range = (C.max() - C.min())/10\r\n    ymin = C.min() - 5*dat_range\r\n    ymax = C.max()+5*dat_range\r\n    plt.ylim(ymin,ymax)\r\n    point_size = 20\r\n    dot = u'$\\u00B7$'\r\n\r\n\r\n    '''Plot setup and countdown phases'''\r\n    for K in dict_buy:\r\n        if K in bad_dict_buy:       #Plot bad setups with smaller font\r\n            fontsize = 10\r\n        else:\r\n            fontsize = 15\r\n\r\n        # for I in range(setup_length):     #Uncomment this line if want to print the full setup\r\n\r\n        for I in [0,setup_length-1]:\r\n            x = A.index[K+I]\r\n            y = C[K] - dat_range - I*dat_range/10\r\n            plt.text(x,y,'-'+str(I+1),color = 'blue',fontsize = fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n        '''If current setup is later recharged then print recharge point\r\n        A recharge that later failed is denoted by RF, a normal recharge is denoted R'''\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n            if recharge_point in (failed_buy_setup1 + failed_buy_setup2):\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point]+ dat_range/2\r\n            plt.text(x, y, 'R' + f, color='cyan', fontsize=15, va ='center' )\r\n\r\n\r\n\r\n        # for J in range(len(dict_buy[K])):     #Uncomment this line if want to print the full countdown\r\n        if K not in recharges:\r\n            for J in [0,len(dict_buy[K])-1]:\r\n                ind = dict_buy[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] - 2*dat_range - dat_range*J/40\r\n                plt.text(x,y,'-'+str(J+1),color = 'red',fontsize = fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x, y2, dot, color='black', fontsize=point_size,ha = 'center', va ='center')\r\n        elif (K in recharges and recharges[K] in (failed_buy_setup1 + failed_buy_setup2)):\r\n            for J in [0,len(dict_buy[K])-1]:\r\n                ind = dict_buy[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] - 2*dat_range - dat_range*J/40\r\n                plt.text(x,y,'(-'+str(J+1)+')',color = 'red',fontsize = fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x, y2, dot, color='black', fontsize=point_size,ha = 'center', va ='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''DO THE SAME FOR SELL SETUPS'''\r\n    for K in dict_sell:\r\n        if K in bad_dict_sell:\r\n            fontsize = 10\r\n        else:\r\n            fontsize = 15\r\n        # for I in range(setup_length):\r\n\r\n\r\n        for I in [0, setup_length - 1]:\r\n\r\n            x = A.index[K + I]\r\n            y = C[K] + dat_range + dat_range*I/10\r\n            plt.text(x, y, '+'+str(I + 1), color='green', fontsize=fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n\r\n            if recharge_point in (failed_sell_setup1 + failed_sell_setup2):\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point]- dat_range/2\r\n            plt.text(x, y, 'R'+f, color='cyan', fontsize=15, va ='center')\r\n\r\n\r\n        if K not in recharges:\r\n            for J in [0,len(dict_sell[K])-1]:\r\n            # for J in range(len(dict_sell[K])):\r\n                ind = dict_sell[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] + 2*dat_range + dat_range*J/40\r\n                plt.text(x, y, '+'+str(J + 1), color='purple', fontsize=fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n        elif  (K in recharges and recharges[K] in (failed_sell_setup1 + failed_sell_setup2)):\r\n\r\n            for J in [0,len(dict_sell[K])-1]:\r\n            # for J in range(len(dict_sell[K])):\r\n                ind = dict_sell[K][J]\r\n                x = A.index[ind]\r\n                y = C[ind] + 2*dat_range + dat_range*J/40\r\n                plt.text(x, y, '(+'+str(J + 1)+')', color='purple', fontsize=fontsize,ha = 'center', va ='center')\r\n                y2 = C[ind]\r\n                plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''Plot if Time Series ends in the middle of a setup'''\r\n\r\n    if pbsu:\r\n        ind = pbsu[0]\r\n        for I in [0,(A.shape[0] - 1 - ind)]:\r\n            x = A.index[ind+I]\r\n            y = C[ind + I] - dat_range\r\n            plt.text(x,y,'-'+str(I+1),color = 'blue',fontsize = fontsize,ha = 'center', va ='center')\r\n            y2 = C[K+I]\r\n            plt.text(x,y2,dot,color = 'black', fontsize = point_size,ha = 'center', va ='center')\r\n    elif pssu:\r\n        ind = pssu[0]\r\n        for I in [0, A.shape[0] - 1 - ind]:\r\n            x = A.index[ind + I]\r\n            y = C[ind+I] + dat_range\r\n            plt.text(x, y, '+' + str(I + 1), color='blue', fontsize=fontsize, ha='center', va='center')\r\n            y2 = C[K + I]\r\n            plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n\r\n\r\n\r\n\r\n    '''PLOT SETUPS THAT LATER FAILED\r\n    We plot fails in different colours based on how they failed\r\n    Magenta means setup failed because the time series did not move enough in required direction\r\n    Black means setup failed because an opposite setup occurred before the countdown was completed\r\n    '''\r\n\r\n    down_arrow = u'$\\u2193$'\r\n    up_arrow = u'$\\u2191$'\r\n\r\n    y = ymax - dat_range/1.8\r\n\r\n    for K in failed_sell_setup1:             #Failed because it was a weak setup\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed sell', color='magenta', fontsize= 15, ha = 'center',)\r\n        plt.text(x, y - dat_range, down_arrow, color='magenta',  fontsize=20,ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    for K in failed_sell_setup2:         #Failed because it was pushed out by another setup\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed sell', color='black', fontsize=15, ha = 'center')\r\n        plt.text(x, y - dat_range, down_arrow, fontsize=20, color = 'black',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    y = ymin + dat_range/10\r\n\r\n    for K in failed_buy_setup1:\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed buy', color='magenta', fontsize=15, ha = 'center')\r\n        plt.text(x, y + dat_range, up_arrow, fontsize=20, color = 'magenta',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n    for K in failed_buy_setup2:\r\n        x = A.index[K]\r\n        plt.text(x, y, 'failed buy ', color='black', fontsize=15, ha = 'center')\r\n        plt.text(x, y+dat_range, up_arrow, fontsize=20, color = 'black',ha = 'center')\r\n        y2 = C[K]\r\n        plt.text(x, y2, dot, color='black', fontsize=point_size, ha='center', va='center')\r\n\r\n\r\n    '''Sometimes a failed setup is recharged before it fails. Plot these'''\r\n    for K in failed_buy_setup1  + failed_buy_setup2 + failed_sell_setup1 + failed_buy_setup2:\r\n        if K in recharges:\r\n            recharge_point = recharges[K]\r\n\r\n            if recharge_point in failed_buy_setup1  + failed_buy_setup2 + failed_sell_setup1 + failed_buy_setup2:\r\n                f = 'F'\r\n            else:\r\n                f = ''\r\n\r\n            x = A.index[recharge_point]\r\n            y = C[recharge_point] + dat_range/2\r\n            plt.text(x, y, 'R' + f, color='cyan', fontsize=fontsize, va='center')\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef strat(A,dict_buy,dict_sell,recharges,countdown_length = 13, position_length = 5):\r\n    '''IMPLEMENT STRATEGY'''\r\n\r\n    #Calculate percentage changes between days\r\n    perc_change = lambda x,y : (y-x)/x\r\n    change = [0 if I == 0 else perc_change(A['Close'][I-1],A['Close'][I]) for I in range(A.shape[0])]\r\n    results = [0]*A.shape[0]\r\n\r\n    for K in dict_buy:\r\n        if K not in recharges and len(dict_buy[K]) == countdown_length:\r\n            termination = dict_buy[K][-1]\r\n            for I in range(position_length):\r\n                results[termination + 1 + I] += change[termination + 1 + I]\r\n    for K in dict_sell:\r\n        if K not in recharges and len(dict_sell[K]) == countdown_length:\r\n            termination = dict_sell[K][-1]\r\n            for I in range(position_length):\r\n                results[termination + 1 + I] -= change[termination + 1 + I]\r\n\r\n    return results\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef demark(A, setup_tol = 4,\r\n           countdown_tol = 2,\r\n           setup_length = 9,\r\n           countdown_length = 13,\r\n           countdown_check = 8,\r\n           aggresive = False,\r\n           plot_results = True,\r\n           what_to_plot = 'Close',\r\n           strong_recharge = False,\r\n           strong_recharge_length = 18,\r\n           position_length = 5,\r\n           verbose = True\r\n           ):\r\n\r\n\r\n    '''Calculates demark values of a time series\r\n    Data should be presented as a dataframe A with columns 'Open','Close', 'High', 'Low' and indexed by Date '''\r\n\r\n\r\n\r\n    #Search for price flips\r\n    flip = [0 if I in range(setup_tol) else A['Close'][I] > A['Close'][I-setup_tol] for I in range(A.shape[0])]\r\n\r\n    #Search for Buy set up\r\n    bsu = [I + 1 for I in range(A.shape[0] - setup_length) if flip[I] and not any(flip[I+1:I+setup_length+1])]\r\n    # Search for Sell set up\r\n    ssu = [I + 1 for I in range(A.shape[0] - setup_length) if (not flip[I]) and all(flip[I + 1:I + setup_length+1])]\r\n\r\n\r\n\r\n    '''Check if we are in the middle of a setup at end of time series'''\r\n    pbsu = [I + 1 for I in range(A.shape[0] - setup_length+1,A.shape[0]-1) if flip[I] and not any(flip[I+1:])]\r\n    if pbsu:\r\n        pbsu = [pbsu[0]]    #Take start point of setup\r\n    pssu = [I + 1 for I in range(A.shape[0] - setup_length+1,A.shape[0]-1) if (not flip[I]) and all(flip[I+1:])]\r\n    if pssu:\r\n        pssu = [pssu[0]]      #Take start point of setup\r\n\r\n\r\n\r\n    '''Search for BUY countdown phases and store in dictionary'''\r\n    dict = {}              #will store starting points/countdown points for both buy and sell setups\r\n    dict_buy = {}          #will store starting points/countdown points for buy setups\r\n\r\n\r\n    if aggresive:\r\n        B = A['Low']\r\n    else:\r\n        B = A['Close']\r\n\r\n    buy_countdown_condition = [0 if I in range(countdown_tol) else B[I] <= A['Low'][I-countdown_tol] for I in range(A.shape[0])]\r\n\r\n    for K in bsu:\r\n        potential_numbers = [I for I in range(min(K+setup_length-1,A.shape[0]),A.shape[0]) if buy_countdown_condition[I]]\r\n        if len(potential_numbers) >= countdown_length:\r\n        # Check for countdown condiiton - need that last bar of countdown phase has lower close than countdown_check'th bar\r\n            correct_13 = []\r\n            for J in potential_numbers[countdown_length - 1 : ]:\r\n                if A['Low'][J] <=  A['Low'][potential_numbers[countdown_check - 1]]:\r\n                        correct_13 = [J]\r\n                        break\r\n            dict_buy[K] = potential_numbers[0:countdown_length-1] + correct_13\r\n            dict[K] = potential_numbers[0:countdown_length - 1] + correct_13\r\n        else:\r\n            dict_buy[K] = potential_numbers\r\n            dict[K] = potential_numbers\r\n\r\n\r\n    '''Search for SELL countdown phases and store in dictionary'''\r\n    if aggresive:\r\n        B = A['High']\r\n    else:\r\n        B = A['Close']\r\n\r\n    sell_countdown_condition = [0 if I in range(countdown_tol) else B[I] >= A['High'][I - countdown_tol] for I in\r\n                          range(A.shape[0])]\r\n\r\n    dict_sell = {}          #will store starting points/countdown points for sell setups\r\n    for K in ssu:\r\n        potential_numbers = [I for I in range(K + setup_length-1, A.shape[0]) if sell_countdown_condition[I]]\r\n        if len(potential_numbers) >= countdown_length:\r\n            # Check for countdown condiiton - need that last bar of countdown phase has lower close than countdown_check'th bar\r\n            correct_13 = []\r\n            for J in potential_numbers[countdown_length - 1:]:\r\n                if A['High'][J] >= A['High'][potential_numbers[countdown_check - 1]]:\r\n                    correct_13 = [J]\r\n                    break\r\n            dict_sell[K] = potential_numbers[0:countdown_length - 1] + correct_13\r\n            dict[K] = potential_numbers[0:countdown_length - 1] + correct_13\r\n\r\n        else:\r\n            dict_sell[K] = potential_numbers[0:countdown_length - 1]\r\n            dict[K] = potential_numbers[0:countdown_length - 1]\r\n\r\n\r\n\r\n\r\n\r\n    '''CANCELLING SETUPS\r\n    Various ways of cancelling a buy/sell setup'''\r\n\r\n    failed_buy_setup1 = []      #Stores setups that failed due to the tdst level stuff\r\n    failed_sell_setup1 = []\r\n    failed_buy_setup2 = []      #Stores setups that failed due to being interrupted by a sell setup\r\n    failed_sell_setup2 = []\r\n\r\n\r\n    for K in list(dict_buy.keys()):\r\n        #check if a true low that is higher than tdst level occurs during setup\r\n        #tdst leve is the true high of day 1 of the set up period - true high includes the close of yesterday as if it were today\r\n        tdst_level = float(max(A['High'][K],A['Close'][K-1]))\r\n        for I in range(K+1,K+setup_length):\r\n            if min(A['Low'][I], A['Close'][I-1]) > tdst_level:\r\n                failed_buy_setup1.append(K)\r\n                del(dict_buy[K])\r\n                del (dict[K])\r\n                break\r\n\r\n    for K in list(dict_sell.keys()):\r\n        #check if a true high that is lower than tdst level occurs during setup\r\n        # tdst leve is the true low of day 1 of the set up period - true low includes the close of yesterday as if it were today\r\n        tdst_level = float(min(A['Low'][K], A['Close'][K - 1]))\r\n        for I in range(K + 1, setup_length):\r\n            if max(A['High'][I], A['Close'][I - 1]) < tdst_level:\r\n                failed_sell_setup1.append(K)\r\n\r\n                del (dict_sell[K])\r\n                del (dict[K])\r\n                break\r\n\r\n\r\n    #Stay tuned for more ways to break a setup\r\n\r\n\r\n\r\n    '''RECHARGES:\r\n    Things are considered to have been recharged if another setup of the same type occurs before the end of the countdown phase\r\n    and they have not been killed already before the recharge point'''\r\n\r\n    recharges = {}          #stores setups that were recharged and where they were recharged\r\n\r\n    U = list(dict_buy.keys())\r\n    U.sort()\r\n    for K in U:\r\n        termination = dict_buy[K][-1]\r\n        try:\r\n            price_flip = [I for I in range(termination,A.shape[0]) if flip[I]][0]\r\n        except IndexError:\r\n            price_flip = A.shape[0]\r\n        if [I for I in U if (I <= price_flip - setup_length and I > K)]:\r\n            recharge_point = [I for I in U if (I <= price_flip - setup_length and I > K)][0]\r\n\r\n            #check that setup has not already failed before recharge_point\r\n            if not [I for I in dict_sell.keys() if I <= recharge_point - setup_length and I >= K]:\r\n                tdst_level = float(max(A['High'][K], A['Close'][K - 1]))\r\n                dead = False\r\n                for I in range(K + 1, recharge_point):\r\n                    if min(A['Low'][I], A['Close'][I - 1]) > tdst_level:\r\n                        dead = True\r\n                        failed_buy_setup1.append(K)\r\n                        del (dict_buy[K])\r\n                        del (dict[K])\r\n                        break\r\n                if not dead:\r\n                    if not strong_recharge:\r\n                        recharges[K] = recharge_point\r\n                    else:\r\n                        if not any(flip[I:I+strong_recharge_length-1]):\r\n                            recharges[K] = recharge_point\r\n                        #Not sure about next 3 lines... (deletes a recharge setup if it isnt long enough)\r\n                        else:\r\n                            del (dict_buy[K])\r\n                            del (dict[K])\r\n\r\n\r\n    #Same but for sell setups\r\n\r\n    V = list(dict_sell.keys())\r\n    V.sort()\r\n    for K in V:\r\n        termination = dict_sell[K][-1]\r\n        try:\r\n            price_flip = [I for I in range(termination,A.shape[0]) if not flip[I]][0]\r\n        except IndexError:\r\n            price_flip = A.shape[0]\r\n        if [I for I in V if (I <= price_flip - setup_length and I > K)]:\r\n            recharge_point  = [I for I in V if (I <= price_flip - setup_length and I > K)][0]\r\n\r\n            #check that setup has not already failed before recharge_point\r\n            if not [I for I in dict_buy.keys() if I <= recharge_point - setup_length and I >= K]:\r\n                tdst_level = float(min(A['Low'][K], A['Close'][K - 1]))\r\n                dead = False\r\n                for I in range(K + 1, recharge_point):\r\n                    if max(A['High'][I], A['Close'][I - 1]) < tdst_level:\r\n                        dead = True\r\n                        failed_sell_setup1.append(K)\r\n                        del (dict_sell[K])\r\n                        del (dict[K])\r\n                        break\r\n                if not dead:\r\n                    if not strong_recharge:\r\n                        recharges[K] = recharge_point\r\n                    else:\r\n                        if all(flip[I:I+strong_recharge_length-1]):\r\n                            recharges[K] = recharge_point\r\n                        # Not sure about next 3 lines... (deletes a recharge setup if it isnt long enough)\r\n                        else:\r\n                            del (dict_sell[K])\r\n                            del (dict[K])\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n    '''MORE WAYS TO BREAK A SETUP\r\n    A sell setup prior to completion of buy setup cancels the buy setup\r\n    And vice versa'''\r\n\r\n    dict_keys = list(dict.keys())\r\n    dict_keys.sort()\r\n\r\n    for K in dict_keys:\r\n        termination = dict[K][-1]\r\n        if K in dict_buy:\r\n            if [I for I in dict_sell.keys() if I <= termination - setup_length and I >= K]:\r\n                failed_buy_setup2.append(K)\r\n                if verbose:\r\n                    print('buy', str(A.index[K])[0:10], ' failed because of', [str(A.index[I])[:10] for I in dict_sell.keys() if I <= termination - setup_length and I >= K] )\r\n                del (dict_buy[K])\r\n                del (dict[K])\r\n        else:\r\n            if [I for I in dict_buy.keys() if I <= termination - setup_length and I >= K]:\r\n                failed_sell_setup2.append(K)\r\n\r\n                if verbose:\r\n                    print('sell ', str(A.index[K])[0:10], ' failed because of', [str(A.index[I])[:10] for I in dict_buy.keys() if I <= termination - setup_length and I >= K] )\r\n\r\n                del (dict_sell[K])\r\n                del (dict[K])\r\n\r\n\r\n    for K in list(dict_buy.keys()):\r\n        #check if a true low that is higher than tdst level occurs during whole process\r\n        #tdst leve is the true high of day 1 of the set up period - true high includes the close of yesterday as if it was today\r\n        tdst_level = float(max(A['High'][K],A['Close'][K-1]))\r\n        for I in range(K+1,dict_buy[K][-1]):\r\n            if min(A['Low'][I], A['Close'][I-1]) > tdst_level:\r\n                failed_buy_setup1.append(K)\r\n                del (dict_buy[K])\r\n                del (dict[K])\r\n                break\r\n\r\n    for K in list(dict_sell.keys()):\r\n        #check if a true high that is lower than tdst level occurs during whole process\r\n        # tdst leve is the true low of day 1 of the set up period - true low includes the close of yesterday as if it was today\r\n        tdst_level = float(min(A['Low'][K], A['Close'][K - 1]))\r\n        for I in range(K + 1, dict_sell[K][- 1]):\r\n            if max(A['High'][I], A['Close'][I - 1]) < tdst_level:\r\n                failed_sell_setup1.append(K)\r\n                del (dict_sell[K])\r\n                del (dict[K])\r\n                break\r\n\r\n\r\n\r\n\r\n    '''BAD SETUPS\r\n    we consider the previous setup's TDST line (range basically) to see if current setup breaks out that range\r\n    Setup is bad if it does not do that'''\r\n\r\n    bad_dict_buy = []\r\n    bad_dict_sell = []\r\n    dict_keys = list(dict.keys())\r\n    dict_keys.sort()\r\n\r\n    for I in range(1,len(dict_keys)):\r\n        K = dict_keys[I-1]\r\n        L = dict_keys[I]\r\n        if K in dict_buy and L in dict_sell:\r\n            previous_tdst_level = max(A['High'][K],A['Close'][K-1])\r\n            current_comp_setup = A['Close'][L+setup_length-1]\r\n            if current_comp_setup < previous_tdst_level:\r\n                bad_dict_sell.append(L)\r\n        elif K in dict_sell and L in dict_buy:\r\n            previous_tdst_level = min(A['Low'][K], A['Close'][K - 1])\r\n            current_comp_setup = A['Close'][L + setup_length - 1]\r\n            if current_comp_setup > previous_tdst_level:\r\n                bad_dict_buy.append(L)\r\n\r\n\r\n\r\n\r\n    'PLOT RESULTS'\r\n    if plot_results:\r\n\r\n        plot_it(A,\r\n                dict_buy,\r\n                dict_sell,\r\n                bad_dict_buy,\r\n                bad_dict_sell,\r\n                recharges,\r\n                failed_buy_setup1,\r\n                failed_buy_setup2,\r\n                failed_sell_setup1,\r\n                failed_sell_setup2,\r\n                pbsu,\r\n                pssu,\r\n                what_to_plot,\r\n                setup_length)\r\n\r\n\r\n\r\n\r\n    # return dict_buy, dict_sell, failed_buy_setup1,failed_buy_setup2, failed_sell_setup1, failed_sell_setup2, recharges,\r\n\r\n    return dict_buy, dict_sell, recharges\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef make_portfolio(setup_tol = 4,\r\n           countdown_tol = 2,\r\n           setup_length = 9,\r\n           countdown_length = 13,\r\n           countdown_check = 8,\r\n           aggresive = False,\r\n           plot_results = False,\r\n           what_to_plot = 'Close',\r\n           strong_recharge = False,\r\n           strong_recharge_length = 18,\r\n           position_length = 5,\r\n           verbose = False):\r\n\r\n\r\n    args = {\r\n            'setup_tol' : setup_tol,\r\n           'countdown_tol' : countdown_tol ,\r\n           'setup_length' : setup_length,\r\n           'countdown_length' : countdown_length,\r\n           'countdown_check' : countdown_check,\r\n           'aggresive' : aggresive,\r\n           'plot_results' : plot_results,\r\n           'what_to_plot' : what_to_plot,\r\n           'strong_recharge' : strong_recharge,\r\n           'strong_recharge_length' : strong_recharge_length,\r\n           'position_length' : position_length,\r\n           'verbose' : verbose\r\n    }\r\n\r\n    data = get_data()\r\n    total_results = []\r\n\r\n    for A in data:\r\n        dict_buy, dict_sell, _,_,_,_, recharges = demark(A,**args)\r\n        results = strat(A,dict_buy,dict_sell,recharges,countdown_length, position_length)\r\n        total_results.append(results)\r\n\r\n    success = [sum(I) for I in total_results]\r\n    amalgamated_results = [sum([total_results[I][J] for I in range(len(total_results))]) for J in range(A.shape[0])]\r\n\r\n    #plot success\r\n    plt.plot(A.index,amalgamated_results)\r\n\r\n\r\n\r\n    return total_results\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n# if __name__ == \"__main__\":\r\n#     dict_buy, dict_sell, failed_buy_setup1, failed_buy_setup2, failed_sell_setup1, failed_sell_setup2, recharges = demark(C)\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"yoniberger"}}