def testPolicy(symbol="JPM",
               sd=dt.datetime(2008, 1, 1),
               ed=dt.datetime(2009, 12, 31),
               sv=100000):
    dates = pd.date_range(sd, ed)
    a = []
    a.append(symbol)
    prices_all = get_data(a, dates)
    prices = prices_all[symbol]
    prices_SPY = prices_all['SPY']
    orders = []
    sma = indicators.rolling_mean(prices, 10, True)
    ub, lb = indicators.bollinger_bands(prices, 10, True)
    cross, macd = indicators.macd(prices, True)
    rsi, ind = indicators.rsi(pd.DataFrame(prices), 10, True)

    orders = []
    holdings = 0

    for i in prices.index:
        if ind.loc[i][0] == 1 and cross.loc[i][0] == 1 and ub.loc[i] == 1 and (
                holdings == 0 or holdings == 1000):
            orders.append((i, symbol, 'SELL', 1000))
            holdings -= 1000
        elif ind.loc[i][0] == -1 and cross.loc[i][0] == -1 and lb.loc[
                i] == 1 and (holdings == 0 or holdings == -1000):
            orders.append((i, symbol, 'BUY', 1000))
            holdings += 1000

    orders = pd.DataFrame(orders)
    orders.columns = ['Date', 'Symbol', 'Order', 'Shares']
    orders['Date'] = orders['Date'].apply(lambda x: x.strftime('%Y-%m-%d'))

    return orders
Ejemplo n.º 2
0
def historicalData(company):
    if request.method == 'GET':
        tableName = "historical_data_" + company
        startDate = request.args.get('from')
        endDate = request.args.get('to')
        print('start date and end date: ',startDate, endDate)
        if startDate == '' or endDate =='':
            db = mysql.connector.connect(**config)
            cursor = db.cursor()
            sql = "SELECT id, DATE_FORMAT(time, '%Y-%m-%d')  AS time, open, high, low, close,volume FROM " + tableName
            cursor.execute(sql)
            records = cursor.fetchall()
            cursor.close()
            db.close()
            return json.dumps(records)
        else:
            start = datetime.datetime.strptime(startDate, '%Y-%m-%d')
            end = datetime.datetime.strptime(endDate, '%Y-%m-%d')
            indStart = start - datetime.timedelta(days=90)
            db = mysql.connector.connect(**config)
            cursor = db.cursor()

            # count number of rows for Start
            sql = "SELECT COUNT(*) as cnt " + \
                  "FROM %s " % (tableName) + \
                  "WHERE DATE(time) BETWEEN DATE('%s') AND DATE('%s') order by time asc" % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
            cursor.execute(sql)
            daysDiff = cursor.fetchall()[0][0]

            sql = "SELECT DATE_FORMAT(time, '%Y-%m-%d')  AS time, close " + \
                  "FROM %s " % (tableName) + \
                  "WHERE DATE(time) BETWEEN DATE('%s') AND DATE('%s') order by time asc" % (indStart.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
            cursor.execute(sql)
            records = cursor.fetchall()
            cursor.close()
            db.close()

            dates = [record[0] for record in records]
            prices = [record[1] for record in records]
            macd = indicators.macd(prices)
            rsi = indicators.rsiFunc(prices)
            movingAvgShort = indicators.moving_avg(prices, period=5)
            movingAvgLong = indicators.moving_avg(prices, period=50)

            dates = dates[-daysDiff:]
            prices = prices[-daysDiff:]
            macd = macd[-daysDiff:]
            rsi = rsi[-daysDiff:]
            movingAvgShort = movingAvgShort[-daysDiff:]
            movingAvgLong = movingAvgLong[-daysDiff:]

            stockData = {"dates": dates,
                         "prices": prices,
                         "macd": macd,
                         "rsi": rsi,
                         "movingAvgShort": movingAvgShort,
                         "movingAvgLong": movingAvgLong}

            return json.dumps(stockData)
Ejemplo n.º 3
0
def addIndicators(s):
    stks = deepcopy(s)
    #stks['trade_count'] = 0
    #stks['signal'] = ''
    #stks['DailyReturn'] = ''
    #stks['DailyReturn'] = stks['DailyReturn'].apply(list)
    for ticker, stk in stks.items():
        print('Adding indicators for', ticker)
        df = stk['data']
        df['Stoch'] = ind.stochasticOsc(df)['%K']
        df['MACD'] = ind.macd(df)['MACD']
        df['MacdSignal'] = ind.macd(df)['Signal']
        df['ATR'] = ind.atr(df, period=60)['ATR']
        df.dropna(inplace=True)
        df.reset_index(drop=True, inplace=True)
        signal[ticker] = ''
        pct_change[ticker] = [0]
        trade_count[ticker] = 0
    return stks
Ejemplo n.º 4
0
def main():
    otp_data = pd.read_csv('data.csv')

    indicators = [
        ind.macd(otp_data),
    ]

    plotter = plt.plotter(otp_data)

    for i in indicators:
        plotter.add_plot(i)

    plotter.plot()
Ejemplo n.º 5
0
def update(path):
    df = pd.read_csv(path)

    df = indicators.moving_average(df, 15)
    df = indicators.exponential_moving_average(df, 30)
    df = indicators.relative_strength_index(df, 14)
    df = indicators.macd(df, 12, 26)

    # print(df)
    df = df.astype(float)

    f = lambda x: mdates.date2num(datetime.datetime.fromtimestamp(x))
    df['Date'] = df['Date'].apply(f)

    # print(df['Date'])
    return df
Ejemplo n.º 6
0
    def macd(data: list, data_properties: dict) -> dict:
        """
        Strategy for Moving Average Convergence/Diversion.
        :param data: list[numeric]
        :param data_properties: dict
        :return: dict
                Result from strategy_builder
        """
        close = data_parser.get_close(data)
        macd = indicators.macd(close)
        macd_series = macd[Keys.macd_value]
        macd_signal = macd[Keys.macdsignal]
        buy = Condition(data1=macd_series, data2=macd_signal, operation=Operation.CROSSOVER)
        sell = Condition(data1=macd_series, data2=macd_signal, operation=Operation.CROSSUNDER)
        chart_1 = ChartElement(data=macd, label="macd", chart_type=ChartType.LINE, plot=ChartAxis.DIFFERENT_AXIS)
        charts = [chart_1]
        result = strategy_builder(data_properties=data_properties, data_list=data, strategy=BUY, buy=buy, sell=sell,
                                  charts=charts)

        return result
Ejemplo n.º 7
0
def get_discretized_indicators(sd, ed, symbol):
    prices, _ = get_df_prices(sd, ed, symbol)

    # EMA 2 States: Price <= EMA: 0, Price > EMA: 1
    ema_20 = indicators.ema(sd, ed, symbol, window_size=20)
    ema_30 = indicators.ema(sd, ed, symbol, window_size=30)
    ema_50 = indicators.ema(sd, ed, symbol, window_size=50)

    ema_20 = (prices > ema_20) * 1
    ema_30 = (prices > ema_30) * 1
    ema_50 = (prices > ema_50) * 1

    # MACD 2 States: MACD <= Signal: 0, MACD > Signal: 1
    macd_raw, macd_signal = indicators.macd(sd, ed, symbol)
    macd = (macd_raw > macd_signal) * 1

    # TSI 2 States: TSI <= 0: 0, TSI > 0: 1
    tsi = indicators.tsi(sd, ed, symbol)
    tsi = (tsi > 0) * 1

    return ema_20, ema_30, ema_50, macd, tsi
Ejemplo n.º 8
0
def main(argv):
    i = 0
    prices = []
    parser = OptionParser()
    try:
        parser.add_option("-p", "--period", dest="periods")
        #parser.add_option("-h",  dest="help")
        (opts, args) = parser.parse_args(argv)
    except OptionParser.OptionError:
        print("trading_bot.py parse_error")
        sys.exit(2)
    arg = float(opts.periods)
    if (arg in [1, 10, 60, 300, 900, 1800, 7200, 14400, 86400]):
        period = arg
    else:
        print(
            'Required periods in 1,10,60,300,900,1800,7200,14400, or 86400 second increments'
        )
        sys.exit(2)
    macd = 0.
    while True:
        data = conn.request("ticker", query=ticker, json=False)
        prices.append(float(data["bid"][0]))
        if len(prices) >= 30:
            (slow, fast, temp) = ind.macd(prices)
            print(
                str(datetime.datetime.now()) +
                "\n Period: %ss \n Pair: %s \n Ask: %s \n Bid: %s \n Macd: %s"
                % (period, pair, data["ask"][0], data["bid"][0], temp))
            if temp * macd < 0:
                if temp > 0:
                    print("BUY" + " \n Ancient macd: %s \n Nouveau macd: %s" %
                          (macd, temp))
                else:
                    print("SELL" + " \n Ancient macd: %s \n Nouveau macd: %s" %
                          (macd, temp))
            macd = temp
        print("\n next")
        time.sleep(period)
def add_dataframe_indicators(stock_frame: pd.DataFrame) -> pd.DataFrame:
    """Adds the different technical indicator to our DataFrame.

    ### Parameters
    ----------
    stock_frame : pd.DataFrame
        A pandas DataFrame with the Open, Close,
        High, and Low price.

    ### Returns
    -------
    pd.DataFrame
        A Pandas DataFrame with the indicators added
        to the data frame.
    """

    # Add the MACD Indicator.
    stock_frame = macd(stock_frame=stock_frame, fast_period=12, slow_period=26)

    # Add the EMA Indicator.
    stock_frame = ema(stock_frame=stock_frame, period=50, alpha=1.0)

    return stock_frame
    data2.set_value(index, 'bol_bands_upper', data3.at[int(row['row_index']),
                                                       'bol_bands_upper'])
    data2.set_value(index, 'bol_bands_lower', data3.at[int(row['row_index']),
                                                       'bol_bands_lower'])

#data2.plot(x='row_index',y=['ema5','CLOSE'])
#data2.plot(x='row_index',y=['ema5','CLOSE'])
#data2['money_flow_index','ema5','CLOSE'].plot()
#data2['ema5','CLOSE'].plot()
#data2.plot(subplots=True, figsize=(6, 6));
#data2.plot(legend=False)
#plt.legend(loc='best')
#plt.show()
"""
#grap = data2.drop(['OPEN', 'LOW', 'HIGH'])
graph = data2
plt.figure()
#ax.right_ax.set_ylabel('CLOSE')
#graph.plot(legend=False)
ax = graph.plot(secondary_y=['money_flow_index'])
#ax.set_ylabel('money_flow_index')
ax.right_ax.set_ylabel('mfi')
plt.show()

"""
#macd
macd(data2, period_long=26, period_short=12, period_signal=9, column='CLOSE')

#print data2
data2.to_csv('sample.csv')
Ejemplo n.º 11
0
    def testPolicy(self, symbol, sd, ed, sv):

        # setting up
        symbol = symbol[0]
        df = get_data([symbol], pd.date_range(sd, ed))
        df_price = df[[symbol]]
        df_price = df_price.ffill().bfill()
        normalized_df_price = df_price[symbol] / df_price[symbol][0]

        df_trades = df[['SPY']]
        df_trades = df_trades.rename(columns={
            'SPY': symbol
        }).astype({symbol: 'int32'})
        df_trades[:] = 0
        dates = df_trades.index

        # getting indicators
        ema_20 = indicators.ema(sd, ed, symbol, plot=True, window_size=20)
        macd_raw, macd_signal = indicators.macd(sd, ed, symbol, plot=True)
        tsi = indicators.tsi(sd, ed, symbol, plot=True)

        # current_cash = sv
        current_position = 0
        last_action = 0

        # making trades
        for i in range(len(dates)):
            today = dates[i]
            last_action += 1

            # EMA_20 Vote
            normalized_df_price_today = normalized_df_price.loc[today]
            ema_20_today = ema_20.loc[today]
            # if normalized_df_price_today > ema_20_today + 0.1:
            if normalized_df_price_today > ema_20_today:
                ema_vote = 1
            # elif normalized_df_price_today < ema_20_today - 0.1:
            elif normalized_df_price_today < ema_20_today:
                ema_vote = -1
            else:
                ema_vote = 0

            # MACD Vote
            macd_raw_today = macd_raw.loc[today].loc[symbol]
            macd_signal_today = macd_signal.loc[today].loc[symbol]
            # if macd_signal_today > macd_raw_today + 0.2:
            if macd_signal_today > macd_raw_today:
                macd_vote = 2
            # elif macd_signal_today < macd_raw_today - 0.2:
            elif macd_signal_today < macd_raw_today:
                macd_vote = -10
            else:
                macd_vote = 1

            # TSI vote
            tsi_today = tsi.loc[today].loc[symbol]
            # if tsi_today > 0.05:
            if tsi_today > 0.1:
                tsi_vote = 1
            # elif tsi_today < -0.05:
            elif tsi_today < 0.1:
                tsi_vote = -1
            else:
                tsi_vote = 0

            # pooling the votes
            pool = macd_vote + tsi_vote + ema_vote
            if pool >= 3:
                # long
                action = 1000 - current_position

            elif pool <= -3:
                # short
                action = -1000 - current_position
            else:
                # out
                action = -current_position

            # don't trade too frequent
            if last_action >= 3:
                df_trades.loc[dates[i]].loc[symbol] = action
                current_position += action
                last_action = 0

        return df_trades
Ejemplo n.º 12
0
 def calculateIndicators(self, b=20, r=14, m=[12, 26, 9]):
     self.bb, self.s, self.l, self.h = bollinger(self.value, b)
     self.macd, self.signal, self.hist = macd(self.value, m[0], m[1], m[2])
     self.rsi = rsi(self.open, self.close, r)
     self.bbn = bollingernormalized(self.value, self.bb, self.s)
Ejemplo n.º 13
0
def Function_for_file_generation():
    global specific_insts
    global ggpath
    ggpath = gpath
    debug = False
    if not debug:
        pd.set_option('display.max_columns', 50)
        pd.set_option('display.width', 1000)
    # user_input=input("Do You Want Select Instrument Manually:y or n")
    # man_inst_names=None
    # if user_input=="y":
    #     man_inst_names=input("Enter Instrument names Separated by Space:")
    #     man_inst_names = man_inst_names.split()

    instruments = INSTRUMENTS
    # # instruments = ['AUD_CAD',]
    # # instruments = ['AUD_CHF',]
    # instruments = ['AUD_CHF', 'AUD_CAD']
    # instruments = ['AUD_CAD',]
    #data = get_file_data()
    stock = get_oanda_api(instruments, granularity='D')

    # print (stock['AUD_CAD'])

    # stock = get_ig_api(instruments)
    # print (stock['AUD_CAD'])

    # instruments = data['Close'].columns.values
    # # Initialize all assign all instrument data to dataframes
    # stock = {}
    # for instrument in instruments:
    #     values = {}
    #     for key in COLUMNS:
    #         values[key] = data.get(key, {}).get(instrument)
    #     values['Date'] = data.get('Date').iloc[:len(values[key]), 0]
    #     stock[instrument] = pd.DataFrame(values, columns=COLUMNS)

    # print(stock[SELECTED_INSTRUMENT])
    # return
    # Calculate the MACD, RSI and Profit and Loss for all instrument paid
    # Also, Calculate the MACD, RSI and Profit and Loss percentile for all
    # instruments
    instruments_list = []
    CCI_list = []
    dic_for_all_cci = {}

    for instrument in instruments:

        nsize = len(stock[instrument]['Close'])

        # Calculate MACD
        stock[instrument] = stock[instrument].join(
            macd(stock[instrument]['Close']))

        # Calculate RSI for n = 14
        stock[instrument] = stock[instrument].join(
            rsi(stock[instrument]['Close']))

        #changeInPrice
        stock[instrument]["Change In Price"] = change_in_price(
            stock[instrument]["Close"].values)

        # Calculate Profile and Loss
        stock[instrument] = stock[instrument].join(
            pnl(stock[instrument]['Close']))
        # Calculate MACD Percentile
        stock[instrument] = stock[instrument].join(
            macd_percentile(stock[instrument]['MACD']))
        # Calculate RSI Percentile
        stock[instrument] = stock[instrument].join(
            rsi_percentile(stock[instrument]['RSI']))
        # Calculate  Profile and Loss Percentile
        stock[instrument] = stock[instrument].join(
            pnl_percentile(stock[instrument]['Profit/Loss']))

        #Calculate CCI
        high = stock[instrument]["High"].values
        close = stock[instrument]["Close"].values
        low = stock[instrument]["Low"].values
        #create instrument dataframe
        ccis = talib.CCI(high, low, close, timeperiod=14)
        #ccis=list(ccis)
        instruments_list.append(instrument)
        CCI_list.append(ccis[-1])
        dic_for_all_cci[instrument] = ccis
        stock[instrument]["CCI"] = ccis

        # Calculate Divergence factor 1 and 2
        stock[instrument] = stock[instrument].join(
            pd.Series((stock[instrument]['MACD Percentile'] + 0.1 -
                       stock[instrument]['RSI Percentile']) / 2.0,
                      name='Divergence Factor 1'))
        stock[instrument] = stock[instrument].join(
            pd.Series(stock[instrument]['Divergence Factor 1'] -
                      stock[instrument]['PNL Percentile'],
                      name='Divergence Factor 2'))

        # Calculate Divergence factor 3
        n = 19
        for i in range(nsize):
            stock[instrument].loc[i:nsize, 'Macd_20'] = (
                stock[instrument]['MACD'].iloc[i] -
                stock[instrument]['MACD'].iloc[i - n])
            stock[instrument].loc[i:nsize, 'Prc_20'] = (
                (stock[instrument]['Close'].iloc[i] -
                 stock[instrument]['Close'].iloc[i - n])
            ) / stock[instrument]['Close'].iloc[i - n]
            stock[instrument].loc[i:nsize, 'Divergence Factor 3'] = (
                stock[instrument]['Macd_20'].iloc[i] /
                stock[instrument]['Close'].iloc[i]
            ) - stock[instrument]['Prc_20'].iloc[i]

        stock[instrument] = stock[instrument].join(
            rsi(stock[instrument]['Close'], 20, name='RSI_20'))

        # Calculate the momentum factors
        stock[instrument] = stock[instrument].join(
            pnl_n(stock[instrument]['Close'], 10))
        stock[instrument] = stock[instrument].join(
            pnl_n(stock[instrument]['Close'], 30))

        stock[instrument]['Close_fwd'] = stock[instrument]['Close'].shift(-2)
        stock[instrument].loc[
            -1:nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-1]
        stock[instrument].loc[
            -2:nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-2]

        stock[instrument] = stock[instrument].join(
            macd(stock[instrument]['Close_fwd'], name='MACD_fwd'))
        n = 19
        stock[instrument] = stock[instrument].join(
            pd.Series(stock[instrument]['MACD_fwd'].diff(n) -
                      stock[instrument]['MACD'],
                      name='M_MACD_CHANGE'))

        stock[instrument] = stock[instrument].join(
            rsi(stock[instrument]['Close_fwd'], n=20, name='RSI_20_fwd'))
        stock[instrument] = stock[instrument].join(
            pd.Series(stock[instrument]['RSI_20_fwd'] -
                      stock[instrument]['RSI_20'],
                      name='M_RSI_CHANGE'))

        # Calculate the ADX, PDI & MDI
        _adx, _pdi, _mdi = adx(stock[instrument])

        stock[instrument] = stock[instrument].join(_adx)
        stock[instrument] = stock[instrument].join(_pdi)
        stock[instrument] = stock[instrument].join(_mdi)

        # Calculate the Moving Averages: 5, 10, 20, 50, 100
        for period in [5, 10, 20, 50, 100]:
            stock[instrument] = stock[instrument].join(
                moving_average(stock[instrument]['Close'],
                               period,
                               name=f'{period}MA'))

        # Calculate the Williams PCTR
        stock[instrument] = stock[instrument].join(williams(stock[instrument]))

        # Calculate the Minmax Range
        n = 17
        for i in range(nsize):
            maxval = stock[instrument]['High'].iloc[i - n:i].max()
            minval = stock[instrument]['Low'].iloc[i - n:i].min()
            rng = abs(maxval) - abs(minval)
            # where is the last price in the range of minumimn to maximum
            pnow = stock[instrument]['Close'].iloc[i - n:i]
            if len(pnow.iloc[-1:i].values) > 0:
                whereinrng = (
                    (pnow.iloc[-1:i].values[0] - abs(minval)) / rng) * 100.0
                stock[instrument].loc[i:nsize, 'MinMaxPosition'] = whereinrng
                stock[instrument].loc[i:nsize, 'High_Price(14)'] = maxval
                stock[instrument].loc[i:nsize, 'Low_Price(14)'] = minval

        stock[instrument]['Divergence factor Avg'] = (
            stock[instrument]['Divergence Factor 1'] +
            stock[instrument]['Divergence Factor 2'] +
            stock[instrument]['Divergence Factor 3']) / 3.0

        stock[instrument]['Momentum Avg'] = (
            stock[instrument]['M_MACD_CHANGE'] +
            stock[instrument]['M_RSI_CHANGE'] +
            stock[instrument]['Profit/Loss_10'] +
            stock[instrument]['Profit/Loss_30']) / 4.0

        df_instrument = pd.DataFrame()
        df_instrument["Open"] = stock[instrument]["Open"]
        df_instrument["High"] = stock[instrument]['High']
        df_instrument["Low"] = stock[instrument]['Low']
        df_instrument["Close"] = stock[instrument]['Close']
        df_instrument["Volume"] = stock[instrument]['Volume']
        df_instrument["Price"] = stock[instrument]['Close']
        df_instrument["Change In Price"] = change_in_price(
            stock[instrument]['Close'].values)
        df_instrument["CCI"] = stock[instrument]['CCI']
        df_instrument["PNL Percentile"] = stock[instrument]['PNL Percentile']
        df_instrument["Divergence Factor 1"] = stock[instrument][
            'Divergence Factor 1']
        df_instrument["Divergence Factor 2"] = stock[instrument][
            'Divergence Factor 2']
        df_instrument["Divergence Factor 3"] = stock[instrument][
            'Divergence Factor 3']

        df_instrument["Momentum Factor 1"] = stock[instrument]["M_MACD_CHANGE"]
        df_instrument["Momentum Factor 2"] = stock[instrument]['M_RSI_CHANGE']
        df_instrument["Momentum Factor 3"] = stock[instrument][
            'Profit/Loss_10']
        df_instrument["Momentum Factor 4"] = stock[instrument][
            'Profit/Loss_30']

        df_instrument["RSI"] = stock[instrument]["RSI"]
        df_instrument["MACD"] = stock[instrument]["MACD"]
        df_instrument["WPCTR"] = stock[instrument]["Williams PCTR"]
        df_instrument["pdi"] = stock[instrument]["pdi"]
        df_instrument["mdi"] = stock[instrument]["mdi"]
        df_instrument["adx"] = stock[instrument]["adx"]
        #df_instrument= df_instrument[pd.notnull(df_instrument['CCI'])]
        df_instrument = df_instrument.dropna(how="any")
        df_instrument["CCI Percentile"] = cci_percentile(df_instrument["CCI"])
        df_instrument["Divergence Factor 4"] = df_instrument[
            "CCI Percentile"] - df_instrument["PNL Percentile"]
        df_instrument['Divergence Factor 1 Rank'] = rank_formulation(
            df_instrument['Divergence Factor 1'].values)
        df_instrument['Divergence Factor 2 Rank'] = rank_formulation(
            df_instrument['Divergence Factor 2'].values)
        df_instrument['Divergence Factor 3 Rank'] = rank_formulation(
            df_instrument['Divergence Factor 3'].values)
        df_instrument['Divergence Factor 4 Rank'] = rank_formulation(
            df_instrument['Divergence Factor 4'].values)
        df_instrument['DF Avg Rank'] = (
            df_instrument['Divergence Factor 1 Rank'] +
            df_instrument['Divergence Factor 2 Rank'] +
            df_instrument['Divergence Factor 3 Rank'] +
            df_instrument['Divergence Factor 4 Rank']) / 4.0

        df_instrument['Momentum Factor 1 Rank'] = rank_formulation(
            df_instrument['Momentum Factor 1'].values)
        df_instrument['Momentum Factor 2 Rank'] = rank_formulation(
            df_instrument['Momentum Factor 2'].values)
        df_instrument['Momentum Factor 3 Rank'] = rank_formulation(
            df_instrument['Momentum Factor 3'].values)
        df_instrument['Momentum Factor 4 Rank'] = rank_formulation(
            df_instrument['Momentum Factor 4'].values)
        df_instrument['MF Avg Rank'] = (
            df_instrument['Momentum Factor 1 Rank'] +
            df_instrument['Momentum Factor 2 Rank'] +
            df_instrument['Momentum Factor 3 Rank'] +
            df_instrument['Momentum Factor 4 Rank']) / 4.0

        df_instrument["% Rank of DF Avgs"] = rank_formulation(
            df_instrument['DF Avg Rank'].values)
        df_instrument["% Rank of MF Avgs"] = rank_formulation(
            df_instrument['MF Avg Rank'].values)
        df_instrument = df_instrument[[
            'Open',
            'High',
            'Low',
            'Close',
            'Volume',
            'Price',
            'Change In Price',
            'Divergence Factor 1',
            'Divergence Factor 2',
            'Divergence Factor 3',
            'Divergence Factor 4',
            'DF Avg Rank',
            '% Rank of DF Avgs',
            'Divergence Factor 1 Rank',
            'Divergence Factor 2 Rank',
            'Divergence Factor 3 Rank',
            'Divergence Factor 4 Rank',
            'Momentum Factor 1',
            'Momentum Factor 2',
            'Momentum Factor 3',
            'Momentum Factor 4',
            'Momentum Factor 1 Rank',
            'Momentum Factor 2 Rank',
            'Momentum Factor 3 Rank',
            'Momentum Factor 4 Rank',
            'MF Avg Rank',
            '% Rank of MF Avgs',
            'RSI',
            'MACD',
            'WPCTR',
            'CCI',
            'CCI Percentile',
            'PNL Percentile',
            'pdi',
            'mdi',
            'adx',
        ]]
        df_instrument.to_csv(gpath + "all_folders/" + instrument + ".csv")

    ccis_df = pd.DataFrame(dic_for_all_cci)
    cci_percentile_list = []
    dic = {"Instrument": instruments_list, "CCI": CCI_list}
    new_df = pd.DataFrame(dic)
    cci_percentile_list = cci_percentile(new_df["CCI"]).to_list()

    #sys.exit()
    # calculate the aggregrate for each oeruod
    # calculate the Divergence_Macd_Prc_Rank

    for nrow in range(nsize):
        row = [
            stock[instrument]['Divergence Factor 3'].iloc[nrow]
            for instrument in instruments
        ]
        series = pd.Series(row).rank() / len(row)
        for i, instrument in enumerate(instruments):
            stock[instrument].loc[nrow:nsize,
                                  'Divergence_Macd_Prc_Rank'] = series.iloc[i]

    # calculate the Divergence and Momentum average rank
    indices = [instrument for instrument in instruments]
    columns = [
        'Price',
        "Change In Price",
        'Divergence Factor 1',
        'Divergence Factor 2',
        'Divergence Factor 3',
        'Divergence Factor 1 Rank',
        'Divergence Factor 2 Rank',
        'Divergence Factor 3 Rank',
        'M_MACD_CHANGE',
        'M_RSI_CHANGE',
        'Profit/Loss_10',
        'Profit/Loss_30',
        'M_MACD_CHANGE Rank',
        'M_RSI_CHANGE Rank',
        'Profit/Loss_10 Rank',
        'Profit/Loss_30 Rank',
        'MF Avg Rank',
        '% Rank of MF Avgs',
        'MinMaxPosition',
        'RSI',
        'WPCTR',
        'pdi',
        'mdi',
        'adx',
        'High_Price(14)',
        'Low_Price(14)',
        '5MA',
        '10MA',
        '20MA',
        '50MA',
        '100MA',
        "MACD",
        'PNL Percentile',
        "DF Avg Rank",
        "% Rank of DF Avgs",
    ]

    periods = []
    for i in range(nsize):

        period = []

        for instrument in instruments:

            period.append([
                stock[instrument]['Close'].iloc[i],
                stock[instrument]["Change In Price"].iloc[i],
                stock[instrument]['Divergence Factor 1'].iloc[i],
                stock[instrument]['Divergence Factor 2'].iloc[i],
                stock[instrument]['Divergence Factor 3'].iloc[i],
                None,
                None,
                None,
                stock[instrument]['M_MACD_CHANGE'].iloc[i],
                stock[instrument]['M_RSI_CHANGE'].iloc[i],
                stock[instrument]['Profit/Loss_10'].iloc[i],
                stock[instrument]['Profit/Loss_30'].iloc[i],
                None,
                None,
                None,
                None,
                None,
                None,
                stock[instrument]['MinMaxPosition'].iloc[i],
                stock[instrument]['RSI'].iloc[i],
                stock[instrument]['Williams PCTR'].iloc[i],
                stock[instrument]['pdi'].iloc[i],
                stock[instrument]['mdi'].iloc[i],
                stock[instrument]['adx'].iloc[i],
                stock[instrument]['High_Price(14)'].iloc[i],
                stock[instrument]['Low_Price(14)'].iloc[i],
                stock[instrument]['5MA'].iloc[i],
                stock[instrument]['10MA'].iloc[i],
                stock[instrument]['20MA'].iloc[i],
                stock[instrument]['50MA'].iloc[i],
                stock[instrument]['100MA'].iloc[i],
                stock[instrument]["MACD"].iloc[i],
                stock[instrument]['PNL Percentile'].iloc[i],
                None,
                None,
            ])
        df = pd.DataFrame(data=period, index=indices, columns=columns)
        df['Divergence Factor 1 Rank'] = rank_formulation(
            df["Divergence Factor 1"].values)
        df['Divergence Factor 2 Rank'] = rank_formulation(
            df["Divergence Factor 2"].values)
        df['Divergence Factor 3 Rank'] = rank_formulation(
            df["Divergence Factor 3"].values)

        df['Momentum Factor 1 Rank'] = rank_formulation(
            df['M_MACD_CHANGE'].values)
        df['Momentum Factor 2 Rank'] = rank_formulation(
            df['M_RSI_CHANGE'].values)
        df['Momentum Factor 3 Rank'] = rank_formulation(
            df['Profit/Loss_10'].values)
        df['Momentum Factor 4 Rank'] = rank_formulation(
            df['Profit/Loss_30'].values)

        df['MF Avg Rank'] = (
            df['Momentum Factor 1 Rank'] + df['Momentum Factor 1 Rank'] +
            df['Momentum Factor 1 Rank'] + df['Momentum Factor 1 Rank']) / 4.0
        df['% Rank of MF Avgs'] = rank_formulation(df['MF Avg Rank'].values)

        #df.to_excel("target_data.xlsx")
        periods.append(df)
    pnl_percentile_nparaay = np.array(df["PNL Percentile"].values)
    cci_percentile_nparray = cci_percentile_list
    divergent_factor_4 = cci_percentile_nparray - pnl_percentile_nparaay
    df["CCI"] = CCI_list
    df["CCI Percentile"] = cci_percentile_list
    df["Divergence Factor 4"] = divergent_factor_4
    df['Divergence Factor 4 Rank'] = rank_formulation(
        df['Divergence Factor 1'].values)
    df['DF Avg Rank'] = (
        df['Divergence Factor 1 Rank'] + df['Divergence Factor 2 Rank'] +
        df['Divergence Factor 3 Rank'] + df['Divergence Factor 4 Rank']) / 4.0
    df["% Rank of DF Avgs"] = rank_formulation(df['DF Avg Rank'].values)
    df = df[[
        'Price',
        'Change In Price',
        'Divergence Factor 1',
        'Divergence Factor 2',
        'Divergence Factor 3',
        'Divergence Factor 4',
        'Divergence Factor 1 Rank',
        'Divergence Factor 2 Rank',
        'Divergence Factor 3 Rank',
        'Divergence Factor 4 Rank',
        'DF Avg Rank',
        '% Rank of DF Avgs',  #'Momentum Factor 1','Momentum Factor 2','Momentum Factor 3','Momentum Factor 4',
        #'Momentum Factor 1 Rank','Momentum Factor 2 Rank','Momentum Factor 3 Rank','Momentum Factor 4 Rank','MF Avg Rank', '% Rank of MF Avgs',
        'M_MACD_CHANGE',
        'M_RSI_CHANGE',
        'Profit/Loss_10',
        'Profit/Loss_30',
        'M_MACD_CHANGE Rank',
        'M_RSI_CHANGE Rank',
        'Profit/Loss_10 Rank',
        'Profit/Loss_30 Rank',
        'MF Avg Rank',
        '% Rank of MF Avgs',
        'MinMaxPosition',
        'RSI',
        'MACD',
        'WPCTR',
        'CCI',
        'CCI Percentile',
        'PNL Percentile',
        'pdi',
        'mdi',
        'adx',
        'High_Price(14)',
        'Low_Price(14)',
        '5MA',
        '10MA',
        '20MA',
        '50MA',
        '100MA'
    ]]
    df.to_excel(gpath + "all_folders/" + "target_data.xlsx")
    df.sort_values(by="% Rank of DF Avgs", inplace=True)
    df.to_excel(gpath + "all_folders/" + "ordered_target_data.xlsx")

    top5, last5 = instrument_selection_rules(df)
    specific_insts = None
    specific_insts = top5 + last5
    dflist1 = []
    if specific_insts is not None:
        '''dflist1=Graph_Plots_For_Individual_Instrument(specific_insts,False)
        dflist1.to_csv(gpath+"all_folders"+"/"+"ins_ind_flag.csv")
        dfDiverge=dflist.copy()  
        dfDiverge=dfDiverge.loc[dfDiverge['imp_var'].isin(['Divergence Factor 1','Divergence Factor 2','Divergence Factor 3','Divergence Factor 4'])]
        dfDiverge.reset_index(drop=True,inplace=True)
        dfDiverge.to_csv(gpath+"all_folders"+"/"+"selected_ins_ind_flag.csv")'''
        for rule_instrument in specific_insts:
            data = pd.read_csv(gpath + "all_folders/" + rule_instrument +
                               ".csv",
                               index_col="Date")
            data.to_csv(gpath + "rule_select_inst/" + rule_instrument + ".csv")
    def testPolicy(self,
                   symbol="AAPL",
                   sd=dt.datetime(2010, 1, 1),
                   ed=dt.datetime(2011, 12, 31),
                   sv=100000):
        stockDF = get_data([symbol], pd.date_range(sd, ed))
        stockDF = stockDF[symbol]
        stockDF = stockDF / stockDF.iloc[0]

        df_trades = pd.DataFrame(np.nan,
                                 columns=['Date', 'Symbol', 'Order', 'Shares'],
                                 index=stockDF.index)
        df_trades['Date'] = stockDF.index
        df_trades['Symbol'] = symbol
        df_trades['Shares'] = 0
        df_trades['Order'] = "HOLD"

        bbandDF = indicators.b_bands(stockDF, 20)
        macdDF = indicators.macd(stockDF)
        # posDF = indicators.priceoversma(stockDF, 20)
        exitflag = False
        enterflag = False
        # tsi = indicators.tsi(stockDF)

        currholdings = 0
        prev_date = sd
        prev_date = pd.to_datetime(str(prev_date))
        prev_date = prev_date.strftime('%Y-%m-%d')
        for i in stockDF.index.values:
            date = pd.to_datetime(str(i))
            date = date.strftime('%Y-%m-%d')
            price = stockDF.loc[date]
            upper = bbandDF.loc[date, 'UPPER']
            lower = bbandDF.loc[date, 'LOWER']
            macd = macdDF.loc[date]
            # t = tsi.loc[date]
            # pos = posDF.loc[date, 'PRICE/SMA']

            if price >= upper and exitflag == False:
                exitflag = True
            elif price <= upper and exitflag == True:
                exitflag = False
                if currholdings == 1000:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = -1000
            elif price <= lower and enterflag == False:
                enterflag = True
            elif price >= lower and enterflag == True:
                enterflag = False
                if currholdings == -1000:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = 1000
            if macd > 0 and macdDF.loc[prev_date] < 0:
                if currholdings == -1000:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = 1000
            elif macd < 0 and macdDF.loc[prev_date] > 0:
                if currholdings == 1000:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = -1000
            # if t > 0:
            #     if t > tsi[prev_date]:
            #         if currholdings == -1000:
            #             df_trades.loc[date, 'Order'] = 'BUY'
            #             df_trades.loc[date, "Shares"] = 2000
            #         if currholdings == 0:
            #             df_trades.loc[date, 'Order'] = 'BUY'
            #             df_trades.loc[date, "Shares"] = 1000
            #         currholdings = 1000
            # elif t < 0:
            #     if t < tsi[prev_date]:
            #             if currholdings == 1000:
            #                 df_trades.loc[date, 'Order'] = 'SELL'
            #                 df_trades.loc[date, "Shares"] = 2000
            #             if currholdings == 0:
            #                 df_trades.loc[date, 'Order'] = 'SELL'
            #                 df_trades.loc[date, "Shares"] = 1000
            #             currholdings = -1000

            prev_date = date

            # if pos > 1:
            #         if currholdings == -1000:
            #             df_trades.loc[date, 'Order'] = 'BUY'
            #             df_trades.loc[date, "Shares"] = 2000
            #         if currholdings == 0:
            #             df_trades.loc[date, 'Order'] = 'BUY'
            #             df_trades.loc[date, "Shares"] = 1000
            #         currholdings = 1000
            # if pos < 1:
            #         if currholdings == 1000:
            #             df_trades.loc[date, 'Order'] = 'SELL'
            #             df_trades.loc[date, "Shares"] = 2000
            #         if currholdings == 0:
            #             df_trades.loc[date, 'Order'] = 'SELL'
            #             df_trades.loc[date, "Shares"] = 1000
            #         currholdings = -1000

        buyDF = df_trades[df_trades['Order'] == 'BUY']
        sellDF = df_trades[df_trades['Order'] == 'SELL']
        df_trades2 = df_trades.copy()
        df_trades2['Order'] = "HOLD"
        df_trades2.iloc[0, 2] = "BUY"
        df_trades2.iloc[0, 3] = 1000
        benchmark = compute_portvals(df_trades2, start_val=100000)
        port_val = compute_portvals(df_trades, start_val=100000)
        print "Manual Strategy---------------------------------------"
        port_val_stats(port_val)
        print "Benchmark----------------------------------------------"
        port_val_stats(benchmark)
        port_val = port_val / port_val.iloc[0]
        benchmark = benchmark / benchmark.iloc[0]
        port_val = port_val.rename("Manual Strategy")
        benchmark = benchmark.rename("Benchmark")
        pv = port_val.plot(legend=True,
                           y="Cumulative Return",
                           x="Dates",
                           color="black")
        bm = benchmark.plot(ax=pv,
                            title="Cumulative Returns of " + str(symbol),
                            legend=True,
                            color="blue")
        plt.ylabel("Cumulative Return")
        plt.xlabel("Dates")
        for date in buyDF['Date']:
            plt.axvline(x=date, color="green")
        for date in sellDF['Date']:
            plt.axvline(x=date, color="red")
        plt.show()

        return df_trades
    def addEvidence(self, symbol = "IBM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,1,1), \
        sv = 10000):

        # example usage of the old backward compatible util function
        syms = [symbol]
        dates = pd.date_range(sd + dt.timedelta(-30), ed)
        prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        prices_SPY = prices_all['SPY']  # only SPY, for comparison later
        #if self.verbose: print prices

        # indicators
        cross, macd = indicators.macd(prices)
        rsi, ind = indicators.rsi(prices)
        sma = indicators.rolling_mean(prices)
        macd = macd[macd.index > sd]
        rsi = rsi[rsi.index > sd]
        sma = sma[sma.index > sd]

        prices = prices[prices.index > sd]

        #discretization
        macd_bin = pd.cut(macd.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                               4]).astype(int)
        rsi_bin = pd.cut(rsi.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                             4]).astype(int)
        sma_bin = pd.cut(sma.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                             4]).astype(int)

        state = macd_bin + rsi_bin * 5 + sma_bin * 25
        epoch = 0
        temp = 0
        total_reward = 10
        #

        # add your code to do learning here
        while temp != total_reward and epoch < 200:
            #print(temp, total_reward)
            #print(state)
            epoch += 1
            temp = total_reward
            action = self.learner.querysetstate(0)
            #print("a",action)
            holdings = 0
            port_val = 0
            total_reward = 0
            cash = sv
            for i in range(1, len(prices)):
                if (action == 0):
                    pass
                elif (action == 1):  #buy or long position
                    if (holdings == 1000):
                        pass
                    elif (holdings == 0):  #buy 1000 shares
                        holdings += 1000
                        cash -= (holdings * prices.iloc[i - 1] *
                                 (1 + self.impact))
                        port_val += (holdings * prices.iloc[i - 1] *
                                     (1 + self.impact))
                    elif (holdings == -1000):  #buy 2000 shares
                        holdings += 2000
                        cash -= (holdings * prices.iloc[i - 1] *
                                 (1 + self.impact))
                        port_val += (holdings * prices.iloc[i - 1] *
                                     (1 + self.impact))
                elif (action == 2):  #sell or short position
                    if (holdings == -1000):
                        pass
                    elif (holdings == 0):  #short 1000 shares
                        holdings -= 1000
                        cash += (holdings * prices.iloc[i - 1] *
                                 (1 - self.impact))
                        port_val -= (holdings * prices.iloc[i - 1] *
                                     (1 - self.impact))
                    elif (holdings == 1000):  #short 2000 shares
                        holdings -= 2000
                        cash += (holdings * prices.iloc[i - 1] *
                                 (1 - self.impact))
                        port_val -= (holdings * prices.iloc[i - 1] *
                                     (1 - self.impact))
                #stepreward = int((prices.iloc[i] - prices.iloc[i-1]))*holdings*(1-self.impact)
                stepreward = (
                    (float((prices.iloc[i] - prices.iloc[i - 1])) /
                     float(prices.iloc[i - 1])) - self.impact) * holdings
                action = self.learner.query(state[i], stepreward)
                total_reward += stepreward
    def testPolicy(self, symbol = "IBM", \
        sd=dt.datetime(2009,1,1), \
        ed=dt.datetime(2010,1,1), \
        sv = 10000):

        # here we build a fake set of trades
        # your code should return the same sort of data
        dates = pd.date_range(sd, ed)
        prices_all = ut.get_data([symbol], dates)  # automatically adds SPY
        prices = prices_all[[symbol]]  # only portfolio symbols
        prices_SPY = prices_all['SPY']  # only SPY, for comparison later
        #if self.verbose: print prices

        # indicators
        cross, macd = indicators.macd(prices)
        rsi, ind = indicators.rsi(prices)
        sma = indicators.rolling_mean(prices)

        macd_bin = pd.cut(macd.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                               4]).astype(int)
        rsi_bin = pd.cut(rsi.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                             4]).astype(int)
        sma_bin = pd.cut(sma.transpose().iloc[0], 5, labels=[0, 1, 2, 3,
                                                             4]).astype(int)

        state = macd_bin + rsi_bin * 5 + sma_bin * 25
        holdings = 0
        total_reward = 0
        port_val = sv

        #trades
        trades = prices_all[[
            symbol,
        ]]  # only portfolio symbols
        trades_SPY = prices_all['SPY']  # only SPY, for comparison later
        trades.values[:, :] = 0

        for j, i in state.iteritems():
            #print(int(port_val))
            action = np.argmax(self.learner.Q[i])
            if action == 0:
                reward = int((prices.iloc[i] - prices.iloc[i - 1]) * holdings *
                             (1 - self.impact))
                total_reward += reward

            elif action == 2:
                #selling
                if holdings == 0:
                    reward = int((prices.iloc[i] - prices.iloc[i - 1]) *
                                 -1000 * (1 - self.impact))
                    holdings -= 1000
                    trades.loc[j][0] = -1000
                    total_reward += reward
                    port_val -= prices.iloc[i - 1] * 1000
                elif holdings == 1000:
                    reward = int((prices.iloc[i] - prices.iloc[i - 1]) *
                                 -2000 * (1 - self.impact))
                    holdings -= 2000
                    trades.loc[j][0] = -2000
                    total_reward += reward
                    port_val -= prices.iloc[i - 1] * 2000

            elif action == 1:
                #buying
                if holdings == -1000:
                    reward = int((prices.iloc[i] - prices.iloc[i - 1]) * 2000 *
                                 (1 - self.impact))
                    holdings += 2000
                    trades.loc[j][0] = 2000
                    total_reward += reward
                    port_val += prices.iloc[i - 1] * 2000
                elif holdings == 0:
                    reward = int((prices.iloc[i] - prices.iloc[i - 1]) * 1000 *
                                 (1 - self.impact))
                    holdings += 1000
                    trades.loc[j][0] = 1000
                    total_reward += reward
                    port_val += prices.iloc[i - 1] * 1000
        if self.verbose: print type(trades)  # it better be a DataFrame!
        if self.verbose: print trades
        if self.verbose: print prices_all
        return trades
Ejemplo n.º 17
0
    def testPolicy(self,
                   symbol="AAPL",
                   sd=dt.datetime(2010, 1, 1),
                   ed=dt.datetime(2011, 12, 31),
                   sv=100000):
        stockDF = get_data([symbol], pd.date_range(sd, ed))
        stockDF = stockDF[symbol]
        stockDF = stockDF / stockDF.iloc[0]

        df_trades = pd.DataFrame(np.nan,
                                 columns=['Date', 'Symbol', 'Order', 'Shares'],
                                 index=stockDF.index)
        df_trades['Date'] = stockDF.index
        df_trades['Symbol'] = symbol
        df_trades['Shares'] = 0
        df_trades['Order'] = "HOLD"

        bbandDF = indicators.b_bands(stockDF, 20)
        macdDF = indicators.macd(stockDF)
        # posDF = indicators.priceoversma(stockDF, 20)
        exitflag = False
        enterflag = False
        # tsi = indicators.tsi(stockDF)

        currholdings = 0
        prev_date = sd
        prev_date = pd.to_datetime(str(prev_date))
        prev_date = prev_date.strftime('%Y-%m-%d')
        for i in stockDF.index.values:
            date = pd.to_datetime(str(i))
            date = date.strftime('%Y-%m-%d')
            price = stockDF.loc[date]
            upper = bbandDF.loc[date, 'UPPER']
            lower = bbandDF.loc[date, 'LOWER']
            macd = macdDF.loc[date]

            if price >= upper and exitflag == False and macdDF.loc[
                    prev_date] > 0:
                exitflag = True
            elif price <= upper and exitflag == True and macd < 0 and macdDF.loc[
                    prev_date] > 0:
                exitflag = False
                if currholdings == 1000:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = -1000
            elif price <= lower and enterflag == False:
                enterflag = True
            elif price >= lower and enterflag == True and macd > 0 and macdDF.loc[
                    prev_date] < 0:
                enterflag = False
                if currholdings == -1000:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 2000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 1000
                currholdings = 1000

            prev_date = date

        return df_trades
Ejemplo n.º 18
0
import sys

import indicators

CANDLE_SIZE = 10

inds = [
    indicators.ema(5),
    indicators.ema(10),
    indicators.ema(50),
    indicators.ema(100),
    indicators.ema(200),
    indicators.sma(20),
    indicators.sma(50),
    indicators.rsi(14),
    indicators.macd(12, 26),
    indicators.accdistdelt(),
    indicators.ichimoku_tenkan(),
    indicators.ichimoku_kijun()
]


def main():
    print "Loadng from txt"
    idata = np.genfromtxt(sys.argv[1],
                          delimiter=',',
                          skip_header=2,
                          usecols=[1, 2, 3, 4, 6])

    i = 0
    res = None
Ejemplo n.º 19
0
def chart():
    """
    For plotting normal candle chart or along with indicators
    :return: None
    """
    # prop, data = data_parser.get_data(start_date="2017-08-18")
    # result = Strategies.rsi(data, data_properties=prop)
    # data_properties = result['data_properties']
    # main_chart = []
    # for key, values in data_properties.items():
    #     main_chart.append([key, values])
    # params = result['params']
    # data = result['data']

    # print(params,data_with_indicators)
    # final_data = data_with_indicators[1:]
    # print(final_data)

    data_prop, data = data_parser.get_data(start_date="2007-01-18")
    high = data_parser.get_high(data)
    low = data_parser.get_low(data)
    close = data_parser.get_close(data)
    sma = indicators.sma(close)
    ema = indicators.ema(close)
    macd = indicators.macd(close)
    rsi = indicators.rsi(close)
    stoch = indicators.stoch(high, low, close)
    bbands = indicators.bollinger_bands(close)
    pivot = indicators.pivot(data)
    chart_1 = ChartElement(data=sma,
                           label="sma",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.YELLOW)
    chart_2 = ChartElement(data=ema,
                           label="ema",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.PINK)
    chart_3 = ChartElement(data=stoch,
                           label="stoch",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.DIFFERENT_AXIS,
                           color=ChartColor.PURPLE)
    chart_4 = ChartElement(data=bbands,
                           label="bbands",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.BLUE)
    chart_5 = ChartElement(data=pivot,
                           label="pivot",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.GREEN)
    chart_6 = ChartElement(data=rsi,
                           label="rsi",
                           chart_type=ChartType.LINE,
                           plot=1,
                           color=ChartColor.RED)
    chart_7 = ChartElement(data=macd,
                           label="macd",
                           chart_type=ChartType.LINE,
                           plot=2,
                           color="magenta")
    charts = [chart_4, chart_6]
    buy = Condition(data1=sma, data2=ema, operation=Operation.CROSSOVER)
    sell = Condition(data1=rsi, data2=70, operation=Operation.GREATER_THAN)
    result = strategy.strategy_builder(data_properties=data_prop,
                                       data_list=data,
                                       charts=charts,
                                       buy=buy,
                                       sell=sell,
                                       target=1.0,
                                       sl=0.5,
                                       strategy=strategy.BUY)
    # strategy.show_back_testing_reports(result, auto_open=True)
    data_properties = result['data_properties']
    main_chart = []
    for key, values in data_properties.items():
        main_chart.append([key, values])
    params = result['params']
    # print(params)
    data_list = result['data']
    return render_template("chart.html",
                           chartData=data_list,
                           chart_params=params,
                           main_chart_properties=main_chart)
Ejemplo n.º 20
0
def cum_pl_short():
    """
    Chart for back test reports of Cumulative profit and loss
    :return: None
    """
    data_prop, data = data_parser.get_data(start_date="2000-01-18")
    high = data_parser.get_high(data)
    low = data_parser.get_low(data)
    close = data_parser.get_close(data)
    sma = indicators.sma(close)
    ema = indicators.ema(close)
    macd = indicators.macd(close)
    rsi = indicators.rsi(close)
    stoch = indicators.stoch(high, low, close)
    bbands = indicators.bollinger_bands(close)
    pivot = indicators.pivot(data)
    chart_1 = ChartElement(data=sma,
                           label="sma",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.YELLOW)
    chart_2 = ChartElement(data=ema,
                           label="ema",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.PINK)
    chart_3 = ChartElement(data=stoch,
                           label="stoch",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.DIFFERENT_AXIS,
                           color=ChartColor.PURPLE)
    chart_4 = ChartElement(data=bbands,
                           label="bbands",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.BLUE)
    chart_5 = ChartElement(data=pivot,
                           label="pivot",
                           chart_type=ChartType.LINE,
                           plot=ChartAxis.SAME_AXIS,
                           color=ChartColor.GREEN)
    chart_6 = ChartElement(data=rsi,
                           label="rsi",
                           chart_type=ChartType.LINE,
                           plot=1,
                           color=ChartColor.RED)
    chart_7 = ChartElement(data=macd,
                           label="macd",
                           chart_type=ChartType.LINE,
                           plot=2,
                           color="magenta")
    charts = [chart_1, chart_2, chart_3, chart_6]
    buy = Condition(data1=sma, data2=ema, operation=Operation.CROSSOVER)
    sell = Condition(data1=rsi, data2=70, operation=Operation.GREATER_THAN)
    result = strategy.strategy_builder(data_properties=data_prop,
                                       data_list=data,
                                       charts=charts,
                                       buy=buy,
                                       sell=sell,
                                       target=1.0,
                                       sl=0.5,
                                       strategy=strategy.BUY)

    cum_short = result['short']['DATE_CUM_PL']
    # print(cum_short)

    return render_template("cum_pl_short.html", shortData=cum_short)
Ejemplo n.º 21
0
def be_called(Scode, data, margin, period, upvalue, val):
    MACD = 1
    KDJ = 1
    RSI = 1
    ADX = 1
    CCI = 1
    BBANS = 1
    AD = 1
    OBV = 1
    LB = 1
    CMF = 1
    FI = 1
    EOM = 1
    VPT = 1
    ATR = 1
    KC = 1
    DC = 1
    VI = 1
    TRIX = 1
    MI = 1
    DPO = 1
    KST = 1
    ICH = 1
    TSI = 1
    DR = 1
    CR = 1
    ress = []

    #############
    #MACD NUM=9 0-8
    #############
    if MACD == 1:
        data1 = data.rename(index=str, columns={'Close': '<CLOSE>'})
        #print(data1.head(30))
        data1 = id.macd(data1, 26, 12, 9, '<ClOSE>')
        data_MACD = pandas_to_list(data1)
        for i in data_MACD:
            ress.append(i)

    #############
    #KDJ NUM=3 9-11
    #############
    if KDJ == 1:
        data1 = data1.rename(index=str, columns={'<CLOSE>': 'Close'})
        #print(data1.head(30))
        for i in range(3):
            data_KDJ = zhibiao_kdj(data1, 9, 3, 3)[i].values
            data_KDJ = list(data_KDJ)
            ress.append(data_KDJ)

    #############
    #RSI NUM=1 12
    #############

    data2 = copy.copy(data)
    if RSI == 1:
        data_rsi = rsi(data2).tolist()
        ress.append(data_rsi)

    #############
    #ADX NUM=3 13-15
    #############
    if ADX == 1:
        data3 = copy.copy(data)
        data_adx = []
        data_adx.append(adx(data3).tolist())
        data_adx.append(adx_pos(data3).tolist())
        data_adx.append(adx_neg(data3).tolist())
        for i in data_adx:
            ress.append(i)

    ############
    #CCI NUM=1 16
    ############
    if CCI == 1:
        data_cci = cci(data).tolist()
        ress.append(data_cci)

    ############
    #BBANS NUM=3 17-19
    ############
    if BBANS == 1:
        data_bbans = []
        data_bbans.append(bollinger_hband(data2).tolist())
        data_bbans.append(bollinger_lband(data2).tolist())
        data_bbans.append(bollinger_mavg(data2).tolist())
        for i in data_bbans:
            ress.append(i)

    ############
    #AD NUM=1 20
    ############
    if AD == 1:
        data_adi = acc_dist_index(data2).tolist()
        ress.append(data_adi)

    ############
    #OBV NUM=2 21 22
    ############
    if OBV == 1:
        data_obv = []
        data_obv.append(on_balance_volume(data2).tolist())
        data_obv.append(on_balance_volume_mean(data2).tolist())
        for i in data_obv:
            ress.append(i)

    #################################################################################3
    ############
    #CMF NUM=1 23
    ############
    if CMF == 1:
        data_cmf = chaikin_money_flow(data2).tolist()
        ress.append(data_cmf)

    ############
    #FI NUM=1 24
    ############
    if FI == 1:
        data_fi = force_index(data2).tolist()
        ress.append(data_fi)

    ############
    #EOM EMV NUM=1 25
    ############
    if EOM == 1:
        data_eomemv = ease_of_movement(data2).tolist()
        ress.append(data_eomemv)
    ############
    #VPT NUM=1 26
    ############
    if VPT == 1:
        data_vpt = volume_price_trend(data).tolist()
        ress.append(data_vpt)
    ############
    #ATR NUM=1 27
    ############
    if ATR == 1:
        data_atr = average_true_range(data2).tolist()
        ress.append(data_atr)
    ############
    #KC NUM=2 29
    ############
    if KC == 1:
        data_kc = []
        data_kc.append(keltner_channel_hband(data2).tolist())
        data_kc.append(keltner_channel_lband(data2).tolist())
        for i in data_kc:
            ress.append(i)
    '''
    ############
    #DC NUM=2 30 31
    ############
    '''
    if DC == 1:
        data_dc = []
        data_dc.append(donchian_channel_hband(data2).tolist())
        data_dc.append(donchian_channel_lband(data2).tolist())
        for i in data_dc:
            ress.append(i)
    '''
    ############
    #VI NUM=2 32 33
    ############
    '''
    if VI == 1:
        data_vi = []
        data_vi.append(vortex_indicator_neg(data2).tolist())
        data_vi.append(vortex_indicator_pos(data2).tolist())
        for i in data_vi:
            ress.append(i)
    '''
    ############
    #TRIX NUM=1 34
    ############
    '''
    if TRIX == 1:
        data_trix = trix(data2).tolist()
        ress.append(data_trix)
    '''
    ############
    #MI NUM=1 35
    ############
    '''
    if MI == 1:
        data_mi = mass_index(data2).tolist()
        ress.append(data_mi)
    '''
    ############
    #DPO NUM=1 36
    ############
    '''
    if DPO == 1:
        data_dpo = dpo(data2).tolist()
        ress.append(data_dpo)
    '''
    ############
    #KST NUM=2 37 38
    ############
    '''
    if KST == 1:
        data_kst = []
        data_kst.append(kst(data2).tolist())
        data_kst.append(kst_sig(data2).tolist())
        for i in data_kst:
            ress.append(i)
    '''
    ############
    #Ichimoku NUM=2 39 40
    ############
    '''
    if ICH == 1:
        data_ich = []
        data_ich.append(ichimoku_a(data2).tolist())
        data_ich.append(ichimoku_b(data2).tolist())
        for i in data_ich:
            ress.append(i)
    '''
    ############
    #MFI NUM=1 41
    ############
    '''
    if FI == 1:
        data_mfi = money_flow_index(data2).tolist()
        ress.append(data_mfi)
    '''
    ############
    #TSI NUM=1 42
    ############
    '''
    if TSI == 1:
        data_tsi = tsi(data2).tolist()
        ress.append(data_tsi)
    '''
    ############
    #DR NUM=1 43
    ############
    '''
    if DR == 1:
        data_dr = daily_return(data2).tolist()
        ress.append(data_dr)
    '''
    ############
    #CR NUM=1 44
    ############
    '''
    if CR == 1:
        data_cr = cumulative_return(data2).tolist()
        ress.append(data_cr)
    closelist = data['Volume'].tolist()
    if LB == 1:
        numexp = len(closelist) - 5
        vb = []
        for i in range(5):
            vb.append(np.nan)
        for i in range(5, 5 + numexp):
            try:
                cc = closelist[i] / (closelist[i - 5] + closelist[i - 4] +
                                     closelist[i - 3] + closelist[i - 2] +
                                     closelist[i - 1])
            except ZeroDivisionError:
                cc = 0
            vb.append(cc)

        ress.append(vb)
    # remove five index and back up close for Y
    res = []

    ress.pop(2)
    ress.pop(2)
    ress.pop(0)
    for i in range(len(ress)):
        if i in val:
            res.append(copy.copy(ress[i]))
        else:
            pass
    res1 = copy.copy(res)
    res = copy.copy(res1)
    for i in range(len(ress[0])):
        try:
            ress[0][i] = (ress[1][i] - ress[0][i]) / ress[0][i]
        except ZeroDivisionError:
            ress[0][i] = 0
        if ress[0][i] > 0.03:
            temp = np.zeros([5])
            temp[4] = 1
            ress[1][i] = temp
        elif ress[0][i] > 0.01 and ress[0][i] <= 0.03:
            temp = np.zeros([5])
            temp[3] = 1
            ress[1][i] = temp
        elif ress[0][i] > -0.01 and ress[0][i] <= 0.01:
            temp = np.zeros([5])
            temp[2] = 1
            ress[1][i] = temp
        elif ress[0][i] > -0.03 and ress[0][i] <= -0.01:
            temp = np.zeros([5])
            temp[1] = 1
            ress[1][i] = temp
        else:
            temp = np.zeros([5])
            temp[0] = 1
            ress[1][i] = temp
    value_rate = ress[0]
    plus3 = 0
    plus1 = 0
    noplus = 0
    minus1 = 0
    minus3 = 0
    value_falg = ress[1]
    for i in range(len(value_rate)):
        if value_rate[i] * 100 > 3:
            plus3 += 1
        elif value_rate[i] * 100 > 1 and value_rate[i] * 100 < 3:
            plus1 += 1
        elif value_rate[i] * 100 > -1 and value_rate[i] * 100 < 1:
            noplus += 1
        elif value_rate[i] * 100 > -3 and value_rate[i] * 100 < -1:
            minus1 += 1
        else:
            minus3 += 1
    avg = len(value_rate) / 5
    stand_dev = 0.2 * pow(plus3 - avg, 2) + 0.2 * pow(
        plus1 - avg, 2) + 0.2 * pow(noplus - avg, 2) + 0.2 * pow(
            minus1 - avg, 2) + 0.2 * pow(minus3 - avg, 2)
    ff = open('stand_dev.txt', 'a')
    ff.write(Scode)
    ff.write(',')
    ff.write(str(stand_dev))
    ff.write('\n')
    for i in range(len(res)):
        for j in range(60):
            res[i].pop(0)
    for i in range(60):
        value_falg.pop(0)
    sample = []
    ylabel = []
    for i in range(len(res[0]) - margin - period + 1):
        sampletemp = []
        for j in range(len(res)):
            for k in range(i, i + margin):
                sampletemp.append(res[j][k])
        sample.append(sampletemp)
        ylabel.append(value_falg[i + margin])
    oneday = []
    for i in range(len(res)):
        for j in range(len(res[0]) - margin, len(res[0])):
            oneday.append(res[i][j])
    np_oneday = np.zeros([1, len(oneday)])
    for i in range(len(oneday)):
        np_oneday[0][i] = oneday[i]
    ylabel = np.array(ylabel)
    testingsamples_np = np.array(sample)

    os.mkdir(os.getcwd() + '/Stock_Data_IDV/' + Scode)
    np.save(os.getcwd() + '/Stock_Data_IDV/' + Scode + '/train.npy',
            testingsamples_np)
    np.save(os.getcwd() + '/Stock_Data_IDV/' + Scode + '/ylabel.npy', ylabel)
    np.save(os.getcwd() + '/Stock_Data_IDV/' + Scode + '/oneday.npy',
            np_oneday)

    return 0
Ejemplo n.º 22
0
    def testPolicy(self, symbol = "IBM", \
        sd=dt.datetime(2009,1,1), \
        ed=dt.datetime(2010,1,1), \
        sv = 10000):

        stockDF = ut.get_data([symbol], pd.date_range(sd, ed))
        stockDF = stockDF[symbol]
        stockDF = stockDF / stockDF.iloc[0]
        # print stockDF
        PC = (stockDF / stockDF.shift(1)) - 1
        # print PC

        bbandDF = indicators.b_bands(stockDF,
                                     20)  #test with min_periods = 0 in SMA
        bbandDF["BBP"] = ((bbandDF[symbol] - bbandDF["LOWER"]) /
                          (bbandDF["UPPER"] - bbandDF["LOWER"]))
        bbandDF = bbandDF["BBP"]
        macdDF = indicators.macd(stockDF)

        inds = pd.concat([bbandDF, macdDF], axis=1)
        inds = inds.dropna()
        inds = self.discretize(inds)

        df_trades = pd.DataFrame(np.nan,
                                 columns=['Date', 'Symbol', 'Order', 'Shares'],
                                 index=stockDF.index)
        df_trades['Date'] = stockDF.index
        df_trades['Symbol'] = symbol
        df_trades['Shares'] = 0
        df_trades['Order'] = "HOLD"
        currholdings = 0

        self.QL.rar = 0
        for i in inds.index.values:
            # print "hit"
            date = pd.to_datetime(str(i))
            date = date.strftime('%Y-%m-%d')

            # print reward
            action = self.QL.querysetstate((int(float(inds.loc[date]))))
            # print action
            # print action
            if action == 2:  #sell
                if currholdings == 1000:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = -2000
                    currholdings = -1000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'SELL'
                    df_trades.loc[date, "Shares"] = -1000
                    currholdings = -1000
            elif action == 1:  #buy
                if currholdings == -1000:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 2000
                    currholdings = 1000
                if currholdings == 0:
                    df_trades.loc[date, 'Order'] = 'BUY'
                    df_trades.loc[date, "Shares"] = 1000
                    currholdings = 1000
        # print df_trades["Shares"]
        # print df_trades
        df_trades = df_trades.drop('Symbol', axis=1)
        df_trades = df_trades.drop('Order', axis=1)
        df_trades = df_trades.drop('Date', axis=1)
        # print df_trades
        return df_trades
Ejemplo n.º 23
0
    def addEvidence(self, symbol = "IBM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,1,1), \
        sv = 10000):

        # add your code to do learning here
        stockDF = ut.get_data([symbol], pd.date_range(sd, ed))
        stockDF = stockDF[symbol]
        stockDF = stockDF / stockDF.iloc[0]
        PC = (stockDF / stockDF.shift(1)) - 1

        # print stockDF

        #print PC
        # example usage of the old backward compatible util function
        # syms=[symbol]
        # dates = pd.date_range(sd, ed)
        # prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        # prices = prices_all[syms]  # only portfolio symbols
        # prices_SPY = prices_all['SPY']  # only SPY, for comparison later
        # if self.verbose: print prices
        #
        # # example use with new colname
        # volume_all = ut.get_data(syms, dates, colname = "Volume")  # automatically adds SPY
        # volume = volume_all[syms]  # only portfolio symbols
        # volume_SPY = volume_all['SPY']  # only SPY, for comparison later
        # if self.verbose: print volume

        bbandDF = indicators.b_bands(stockDF,
                                     20)  #test with min_periods = 0 in SMA
        bbandDF["BBP"] = ((bbandDF[symbol] - bbandDF["LOWER"]) /
                          (bbandDF["UPPER"] - bbandDF["LOWER"]))
        bbandDF = bbandDF["BBP"]
        macdDF = indicators.macd(stockDF)

        inds = pd.concat([bbandDF, macdDF], axis=1)
        inds = inds.dropna()
        inds = self.discretize(inds)

        df_trades = pd.DataFrame(np.nan,
                                 columns=['Date', 'Symbol', 'Order', 'Shares'],
                                 index=stockDF.index)
        df_trades['Date'] = stockDF.index
        df_trades['Symbol'] = symbol
        df_trades['Shares'] = 0
        df_trades['Order'] = "HOLD"
        currholdings = 0
        reward = 0
        # print (int(float(inds.iloc[0])))
        # print "----------------------------"
        # print inds.max()
        # print inds.min()

        self.QL.querysetstate(int(float(inds.iloc[0])))

        dft_copy = df_trades.copy()
        converged = False
        epochCount = 0
        stockDF.dropna()
        # print inds
        while not converged:
            epochCount += 1
            currholdings = 0
            # reward = 0

            if (epochCount > 500):
                break

            dft_copy = df_trades.copy()

            for i in inds.index.values:
                # print "hit"
                date = pd.to_datetime(str(i))
                date = date.strftime('%Y-%m-%d')
                # print PC.loc[date].values

                reward = (currholdings * PC.loc[date] * (1 - self.impact))
                # print (int(float(inds.loc[date])))
                action = self.QL.query((int(float(inds.loc[date]))), reward)
                # print action
                # print action
                if action == 2:  #sell
                    if currholdings == 1000:
                        df_trades.loc[date, 'Order'] = 'SELL'
                        df_trades.loc[date, "Shares"] = -2000
                        currholdings = -1000
                    if currholdings == 0:
                        df_trades.loc[date, 'Order'] = 'SELL'
                        df_trades.loc[date, "Shares"] = -1000
                        currholdings = -1000
                elif action == 1:  #buy
                    if currholdings == -1000:
                        df_trades.loc[date, 'Order'] = 'BUY'
                        df_trades.loc[date, "Shares"] = 2000
                        currholdings = 1000
                    if currholdings == 0:
                        df_trades.loc[date, 'Order'] = 'BUY'
                        df_trades.loc[date, "Shares"] = 1000
                        currholdings = 1000

            if (dft_copy.equals(df_trades) and epochCount > 10):
                converged = True

        df_trades = pd.DataFrame(np.nan,
                                 columns=['Date', 'Symbol', 'Order', 'Shares'],
                                 index=stockDF.index)
        df_trades['Date'] = stockDF.index
        df_trades['Symbol'] = symbol
        df_trades['Shares'] = 0
        df_trades['Order'] = "HOLD"
Ejemplo n.º 24
0
def main(instrument, periods, granularity, outfile, daily_alignment):

    print(f'Periods        : {periods}')
    print(f'Instrument     : {instrument}')
    print(f'Granularity    : {granularity}')
    print(f'daily alignment: {daily_alignment}')
    print(f'Outfile        : {outfile}')

    if instrument.upper() not in INSTRUMENTS:
        raise Exception(f'Invalid instrument {instrument}')

    if granularity.upper() not in GRANULARITY:
        raise Exception(f'Invalid granularity {granularity}')

    instrument = instrument.upper()
    granularity = granularity.upper()

    debug = True
    if not debug:
        pd.set_option('display.max_columns', 50)
        # pd.set_option('display.max_rows', 500000)
        pd.set_option('display.width', 1000)

    stock = get_oanda_api(
        [instrument],
        granularity=granularity,
        count=periods,
        daily_alignment=daily_alignment,
    )

    nsize = len(stock[instrument]['Close'])

    # Calculate MACD
    stock[instrument] = stock[instrument].join(
        macd(stock[instrument]['Close'])
    )

    # Calculate RSI for n = 14
    stock[instrument] = stock[instrument].join(
        rsi(stock[instrument]['Close'])
    )
    # Calculate Profile and Loss
    stock[instrument] = stock[instrument].join(
        pnl(stock[instrument]['Close'])
    )
    # Calculate MACD Percentile
    stock[instrument] = stock[instrument].join(
        macd_percentile(stock[instrument]['MACD'])
    )
    # Calculate RSI Percentile
    stock[instrument] = stock[instrument].join(
        rsi_percentile(stock[instrument]['RSI'])
    )
    # Calculate  Profile and Loss Percentile
    stock[instrument] = stock[instrument].join(
        pnl_percentile(stock[instrument]['Profit/Loss'])
    )

    # Calculate Divergence factor 1 and 2
    stock[instrument] = stock[instrument].join(
        pd.Series(
            (
                stock[instrument]['MACD Percentile'] + 0.1 -
                stock[instrument]['RSI Percentile']
            ) / 2.0,
            name='Divergence Factor 1'
        )
    )
    stock[instrument] = stock[instrument].join(
        pd.Series(
            stock[instrument]['Divergence Factor 1'] -
            stock[instrument]['PNL Percentile'],
            name='Divergence Factor 2'
        )
    )

    # Calculate Divergence factor 3
    n = 19
    for i in range(nsize):
        stock[instrument].loc[i: nsize, 'Macd_20'] = (
            stock[instrument]['MACD'].iloc[i] -
            stock[instrument]['MACD'].iloc[i - n]
        )
        stock[instrument].loc[i: nsize, 'Prc_20'] = (
            (stock[instrument]['Close'].iloc[i] -
                stock[instrument]['Close'].iloc[i - n])
        ) / stock[instrument]['Close'].iloc[i - n]
        stock[instrument].loc[i: nsize, 'Divergence Factor 3'] = (
            stock[instrument]['Macd_20'].iloc[i] /
            stock[instrument]['Close'].iloc[i]
        ) - stock[instrument]['Prc_20'].iloc[i]

    stock[instrument] = stock[instrument].join(
        rsi(stock[instrument]['Close'], 20, name='RSI_20')
    )

    # Calculate the momentum factors
    stock[instrument] = stock[instrument].join(
        pnl_n(stock[instrument]['Close'], 10)
    )
    stock[instrument] = stock[instrument].join(
        pnl_n(stock[instrument]['Close'], 30)
    )

    stock[instrument]['Close_fwd'] = stock[instrument]['Close'].shift(-2)
    stock[instrument].loc[-1: nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-1]
    stock[instrument].loc[-2: nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-2]

    stock[instrument] = stock[instrument].join(
        macd(
            stock[instrument]['Close_fwd'],
            name='MACD_fwd'
        )
    )
    n = 19
    stock[instrument] = stock[instrument].join(
        pd.Series(
            stock[instrument]['MACD_fwd'].diff(n) - stock[instrument]['MACD'],
            name='M_MACD_CHANGE'
        )
    )

    stock[instrument] = stock[instrument].join(
        rsi(stock[instrument]['Close_fwd'], n=20, name='RSI_20_fwd')
    )
    stock[instrument] = stock[instrument].join(
        pd.Series(
            stock[instrument]['RSI_20_fwd'] - stock[instrument]['RSI_20'],
            name='M_RSI_CHANGE'
        )
    )

    # Calculate the ADX, PDI & MDI
    _adx, _pdi, _mdi = adx(stock[instrument])

    stock[instrument] = stock[instrument].join(_adx)
    stock[instrument] = stock[instrument].join(_pdi)
    stock[instrument] = stock[instrument].join(_mdi)

    # Calculate the Moving Averages: 5, 10, 20, 50, 100
    for period in [5, 10, 20, 50, 100]:
        stock[instrument] = stock[instrument].join(
            moving_average(
                stock[instrument]['Close'],
                period,
                name=f'{period}MA'
            )
        )

    # Calculate the Williams PCTR
    stock[instrument] = stock[instrument].join(
        williams(stock[instrument])
    )

    # Calculate the Minmax Range
    n = 17
    for i in range(nsize):
        maxval = stock[instrument]['High'].iloc[i - n: i].max()
        minval = stock[instrument]['Low'].iloc[i - n: i].min()
        rng = abs(maxval) - abs(minval)
        # where is the last price in the range of minumimn to maximum
        pnow = stock[instrument]['Close'].iloc[i - n: i]
        if len(pnow.iloc[-1: i].values) > 0:
            whereinrng = (
                (pnow.iloc[-1: i].values[0] - abs(minval)) / rng
            ) * 100.0
            stock[instrument].loc[i: nsize, 'MinMaxPosition'] = whereinrng
            stock[instrument].loc[i: nsize, 'High_Price(14)'] = maxval
            stock[instrument].loc[i: nsize, 'Low_Price(14)'] = minval

    headers = [
        'Close',
        'adx',
        'pdi',
        'mdi',
        'MACD',
        'RSI',
        'Divergence Factor 1',
        'Divergence Factor 2',
        'Divergence Factor 3',
    ]
    stock[instrument].to_csv(
        outfile,
        columns=headers,
        mode='w',
        sep=',',
        date_format='%d-%b-%Y %r',
    )
Ejemplo n.º 25
0
def get_fx_file_paths(fx_pair, agg_level, agg_magnitude, tech_indicators, data_dir, is_test = False,
    train_end = '2019-02-01'):
    input_path = os.path.join(data_dir, 'csv')
    output_path = os.path.join(data_dir, 'npy')
    train_end = np.datetime64(train_end)
    phases = ['train', 'test']
    file_paths = {phase : 
        os.path.join(output_path, 
            f"""{fx_pair.upper()}_{str(agg_magnitude)}_{agg_level}_{phase}_{'_'.join([str(y) 
                for x in list(tech_indicators.items()) 
                for y in x]).replace(', ', '_')}.npy""") 
        for phase in phases}
    # Return if data exists
    if os.path.exists(file_paths['train']):
        return file_paths['train']
    # Otherwise create it
    print(f'Creating data {file_paths}')
    file_list = [x for x in os.listdir(input_path) if fx_pair in x]
    fx_npy = {x : None for x in phases}
    for file_name in tqdm(file_list, total = len(file_list)):
        # Read file and setup
        df = pd.read_csv(os.path.join(input_path, file_name), header = None, 
            names = ['fx_pair', 'timestamp', 'bid', 'ask'])
        df = df.drop('fx_pair', axis = 1)
        df['timestamp'] = pd.to_datetime(df['timestamp'], 
            format = '%Y%m%d %H:%M:%S.%f')
        df = df.dropna()
        df = df[['timestamp', 'bid', 'ask']]
        df['price'] = df[['bid', 'ask']].mean(axis = 1)
        df = df.drop(['bid', 'ask'], axis = 1)
        # Set phase
        if df['timestamp'].max() < train_end:
            phase = 'train'
        else:
            phase = 'test'
        # Aggregate data at level and magnitude
        df = df.set_index('timestamp')
        df = df.resample(f'{agg_magnitude}{agg_level}', 
            label = 'right').ohlc()['price'].reset_index()
        df['timestamp'] = df['timestamp'].astype(int)
        # Calc technical indicators
        for indicator,params in tech_indicators.items():
            if indicator == 'macd':
                df = indicators.macd(df, *params)
            elif indicator == 'rsi':
                df = indicators.rsi(df, *params)
            elif indicator == 'adx':
                df = indicators.adx(df, *params)
            else:
                raise Exception(f'Unrecognized technical indicator {indicator}')
        # Add to phase numpy array
        df_np = df.dropna().to_numpy()
        assert np.sum(np.isnan(df_np)) == 0            
        if fx_npy[phase] is None:
            fx_npy[phase] = df_np
        else:
            fx_npy[phase] = np.vstack([fx_npy[phase], df_np])
    
    for phase in fx_npy.keys():
        np.save(file_paths[phase], fx_npy[phase])
    file_path = file_paths['test'] if is_test else file_paths['train']
    return file_path