sPos, sNeg=max(0., pos), min(0., neg)
        print("sPos:", sPos, "&", "sNeg:", sNeg, )
        if sNeg<-h:
            sNeg=0;tEvents.append(i)
            tEvents_close.append(gRaw[i])
        elif sPos>h:
            sPos=0;tEvents.append(i)
            tEvents_close.append(gRaw[i])
    return pd.DatetimeIndex(tEvents), tEvents_close


from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
from configs_and_settings.settings import stock_data_settings

stock_symbols = stock_data_settings["stock_symbols"]
nvda_stock_data = YahooStockDataFeed(stock_symbols).stocks_data_dict["NVDA"]
nvda_close_data = nvda_stock_data["Close"]
h = 0.1 # Threshold (Filter size)
res = getTEvents(nvda_close_data, h)
myEvents = res[0]
tEvents_close = res[1]
logged = np.log(nvda_close_data).diff().dropna()
# print(myEvents)
print("len of apple.Close:", len(nvda_close_data))
print("len of myEvents:" ,len(myEvents))
print("len of tEvents_close:", len(tEvents_close))


# plt.plot(myEvents, tEvents_close, 'r')
plt.plot(nvda_close_data.index, nvda_close_data, 'b')
plt.show()
        return df

    def combined_int_cat_signals_into_df(self):
        df = pd.concat(self.int_cat_signals, axis=1)
        df = df.dropna()
        df.index = self.combined_num_signals_into_df().index
        return df



if __name__ == "__main__":
    from configs_and_settings.settings import stock_data_settings
    from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
    
    stock_symbols = stock_data_settings["stock_symbols"]
    data_src = YahooStockDataFeed(stock_symbols)
    stock_symbol = "NVDA"
    stock_data = data_src.stocks_data_dict[stock_symbol]
    stock_close = stock_data["Close"]
    sig = ConstructSignal(stock_data)
    sig.get_sma_crossover_signal_int_labels(5)
    sig.get_rsi_signal_int_labels(70, 30)
    sig.combined_num_signals_into_df()
    sig.combined_int_cat_signals_into_df()
#     apple = data_getter_funcs.get_stocks_data()
#     strategy = SimpleBuyAndHoldStrategy(apple)
#
#     strategy_rsi_values = strategy.compute_and_get_rsi_signal_values(time_period=14)
#     strategy_rsi_labels = strategy.get_rsi_signal_labels(overbought_level=70, oversold_level=30)
#
#     strategy_sma_crossover_values = strategy.compute_and_get_sma_crossover_signal_values(short_trend_period=21, long_trend_period=42)
Пример #3
0
from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
from configs_and_settings.settings import stock_data_settings
from technical_indicators.ta_tools import TATools
from signals.collect_and_merge_series_features import CollectAndMergeSeriesFeatures

stock_symbol = "NVDA"
stock_symbols = stock_data_settings["stock_symbols"]

stocks_data_source = YahooStockDataFeed(stock_symbols)
stock_data = stocks_data_source.stocks_data_dict[stock_symbol]
print(stock_data)

rsi = TATools().rsi(stock_data, stock_symbol)
sma = TATools().sma(stock_data, stock_symbol)
macd = TATools().macd(stock_data, stock_symbol)
    #             # print("Signal: 1  on Date: {0}".format(self.current_date))
    #             self.portfolio.long_position_consultant()
    #         #
    #         elif self.current_signal == -1 or self.current_signal == 0:
    #             pass
    #
    #         print("Current cash balance: ", self.portfolio.cash_balance)
    #         print("Current portfolio value: ", self.portfolio.current_portfolio_value)


if __name__ == "__main__":

    stock_symbols_list = stock_data_settings["stock_symbols"]
    trade_bots_num = 2
    trading_strategy = BasicRSIStrategy()
    data_source = YahooStockDataFeed(stock_symbols_list)

    from trading_strategies.basic_rsi_strategy import BasicRSIStrategy
    from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
    from configs_and_settings.settings import stock_data_settings

    stock_symbols = stock_data_settings["stock_symbols"]
    stockslist1 = ["INTC", "AAPL"]
    stockslist2 = ["TXN", "AMD"]
    stock11 = stockslist1[0]
    stock21 = stockslist2[0]
    data_source1 = YahooStockDataFeed(stockslist1)
    data_source2 = YahooStockDataFeed(stockslist2)

    # trading_strategy1 = BasicRSIStrategy()
    # trading_strategy2 = BasicRSIStrategy()
import pandas as pd
from position_side_labeller import take_profit_stop_loss_labeller
from technical_indicators import rsi, sma_crossover
from technical_indicators import sma
from technical_indicators.new_daily_volitility import getDailyVol
from signals.collect_and_merge_series_features import CollectAndMergeSeriesFeatures
from data_preprocessing.data_transformation_and_splitting import uni_and_multivar_ts_matrix_train_test_split
from sklearn.ensemble import RandomForestClassifier
from technical_indicators import average_true_return
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
from configs_and_settings.settings import stock_data_settings

stock_data = YahooStockDataFeed(stock_data_settings["stock_symbols"])
apple_df = stock_data.stocks_data_dict["AAPL"]
# apple = stock_data.StockData("AAPL")
# apple_df = apple.get_ema_smooth_stocks_data(trend_period=21)
# apple_df = apple.get_stocks_data()
apple_close = apple_df["Close"]
apple_close_copy = apple_close.copy()

expiration_days_limit = None
take_profit_pct = 10
stop_loss_pct = 10
position_labels = take_profit_stop_loss_labeller.PositionSideLabeler(
    apple_close_copy,
    take_profit_pct=take_profit_pct,
    stop_loss_pct=stop_loss_pct,
    expiration_days_limit=expiration_days_limit)
from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
from configs_and_settings.settings import stock_data_settings
import pandas as pd

stock_symbols_list = stock_data_settings["stock_symbols"]
data_source = YahooStockDataFeed(stock_symbols_list)
data = data_source.stocks_market_data_dict["AAPL"]

c = data["Close"]
cdiff = c.diff()
cdiff_raw_copy = cdiff.copy()

cdiff_raw_copy[cdiff_raw_copy < 0] = 0  # drop in price
cdiff_raw_copy[cdiff_raw_copy > 0] = 1  # increase in price

drop_count = len(cdiff_raw_copy[cdiff_raw_copy == 0])
increase_count = len(cdiff_raw_copy[cdiff_raw_copy == 1])
print("Tot # of samples: {0}\nTot # of 0 labels: {1}\nTot # of 1 labels: {2}".
      format(len(cdiff_raw_copy), drop_count, increase_count))

vals = cdiff_raw_copy.values
bullish_streaks_list = []
bearish_streaks_list = []
# streaks_dict = {}
current_val = None
count = 0

for val in vals:
    if current_val is None and val == 1:
        count = 1
        current_val = 1
        # self.signal = signal
        # self._generate_signal()

    def generate_signal(self, data):
        # if self.signal is None:
        rsi = TATools().rsi(data, period=self.period)
        rsi_nonan = rsi.dropna()
        rsi_nonan_copy = rsi_nonan.copy()
        rsi_nonan_copy[rsi_nonan_copy < self.over_sold_level] = 1
        rsi_nonan_copy[rsi_nonan_copy > self.over_bought_level] = -1
        rsi_nonan_copy[(rsi_nonan_copy > self.over_sold_level)
                       & (rsi_nonan_copy < self.over_bought_level)] = 0
        self.signal = rsi_nonan_copy
        return self.signal
        # else:
        # pass


if __name__ == "__main__":
    from configs_and_settings.settings import stock_data_settings
    stock_symbols = stock_data_settings["stock_symbols"]
    data1 = YahooStockDataFeed(stock_symbols).stocks_data_dict["NVDA"]
    data2 = YahooStockDataFeed(stock_symbols).stocks_data_dict["AAPL"]
    data3 = YahooStockDataFeed(stock_symbols).stocks_data_dict["AMD"]
    s1 = BasicRSIStrategy(data1)
    rsi1 = s1.signal
    s2 = BasicRSIStrategy(data2)
    rsi2 = s2.signal
    s3 = BasicRSIStrategy(data3, signal=rsi1)
    rsi3 = s3.signal
from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
from configs_and_settings.settings import stock_data_settings

stock_symbols = stock_data_settings["stock_symbols"]
stock_data = YahooStockDataFeed(stock_symbols)
aapl_data = stock_data.stocks_data_dict["AAPL"]
aapl_close = aapl_data["Close"]

aapl_last_50_close = aapl_close.tail(50)

spos = 0
sneg = 0
restart_point = aapl_last_50_close.index[0]
pos_restart_point = aapl_last_50_close.index[0]
neg_restart_point = aapl_last_50_close.index[0]
for bar_date, bar_price in aapl_last_50_close[1:].iteritems():
    period_data = aapl_last_50_close.loc[restart_point:bar_date]
    est = period_data.mean()
    std = period_data.std()
    print(bar_date, bar_price)
Пример #9
0
        cat_sig_list_labels = []
        for s in signal:
            if s < self._oversold_level:
                cat_sig_list_labels.append(1)
            elif s > self._overbought_level:
                cat_sig_list_labels.append(-1)
            else:
                cat_sig_list_labels.append(0)
        return cat_sig_list_labels

    def get_rsi_int_labels_ts(self):
        raw_signal = self.get_rsi_values()
        labelled_signal = raw_signal.copy()
        labelled_signal[labelled_signal < 30] = 1
        labelled_signal[labelled_signal > 70] = -1
        labelled_signal[(labelled_signal > 30) & (labelled_signal < 70)] = 0
        return labelled_signal


if __name__ == "__main__":
    # from data_loader.load_data import data_getter_funcs
    from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
    nvda = YahooStockDataFeed().stocks_data_dict["NVDA"]
    close = nvda["Close"]
    rsi = RSI(close, time_period=14, overbought_level=70, oversold_level=30)
    rsi_values = rsi.get_rsi_values()
    print(rsi_values.head())
    import matplotli
#     print(len(rsi.get_rsi_values()), type(rsi.rsi_values), type(rsi.get_rsi_values()), rsi.rsi_values.shape)
#     print(len(rsi.get_rsi_labels()), type(rsi.rsi_labels), type(rsi.get_rsi_labels()))
Пример #10
0
        # MACDdiff = pd.Series(MACD - MACDsign, name='MACDdiff_' + str(fast_ma_period) + '_' + str(slow_ma_period))
        # macd_data = pd.DataFrame({"MACD":MACD, "MACDsign":MACDsign, "MACDdiff":MACDdiff}, index=pd_dt_idx)
        macd_data = pd.DataFrame({
            "MACD": MACD,
            "MACDsign": MACDsign
        },
                                 index=pd_dt_idx)
        # stock_data_df = stock_data_df.join(MACD)
        # stock_data_df = stock_data_df.join(MACDsign)
        # stock_data_df = stock_data_df.join(MACDdiff)
        return macd_data


if __name__ == "__main__":
    from configs_and_settings.settings import stock_data_settings
    from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
    stock_symbols = stock_data_settings["stock_symbols"]
    stock_symbol = "NVDA"
    stock_data = YahooStockDataFeed(
        stock_symbols).stocks_data_dict[stock_symbol]
    rsi = TATools().rsi(stock_data)
    print(stock_data.head())
    print(rsi.head())
    print(len(stock_data))
    print(len(rsi))
    # import matplotlib.pyplot as plt
    # rsi.plot()
    # rsi.plot().plot_date(rsi)
    # rsi.plot_date()
    # plt.show()
Пример #11
0
        print(
            "{0} final cash sum (not incl. value of remaining open orders): {1}"
            .format(self.bot_type_name, self.current_cash_sum))


if __name__ == "__main__":
    from trading_strategies.basic_rsi_strategy import BasicRSIStrategy
    from data_loader.yahoo_stock_data_feed import YahooStockDataFeed
    from configs_and_settings.settings import stock_data_settings

    stock_symbols = stock_data_settings["stock_symbols"]
    stockslist1 = ["INTC", "AAPL"]
    stockslist2 = ["TXN", "AMD"]
    stock11 = stockslist1[0]
    stock21 = stockslist2[0]
    data_source1 = YahooStockDataFeed(stockslist1)
    data_source2 = YahooStockDataFeed(stockslist2)

    trading_strategy1 = BasicRSIStrategy()
    trading_strategy2 = BasicRSIStrategy()
    # trading_strategy2 = BasicRFCStrategy()
    # trading_strategy2 = BasicRFCStrategy(data2)
    # ['INTC', 'NVDA', 'MU', 'QCOM', 'AMD', 'XLNX', 'ASML', 'TXN', 'AAPL']

    bot1 = PredictiveTradeBot(stockslist1, 1, 100, 15, 10, 10,
                              trading_strategy1, data_source1)
    bot2 = PredictiveTradeBot(stockslist2, 2, 100000, 5, 5, 5,
                              trading_strategy2, data_source2)

    print("\n***** TEST OUTPUT ***** TEST OUTPUT ***** TEST OUTPUT *****\n")
    print("bot1 unique_id: {0} and bot2 unique_id: {1}".format(