chemouda commited on
Commit
c23f932
1 Parent(s): b3c2aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -96,32 +96,38 @@ def run_asset_analysis(symbols, interval):
96
  logger.warning("No data available for any of the selected asset parameters.")
97
  return pd.DataFrame(), None
98
 
99
- def filter_latest_signals(df, signal_column):
100
  if df.empty:
101
  logger.warning("Empty dataframe passed to filter_latest_signals")
102
  return df
103
 
104
  logger.info(f"Filtering signals. Input shape: {df.shape}")
105
 
106
- # Group by Symbol and get the latest date for each
107
- latest_dates = df.groupby('Symbol')['Date'].max().reset_index()
 
 
 
 
 
 
108
 
109
- # Merge with the original dataframe to get the latest signals
110
- latest_signals = pd.merge(df, latest_dates, on=['Symbol', 'Date'])
111
 
112
- logger.info(f"Latest signals shape after merge: {latest_signals.shape}")
113
 
114
  # Filter for the chosen signal
115
  if signal_column != 'All':
116
- latest_signals = latest_signals[latest_signals[signal_column] != '']
117
- logger.info(f"Filtered for {signal_column}. Shape: {latest_signals.shape}")
118
  else:
119
  # Remove rows where all signal columns are empty
120
  signal_columns = ['Signal_1x', 'Signal_2x', 'Signal_3x', 'VuManchu_Signal']
121
- latest_signals = latest_signals[latest_signals[signal_columns].ne('').any(axis=1)]
122
- logger.info(f"Removed rows with all empty signals. Shape: {latest_signals.shape}")
123
 
124
- result = latest_signals.sort_values('Date', ascending=False)
125
  logger.info(f"Final filtered result shape: {result.shape}")
126
  return result
127
 
@@ -136,7 +142,7 @@ def generate_signals(analysis_type, symbols, interval):
136
 
137
  if isinstance(full_results, pd.DataFrame) and not full_results.empty:
138
  logger.info(f"Analysis result shape: {full_results.shape}")
139
- filtered_result = filter_latest_signals(full_results, 'All')
140
  logger.info(f"Filtered result shape: {filtered_result.shape}")
141
  return filtered_result, output_file_path
142
  else:
@@ -151,7 +157,7 @@ def apply_filter(signal_filter):
151
  if full_results is None or full_results.empty:
152
  return "No data available. Please generate signals first.", None
153
 
154
- filtered_result = filter_latest_signals(full_results, signal_filter)
155
  return filtered_result, output_file_path
156
 
157
  with gr.Blocks() as iface:
@@ -176,7 +182,7 @@ with gr.Blocks() as iface:
176
  Use of this tool constitutes acceptance of this disclaimer and an acknowledgment of the inherent risks associated with trading and investing.
177
  """)
178
 
179
- 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 table shows the latest trading signals for each symbol, excluding rows with no signals.")
180
 
181
  with gr.Row():
182
  analysis_type = gr.Radio(["Cryptocurrency", "Asset"], label="Select Analysis Type")
@@ -184,11 +190,11 @@ with gr.Blocks() as iface:
184
  interval = gr.Radio(["1d", "1wk"], label="Select Time Interval")
185
 
186
  with gr.Row():
187
- signal_filter = gr.Dropdown(["All", "Signal_1x", "Signal_2x", "Signal_3x", "VuManchu_Signal"], label="Filter by Latest Signal", value="All")
188
 
189
  generate_button = gr.Button("Generate Signals")
190
 
191
- output_dataframe = gr.Dataframe(label="Latest Trading Signals")
192
  output_file = gr.File(label="Download Full Signals CSV")
193
 
194
  generate_button.click(
 
96
  logger.warning("No data available for any of the selected asset parameters.")
97
  return pd.DataFrame(), None
98
 
99
+ def filter_latest_signals(df, signal_column, interval):
100
  if df.empty:
101
  logger.warning("Empty dataframe passed to filter_latest_signals")
102
  return df
103
 
104
  logger.info(f"Filtering signals. Input shape: {df.shape}")
105
 
106
+ # Determine the date range based on the interval
107
+ today = datetime.now().date()
108
+ if interval == '1d':
109
+ start_date = today - timedelta(days=15) # Last week for daily interval
110
+ elif interval == '1wk':
111
+ start_date = today - timedelta(days=60) # Last month for weekly interval
112
+ else:
113
+ start_date = today - timedelta(days=15) # Default to last week
114
 
115
+ # Filter the dataframe for the specified date range
116
+ df_filtered = df[df['Date'] >= start_date]
117
 
118
+ logger.info(f"Filtered for date range. Shape: {df_filtered.shape}")
119
 
120
  # Filter for the chosen signal
121
  if signal_column != 'All':
122
+ df_filtered = df_filtered[df_filtered[signal_column] != '']
123
+ logger.info(f"Filtered for {signal_column}. Shape: {df_filtered.shape}")
124
  else:
125
  # Remove rows where all signal columns are empty
126
  signal_columns = ['Signal_1x', 'Signal_2x', 'Signal_3x', 'VuManchu_Signal']
127
+ df_filtered = df_filtered[df_filtered[signal_columns].ne('').any(axis=1)]
128
+ logger.info(f"Removed rows with all empty signals. Shape: {df_filtered.shape}")
129
 
130
+ result = df_filtered.sort_values('Date', ascending=False)
131
  logger.info(f"Final filtered result shape: {result.shape}")
132
  return result
133
 
 
142
 
143
  if isinstance(full_results, pd.DataFrame) and not full_results.empty:
144
  logger.info(f"Analysis result shape: {full_results.shape}")
145
+ filtered_result = filter_latest_signals(full_results, 'All', interval)
146
  logger.info(f"Filtered result shape: {filtered_result.shape}")
147
  return filtered_result, output_file_path
148
  else:
 
157
  if full_results is None or full_results.empty:
158
  return "No data available. Please generate signals first.", None
159
 
160
+ filtered_result = filter_latest_signals(full_results, signal_filter, interval)
161
  return filtered_result, output_file_path
162
 
163
  with gr.Blocks() as iface:
 
182
  Use of this tool constitutes acceptance of this disclaimer and an acknowledgment of the inherent risks associated with trading and investing.
183
  """)
184
 
185
+ 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 table shows the trading signals for the last week (1d interval) or last month (1wk interval) for each symbol, excluding rows with no signals.")
186
 
187
  with gr.Row():
188
  analysis_type = gr.Radio(["Cryptocurrency", "Asset"], label="Select Analysis Type")
 
190
  interval = gr.Radio(["1d", "1wk"], label="Select Time Interval")
191
 
192
  with gr.Row():
193
+ signal_filter = gr.Dropdown(["All", "Signal_1x", "Signal_2x", "Signal_3x", "VuManchu_Signal"], label="Filter by Signal", value="All")
194
 
195
  generate_button = gr.Button("Generate Signals")
196
 
197
+ output_dataframe = gr.Dataframe(label="Trading Signals")
198
  output_file = gr.File(label="Download Full Signals CSV")
199
 
200
  generate_button.click(