chemouda commited on
Commit
20da752
·
verified ·
1 Parent(s): 89d0ad2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from crypto_analysis import analyze_crypto, get_top_crypto_symbols
4
+ from asset_analysis import analyze_asset, get_sp500_tickers
5
+ import os
6
+ from datetime import datetime, timedelta
7
+ import logging
8
+
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
+ logger = logging.getLogger(__name__)
12
+
13
+ def run_crypto_analysis(symbols, interval):
14
+ end_date = datetime.today().strftime("%Y-%m-%d")
15
+ start_date = (datetime.today() - timedelta(days=365*5)).strftime("%Y-%m-%d")
16
+ if symbols:
17
+ symbols_list = [symbol.strip().upper() for symbol in symbols.split(",")]
18
+ else:
19
+ symbols_list = get_top_crypto_symbols()[:10] # Limit to top 10 for faster processing
20
+
21
+ all_data = []
22
+ for symbol in symbols_list:
23
+ data = analyze_crypto(symbol, start_date, end_date, interval)
24
+ if data is not None and not data.empty:
25
+ data['Symbol'] = symbol
26
+ all_data.append(data)
27
+
28
+ if all_data:
29
+ combined_data = pd.concat(all_data)
30
+ combined_data = combined_data.reset_index()
31
+ combined_data = combined_data[['Date', 'Symbol', 'Close', 'Signal_1x', 'Signal_2x', 'Signal_3x', 'VuManchu_Signal']]
32
+ combined_data['Date'] = combined_data['Date'].dt.date
33
+ combined_data = combined_data.sort_values('Date', ascending=False) # Sort by date in descending order
34
+
35
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
36
+ output_file = f"output/all_crypto_signals_{interval}_{timestamp}.csv"
37
+ os.makedirs("output", exist_ok=True)
38
+ combined_data.to_csv(output_file, index=False)
39
+ logger.info(f"Crypto analysis complete. Output saved to {output_file}")
40
+ return combined_data.head(15), output_file
41
+ else:
42
+ logger.warning("No data available for the selected crypto parameters.")
43
+ return "No data available for the selected parameters.", None
44
+
45
+ def run_asset_analysis(symbols, interval):
46
+ end_date = datetime.today().strftime("%Y-%m-%d")
47
+ start_date = (datetime.today() - timedelta(days=365*5)).strftime("%Y-%m-%d")
48
+ if symbols:
49
+ symbols_list = [symbol.strip().upper() for symbol in symbols.split(",")]
50
+ else:
51
+ symbols_list = get_sp500_tickers()[:10] # Limit to top 10 for faster processing
52
+
53
+ all_data = []
54
+ for symbol in symbols_list:
55
+ data = analyze_asset(symbol, start_date, end_date, interval, asset_type='stock')
56
+ if data is not None and not data.empty:
57
+ data['Symbol'] = symbol
58
+ all_data.append(data)
59
+
60
+ if all_data:
61
+ combined_data = pd.concat(all_data)
62
+ combined_data = combined_data.reset_index()
63
+ combined_data = combined_data[['Date', 'Symbol', 'Close', 'Signal_1x', 'Signal_2x', 'Signal_3x', 'VuManchu_Signal']]
64
+ combined_data['Date'] = combined_data['Date'].dt.date
65
+ combined_data = combined_data.sort_values('Date', ascending=False) # Sort by date in descending order
66
+
67
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
68
+ output_file = f"output/all_stocks_signals_{interval}_{timestamp}.csv"
69
+ os.makedirs("output", exist_ok=True)
70
+ combined_data.to_csv(output_file, index=False)
71
+ logger.info(f"Asset analysis complete. Output saved to {output_file}")
72
+ return combined_data.head(15), output_file
73
+ else:
74
+ logger.warning("No data available for the selected asset parameters.")
75
+ return "No data available for the selected parameters.", None
76
+
77
+ def main_interface(analysis_type, symbols, interval):
78
+ logger.info(f"Main interface called with: analysis_type={analysis_type}, symbols={symbols}, interval={interval}")
79
+ try:
80
+ if analysis_type == "Cryptocurrency":
81
+ result, file = run_crypto_analysis(symbols, interval)
82
+ else:
83
+ result, file = run_asset_analysis(symbols, interval)
84
+
85
+ if isinstance(result, pd.DataFrame):
86
+ return result, file
87
+ else:
88
+ return result, None
89
+ except Exception as e:
90
+ logger.error(f"An error occurred in main_interface: {e}")
91
+ return f"An error occurred: {str(e)}", None
92
+
93
+ with gr.Blocks() as iface:
94
+ gr.Markdown("# VuManchu Trading Signals Analysis")
95
+ gr.Markdown("Perform technical analysis on cryptocurrencies or stocks using the VuManchu swing trading strategy and SuperTrend indicators. Select the analysis type, input desired symbols or use the defaults, choose the time interval, and view or download the generated trading signals. The sample shows the 15 most recent entries.")
96
+
97
+ with gr.Row():
98
+ analysis_type = gr.Radio(["Cryptocurrency", "Asset"], label="Select Analysis Type")
99
+ symbols = gr.Textbox(label="Enter symbols (comma-separated) or leave blank for default", placeholder="e.g., BTC,ETH,ADA or AAPL,MSFT,GOOGL")
100
+ interval = gr.Radio(["1d", "1wk"], label="Select Time Interval")
101
+
102
+ submit_button = gr.Button("Generate Signals")
103
+
104
+ output_dataframe = gr.Dataframe(label="Sample of Latest Trading Signals: High to Low Risk ordered")
105
+ output_file = gr.File(label="Download Full Signals CSV")
106
+
107
+ submit_button.click(
108
+ main_interface,
109
+ inputs=[analysis_type, symbols, interval],
110
+ outputs=[output_dataframe, output_file]
111
+ )
112
+
113
+ if __name__ == "__main__":
114
+ logger.info("Starting Gradio interface")
115
+ iface.launch(server_name="0.0.0.0", server_port=7860, share=True)