import pandas as pd import numpy as np import yfinance as yf from sklearn.preprocessing…
- User
import pandas as pd import numpy as np import yfinance as yf from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor from sklearn.metrics import mean_squared_error, r2_score from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Dropout from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import EarlyStopping import tensorflow as tf from concurrent.futures import ThreadPoolExecutor, as_completed import time from tqdm.notebook import tqdm import warnings import pandas_ta as ta # Ensure consistent usage
warnings.filterwarnings('ignore') tf.random.set_seed(42) np.random.seed(42)
Function to get list of Indian stocks
def get_indian_stocks(): nse_url = "https://archives.nseindia.com/content/equities/EQUITY_L.csv" try: nse_df = pd.read_csv(nse_url) return [f"{symbol}.NS" for symbol in nse_df['SYMBOL'].tolist()] except Exception as e: print(f"Error fetching Indian stocks list: {str(e)}") return []
Function to fetch stock data with retries and multiple periods
def get_stock_data(symbol, periods=["5y", "max"], retries=3): """ Fetches historical stock data for a given symbol using available periods.
codeParameters: - symbol (str): Stock symbol. - periods (list): List of periods to attempt fetching. - retries (int): Number of retry attempts per period. Returns: - pd.DataFrame or None: Historical stock data or None if fetching fails. """ for period in periods: for attempt in range(retries): try: stock = yf.Ticker(symbol) df = stock.history(period=period) if df.empty: raise ValueError("Empty DataFrame returned") if len(df) < 400: # Ensure we have enough data print(f"Insufficient data for {symbol} with period '{period}'. Trying next period...") break # Move to the next period return df except Exception as e: if attempt == retries - 1: print(f"Error fetching data for {symbol} with period '{period}': {str(e)}") time.sleep(1) # Wait before retrying return None - Assistant
- User
Are there any ready made models which can be used for even faster compilation
- Assistant
- User
Can you generate the entire program using one of these pretrained but fast and accurate algorithms/transformers/whatever?
- Assistant
- User
But that's not what my program is doing is it? My code is scanning the entire Indian market without any hardcoding to identify stocks to invest in.
- Assistant