Ejemplo n.º 1
0
 def handle(self, *args, **options):
     logger.info('Seeding Stock price data...')
     for stock in Stock.objects.all():
         logger.info('Seeding {}...'.format(stock.ticker))
         service = StockDataService(ticker=stock.ticker, name=stock.name, exchange=stock.exchange)
         df_util.bulk_stock_price_data_to_model(service.get_stock_data())
         logger.info('Finished seeding {}...'.format(stock.ticker))
     logger.info('Finished seeding stock data.')
Ejemplo n.º 2
0
def get_latest_stock_data(ticker):
    """
    Method that gets the latest data for a Stock
    :param ticker: Ticker of Stock to get data for
    """
    from stock.models import StockPriceData
    from stock.services import StockDataService

    df = StockDataService.get_stock_data(ticker, period='1d', interval='1m')
    StockPriceData.objects.create_df_data(df)
    print('Added {} minute data'.format(ticker))
Ejemplo n.º 3
0
def get_bulk_day_stock_data(ticker):
    """
    Method that that gets the day data for a Stock
    :param ticker: Stock symbool to get data for
    """
    from stock.models import StockPriceData
    from stock.services import StockDataService

    df = StockDataService.get_stock_data(ticker, period='1y', interval='1d')
    StockPriceData.objects.create_bulk_data(df)
    print('Added {} daily data'.format(ticker))
Ejemplo n.º 4
0
    def create_stocks(self):
        """ Method for creating all of the classes listed stocks """
        for link in self._stock_links:
            resp = requests.get(link)
            soup = bs.BeautifulSoup(resp.text, "lxml")
            table = soup.find('table', {'class': 'quotes'})

            for row in table.findAll('tr')[1:]:
                ticker = row.findAll('td')[0].text
                mapping = str.maketrans(".", "-")
                ticker = ticker.translate(mapping) + ".L"
                name = row.findAll('td')[1].text
                StockDataService(ticker=ticker,
                                 name=name,
                                 exchange=self._symbol).create_stock()
Ejemplo n.º 5
0
def bulk_get_exchanges_minute_data(exchange_symbol):
    """
    Method that gets a specific exchanges listed stocks minute price data and
    creates them in bulk
    :param exchange_symbol: Exchange symbol to get data for
    """
    from exchange.models import Exchange
    from stock.models import Stock, StockPriceData
    from stock.services import StockDataService

    exchange = Exchange.objects.get(symbol=exchange_symbol)
    stocks = Stock.objects.filter(exchange=exchange).order_by('symbol')

    for stock in stocks:
        df = StockDataService.get_stock_data(stock.symbol, 'max', '1m')
        StockPriceData.objects.create_bulk_data(df)
        print('Added {} daily minute data'.format(stock.symbol))
Ejemplo n.º 6
0
def get_exchanges_minute_data(exchange_symbol):
    """
    Method that iterates through an exchange and all of its listed stocks
    and runs the stock_tasks get_day_stock_data method to get the minute data
    for that stock.
    :param exchange_symbol: Exchange symbol to get stocks for
    """
    from exchange.models import Exchange
    from stock.models import Stock, StockPriceData
    from stock.services import StockDataService

    exchange = Exchange.objects.get(symbol=exchange_symbol)
    stocks = Stock.objects.filter(exchange=exchange)

    for stock in stocks:
        df = StockDataService.get_stock_data(stock.ticker, '1d', '1m')
        StockPriceData.objects.create_df_data(df)
        print('Added {} minute data')
Ejemplo n.º 7
0
    def compile_stocks_close_data(self):
        """
        Compiles the closing price data of the stocks and returns a
        dataframe with dates and the stocks respective closing price
        on that date.
        :return: Dataframe of stock closing prices on dates
        """
        main_df = pd.DataFrame()

        # Iterates through the stocks and gets their relevant price information and puts it into
        # a combined df, main_df
        for stock in self._stocks:
            df = StockDataService(ticker=stock).get_price_data()

            # Check that the stock has any price data available
            if not df.empty:
                # Format the df to have the timestamp column as the index and then name the close column
                # the stocks symbol so it can be identified and then drop the data that's not needed
                df.set_index('timestamp', inplace=True)
                df.rename(columns={'close': stock}, inplace=True)
                df.drop([
                    'id', 'stock_id', 'open', 'high', 'low', 'volume',
                    'change', 'change_perc', 'ml_prediction'
                ],
                        1,
                        inplace=True)

                # Set the column to the float dtype instead of object, this is needed unlike
                # index example as thats read from csv into float dtype automatically
                df[stock] = df[stock].astype(float)

                # If main df is empty set it to the df otherwise add it to the
                # existing main df
                if main_df.empty:
                    main_df = df
                else:
                    main_df = main_df.join(df, how='outer')

        main_df.reset_index(drop=True, inplace=True)
        return main_df