Beispiel #1
0
    def next_day_prediction(self):
        collectionname = 'IntraDay'
        for com in self.CompanyP:
            value = mongodb.ReadValue(collectionname, com)['Data']
            df = pd.DataFrame(eval(value))
            # print(df)
            next_date = DataFeed().getNextDate((df['date'].max()).split(' ')[0])
            df.rename(columns={"1. open": "open", "2. high": "high", "3. low": "low", "4. close": "close",
                               "5. volume": "volume"}, inplace=True)

            if 'volume' in df.columns:
                del df['volume']
            dataframe = df.reset_index(drop=True)
            dates = dataframe['date'].copy()
            del dataframe['date']
            seedValue = dataframe.tail(1)
            dataframe, scaler = stocksLSTM.ScaleDataSet(dataframe)
            dataframe = stocksLSTM.prepareDataSet(dataframe)
            model, details = stocksLSTM.trainModel(dataframe)
            seedValue, _ = stocksLSTM.ScaleDataSet(seedValue, scaler)
            p_df = stocksLSTM.predictfulDay(model, details, seedValue)
            p_df = stocksLSTM.deScaleData(p_df, scaler)
            rng = pd.date_range(str(next_date) + ' ' + '09:35:00', periods=100, freq='5min')
            ts = pd.Series(rng)
            p_df['date'] = ts
            p_df['date'] = p_df['date'].astype(str)
            # print(p_df)
            mongodb.UpdateValue('FuturePrediction', com, p_df.to_dict(orient='list'))
Beispiel #2
0
    def same_day_prediction(self):
        collectionname = 'IntraDay'
        for com in self.CompanyP:
            value = mongodb.ReadValue(collectionname, com)['Data']
            df = pd.DataFrame(eval(value))
            # print(df)
            next_date = DataFeed().getNextDate(
                (df['date'].max()).split(' ')[0])
            df.rename(columns={
                "1. open": "open",
                "2. high": "high",
                "3. low": "low",
                "4. close": "close",
                "5. volume": "volume"
            },
                      inplace=True)
            if 'volume' in df.columns:
                del df['volume']
            dataframe = df.reset_index(drop=True)
            dates = dataframe['date'].copy()
            del dataframe['date']

            testEnd = dataframe.iloc[312:].copy()
            trainStart = dataframe.drop(dataframe.index[312:])

            trainStart, scaler = stocksLSTM.ScaleDataSet(trainStart)
            testEnd, _ = stocksLSTM.ScaleDataSet(testEnd, scaler)

            # testEnd = testEnd.shift(-1)
            # testEnd = testEnd.dropna()
            # testEnd.reset_index(drop=True, inplace=True)
            trainStart = stocksLSTM.prepareDataSet(trainStart)
            model, details = stocksLSTM.trainModel(trainStart)

            presults = stocksLSTM.predict(model, testEnd)
            presults = stocksLSTM.deScaleData(presults, scaler)
            ndates = pd.DataFrame(dates[312:], columns=['date'])
            # ndates = ndates.shift(-1)
            # ndates = ndates.dropna()
            ndates.reset_index(drop=True, inplace=True)
            presults = pd.concat([presults, ndates], axis=1)

            date_filter = (presults['date'].max()).split(' ')[0]
            mongodb.UpdateValue('PredictionStore',
                                com + ' ' + str(date_filter),
                                presults.to_dict(orient='list'))
Beispiel #3
0
    def selected_predict(self, company_name, start_date, end_date,
                         nopredictions):
        try:
            collectionname = 'IntraDay'
            t1 = start_date.split(' ')[1]
            t2 = end_date.split(' ')[1]
            FMT = '%H:%M:%S'
            tdelta = datetime.strptime(t2, FMT) - datetime.strptime(t1, FMT)
            total_minutes = int(tdelta.total_seconds() / 60)
            value = mongodb.ReadValue(collectionname, company_name)['Data']
            df = pd.DataFrame(eval(value))
            df.rename(columns={
                "1. open": "open",
                "2. high": "high",
                "3. low": "low",
                "4. close": "close",
                "5. volume": "volume"
            },
                      inplace=True)

            if 'volume' in df.columns:
                del df['volume']

            test_df = df[(df['date'] >= start_date) & (df['date'] <= end_date)]

            max_date = (test_df['date'].max()).split(' ')[0]
            max_time = (test_df['date'].max()).split(' ')[1]

            testEnd = test_df
            trainStart = df.tail(312)

            # dates = df['date'].copy()
            del testEnd['date']
            del trainStart['date']

            trainStart, scaler = stocksLSTM.ScaleDataSet(trainStart)
            testEnd, _ = stocksLSTM.ScaleDataSet(testEnd, scaler)

            trainStart = stocksLSTM.prepareDataSet(trainStart)

            model, details = stocksLSTM.trainModel(trainStart)

            presults = stocksLSTM.predict(model, testEnd)
            presults = stocksLSTM.deScaleData(presults, scaler)

            temp_time = datetime.strptime(max_time, "%H:%M:%S")
            time = temp_time.time()
            gen_date = None
            rng = None
            global frequency
            global points

            if nopredictions == 5:
                frequency = '5min'
                points = total_minutes / 5
            elif nopredictions == 15:
                frequency = '15min'
                points = total_minutes / 15
            elif nopredictions == 30:
                frequency = '30min'
                points = total_minutes / 30
            elif nopredictions == 60:
                frequency = '60min'
                points = total_minutes / 60

            if time.hour == 16:
                gen_date = self.getNextDate(max_date)
                rng = pd.date_range(str(gen_date) + ' ' + '09:35:00',
                                    periods=points,
                                    freq=frequency)
            else:
                rng = pd.date_range(str(max_date) + ' ' + str(max_time),
                                    periods=points,
                                    freq=frequency)

            ts = pd.Series(rng)
            presults['date'] = ts
            presults['date'] = presults['date'].astype(str)
            presults.rename(columns={
                "openP": "open",
                "highP": "high",
                "lowP": "low",
                "closeP": "close"
            },
                            inplace=True)
            return presults.to_dict(orient='list')
        except Exception as e:
            print(str(e))
            return 'False'
Beispiel #4
0
    def same_day_prediction(self):
        collectionname = ['IntraDay5 mins', 'IntraDay4 hours']
        for col in collectionname:
            for com in self.CompanyP:
                value = mongodb.ReadValue(col, com)['Data']
                df = pd.DataFrame(eval(value))
                # print(df)
                # df = {"date": ['20190502  13:30:00', '20190502  16:00:00'], "open": [209.95, 208.65],
                #       "high": [212.65, 210.29], "low": [208.13, 208.41], "close": [208.63, 209.17],
                #       "volume": [149612, 100915]}
                # next_date = DataFeed().getNextDate((df['date'].max()).split(' ')[0])
                df.rename(columns={
                    0: "date",
                    1: "open",
                    2: "high",
                    3: "low",
                    4: "close",
                    5: "volume"
                },
                          inplace=True)

                if 'volume' in df.columns:
                    del df['volume']

                dataframe = df.reset_index(drop=True)
                dates = dataframe['date'].copy()
                signals = dataframe['signal'].copy()
                del dataframe['date']
                del dataframe['signal']

                dfleng = len(dataframe.index) - 1
                testEnd = dataframe.iloc[dfleng:].copy()
                trainStart = dataframe.drop(dataframe.index[dfleng:])

                trainStart, scaler = stocksLSTM.ScaleDataSet(trainStart)
                testEnd, _ = stocksLSTM.ScaleDataSet(testEnd, scaler)

                # testEnd = testEnd.shift(-1)
                # testEnd = testEnd.dropna()
                # testEnd.reset_index(drop=True, inplace=True)
                trainStart = stocksLSTM.prepareDataSet(trainStart)
                model, details = stocksLSTM.trainModel(trainStart)

                presults = stocksLSTM.predict(model, testEnd)
                presults = stocksLSTM.deScaleData(presults, scaler)
                ndates = pd.DataFrame(dates[dfleng:], columns=['date'])
                nsignals = pd.DataFrame(signals[dfleng:], columns=['signal'])

                # ndates = ndates.shift(-1)
                # ndates = ndates.dropna()
                ndates.reset_index(drop=True, inplace=True)
                nsignals.reset_index(drop=True, inplace=True)
                presults = pd.concat([presults, ndates], axis=1)
                presults = pd.concat([presults, nsignals], axis=1)
                print(presults)
                presults = pd.DataFrame(presults)
                presults.rename(columns={
                    "date": "dateP",
                    "open": "openP",
                    "high": "highP",
                    "low": "lowP",
                    "close": "closeP",
                    "signal": "signalP"
                },
                                inplace=True)
                presults1 = self.strategy(presults)

                df.reset_index(drop=True, inplace=True)
                presults = pd.concat([presults, df], axis=1)
                print(presults1)
                date_filter = (presults['date'].max()).split(' ')[0]
                # mongodb.UpdateValue(col+"Predict", com + ' ' + str(date_filter), presults1.to_dict(orient='list'))
                mongodb.UpdateValue(col + "Predict", com,
                                    presults1.to_dict(orient='list'))