Exemple #1
0
def getPriceFromAPI_m(symbol, isCrypto):
    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1M"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        return df

    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NYSE",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1M"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        return df
    return -1
Exemple #2
0
def getPriceFromAPI(symbol, isCrypto):
    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        price = df['Open'][-1]
        return round(decimal.Decimal(price), 2)
    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "INDEXDJX",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        price = df['Open'][-1]
        return round(decimal.Decimal(price), 2)

    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "INDEXSP",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        price = df['Open'][-1]
        return round(decimal.Decimal(price), 2)

    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NYSE",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    if not df.empty:
        price = df['Open'][-1]
        return round(decimal.Decimal(price), 2)
    return -1
Exemple #3
0
    def google_finance_stocks(self,
                              symbol,
                              start_date=(2000, 1, 1),
                              end_date=None):
        """
        symbol is a string representing a stock symbol, e.g. 'AAPL'
     
        start_date and end_date are tuples of integers representing the year, month,
        and day
     
        end_date defaults to the current date when None
        """
        # Dow Jones
        param = {
            'q': "FB",  # Stock symbol (ex: "AAPL")
            'i':
            "86400",  # Interval size in seconds ("86400" = 1 day intervals)
            'x':
            "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
            'p': "3M"  # Period (Ex: "1Y" = 1 year)
        }

        # get price data (return pandas dataframe)
        df = get_price_data(param)
        print(df)
        return df
Exemple #4
0
def update_main_graph(selected_dropdown_value, selected_radio_value):

    if selected_radio_value == 86400 * 7:
        scale = "2Y"
        scale_title = "Weekly"
    elif selected_radio_value == 86400:
        scale = "2M"
        scale_title = "Daily"

    param = {
        'q': selected_dropdown_value,  # Stock symbol (ex: "AAPL")
        'i':
        selected_radio_value,  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "MCX",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': scale  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)

    close_chart = go.Scatter(x=df.index, y=df.Close, name='Close')

    ema10_chart = go.Scatter(x=df.index,
                             y=df.Close.ewm(span=10, adjust=False).mean(),
                             name='ema10')

    ema20_chart = go.Scatter(x=df.index,
                             y=df.Close.ewm(span=20, adjust=False).mean(),
                             name='ema20')

    fi2_chart = go.Scatter(x=df.index,
                           y=rawfi(df).ewm(span=2, adjust=False).mean(),
                           name='fi2')

    fi13_chart = go.Scatter(x=df.index,
                            y=rawfi(df).ewm(span=13, adjust=False).mean(),
                            name='fi13')

    vol_chart = go.Bar(x=df.index, y=df.Volume, name='Volume')

    stacked_chart = tools.make_subplots(rows=3,
                                        cols=1,
                                        specs=[[{}], [{}], [{}]],
                                        shared_xaxes=True,
                                        shared_yaxes=False,
                                        vertical_spacing=0.001)

    stacked_chart.append_trace(close_chart, 1, 1)
    stacked_chart.append_trace(ema10_chart, 1, 1)
    stacked_chart.append_trace(ema20_chart, 1, 1)
    stacked_chart.append_trace(fi2_chart, 2, 1)
    stacked_chart.append_trace(fi13_chart, 2, 1)
    stacked_chart.append_trace(vol_chart, 3, 1)

    stacked_chart['layout'].update(height=800,
                                   width=800,
                                   title=scale_title + ' analytics for ' +
                                   selected_dropdown_value)

    return stacked_chart
    def getData(self, params):
        params.pop("output_id", None)  # caching layer
        if self.params_cache != params:  # caching layer
            ticker = params['ticker']
            if ticker == 'empty':
                ticker = params['custom_ticker'].upper()

            xchng = "NASD"

            param = {
                'q': ticker,  # Stock symbol (ex: "AAPL")
                'i':
                "86400",  # Interval size in seconds ("86400" = 1 day intervals)
                'x':
                xchng,  # Stock exchange symbol on which stock is traded (ex: "NASD")
                'p': "3M"  # Period (Ex: "1Y" = 1 year)
            }
            # get price data (return pandas dataframe)
            df = get_price_data(param)

            df['Date'] = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%s')
            df = df.drop(['Volume'], axis=1)
            self.data_cache = df  # caching layer
            self.params_cache = params  # caching layer
        return self.data_cache
Exemple #6
0
def load_google_prices(ticker, exchange='LON', period='1Y'):
    param = {'q': ticker, 'i': '86400', 'x': exchange, 'p': period}
    try:
        return gc.get_price_data(param)
    except Exception as e:
        logger.warn('Failed: %s' % str(e))
        return pd.DataFrame([])
Exemple #7
0
def move_AAPL():
    param = {
        'q': ".IXIC",  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "INDEXNASDAQ",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "3M"  # Period (Ex: "1Y" = 1 year)
    }
    df = get_price_data(param)
    df.to_csv('C:/Users/ansha/Anaconda3/FlaskApp/nasd1.csv')
    line = pd.read_csv("nasd1.csv", index_col=False)

    df = pd.DataFrame(data=line)

    dates = df['Unnamed: 0']

    o = df['Open']
    h = df['High']
    l = df['Low']
    c = df['Close']
    line_chart = pygal.Line(x_label_rotation=20,
                            x_labels_major_every=6,
                            show_minor_x_labels=False,
                            human_readable=True)
    line_chart.title = 'NASDAQ'
    line_chart.x_labels = map(str, dates)
    line_chart.add('Open', o)
    line_chart.add('High', h)
    line_chart.add('Low', l)
    line_chart.add('Close', c)
    graph_data = line_chart.render_data_uri()
    return render_template("an.html", graph_data=graph_data)
Exemple #8
0
def collect_market(stock, years=10):
    param = {}
    param['q'] = stock
    param['i'] = '86400'
    param['p'] = str(years) + 'Y'
    df = get_price_data(param)
    #df['date'] = df.index.map(lambda x: x.strftime('%Y-%m'))
    #df = df.groupby('date').mean()
    return df.loc[:, df.columns != 'Volume']
Exemple #9
0
def get_price(symbol):
    param1 = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "15Y"  # Period (Ex: "1Y" = 1 year)
    }
    param2 = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NYSE",  # Stock exchange symbol on which stock is traded (ex: "NYSE")
        'p': "15Y"  # Period (Ex: "1Y" = 1 year)
    }
    df = get_price_data(param1)
    if df.empty:
        df = get_price_data(param2)
    return df
Exemple #10
0
def get_data(security):
    param = {
        'q': security, # Stock symbol (ex: "AAPL")
        'i': "86400", # Interval size in seconds ("86400" = 1 day intervals)
        'x': "BMV", # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y" # Period (Ex: "1Y" = 1 year)
    }
    # get price data (return pandas dataframe)
    df = get_price_data(param)
    return df
def getGdata(id=None, interval_size=86400, stock_exchange_symbol=None, period="1Y"):
    if (not id or not stock_exchange_symbol):
        print("MISSING params")
        return;
    param = {
        'q': id, # Stock symbol (ex: "AAPL")
        'i': interval_size, # Interval size in seconds ("86400" = 1 day intervals)
        'x': stock_exchange_symbol, # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': period # Period (Ex: "1Y" = 1 year)
    }
    return get_price_data(param)
Exemple #12
0
def getLivePrice(shareCode, shareExchange):
    #print(shareCode, shareExchange)
    params = {
        'q': shareCode,
        'x': shareExchange,
        'i': "1",
    }
    data_list = get_price_data(params)
    #print(data_list)
    data = pd.DataFrame(data_list)
    price = data.at[data.last_valid_index(), 'High']
    return price
    def get_current_data(self):
        print("Getting current data...")
        df = get_price_data(self.param_choice)

        ### Get data from alphavantage #######################################
        symbol = "NZ50G"
        # symbol = "GOOG"
        # symbol = ".DJI"

        api_key = "W8Q9GP1SHM5OK409"

        function = "TIME_SERIES_INTRADAY"
        interval = "1min"
        url_interday = "https://www.alphavantage.co/query?function=%s&symbol=%s&outputsize=compact&interval=%s&apikey=%s" % (
            function, symbol, interval, api_key)

        r = requests.get(url_interday)

        data = r.json()
        close_list = []
        # print(data)
        for key in data:
            for inner_key in data[key]:
                record = data[key][inner_key]
                if isinstance(record, dict):
                    close = record["4. close"]
                    close_list.append(close)
        close_list = close_list[:self.configs.time_steps]
        close_list.reverse()

        seq = np.matrix(close_list).transpose()
        seq = [float(np.array(i)) for i in seq]

        seq = [
            np.array(seq[i * self.input_size:(i + 1) * self.input_size])
            for i in range(len(seq) // self.input_size)
        ]

        # Split into groups of `num_steps`

        X = seq

        # normalize = False
        normalize = True
        normFactorX = np.mean(X)
        self.normFactorX = normFactorX
        if normalize:
            X = X / normFactorX
        else:
            normFactorX = 1

        self.normFactor = normFactorX
        return [X]
Exemple #14
0
def print_single_stock():
    param = {
        'q': "VGHCX",  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "MUTF",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1M"  # Period (Ex: "1Y" = 1 year)
    }

    # get price data (return pandas dataframe)
    df = get_price_data(param)
    print(df[-5:])
Exemple #15
0
def update_stock_history(stock, period):
    param = {
        'q': stock,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        #'i': "300", 		# Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "TPE",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': period  # Period (Ex: "1Y" = 1 year)
    }

    df = get_price_data(param)
    return df
Exemple #16
0
def price_last_52_week(a):
    # Dow Jones
    param = {
        'q': a,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "INDEXDJX",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }
    # get price data (return pandasR dataframe)
    df = get_price_data(param)

    return df
def get_stock(symbol, time_stamp='', interval=60, period='5d'):
    param = {
        'q': symbol, # Stock symbol (ex: "AAPL")
        'i': interval, # Interval size in seconds ("86400" = 1 day intervals)
        'p': period # Period
    }

    df = get_price_data(param)
    file_path = './data/'+symbol+'_'+time_stamp+'.xlsx'
    writer = pd.ExcelWriter(file_path)
    df.to_excel(writer,'Sheet1')
    writer.save()
    time.sleep(1)
def fetch():
    with open(os.path.join("./parsed/", "training_data.json"), 'w') as init:
        init.write(json.dumps({}))

    data = json.load(open('./parsed/filtered_titles.json'))
    count = 0
    for majorkey, subdict in data.items():
        title = majorkey
        symbol = subdict['symbol']
        date = subdict['date']

        params = {
            'q': symbol,  # Symbol
            'i': "3600",  # Interval (seconds)
            'x': 'LON',  # Exchange
            'p': "1Y"  # Period from today (d, Y)
        }

        try:

            historical_data = get_price_data(
                params
            )  # Updated local wrapper version to also return index as datetime
            json_port = json.loads(historical_data.to_json(orient='index'))

            market_open_time = '09:00:00'  # From data point's im getting, this is the shortest time I can get. UTC

            # Time key to verify day open existence
            time_key = str(date) + ' ' + str(market_open_time)
            if time_key in json_port:

                if int(json_port[date + ' ' + market_open_time]['Close']) >= int(json_port[date + ' ' + market_open_time]['Open']) * 1.03\
                        or (json_port[date + ' ' + market_open_time]['Close']) <= int(json_port[date + ' ' + market_open_time]['Open']) * 0.97:
                    result = 1
                else:
                    result = 0
                if not os.path.exists("./final/") and result is not None:
                    os.makedirs("./final/")
                with open(os.path.join("./parsed/", "training_data.json"),
                          'r+') as output:
                    new_obj = json.loads(output.read())
                    new_obj[title] = result
                    output.seek(0)
                    # print(new_obj)
                    print(count)
                    json.dump(new_obj, output)
                db.reference('/').update({title: result})
                count += 1
        except:
            print('Oops, something went wrong!')
    def getData(self, params):
        ticker = params['ticker']
        if ticker == 'empty':
            ticker = params['custom_ticker'].upper()

        xchng = "NASD"
        param = {
            'q': ticker,  # Stock symbol (ex: "AAPL")
            'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
            'x': xchng,  # Stock exchange symbol on which stock is traded (ex: "NASD")
            'p': "3M"  # Period (Ex: "1Y" = 1 year)
        }
        df = get_price_data(param)
        return df
Exemple #20
0
def get_stock_price(istock_name):

    # Price from google finance, last 3 months average
    param = {
        'q': istock_name, # Stock symbol (ex: "AAPL")
        'i': "86400", # Interval size in seconds ("86400" = 1 day intervals)
        'x': "HEL", # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y" # Period (Ex: "1Y" = 1 year)
    }
    df = gf.get_price_data(param)
    stock_price = (df.Close.tolist())
    averaged_price = np.average(stock_price)

    return averaged_price
Exemple #21
0
 def __init__(self, ticker, dtfrom, dtto, indxlib):
     self.myhushhushkey = 'YOUAPIKEYPLEASE'
     self.ticker = ticker
     self.dtfrom = dtfrom
     self.dtto = dtto
     try:
         self.df = ql.get("WIKI/{}".format(self.ticker), trim_start=dtfrom,
                                             trim_end=dtto, authtoken=self.myhushhushkey)
     except (NotFoundError, AuthenticationError, InvalidRequestError):
         print('Invalid request: \nCheck credentials and/or params')
         quit()
     #risk free returns for risk analysis comming soon
     self.rf = ql.get("USTREASURY/BILLRATES", trim_start=dtfrom,
                                         trim_end=dtto, authtoken=self.myhushhushkey)
     self.snp = gfc.get_price_data(indxlib)
    def getData(self, params):
        ticker = params['ticker']
        if ticker == 'empty':
            ticker = params['custom_ticker'].upper()

        xchng = "NASD"

        param = {
            'q': ticker,  # Stock symbol (ex: "AAPL")
            'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
            'x': xchng,  # Stock exchange symbol on which stock is traded (ex: "NASD")
            'p': "3M"  # Period (Ex: "1Y" = 1 year)
        }
        # get price data (return pandas dataframe)
        df = get_price_data(param)
        return df
Exemple #23
0
def get_stock_data(param):
	'''based on input parameter get price data (return pandas dataframe),
	save as csv file
		:input: param
        :type: dict 
        :return: None
        :type: None
	'''
	# input should be a dict with only for pairs
	assert isinstance(param, dict) 
	assert all(map(lambda i:isinstance(i,str),param))
	assert 'q' in param # Stock symbol (ex: "AAPL")
	assert 'i' in param # Interval size in seconds ("86400" = 1 day intervals)
	assert 'x' in param # Stock exchange symbol on which stock is traded (ex: "NASD")
	assert 'p' in param # Period (Ex: "1Y" = 1 year)
	df = get_price_data(param)
	df.to_csv(param['q'] + '_' + param['i'] + '_' + param['x'] + '_' + param['p'] + '.csv')
	return 
Exemple #24
0
def create_data():
    url = "CME_CL1.csv"
    crude_oil = pd.read_csv(url, index_col=0, parse_dates=True)
    crude_oil.sort_index(inplace=True)
    crude_oil_last = crude_oil['Last']

    param = {'q': 'XOM', 'i': 86400, 'x': "NYSE", 'p': '40Y'}
    df = get_price_data(param)
    df.set_index(df.index.normalize(), inplace=True)
    stock_close = df['Close']

    oil_price, stock_price = crude_oil_last.align(stock_close, join='inner')

    split_index = int(3 * len(oil_price) / 4)
    oil_train = oil_price.iloc[:split_index]
    stock_train = oil_price.iloc[:split_index]

    oil_test = oil_price.iloc[split_index:]
    stock_test = oil_price.iloc[split_index:]

    return oil_train, stock_train, oil_test, stock_test
    def store_databases(self):

        # specify client using URI
        # specify database --> mongo_client['stocks']
        # create a collection i.e. table for each stock

        # URI: mongodb://user:[email protected]:51508/realtime_database

        connection = MongoClient(
            'mongodb://*****:*****@ds151508.mlab.com:51508/realtime_database')
        db = connection['realtime_database']
        db.authenticate('user', 'user')

        for param in self.params:
            # collection for each stock
            db_collection = db['{}'.format(param['q'])]

            ## Insert stock data as documents in a collection

            _stock_data = get_price_data(param)
            print _stock_data
            db_collection.insert_many(_stock_data.to_dict('records'))
Exemple #26
0
    def getData(self, params):
        params.pop("output_id", None)    # caching layer
        if self.params_cache != params:   # caching layer
            ticker = params['ticker']
            if ticker == 'empty':
                ticker = params['custom_ticker'].upper()

            xchng = "NASD"

            param = {
                'q': ticker,  # Stock symbol (ex: "AAPL")
                'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
                'x': xchng,  # Stock exchange symbol on which stock is traded (ex: "NASD")
                'p': "3M"  # Period (Ex: "1Y" = 1 year)
            }
            # get price data (return pandas dataframe)
            df = get_price_data(param)
            df = df.drop(['Volume'], axis=1)

            self.data_cache = df        # caching layer
            self.params_cache = params  # caching layer
        return self.data_cache
    def get_data(self):
        print(self.param_choice)
        df = get_price_data(self.param_choice)

        seq = np.matrix(df['Close'].values).transpose()
        seq = [float(np.array(i)) for i in seq]

        seq = [
            np.array(seq[i * self.input_size:(i + 1) * self.input_size])
            for i in range(len(seq) // self.input_size)
        ]

        # Split into groups of `num_steps`

        X = np.array([
            seq[i:i + self.time_step]
            for i in range(len(seq) - self.time_step)
        ])
        y = np.array([
            seq[i + self.time_step] for i in range(len(seq) - self.time_step)
        ])

        normalize = False
        normalize = True
        if normalize:
            normFactorX = np.mean(X)
            normFactory = np.mean(y)
            X = X / normFactorX
            y = y / normFactory

        train_size = len(X) - len(X) // 20

        self.train_X, self.test_X = X[:train_size], X[train_size:]
        self.train_y, self.test_y = y[:train_size], y[train_size:]

        return self.train_X, self.train_y, self.test_X, self.test_y
# PARAMETROS
indice = sys.argv[1]
#indice = 'AENA'
#valorAjuste = -1*(150+15+5+0)
ganas = 0
total = 0

# Cargamos Vectores
param = {
    'q': indice,  # Stock symbol (ex: "AAPL")
    'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
    'x': "BME",  # Stock exchange symbol on which stock is traded (ex: "NASD")
    'p': "5Y"  # Period (Ex: "1Y" = 1 year)
}
df = gfc.get_price_data(param)

beneficio = 0
dinero = 3000
for cuentaArray in range(155, 155 + 80):
    valorAjuste = -1 * (cuentaArray + 15 + 5 + 0)

    valoresVol = df['High'].values
    valoresDiff = df['Volume'].values
    valoresCopiar = df['Close'].values

    # Ajustamos los valores
    valoresCopiar = valoresCopiar[valorAjuste:]
    valoresDiff = valoresDiff[valorAjuste:]
    valoresVol = valoresVol[valorAjuste:]
Exemple #29
0
def predict_next_day(symbol, isAdjust=False):
    number_of_days = 30
    number_of_features = 5
    # Construct API Params
    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }
    # Getting the current date
    # today = dt.now()
    # end_date = today.strftime('%Y-%m-%d')
    # end_date = '2017-10-25'
    # Getting the stock data from start_date to current date
    # data = pdr.get_data_yahoo(symbol, start=start_date, end=end_date)
    data = get_price_data(param)
    # print(data)
    # print(data==None)
    data = data[
        -number_of_days:]  # Only keeping the data for the past number_of_days days

    # Create a model holder
    model = Sequential()
    model.add(LSTM(50, input_shape=(number_of_days, 1)))
    model.add(Dense(1))
    model.compile(loss='mse', optimizer='adam')

    # # Loading model and scalers
    # scaler_filename = "saved_scalers/scaler.save." + symbol
    # scaler = joblib.load(scaler_filename)
    # model_filename = 'saved_models/weights.stock.' + symbol
    # model.load_weights(model_filename)

    # Loading model and scalers
    scaler_filename = "saved_scalers/scaler.save." + symbol
    scaler = joblib.load(scaler_filename)
    model_filename = 'saved_models/weights.stock.' + symbol
    model.load_weights(model_filename)

    # Preprocess the data
    length = len(data)
    prices = (data['Close'].values.astype('float32'))
    prices = np.reshape(prices, (length, 1))
    prices = scaler.fit_transform(prices)
    prices = np.array([prices])

    # Predict next day
    predict = model.predict(prices)
    next_day_price = scaler.inverse_transform(predict)
    """ Using adjustment model"""
    if isAdjust:

        # Create adjust model
        adj_model = Sequential()
        adj_model.add(
            Dense(units=5,
                  activation='relu',
                  input_shape=(number_of_features, )))
        adj_model.add(Dense(units=10, activation='relu'))
        adj_model.add(Dense(units=1))

        # Loading adj model and scalers
        adj_model_filename = 'saved_models/weights.stock.adjust.' + symbol
        adj_model.load_weights(adj_model_filename)
        adj_scaler_filename = "saved_scalers/scaler.save.adjust." + symbol
        adj_scaler = joblib.load(adj_scaler_filename)
        # Loading error scaler
        error_scaler_filename = "saved_scalers/scaler.save.error." + symbol
        error_scaler = joblib.load(error_scaler_filename)

        # Using adjustment model
        data_pre_scaled = data.values  #returns a numpy array
        data_scaled = adj_scaler.transform(data_pre_scaled)
        data_adj = np.array(data_scaled)

        # Use adj_model to predict difference
        error = adj_model.predict(data_adj[-1].reshape(-1, 5))

        # invert predictions and targets to unscaled
        error_unscaled = error_scaler.inverse_transform(error)

        # Add error to LSTM prediction
        next_day_adjusted_price = error_unscaled + next_day_price
        next_day_price = next_day_adjusted_price
    return next_day_price[0][0]
Exemple #30
0
def predict_next_few_days(symbol, days):

    number_of_days = 30
    # # Getting the current date
    # # today = dt.now()
    # # end_date = today.strftime('%Y-%m-%d')
    # end_date = '2017-10-31'
    # # Getting the stock data from start_date to current date
    # data = pdr.get_data_yahoo(symbol, start=start_date, end=end_date)

    # Construct API Params
    param = {
        'q': symbol,  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "NASD",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1Y"  # Period (Ex: "1Y" = 1 year)
    }
    data = get_price_data(param)  # Getting the current data
    data = data[
        -number_of_days:]  # Only keeping the data for the past number_of_days days

    # Create a model holder
    model = Sequential()
    model.add(LSTM(50, input_shape=(number_of_days, 1)))
    model.add(Dense(1))
    model.compile(loss='mse', optimizer='adam')

    # Loading model and scalers
    scaler_filename = "saved_scalers/scaler.save." + symbol
    scaler = joblib.load(scaler_filename)
    model_filename = 'saved_models/weights.stock.' + symbol
    model.load_weights(model_filename)
    # error_scaler_filename = "saved_scalers/scaler.save.error." + symbol
    # error_scaler = joblib.load(error_scaler_filename)
    # adj_model_filename = 'saved_models/weights.stock.adjust.' + symbol
    # adj_model.load_weights(adj_model_filename)

    # Preprocess the data
    length = len(data)
    prices = (data['Close'].values.astype('float32'))
    prices = np.reshape(prices, (length, 1))
    prices = scaler.fit_transform(prices)
    array_prices = np.array([prices])

    # Predict next few days
    for i in range(days):
        predict = model.predict(array_prices)
        # Add the new predicted price to the stack
        array_prices = np.append([array_prices[0][1:]],
                                 np.array(predict).reshape(1, -1, 1),
                                 axis=1)

    # To fix a bug in tensorflow with the current environment set up
    from keras import backend as K
    K.clear_session()

    # Predict the price in USD
    next_day_price = scaler.inverse_transform(predict)

    return next_day_price
Exemple #31
0
# Code Thomas

from googlefinance.client import get_price_data, get_prices_data, get_prices_time_data

# Dow Jones
param = {
    'q': ".DJI",  # Stock symbol (ex: "AAPL")
    'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
    'x':
    "INDEXDJX",  # Stock exchange symbol on which stock is traded (ex: "NASD")
    'p': "1Y",  # Period (Ex: "1Y" = 1 year)
}
# get price data (return pandas dataframe)
df = get_price_data(param)
print(df)
#                          Open      High       Low     Close     Volume
# 2016-05-17 05:00:00  17531.76   17755.8  17531.76  17710.71   88436105
# 2016-05-18 05:00:00  17701.46  17701.46  17469.92  17529.98  103253947
# 2016-05-19 05:00:00  17501.28  17636.22  17418.21  17526.62   79038923
# 2016-05-20 05:00:00  17514.16  17514.16  17331.07   17435.4   95531058
# 2016-05-21 05:00:00  17437.32  17571.75  17437.32  17500.94  111992332
# ...                       ...       ...       ...       ...        ...

params = [
    # Dow Jones
    {
        'q': ".DJI",
        'x': "INDEXDJX",
    },
    # NYSE COMPOSITE (DJ)
    {
Exemple #32
0
def move_NASDAQ():
    test_start_date = datetime.datetime.now() - datetime.timedelta(days=5)
    test_end_date = datetime.datetime.now()  #- datetime.timedelta(days = 1)
    df_stocks = pd.read_pickle('pickled_para_NDAQ.pkl')
    df_stocks['prices'] = df_stocks['close'].apply(np.int64)
    # selecting the prices and articles
    df_stocks = df_stocks[['prices', 'articles']]
    df_stocks['articles'] = df_stocks['articles'].map(lambda x: x.lstrip('.-'))
    df_stocks
    df = df_stocks[['prices']].copy()
    # Adding new columns to the data frame
    df["compound"] = ''
    df["neg"] = ''
    df["neu"] = ''
    df["pos"] = ''
    sid = SentimentIntensityAnalyzer()
    for date, row in df_stocks.T.iteritems():
        try:
            sentence = unicodedata.normalize('NFKD', df_stocks.loc[date,
                                                                   'articles'])
            ss = sid.polarity_scores(sentence)
            df.set_value(date, 'compound', ss['compound'])
            df.set_value(date, 'neg', ss['neg'])
            df.set_value(date, 'neu', ss['neu'])
            df.set_value(date, 'pos', ss['pos'])
        except TypeError:
            print(df_stocks.loc[date, 'articles'])
            print(date)

    test = df.ix[test_start_date:test_end_date]

    ##
    #test_start_date = '2018-03-09'
    #test_end_date = '2018-03-10'
    test = df.ix[test_start_date:test_end_date]

    ##

    # Calculating the sentiment score
    sentiment_score_list = []
    for date, row in test.T.iteritems():
        sentiment_score = np.asarray([
            df.loc[date, 'compound'], df.loc[date, 'neg'], df.loc[date, 'neu'],
            df.loc[date, 'pos']
        ])
        #sentiment_score = np.asarray([df.loc[date, 'neg'],df.loc[date, 'pos']])
        sentiment_score_list.append(sentiment_score)
        numpy_df_test = np.asarray(sentiment_score_list)

    filename = 'finalized_model_NDAQ.sav'
    loaded_model = pickle.load(open(filename, 'rb'))
    #loaded_model.fit(numpy_df_test,test['prices'])
    result = loaded_model.predict(numpy_df_test)

    difference = result[1] - result[0]
    if (difference > 0):
        sentence = "Stock Price will rise"
    else:
        sentence = "Stock Price will fall"
    param = {
        'q': ".IXIC",  # Stock symbol (ex: "AAPL")
        'i': "86400",  # Interval size in seconds ("86400" = 1 day intervals)
        'x':
        "INDEXNASDAQ",  # Stock exchange symbol on which stock is traded (ex: "NASD")
        'p': "1M"  # Period (Ex: "1Y" = 1 year)
    }
    df = get_price_data(param)
    df.to_csv('C:/Users/ansha/Anaconda3/FlaskApp/nasd1.csv')
    line = pd.read_csv("nasd1.csv", index_col=False)

    df = pd.DataFrame(data=line)

    dates = df['Unnamed: 0']

    o = df['Open']
    h = df['High']
    l = df['Low']
    c = df['Close']
    line_chart = pygal.Line(x_label_rotation=20,
                            x_labels_major_every=3,
                            show_minor_x_labels=False,
                            human_readable=True)
    line_chart.title = 'NASDAQ'
    line_chart.x_labels = map(str, dates)
    line_chart.add('Open', o)
    line_chart.add('High', h)
    line_chart.add('Low', l)
    line_chart.add('Close', c)
    graph_data = line_chart.render_data_uri()

    return render_template("an1.html",
                           data=sentence,
                           graph_data=graph_data,
                           data1=difference)





if __name__ == "__main__":

    # get price data (return pandas dataframe)
    param = {
	'q': "0050.TW", # Stock symbol (ex: "AAPL")
	'i': "86400", # Interval size in seconds ("86400" = 1 day intervals)
	#'x': "TWSE", # Stock exchange symbol on which stock is traded (ex: "NASD")
	'p': "2Y" # Period (Ex: "1Y" = 1 year)
    }
    price_df = get_price_data(param)
    print (price_df)

    # calculate KD
    kd = Get_kd(price_df)
    print (kd)

    today_price = price_df.tail(1)
    user = ""
    pwd = ""
    recipient = [""]
    subject = "%s KD analysis of 0050.TW" % date.today()
    body = """
    %s
    0050.TW
    Open: %d