Badumetsibb commited on
Commit
39b18f2
·
verified ·
1 Parent(s): 781e5f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -8
app.py CHANGED
@@ -129,17 +129,67 @@ class CausalReasoningNetwork:
129
  # ... (Same as your v15 code)
130
  atr=self.data[volatility_indicator];low_vol_threshold=atr.quantile(0.33);high_vol_threshold=atr.quantile(0.66);ema_slope=self.data[trend_indicator].diff(periods=3);regimes=[];[regimes.append('TRENDING' if abs((ema_slope.iloc[i] if pd.notna(ema_slope.iloc[i]) else 0)) > ema_slope.quantile(0.75) else 'BREAKOUT') if atr.iloc[i]>high_vol_threshold else regimes.append('RANGING') if atr.iloc[i]<low_vol_threshold else regimes.append('CHOPPY') for i in range(len(self.data))];self.data['regime']=regimes;return self.data
131
 
 
132
  class RuleBasedSituationRoom:
133
- def __init__(self, params): self.params = params
 
 
134
  def generate_thesis(self, predictions, sequence_df):
135
- # ... (Same as your v15 code)
136
- latest_data=sequence_df.iloc[-1];current_price=latest_data['close']
137
- if not predictions:dir_ema="BUY" if current_price>latest_data['EMA_20'] else "SELL";action,confidence,strategy,reasoning=dir_ema,"LOW","Trend Following",f"Simple EMA Crossover ({dir_ema})."
138
- else:dir_5m="BUY" if predictions['5m']>current_price else "SELL";dir_15m="BUY" if predictions['15m']>current_price else "SELL";dir_1h="BUY" if predictions['1h']>current_price else "SELL";action,confidence,reasoning,strategy="NO_TRADE","LOW","Prediction horizons diverge.","Range Play";(action,confidence,reasoning,strategy)=(dir_5m,"HIGH",f"Strong confluence across all horizons ({dir_5m}).","Trend Following") if dir_5m==dir_15m==dir_1h else (action,confidence,reasoning,strategy)=(dir_5m,"MEDIUM",f"Short/Medium-term confluence ({dir_5m}).","Scalp") if dir_5m==dir_15m else (action,confidence,reasoning,strategy)=(dir_15m,"LOW",f"Medium/Long-term confluence ({dir_15m}).","Trend Following") if dir_15m==dir_1h else None
139
- if action=="NO_TRADE":return {"action":"NO_TRADE","confidence":"LOW","strategy_type":strategy,"reasoning":reasoning}
140
- atr=latest_data['ATR'] if pd.notna(latest_data['ATR']) and latest_data['ATR']>0 else 0.0001;sl_mult,tp_mult=self.params.get('sl_atr_multiplier',2.0),self.params.get('tp_atr_multiplier',4.0);tp_mult*=0.75 if confidence=="MEDIUM" else 0.5 if confidence=="LOW" else 1.0
141
- entry,stop_loss,take_profit=(current_price,current_price-(sl_mult*atr),current_price+(tp_mult*atr)) if action=="BUY" else (current_price,current_price+(sl_mult*atr),current_price-(tp_mult*atr));return {"action":action,"entry":f"{entry:.5f}","stop_loss":f"{stop_loss:.5f}","take_profit":f"{take_profit:.5f}","confidence":confidence,"reasoning":reasoning,"strategy_type":strategy}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
 
 
 
 
 
 
 
 
 
143
  class MarketRegimeFilter:
144
  def __init__(self): self.allowed_strategies = {'TRENDING': ['Trend Following'], 'BREAKOUT': ['Trend Following', 'Scalp'], 'CHOPPY': ['Scalp'], 'RANGING': []}
145
  def should_trade(self, current_regime, trade_thesis): return False if trade_thesis['action'] == 'NO_TRADE' else trade_thesis['strategy_type'] in self.allowed_strategies.get(current_regime, [])
 
129
  # ... (Same as your v15 code)
130
  atr=self.data[volatility_indicator];low_vol_threshold=atr.quantile(0.33);high_vol_threshold=atr.quantile(0.66);ema_slope=self.data[trend_indicator].diff(periods=3);regimes=[];[regimes.append('TRENDING' if abs((ema_slope.iloc[i] if pd.notna(ema_slope.iloc[i]) else 0)) > ema_slope.quantile(0.75) else 'BREAKOUT') if atr.iloc[i]>high_vol_threshold else regimes.append('RANGING') if atr.iloc[i]<low_vol_threshold else regimes.append('CHOPPY') for i in range(len(self.data))];self.data['regime']=regimes;return self.data
131
 
132
+ # THIS IS THE NEW, FIXED CLASS
133
  class RuleBasedSituationRoom:
134
+ def __init__(self, params):
135
+ self.params = params
136
+
137
  def generate_thesis(self, predictions, sequence_df):
138
+ latest_data = sequence_df.iloc[-1]
139
+ current_price = latest_data['close']
140
+
141
+ # Default case: EMA Crossover
142
+ if not predictions:
143
+ action = "BUY" if current_price > latest_data['EMA_20'] else "SELL"
144
+ confidence = "LOW"
145
+ strategy = "Trend Following"
146
+ reasoning = f"Simple EMA Crossover ({action})."
147
+ # Predictive model case
148
+ else:
149
+ dir_5m = "BUY" if predictions['5m'] > current_price else "SELL"
150
+ dir_15m = "BUY" if predictions['15m'] > current_price else "SELL"
151
+ dir_1h = "BUY" if predictions['1h'] > current_price else "SELL"
152
+
153
+ # This is the corrected if/elif/else block
154
+ if dir_5m == dir_15m == dir_1h:
155
+ action, confidence, reasoning, strategy = dir_5m, "HIGH", f"Strong confluence across all horizons ({dir_5m}).", "Trend Following"
156
+ elif dir_5m == dir_15m:
157
+ action, confidence, reasoning, strategy = dir_5m, "MEDIUM", f"Short/Medium-term confluence ({dir_5m}).", "Scalp"
158
+ elif dir_15m == dir_1h:
159
+ action, confidence, reasoning, strategy = dir_15m, "LOW", f"Medium/Long-term confluence ({dir_15m}).", "Trend Following"
160
+ else:
161
+ action, confidence, reasoning, strategy = "NO_TRADE", "LOW", "Prediction horizons diverge.", "Range Play"
162
+
163
+ if action == "NO_TRADE":
164
+ return {"action": "NO_TRADE", "confidence": "LOW", "strategy_type": strategy, "reasoning": reasoning}
165
+
166
+ atr = latest_data['ATR'] if pd.notna(latest_data['ATR']) and latest_data['ATR'] > 0 else 0.0001
167
+ sl_mult = self.params.get('sl_atr_multiplier', 2.0)
168
+ tp_mult = self.params.get('tp_atr_multiplier', 4.0)
169
+
170
+ if confidence == "MEDIUM":
171
+ tp_mult *= 0.75
172
+ elif confidence == "LOW":
173
+ tp_mult *= 0.5
174
+
175
+ if action == "BUY":
176
+ entry = current_price
177
+ stop_loss = current_price - (sl_mult * atr)
178
+ take_profit = current_price + (tp_mult * atr)
179
+ else: # SELL
180
+ entry = current_price
181
+ stop_loss = current_price + (sl_mult * atr)
182
+ take_profit = current_price - (tp_mult * atr)
183
 
184
+ return {
185
+ "action": action,
186
+ "entry": f"{entry:.5f}",
187
+ "stop_loss": f"{stop_loss:.5f}",
188
+ "take_profit": f"{take_profit:.5f}",
189
+ "confidence": confidence,
190
+ "reasoning": reasoning,
191
+ "strategy_type": strategy
192
+ }
193
  class MarketRegimeFilter:
194
  def __init__(self): self.allowed_strategies = {'TRENDING': ['Trend Following'], 'BREAKOUT': ['Trend Following', 'Scalp'], 'CHOPPY': ['Scalp'], 'RANGING': []}
195
  def should_trade(self, current_regime, trade_thesis): return False if trade_thesis['action'] == 'NO_TRADE' else trade_thesis['strategy_type'] in self.allowed_strategies.get(current_regime, [])