Spaces:
Paused
Paused
Update app.py
Browse files97 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514697 unmodified lines# Gradio Interfacedef predict_and_trade(stock_ticker, api_key, api_secret, base_url):# Fetch datastock_data = yf.download(stock_ticker, period="1y", interval="1d")stock_data = append_indicators(stock_data)# Prepare datasequence_length = 60X, y, scaler = prepare_data(stock_data, sequence_length)# Build and train the modelinput_shape = (X.shape[1], X.shape[2])model = build_lstm_model(input_shape)model = train_model(model, X, y)# Make predictionspredictions = generate_predictions(model, stock_data[['Close', 'RSI', 'VWAP', 'BBU_20_2.0', 'BBM_20_2.0', 'BBL_20_2.0']], scaler, sequence_length)# Create the Alpaca clientclient = create_client(api_key, api_secret, base_url)# Place orders based on predictionscurrent_price = stock_data['Close'].iloc[-1]rsi = stock_data['RSI'].iloc[-1]orders = placing_order(predictions, client, stock_ticker, current_price, rsi)return predictions.tolist(), [order.json() for order in orders]# Gradio Interface Setupinput_stock = gr.Textbox(label="Stock Ticker (e.g., AAPL)")input_api_key = gr.Textbox(label="Alpaca API Key", type="password")input_api_secret = gr.Textbox(label="Alpaca API Secret", type="password")input_base_url = gr.Textbox(label="Alpaca Base URL", value="https://paper-api.alpaca.markets")output_predictions = gr.Dataframe(label="Predicted Prices")output_orders = gr.JSON(label="Placed Orders")interface = gr.Interface(fn=predict_and_trade,inputs=[input_stock, input_api_key, input_api_secret, input_base_url],outputs=[output_predictions, output_orders],description="Predict stock prices and place trades using an LSTM model and Alpaca API.")if __name__ == "__main__":interface.launch(share=True)97 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315497 unmodified lines# Gradio Interfacedef predict_and_trade(stock_ticker, api_key, api_secret, base_url):tickers = [ticker.strip() for ticker in stock_ticker.split(',')]results = []for ticker in tickers:# Fetch datastock_data = yf.download(ticker, period="1y", interval="1d")stock_data = append_indicators(stock_data)# Prepare datasequence_length = 60X, y, scaler = prepare_data(stock_data, sequence_length)# Build and train the modelinput_shape = (X.shape[1], X.shape[2])model = build_lstm_model(input_shape)model = train_model(model, X, y)# Make predictionspredictions = generate_predictions(model, stock_data[['Close', 'RSI', 'VWAP', 'BBU_20_2.0', 'BBM_20_2.0', 'BBL_20_2.0']], scaler, sequence_length)# Create the Alpaca clientclient = create_client(api_key, api_secret, base_url)# Place orders based on predictionscurrent_price = stock_data['Close'].iloc[-1]rsi = stock_data['RSI'].iloc[-1]print("error in placing order")orders = placing_order(predictions, client, ticker, current_price, rsi)print("error in appending result")results.append([ticker,predictions.tolist(),[order.json() for order in orders]])print("error in returning")return results# Gradio Interface Setupinput_stock = gr.Textbox(label="Stock Ticker (e.g., AAPL, MSFT, GOOGL)")input_api_key = gr.Textbox(label="Alpaca API Key", type="password")input_api_secret = gr.Textbox(label="Alpaca API Secret", type="password")input_base_url = gr.Textbox(label="Alpaca Base URL", value="https://paper-api.alpaca.markets/v2")output_results = gr.Dataframe(label="Results")interface = gr.Interface(fn=predict_and_trade,inputs=[input_stock, input_api_key, input_api_secret, input_base_url],outputs=output_results,description="Predict stock prices and place trades using an LSTM model and Alpaca API.")if __name__ == "__main__":interface.launch(debug=True, share=True)
app.py+41-33