def create_df(ticker, days, std, a, b, signal, start, end, freq):
    df = yf.download(ticker, start=start, end=end, interval=freq)
    data = pd.DataFrame()
    data['Close'] = df['Close']
    data = BBANDS(df, days, std)
    data['Macd'] = MACD(df, a, b, signal)[0]
    data['Signal_line'] = MACD(df, 12, 26, 9)[1]
    data['EWMA_short'] = df['Close'].ewm(span=20, adjust=False).mean()
    data['EWMA_long'] = df['Close'].ewm(span=40, adjust=False).mean()

    return data
Esempio n. 2
0
def compute_data(token):
    global one_hour_rsi
    #enddate = datetime.datetime(2020, 5, 4, 15,30,0,0)
    enddate = datetime.datetime.today()
    startdate = enddate - datetime.timedelta(3)
    try:
        df = historical_data.get(kite, token, startdate, enddate, candlesize)
        df = SuperTrend.calc(df, supertrend_period, supertrend_multiplier)
        df = MACD.calc(df)
        rsi = historical_data.get(kite, token, startdate, enddate, "60minute")
        rsi = RSI.calc(rsi)
        one_hour_rsi = rsi.RSI_14.values[-1]
    except Exception as e:
        print("******* ERROR Computing Historical Data ********", token, e)
    return df
Esempio n. 3
0
def compute_data(token):
    global one_hour_rsi
    #enddate = datetime.datetime(2020, 5, 4, 15,30,0,0)
    enddate = datetime.datetime.today()
    startdate = enddate - datetime.timedelta(15)
    try:
        df = historical_data.get(kite, token, startdate, enddate, candlesize)
        df = EMA.calc(df, 'close', 'ema_5', 5)
        df = EMA.calc(df, 'close', 'ema_20', 20)
        df = MACD.calc(df)
        df = MFI.calc(df)
        df = VWAP.calc(df)
        rsi = historical_data.get(kite, token, startdate, enddate, "60minute")
        rsi = RSI.calc(rsi)
        one_hour_rsi = rsi.RSI_14.values[-2]
    except Exception as e:
        print("******* ERROR Computing Historical Data ********", token, e)
    return df
def test_run():
    # Read data
    dates = pd.date_range('2008-1-1', '2009-12-31')
    syms = ['AAPL']
    prices_all = get_data(syms, dates)
    prices_port = prices_all[syms]  # only price of each stock in portfolio
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    bb = BB(prices_port, lookback=20)
    macd = MACD(prices_port)
    rsi = RSI(prices_port, lookback=14)
    df = pd.concat([prices_port, bb, macd, rsi], axis=1)
    df = df.fillna(method='bfill')
    #print df
    #plot_data(df, title="Bollinger Bands and stock price", ylabel="Stock price", xlabel="Date")
    orders = pd.DataFrame(columns=['Date', 'Symbol', 'Order', 'Shares'])

    holding = 0  #holding =200 long; holding=-200 short
    window = dt.datetime(2008, 1, 1)  # window should more than 21
    for (i_row, row), (i_prev, prev) in zip(df.iterrows(),
                                            df.shift(1).iterrows()):
        if prev['MACD Line'] >= 0 and row['MACD Line'] < 0 and (
                i_row - window).days > 21 and holding > -200:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding)
            ]
            holding -= 200 + holding
            window = i_row
            plt.axvline(i_row, color='r')
        if prev['MACD Line'] <= 0 and row['MACD Line'] > 0 and (
                i_row - window).days > 21 and holding < 200:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding)
            ]  #max buy
            holding += 200 - holding
            window = i_row
            plt.axvline(i_row, color='g')
        if prev['MACD Line'] >= prev['Signal Line'] and row['MACD Line'] < row[
                'Signal Line'] and (
                    i_row -
                    window).days > 21 and holding > -200 and row['RSI'] > 70:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding)
            ]
            holding -= 200 + holding
            window = i_row
            plt.axvline(i_row, color='r')
        if prev['MACD Line'] <= prev['Signal Line'] and row['MACD Line'] > row[
                'Signal Line'] and (i_row -
                                    window).days > 21 and holding < 200:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding)
            ]  #max buy
            holding += 200 - holding
            window = i_row
            plt.axvline(i_row, color='g')

        if prev[syms[0]] <= prev['Lower Band'] and row[
                syms[0]] > row['Lower Band'] and (
                    i_row -
                    window).days > 21 and holding < 200:  # cross up Lower Band
            orders.loc[len(orders)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding)
            ]  #max buy
            holding += 200 - holding
            window = i_row
            plt.axvline(i_row, color='g')
        if prev[syms[0]] <= prev['Middle Band'] and row[
                syms[0]] > row['Middle Band'] and (
                    i_row - window).days > 21 and holding > -200 and row[
                        'RSI'] > 70:  # cross up Middle Band
            orders.loc[len(orders)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding)
            ]
            holding -= 200 + holding
            window = i_row
            plt.axvline(i_row, color='r')
        # if prev[syms[0]] >= prev['Upper Band'] and row[syms[0]] < row['Upper Band'] and (i_row-window).days>21 and holding>-200 and row['RSI']>70: # cross down Upper Band
        #     orders.loc[len(orders)] = [i_row, syms[0], 'SELL', str(200+holding)]
        #     holding -= 200+holding
        #     window = i_row
        #     plt.axvline(i_row, color='r')
        if prev[syms[0]] >= prev['Middle Band'] and row[
                syms[0]] < row['Middle Band'] and (
                    i_row - window
                ).days > 21 and holding < 200:  # cross down Middle Band
            orders.loc[len(orders)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding)
            ]
            holding += 200 - holding
            window = i_row
            plt.axvline(i_row, color='g')

    file_dir = os.path.join('orders', 'orders.csv')
    orders.to_csv(file_dir)

    Compare_df = BestPossible()
    benchmark = Compare_df['Benchmark']
    of = "./orders/orders.csv"
    sv = 100000
    portvals = compute_portvals(orders_file=of, start_val=sv)
    portvals = portvals / portvals.ix[0, :]
    benchmark = benchmark / benchmark.ix[0, :]
    #plot_data(plot_df, title="Benchmark vs. Rule-based portfolio", ylabel="Normalized price", xlabel="Date")
    prices_port = prices_port / prices_port.ix[0, :]
    plt.plot(prices_port.index,
             portvals,
             label='Rule-based portfolio',
             color='b')
    plt.plot(prices_port.index, prices_port, label='APPL', color='m')
    plt.plot(benchmark.index, benchmark, label='Benchmark', color='k')
    plt.title('Benchmark vs. Rule-based portfolio')
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.legend(loc='lower right')
    plt.show()
Esempio n. 5
0
def test_run():
    # Read data
    dates = pd.date_range('2008-1-1', '2009-12-31')
    syms = ['AAPL']
    prices_all = get_data(syms, dates)
    prices_port = prices_all[syms]  # only price of each stock in portfolio
    macd = MACD(prices_port)
    df_RB = pd.concat([macd['MACD Line'], macd['Signal Line']], axis=1)
    df_RB = (df_RB - df_RB.mean(axis=0)) / df_RB.std(axis=0)
    df_RB['Color'] = 'k'
    holding = 0  # holding =200 long; holding=-200 short
    window = date.datetime(2008, 1, 1)  # window should more than 21
    for (i_row, row), (i_prev, prev) in zip(df_RB.iterrows(),
                                            df_RB.shift(1).iterrows()):
        if prev['MACD Line'] >= 0 and row['MACD Line'] < 0 and holding > -200:
            holding -= 200 + holding
            window = i_row
            df_RB.ix[i_row, 'Color'] = 'r'
        if prev['MACD Line'] <= 0 and row['MACD Line'] > 0 and holding < 200:
            holding += 200 - holding
            window = i_row
            df_RB.ix[i_row, 'Color'] = 'g'
        if prev['MACD Line'] >= prev['Signal Line'] and row['MACD Line'] < row[
                'Signal Line'] and holding > -200:
            holding -= 200 + holding
            window = i_row
            df_RB.ix[i_row, 'Color'] = 'r'
        if prev['MACD Line'] <= prev['Signal Line'] and row['MACD Line'] > row[
                'Signal Line'] and holding < 200:
            holding += 200 - holding
            window = i_row
            df_RB.ix[i_row, 'Color'] = 'g'

    #print df_RB
    df_RB = pd.DataFrame(
        dict(a=df_RB['MACD Line'],
             b=df_RB['Signal Line'],
             label=df_RB['Color']))
    groups = df_RB.groupby('label')
    fig, ax = plt.subplots()
    for name, group in groups:
        ax.plot(group.a,
                group.b,
                marker='o',
                linestyle='',
                ms=12,
                label='MACD Line',
                color=name)
    ax.set_xlim([-1.5, 1.5])
    ax.set_ylim([-1.5, 1.5])
    ax.set_xlabel('MACD Line Value')
    ax.set_ylabel('Signal Line Value')
    plt.show()

    df_train = pd.read_csv(os.path.join('orders', 'Training data.csv'))
    df_train = pd.DataFrame(
        dict(a=df_train['MACD_ZERO'],
             b=df_train['MACD_SIGNAL'],
             label=df_train['Color']))

    groups = df_train.groupby('label')
    fig, ax = plt.subplots()
    for name, group in groups:
        ax.plot(group.a,
                group.b,
                marker='o',
                linestyle='',
                ms=12,
                label=name,
                color=name)
    ax.set_xlim([-1.5, 1.5])
    ax.set_ylim([-1.5, 1.5])
    ax.set_xlabel('MACD Line Value')
    ax.set_ylabel('Signal Line Value')
    plt.show()

    df_train['Color2'] = 'k'

    inf = open('orders/indicators.csv')
    data = np.array(
        [map(float,
             s.strip().split(',')) for s in inf.readlines()])
    predY_list = [0] * data.shape[0]

    for i in range(10):  # bag 10 times
        learner = dt.DTclass(leaf_size=5, verbose=False)
        train_rows = int(round(1.0 * data.shape[0]))
        test_rows = data.shape[0] - train_rows
        Xtrain = data[:train_rows, 0:-1]
        Ytrain = data[:train_rows, -1]
        Xtest = Xtrain
        Ytest = Ytrain
        learner.addEvidence(Xtrain, Ytrain)  # training step
        predY = learner.query(Xtest)  # query
        predY_list = predY_list + predY
    predY = predY_list / 10.0
    df_train['Decision'] = predY
    holding = 0

    for (i_row, row) in df_train.iterrows():
        if row['Decision'] < 0 and holding > -200:
            holding -= 200 + holding
            df_train.ix[i_row, 'Color2'] = 'r'
        if row['Decision'] > 0 and holding < 200:
            holding += 200 - holding
            df_train.ix[i_row, 'Color2'] = 'g'

    df_test = df_train.copy()

    groups = df_train.groupby('Color2')
    fig, ax = plt.subplots()
    for name, group in groups:
        ax.plot(group.a,
                group.b,
                marker='o',
                linestyle='',
                ms=12,
                label=name,
                color=name)
    ax.set_xlim([-1.5, 1.5])
    ax.set_ylim([-1.5, 1.5])
    ax.set_xlabel('MACD Line Value')
    ax.set_ylabel('Signal Line Value')
    plt.show()
Esempio n. 6
0
def testPolicy(symbol='JPM',
               sd=dt.datetime(2008, 1, 1),
               ed=dt.datetime(2009, 12, 31),
               sv=100000):
    """Accepts a prices df, returns a trade dataframe based on BB, MACD, and Stoch Osc"""

    df = get_data([symbol], pd.date_range(sd, ed))
    if symbol != 'SPY':
        df.drop('SPY', axis=1, inplace=True)

    # BB was implemented into the SMA, the threshold value had been iteratively optimized
    sma, upper, lower = simple_ma(df,
                                  window=19,
                                  bollinger=True,
                                  threshold=1.45102)

    exp12 = exp_ma(df, days=12)
    exp12 = exp12.rename(columns={exp12.columns[0]: 'ema12'})

    exp24 = exp_ma(df, days=24)
    exp24 = exp24.rename(columns={exp24.columns[0]: 'ema24'})

    exp_diff = (exp24['ema24'] - exp12['ema12']).rename('ema_diff')
    exp_diff = exp_diff.to_frame()
    #    ema_diff = (exp_diff.shift(1)/exp_diff)/abs(exp_diff.shift(1)/exp_diff)

    macd, macd_s = MACD(df, ema1_days=12, ema2_days=24, macd_signal_days=9)
    k, d = stoc_osc(df, k_window=12, d_window=3)

    md = (macd['macd'] - macd_s['macd_signal']).rename('m-d')
    md = md.to_frame()
    md_diff = (md.shift(1) / md) / abs(
        md.shift(1) / md)  # checks if MACD crosses its signal (-1 if it does)

    kd = (k['K'] - d['D']).rename('k-d')
    kd = kd.to_frame()
    kd_diff = (kd.shift(1) / kd) / abs(
        kd.shift(1) / kd)  # checks if k-d crosses zero (-1 if it does)

    # df2 is a dashboard of indicator values
    df2 = pd.concat(
        [df, sma, exp12, exp24, upper, lower, macd, macd_s, md, k, d, kd],
        axis=1)

    # generating a long/short df marked with 1's or 0's, 1's meaning perform the action
    df_buy = pd.DataFrame(index=df2.index)
    df_sell = df_buy.copy()

    df_buy['BB'] = np.where(df2.ix[:, 0] < df2.ix[:, 'lower_band'], 1, 0)
    df_buy['Stoch_D'] = np.where(df2.ix[:, 'D'] < 30, 1, 0)
    df_buy['KD'] = np.where(kd_diff.ix[:, 0] == -1, 1, 0)
    df_buy['MACD'] = np.where(md_diff.ix[:, 0] == -1, 1, 0)
    #    df_buy['ema_diff'] = np.where(ema_diff.ix[:,0] == -1, 1, 0)

    df_sell['BB'] = np.where((df2.ix[:, 0] > df2.ix[:, 'upper_band']), 1, 0)
    df_sell['Stoch_D'] = np.where(df2.ix[:, 'D'] > 70, 1, 0)
    df_sell['KD'] = df_buy['KD']
    df_sell['MACD'] = df_buy['MACD']
    #    df_sell['ema_diff'] = df_buy['ema_diff']

    df_trades = pd.DataFrame(index=df.index)
    df_trades[df.columns[0]] = 0
    holding = 0

    # Trading Scheme is to long/short primarily off BB crossings. The second criteria is off the strength of the momentum indicators
    for i in df_trades.index:
        if holding == 0:
            if df_buy.ix[i, 'BB'] == 1 and df_buy.ix[i, 'Stoch_D'] == 1 and (
                    df_buy.ix[i, 'KD'] == 1 or df_buy.ix[i, 'MACD'] == 1):
                df_trades.ix[i, 0] = 1000
                holding = 1000
            elif df_sell.ix[i,
                            'BB'] == 1 and df_sell.ix[i, 'Stoch_D'] == 1 and (
                                df_sell.ix[i, 'KD'] == 1
                                or df_sell.ix[i, 'MACD'] == 1):
                df_trades.ix[i, 0] = -1000
                holding = -1000
        elif holding == 1000:
            if df_sell.ix[i, 'BB'] == 1 and df_sell.ix[i, 'Stoch_D'] == 1 and (
                    df_sell.ix[i, 'KD'] == 1 or df_sell.ix[i, 'MACD'] == 1):
                df_trades.ix[i, 0] = -2000
                holding = -1000

        elif holding == -1000:
            if df_buy.ix[i, 'BB'] == 1 and df_buy.ix[i, 'Stoch_D'] == 1 and (
                    df_buy.ix[i, 'KD'] == 1 or df_buy.ix[i, 'MACD'] == 1):
                df_trades.ix[i, 0] = 2000
                holding = 1000

    return (df_trades)
Esempio n. 7
0
    def __init__(self,
                 close,
                 rsi: indicators.RSI,
                 macd: indicators.MACD,
                 fee=0.0):
        Strategy.__init__(self, close.index, fee=0.0)

        # indique le signal en cours sachant que MACD > RSI
        buysignalstrat = None

        macdcrossedfromdown = False
        macdcrossedfromtop = False

        lowthreshpassed = False
        lowlowthreshpassed = False
        hithreshpassed = False
        hihithreshpassed = False
        hasbought = False
        self.signals['rsi'] = rsi.data()
        self.signals['macd'], self.signals['macd_signal'] = macd.data()
        for index, row in self.signals.iterrows():
            # achat en fonction du RSI
            if row['rsi'] < 33:
                lowthreshpassed = True
            if row['rsi'] < 20:
                lowlowthreshpassed = True
            if row['rsi'] > 66:
                hithreshpassed = True
            if row['rsi'] > 80:
                hihithreshpassed = True

            if row['rsi'] > 20 and lowlowthreshpassed == True:
                lowlowthreshpassed = False
                if hasbought == False:
                    hasbought = True
                    buysignalstrat = "RSI"
            if row['rsi'] > 33 and lowthreshpassed == True:
                lowthreshpassed = False
                if hasbought == False:
                    hasbought = True
                    buysignalstrat = "RSI"
            # on revend par RSI seulement si c'est le seul signal qui a généré l'achat
            if row['rsi'] < 80 and hihithreshpassed == True:
                hihithreshpassed = False
                if hasbought == True and buysignalstrat == "RSI":
                    hasbought = False
                    buysignalstrat = None
            if row['rsi'] < 66 and hithreshpassed == True:
                hithreshpassed = False
                if hasbought == True and buysignalstrat == "RSI":
                    hasbought = False
                    buysignalstrat = None

            # achat en fonction de la MACD
            # TODO : lorsque le marché stagne, la MACD génère beaucoup de faux signaux, comment les éviter ?
            if row['macd'] > row[
                    'macd_signal'] and macdcrossedfromdown == False:
                macdcrossedfromdown = True
                macdcrossedfromtop = False
                if hasbought == False:
                    hasbought = True
                # même si nous avions acheté avec le RSI, si le signal est confirmé par la MACD
                # il sera considéré comme étant prévalant
                buysignalstrat = "MACD"
            if row['macd'] < row['macd_signal'] and macdcrossedfromtop == False:
                macdcrossedfromtop = True
                macdcrossedfromdown = False
                if hasbought == True:
                    hasbought = False
                    buysignalstrat = None

            if hasbought == True:
                self.signals['signal'].loc[index] = 1.0

        self.signals['positions'] = self.signals['signal'].diff()
Esempio n. 8
0
 def __init__(self, close, macd: indicators.MACD, fee=0.0):
     Strategy.__init__(self, close.index, fee=0.0)
     line, signal = macd.data()
     self.signals['signal'] = np.where(line > signal, 1.0, 0.0)
     self.signals['positions'] = self.signals['signal'].diff()
def test_run():
    #construct indicator and decision
    dates = pd.date_range('2008-1-1', '2009-12-31')
    syms = ['AAPL']
    prices_all = get_data(syms, dates)
    prices_port = prices_all[syms]  # only price of each stock in portfolio

    bb = BB(prices_port, lookback=20)
    bb=bb.fillna(method='bfill')
    macd = MACD(prices_port)
    rsi = RSI(prices_port, lookback=14)
    df = pd.concat([prices_port, bb, macd, rsi], axis=1)
    df = df.fillna(method='bfill')

    indicators = pd.DataFrame(columns=['MACD_ZERO', 'MACD_SIGNAL', 'ACROSS_BAND', 'BB_value', 'RSI','Decision'])

    indicators['BB_value'] = (df[syms[0]] - bb['Middle Band']) / (bb['Upper Band'] - bb['Middle Band'])
    indicators['MACD_ZERO']=macd['MACD Line']
    indicators['MACD_SIGNAL'] = macd['Signal Line']
    indicators['ACROSS_BAND'] = bb['Middle Band']
    indicators['RSI'] = rsi['RSI']
    indicators2 = indicators[['MACD_SIGNAL', 'MACD_ZERO']]
    indicators2 = (indicators2 - indicators2.mean(axis=0)) / indicators2.std(axis=0)
    indicators2['Color']='k'
    # construct indicators
    # for (i_row, row), (i_prev, prev) in zip(df.iterrows(), df.shift(1).iterrows()):
    #     if prev['MACD Line'] >= 0 and row['MACD Line'] < 0:  # MACD prev>0,now<0, SELL MACD_ZERO=-1
    #         indicators.ix[i_row,'MACD_ZERO']=-1
    #     if prev['MACD Line'] <= 0 and row['MACD Line'] > 0:  # MACD prev<0,now>0, BUY MACD_ZERO=1
    #         indicators.ix[i_row, 'MACD_ZERO'] = 1
    #     if prev['MACD Line'] >= prev['Signal Line'] and row['MACD Line'] < row['Signal Line']: # MACD prev>Signal Line,now<Signal line, SELL MACD_Signal=-1
    #         indicators.ix[i_row,'MACD_SIGNAL']=-1
    #     if prev['MACD Line'] <= prev['Signal Line'] and row['MACD Line'] > row['Signal Line']:  # MACD prev<Signal Line,now>Signal line, BUY MACD_Signal=1
    #         indicators.ix[i_row, 'MACD_SIGNAL'] = 1
    #     if prev[syms[0]] <= prev['Lower Band'] and row[syms[0]] > row['Lower Band']:  # cross up Lower Band, BUY Acroo_band=1
    #         indicators.ix[i_row, 'ACROSS_BAND'] = 1
    #     if prev[syms[0]] >= prev['Upper Band'] and row[syms[0]] < row['Upper Band']: # cross down Upper Band SELL Acroo_band=-1
    #         indicators.ix[i_row, 'ACROSS_BAND'] = -1
    #     if row['RSI']>70:     #RSI>70 overbought, likely to sell it
    #         indicators.ix[i_row, 'RSI'] = -1
    #     if row['RSI'] < 30:   #RSI<30, oversold, likely to buy it.
    #         indicators.ix[i_row, 'RSI'] = 1
    # construct decision

    indicators = (indicators - indicators.mean(axis=0)) / indicators.std(axis=0)

    YBUY=0.05
    YSELL=-0.05
    for (i_row, row), (i_future, future) in zip(df.iterrows(), df.shift(-21).iterrows()):
        if (future[syms[0]]/row[syms[0]]-1)> YBUY: # if 21 days return exceed YBUY, then BUY Decision=1
            indicators.ix[i_row, 'Decision'] = 1
            indicators2.ix[i_row, 'Color'] = 'g'
        if (future[syms[0]] / row[syms[0]] - 1) < YSELL:  # if 21 days return less than YSELL, then SELL Decision=-1
            indicators.ix[i_row, 'Decision'] = -1
            indicators2.ix[i_row, 'Color'] = 'r'

    indicators=indicators.fillna(0)


    #print indicators
    file_dir = os.path.join('orders', 'indicators.csv')
    file_dir2 = os.path.join('orders', 'Training data.csv')
    indicators.to_csv(file_dir, header=False, index=False)

    indicators2.to_csv(file_dir2)
Esempio n. 10
0
datemin = dt.datetime(2016, 1, 1)
datemax = dt.datetime(2019, 1, 2)

def print_transactions(t):
    s=0
    for i in range(len(t)-1):
        if t[i]==1.0 and t[i+1] ==-1.0:
            s+=closep[i]
            print("Sold at Rs.{0} on {1} totalamt= {2}".format(closep[i],mdates.num2date(date[i]),s))
        elif t[i]==-1.0 and t[i+1] ==1:
            s-=closep[i]
            print("Bought at Rs.{0} on {1} totalamt= {2}".format(closep[i],date[i],s))

if __name__ == "__main__":
    #x= EMA(df,'close',3,3)
    x=MACD(df,base='close')

    ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
    ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)
    ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)

    ax2.set_ylabel('₹')
    ax2.xaxis_date()
    ax2.xaxis.set_major_formatter(plt.NullFormatter())
    ax2.xaxis.set_major_locator(mticker.MultipleLocator(14))

    #ax2.legend(loc='best')
    ax3.tick_params(labelrotation=45)
    ax3.xaxis.set_major_formatter(mdates.DateFormatter("%d-%m-%Y"))
    ax3.xaxis.set_major_locator(mticker.MultipleLocator(14))
    
Esempio n. 11
0
import pandas as pd
import matplotlib.pyplot as plt
from indicators import MACD

# create a timerange you are interested in
periods = pd.date_range('2017-02-14', '2017-02-17', freq='4H')
# create an empty dataframe with dates as index
df = pd.DataFrame(index=periods)
# load data from json
dfBTC = pd.read_json('./btc_usd.json', orient='records')
# change index column of the new dataframe
dfBTC.set_index('date', inplace=True)

# df = df.join(dfBTC)

df = MACD(dfBTC, 12, 26)
df.set_index('date', inplace=True)

df['close'].plot()
plt.show()

print df

# plot chart
# ax = df['close'].plot()

# plt.show()
Esempio n. 12
0
 def __init__(self, macd=MACD(), order=Order()):
     super(MACDStrategy, self).__init__(macd=macd, order=order)
Esempio n. 13
0
    print("====")
    print('')
    print(mfi.tail(30))
    print('')
    plot_MFI(sym, mfi, pdf)

    wr = WilliamsR(hdf, ldf, pdf, lookback)
    print('Williams %R:')
    print("============")
    print('')
    print(wr.tail(30))
    print('')
    plot_WilliamsR(sym, wr, pdf)

    fast_period, slow_period, signal_period = 12, 26, 9
    macd = MACD(fast_period, slow_period, signal_period, pdf)
    print('MACD:')
    print("=====")
    print('')
    print(macd.tail(30))
    print('')
    plot_MACD(sym, macd, pdf)

    optimal = testPolicy(sym, sd, ed, sv)
    # print("Optimal strategy:")
    # print("=================")
    # print(optimal)
    # print('')

    odf = build_order_dataframe(optimal)
    # print("Optimal order dataframe:")
Esempio n. 14
0
    indicator = Indicator()

    requests = 0
    data = []

    try:
        indicator.set_candles(product=token,
                              callback=get_time(callback),
                              begin=get_time(begin),
                              granularity=gra)
        print(indicator.candles[-1])
    except Exception as e:
        print(f"{token} failed because of ", e)

    macd = MACD()
    macd.candles = indicator.candles
    percentage: float
    try:
        macd.set_indicator()
        percentage = (macd.signal[-1] * 100) / macd.macd[-1]
        product_data.append(
            [token, percentage, macd.signal[-1], macd.macd[-1]])
    except Exception as e:
        print(f"{token} failed because of ", e)
        percentage = 0

product_data.sort(key=lambda x: x[1], reverse=True)
writer = open("../txt_files/product_percentage", "w")
for line in product_data:
    writer.write(str(line) + "\n")
def test_run():
    Traindates = pd.date_range('2008-1-1', '2009-12-31')
    TestDates = pd.date_range('2010-1-1', '2011-12-31')
    syms = ['AAPL']
    prices_train = get_data(syms, Traindates)
    prices_test = get_data(syms, TestDates)
    prices_train = prices_train[syms]
    prices_test = prices_test[syms]

    bb_train = BB(prices_train, lookback=20)
    bb_train = bb_train.fillna(method='bfill')
    bb_test = BB(prices_test, lookback=20)
    bb_test = bb_test.fillna(method='bfill')

    macd_train = MACD(prices_train)
    macd_test = MACD(prices_test)

    rsi_train = RSI(prices_train, lookback=14)
    rsi_test = RSI(prices_test, lookback=14)
    df_train = pd.concat([prices_train, bb_train, macd_train, rsi_train],
                         axis=1)
    df_train = df_train.fillna(method='bfill')
    df_test = pd.concat([prices_test, bb_test, macd_test, rsi_test], axis=1)
    df_test = df_test.fillna(method='bfill')

    indicators_train = pd.DataFrame(columns=[
        'MACD_ZERO', 'MACD_SIGNAL', 'ACROSS_BAND', 'BB_value', 'RSI',
        'Decision'
    ])
    indicators_test = pd.DataFrame(columns=[
        'MACD_ZERO', 'MACD_SIGNAL', 'ACROSS_BAND', 'BB_value', 'RSI',
        'Decision'
    ])

    indicators_train['BB_value'] = (df_train[syms[0]] - bb_train['Middle Band']
                                    ) / (bb_train['Upper Band'] -
                                         bb_train['Middle Band'])
    indicators_train['MACD_ZERO'] = macd_train['MACD Line']
    indicators_train['MACD_SIGNAL'] = macd_train['Signal Line']
    indicators_train['ACROSS_BAND'] = bb_train['Middle Band']
    indicators_train['RSI'] = rsi_train['RSI']
    indicators_train = (indicators_train - indicators_train.mean(axis=0)
                        ) / indicators_train.std(axis=0)

    indicators_test['BB_value'] = (
        df_test[syms[0]] - bb_test['Middle Band']) / (bb_test['Upper Band'] -
                                                      bb_test['Middle Band'])
    indicators_test['MACD_ZERO'] = macd_test['MACD Line']
    indicators_test['MACD_SIGNAL'] = macd_test['Signal Line']
    indicators_test['ACROSS_BAND'] = bb_test['Middle Band']
    indicators_test['RSI'] = rsi_test['RSI']
    indicators_test = (indicators_test - indicators_test.mean(axis=0)
                       ) / indicators_test.std(axis=0)

    YBUY = 0.05
    YSELL = -0.05
    for (i_row, row), (i_future,
                       future) in zip(df_train.iterrows(),
                                      df_train.shift(-21).iterrows()):
        if (future[syms[0]] / row[syms[0]] - 1
            ) > YBUY:  # if 21 days return exceed YBUY, then BUY Decision=1
            indicators_train.ix[i_row, 'Decision'] = 1
        if (
                future[syms[0]] / row[syms[0]] - 1
        ) < YSELL:  # if 21 days return less than YSELL, then SELL Decision=-1
            indicators_train.ix[i_row, 'Decision'] = -1

    indicators_train = indicators_train.fillna(0)

    for (i_row, row), (i_future, future) in zip(df_test.iterrows(),
                                                df_test.shift(-21).iterrows()):
        if (future[syms[0]] / row[syms[0]] - 1
            ) > YBUY:  # if 21 days return exceed YBUY, then BUY Decision=1
            indicators_test.ix[i_row, 'Decision'] = 1
        if (
                future[syms[0]] / row[syms[0]] - 1
        ) < YSELL:  # if 21 days return less than YSELL, then SELL Decision=-1
            indicators_test.ix[i_row, 'Decision'] = -1

    indicators_test = indicators_test.fillna(0)

    #print indicators
    file_dir_train = os.path.join('orders', 'indicators_train.csv')
    file_dir_test = os.path.join('orders', 'indicators_test.csv')
    indicators_train.to_csv(file_dir_train, header=False, index=False)
    indicators_test.to_csv(file_dir_test, header=False, index=False)

    inf_train = open('orders/indicators_train.csv')
    inf_test = open('orders/indicators_test.csv')
    data_train = np.array(
        [map(float,
             s.strip().split(',')) for s in inf_train.readlines()])
    data_test = np.array(
        [map(float,
             s.strip().split(',')) for s in inf_test.readlines()])
    predY_list = [0] * data_test.shape[0]

    for i in range(10):  # bag 10 times
        learner = dt.DTclass(leaf_size=5, verbose=False)
        train_rows = int(round(1.0 * data_train.shape[0]))
        test_rows = int(round(1.0 * data_test.shape[0]))
        Xtrain = data_train[:train_rows, 0:-1]
        Ytrain = data_train[:train_rows, -1]
        Xtest = data_test[:train_rows, 0:-1]
        Ytest = data_test[:train_rows, -1]
        learner.addEvidence(Xtrain, Ytrain)  # training step
        predY = learner.query(Xtest)  # query
        predY_list = predY_list + predY
    predY = predY_list / 10.0

    prices_test['decision'] = predY
    #print prices_port
    orders = pd.DataFrame(columns=['Date', 'Symbol', 'Order', 'Shares'])
    holding = 0  # holding =200 long; holding=-200 short
    window = date.datetime(2008, 1, 1)  # window should more than 21
    for (i_row, row) in prices_test.iterrows():
        if row['decision'] < 0 and (i_row -
                                    window).days > 21 and holding > -200:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding)
            ]
            holding -= 200 + holding
            window = i_row
            plt.axvline(i_row, color='r')
        if row['decision'] > 0 and (i_row -
                                    window).days > 21 and holding < 200:
            orders.loc[len(orders)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding)
            ]  # max buy
            holding += 200 - holding
            window = i_row
            plt.axvline(i_row, color='g')

    file_dir = os.path.join('orders', 'orders_ML_test.csv')
    orders.to_csv(file_dir)

    of_ML_test = "./orders/orders_ML_test.csv"
    #of = "./orders/orders.csv"
    sv = 100000

    #calculate benchmark of testing data
    dfprices = pd.DataFrame(prices_test)
    dfprices['Cash'] = sv - 200 * dfprices.ix[0, 'AAPL']
    dfprices['Holding'] = 200
    dfprices['Stock values'] = dfprices['Holding'] * dfprices['AAPL']
    dfprices['Port_val'] = dfprices['Cash'] + dfprices['Stock values']
    dfprices['Benchmark'] = dfprices['Port_val'] / dfprices['Port_val'].ix[
        0, :]
    benchmark = dfprices['Benchmark']

    #calculate rule based on test period
    orders_test = pd.DataFrame(columns=['Date', 'Symbol', 'Order', 'Shares'])

    holding_RB = 0  # holding =200 long; holding=-200 short
    window_RB = date.datetime(2010, 1, 1)  # window should more than 21
    for (i_row, row), (i_prev, prev) in zip(df_test.iterrows(),
                                            df_test.shift(1).iterrows()):
        if prev['MACD Line'] >= 0 and row['MACD Line'] < 0 and (
                i_row - window_RB).days > 21 and holding_RB > -200:
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding_RB)
            ]
            holding_RB -= 200 + holding_RB
            window_RB = i_row
        if prev['MACD Line'] <= 0 and row['MACD Line'] > 0 and (
                i_row - window_RB).days > 21 and holding_RB < 200:
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding_RB)
            ]  # max buy
            holding_RB += 200 - holding_RB
            window_RB = i_row
        if prev['MACD Line'] >= prev['Signal Line'] and row['MACD Line'] < row[
                'Signal Line'] and (
                    i_row - window_RB
                ).days > 21 and holding_RB > -200 and row['RSI'] > 70:
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding_RB)
            ]
            holding_RB -= 200 + holding_RB
            window_RB = i_row
        if prev['MACD Line'] <= prev['Signal Line'] and row['MACD Line'] > row[
                'Signal Line'] and (i_row -
                                    window_RB).days > 21 and holding_RB < 200:
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding_RB)
            ]  # max buy
            holding_RB += 200 - holding_RB
            window_RB = i_row

        if prev[syms[0]] <= prev['Lower Band'] and row[
                syms[0]] > row['Lower Band'] and (
                    i_row - window_RB
                ).days > 21 and holding_RB < 200:  # cross up Lower Band
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding_RB)
            ]  # max buy
            holding_RB += 200 - holding_RB
            window_RB = i_row
        if prev[syms[0]] <= prev['Middle Band'] and row[
                syms[0]] > row['Middle Band'] and (
                    i_row - window_RB).days > 21 and holding_RB > -200 and row[
                        'RSI'] > 70:  # cross up Middle Band
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'SELL',
                str(200 + holding_RB)
            ]
            holding_RB -= 200 + holding_RB
            window_RB = i_row
        # if prev[syms[0]] >= prev['Upper Band'] and row[syms[0]] < row['Upper Band'] and (i_row-window).days>21 and holding>-200 and row['RSI']>70: # cross down Upper Band
        #     orders.loc[len(orders)] = [i_row, syms[0], 'SELL', str(200+holding)]
        #     holding -= 200+holding
        #     window = i_row
        #     plt.axvline(i_row, color='r')
        if prev[syms[0]] >= prev['Middle Band'] and row[
                syms[0]] < row['Middle Band'] and (
                    i_row - window_RB
                ).days > 21 and holding_RB < 200:  # cross down Middle Band
            orders_test.loc[len(orders_test)] = [
                i_row, syms[0], 'BUY',
                str(200 - holding_RB)
            ]
            holding_RB += 200 - holding_RB
            window_RB = i_row

    file_dir_RB_test = os.path.join('orders', 'orders_RB_test.csv')
    orders_test.to_csv(file_dir_RB_test)

    portvals_RB = compute_portvals(start_date=date.datetime(2010, 1, 1),
                                   end_date=date.datetime(2011, 12, 31),
                                   orders_file=file_dir_RB_test,
                                   start_val=sv)
    portvals_RB = portvals_RB / portvals_RB.ix[0, :]
    benchmark = benchmark / benchmark.ix[0, :]
    portvals_ML = compute_portvals(start_date=date.datetime(2010, 1, 1),
                                   end_date=date.datetime(2011, 12, 31),
                                   orders_file=of_ML_test,
                                   start_val=sv)
    portvals_ML = portvals_ML / portvals_ML.ix[0, :]
    prices_test = prices_test / prices_test.ix[0, :]
    plt.plot(prices_test.index,
             portvals_RB,
             label='Rule-based portfolio',
             color='b')
    plt.plot(prices_test.index,
             portvals_ML,
             label='ML-based portfolio Out of Sample',
             color='g')
    #plt.plot(prices_port.index, prices_port, label='APPL', color='m')
    plt.plot(benchmark.index, benchmark, label='Benchmark', color='k')
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.legend(loc='upper left')
    plt.show()
Esempio n. 16
0
def application():

    if request.method == 'POST':

        new_request = request.get_json(force=True)
        new_ticker = get_ticker(new_request)

        private_client = AuthenticatedClient(Data.API_Public_Key,
                                             Data.API_Secret_Key,
                                             Data.Passphrase)

        new_order = Order(private_client)
        position = OpenPosition(new_order)
        funds = Capital(private_client)

        indicator = Indicator()
        macd = MACD()

        candle_ticker: str
        stop_time: int
        candle_gra = int

        if position.get_position() and last_instance():

            candle_ticker = new_order.get_key("product_id")
            stop_time = get_time(27976)
            candle_gra = 300

            writer = open(Data.Time, "w")
            writer.write(str(time.time() + 5.0))
            writer.close()

        elif position.get_position() is False:

            candle_ticker = new_ticker
            stop_time = get_time(83925)
            candle_gra = 900

        try:
            indicator.set_candles(product=candle_ticker,
                                  callback=stop_time,
                                  begin=get_time(0),
                                  granularity=candle_gra)
        except ValueError as ve:
            print(ve.with_traceback())
        except NameError as ne:
            print(ne.with_traceback())

        indicator_list = [indicator, macd]
        try:
            for value in indicator_list:
                value.candles = indicator.candles
                value.set_indicator()
                value.set_dates()
        except Exception as e:
            print(e.with_traceback())

        indicator_5m = Indicator()
        macd_5m = MACD()

        if position.get_position() is False:

            if macd.hist[-1] > macd.hist[-2]:
                try:
                    indicator_5m.set_candles(product=new_ticker,
                                             callback=get_time(27976),
                                             begin=get_time(0),
                                             granularity=900)
                except ValueError as ve:
                    print(ve.with_traceback())
        else:
            indicator_5m = indicator
            macd_5m = macd

        volume_5m = VolSMA(timeperiod=20)
        bands2dev_5m = BB()
        bands1dev_5m = BB(ndbevup=1, nbdevdn=1)
        rsi_5m = RSI()
        ema_5m = EMA()
        momentum_5m = Momentum()

        indicators = [
            indicator_5m, macd_5m, volume_5m, bands1dev_5m, bands2dev_5m,
            rsi_5m, ema_5m, momentum_5m
        ]

        try:
            for value in indicators:
                value.candles = indicator_5m.candles
                value.set_indicator()
                value.set_dates()
        except Exception as e:
            print(e.with_traceback())

        strategy_5m = Strategy(indicator_5m, macd_5m, bands1dev_5m,
                               bands2dev_5m, volume_5m, rsi_5m, ema_5m,
                               new_order)

        try:
            strategy_5m.strategy(-1)
        except Exception as e:
            print(e.with_traceback())

        trade_side: str
        trade_product: str
        trade_funds: str

        if (new_order.is_bottom()) and (position.get_position() is False):
            trade_side = "buy"
            trade_product = new_ticker
            trade_funds = funds.get_capital()
        elif (new_order.is_top()) and (position.get_position()):
            trade_side = "sell"
            trade_product = new_order.get_key("product_id")
            trade_funds = get_size(trade_product,
                                   new_order.get_key("filled_size"))

        try:
            new_trade = private_client.place_market_order(
                product_id=trade_product, side=trade_side, funds=trade_funds)
            writer = open(Data.Path, "w")
            writer.write(new_trade['id'])
            writer.close()
            writer = open(Data.Time, "w")
            writer.write(new_trade['done_at'])
            writer.close()
        except NameError as ne:
            print(ne.with_traceback())
        except KeyError as ke:
            print(ke.with_traceback())

        return 'success', 200

    elif request.method == 'GET':
        return redirect('http://3.218.228.129/login')

    else:
        abort(400)
Esempio n. 17
0
    #Get full dataset
    dfPrices = get_data_google(
        stockSym, pd.date_range(before_start_date, end_sample_date))
    dfSPY = dfPrices[['SPY']]
    dfPrices = dfPrices[stockSym]

    #Normalize Price
    priceNormed = dfPrices / dfPrices.ix[0]
    SPYNormed = dfSPY / dfSPY.ix[0]

    #Call indicators
    topBand, bottomBand, bbValue, bbp = Bollinger_Bands(
        stockSym, dfPrices, windowSize)
    RSIValueSMA, RSIValueEWM = RSI(stockSym, dfPrices, windowSize)
    MACDValue, signal, MACDIndicator = MACD(stockSym, dfPrices)

    SPYInd = SPYIndicator(dfSPY)

    #Standardize
    bbp = (bbp - bbp.mean()) / (bbp.std())
    RSIValueEWM = (RSIValueEWM - RSIValueEWM.mean()) / (RSIValueEWM.std())
    MACDValue = (MACDValue - MACDValue.mean()) / (MACDValue.std())
    signal = (signal - signal.mean()) / (signal.std())
    MACDIndicator = (MACDIndicator -
                     MACDIndicator.mean()) / (MACDIndicator.std())

    #5 Day Future Returns
    futureReturn = (dfPrices / dfPrices.shift(windowSize)) - 1.0
    futureReturn = futureReturn.shift(-windowSize)