dixitramjendra commited on
Commit
144cd95
·
1 Parent(s): 53daa8c

Add signal lifecycle status system with visual badges

Browse files
Files changed (2) hide show
  1. forex_macd_dashboard.html +62 -2
  2. forex_macd_strategy.py +53 -2
forex_macd_dashboard.html CHANGED
@@ -213,6 +213,49 @@
213
  border-top-color: var(--neutral);
214
  }
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  /* Reentry Opportunity Styling */
217
  .reentry-panel {
218
  background: rgba(59, 130, 246, 0.08);
@@ -609,6 +652,22 @@
609
  return `${day}/${month}/${year}, ${hours}:${minutes}:${seconds}`;
610
  }
611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612
  let currentCategory = 'ForexLive';
613
  let cachedData = null;
614
  let selectedHistoryDate = null;
@@ -827,13 +886,14 @@
827
  const typeColor = inst.signal.type === 'BUY' ? 'bullish' : 'bearish';
828
  const entryPrice = inst.signal.entry_price || inst.signal.price;
829
  const isTrailing = inst.signal.current_sl && inst.signal.current_sl !== inst.signal.sl;
 
830
 
831
  signalHtml = `
832
  <div class="signal-panel ${typeClass}">
833
  <div class="signal-row" style="margin-bottom: 1rem; border-bottom: 1px solid #334155; padding-bottom: 0.5rem;">
834
- <span class="signal-label">🔴 ACTIVE SIGNAL</span>
835
- <span class="signal-val ${typeColor}" style="font-size: 1.2rem;">${inst.signal.type}</span>
836
  </div>
 
837
  <div class="signal-row">
838
  <span class="signal-label">Entry Price</span>
839
  <span class="signal-val">${formatPrice(entryPrice, inst.instrument)}</span>
 
213
  border-top-color: var(--neutral);
214
  }
215
 
216
+ /* Lifecycle Status Badges */
217
+ .lifecycle-badge {
218
+ display: inline-flex;
219
+ align-items: center;
220
+ gap: 0.5rem;
221
+ padding: 0.5rem 1rem;
222
+ border-radius: 0.5rem;
223
+ font-size: 0.85rem;
224
+ font-weight: 700;
225
+ margin-top: 0.5rem;
226
+ letter-spacing: 0.3px;
227
+ }
228
+
229
+ .lifecycle-new {
230
+ background: rgba(16, 185, 129, 0.2);
231
+ color: #10b981;
232
+ border: 1px solid #10b981;
233
+ }
234
+
235
+ .lifecycle-reentry {
236
+ background: rgba(139, 92, 246, 0.2);
237
+ color: #8b5cf6;
238
+ border: 1px solid #8b5cf6;
239
+ }
240
+
241
+ .lifecycle-partial-tp {
242
+ background: rgba(251, 191, 36, 0.2);
243
+ color: #fbbf24;
244
+ border: 1px solid #fbbf24;
245
+ }
246
+
247
+ .lifecycle-trailing {
248
+ background: rgba(59, 130, 246, 0.2);
249
+ color: #3b82f6;
250
+ border: 1px solid #3b82f6;
251
+ }
252
+
253
+ .lifecycle-active {
254
+ background: rgba(156, 163, 175, 0.2);
255
+ color: #9ca3af;
256
+ border: 1px solid #9ca3af;
257
+ }
258
+
259
  /* Reentry Opportunity Styling */
260
  .reentry-panel {
261
  background: rgba(59, 130, 246, 0.08);
 
652
  return `${day}/${month}/${year}, ${hours}:${minutes}:${seconds}`;
653
  }
654
 
655
+ function getLifecycleBadge(signal, hasReentry) {
656
+ if (!signal) return '';
657
+
658
+ const status = signal.lifecycle_status || 'Active';
659
+ const badges = {
660
+ 'New Signal': { class: 'lifecycle-new', emoji: '🟢', text: 'New Signal' },
661
+ 'Reentry Opportunity': { class: 'lifecycle-reentry', emoji: '🔁', text: 'Reentry Opportunity' },
662
+ 'Partial TP Hit': { class: 'lifecycle-partial-tp', emoji: '🟡', text: 'Partial TP Hit' },
663
+ 'Trailing SL Active': { class: 'lifecycle-trailing', emoji: '🔵', text: 'Trailing SL Active' },
664
+ 'Active': { class: 'lifecycle-active', emoji: '⚪', text: 'Active' }
665
+ };
666
+
667
+ const badge = badges[status] || badges['Active'];
668
+ return `<div class="lifecycle-badge ${badge.class}">${badge.emoji} ${badge.text}</div>`;
669
+ }
670
+
671
  let currentCategory = 'ForexLive';
672
  let cachedData = null;
673
  let selectedHistoryDate = null;
 
886
  const typeColor = inst.signal.type === 'BUY' ? 'bullish' : 'bearish';
887
  const entryPrice = inst.signal.entry_price || inst.signal.price;
888
  const isTrailing = inst.signal.current_sl && inst.signal.current_sl !== inst.signal.sl;
889
+ const lifecycleBadge = getLifecycleBadge(inst.signal, inst.re_entry !== null);
890
 
891
  signalHtml = `
892
  <div class="signal-panel ${typeClass}">
893
  <div class="signal-row" style="margin-bottom: 1rem; border-bottom: 1px solid #334155; padding-bottom: 0.5rem;">
894
+ <span class="signal-val ${typeColor}" style="font-size: 1.2rem;">${inst.signal.type} SIGNAL</span>
 
895
  </div>
896
+ ${lifecycleBadge}
897
  <div class="signal-row">
898
  <span class="signal-label">Entry Price</span>
899
  <span class="signal-val">${formatPrice(entryPrice, inst.instrument)}</span>
forex_macd_strategy.py CHANGED
@@ -282,6 +282,39 @@ def log_signal_event(instrument, event_type, price, signal_data=None):
282
  except Exception as e:
283
  print(f" ❌ Error logging event: {e}")
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  # ================= STRATEGY LOGIC =================
286
  def analyze_instrument(instrument: Dict) -> Dict:
287
  symbol = instrument['symbol']
@@ -503,8 +536,12 @@ def analyze_instrument(instrument: Dict) -> Dict:
503
  log_signal_event(name, "TP1_HIT", current_price, active_signal)
504
  if CONFIG['risk']['trailing_sl']['move_to_breakeven_at_tp1']:
505
  active_signal['current_sl'] = active_signal['entry_price']
 
506
  print(f" 🛡️ {name}: SL moved to Breakeven")
507
  save_active_signals()
 
 
 
508
 
509
  # TP2
510
  if not active_signal['tp_hits'][1] and current_price >= active_signal['tp2']:
@@ -513,8 +550,12 @@ def analyze_instrument(instrument: Dict) -> Dict:
513
  log_signal_event(name, "TP2_HIT", current_price, active_signal)
514
  if CONFIG['risk']['trailing_sl']['move_to_tp1_at_tp2']:
515
  active_signal['current_sl'] = active_signal['tp1']
 
516
  print(f" 🛡️ {name}: SL moved to TP1")
517
  save_active_signals()
 
 
 
518
 
519
  # TP3
520
  if not active_signal['tp_hits'][2] and current_price >= active_signal['tp3']:
@@ -584,8 +625,12 @@ def analyze_instrument(instrument: Dict) -> Dict:
584
  log_signal_event(name, "TP1_HIT", current_price, active_signal)
585
  if CONFIG['risk']['trailing_sl']['move_to_breakeven_at_tp1']:
586
  active_signal['current_sl'] = active_signal['entry_price']
 
587
  print(f" 🛡️ {name}: SL moved to Breakeven")
588
  save_active_signals()
 
 
 
589
 
590
  # TP2
591
  if not active_signal['tp_hits'][1] and current_price <= active_signal['tp2']:
@@ -594,8 +639,12 @@ def analyze_instrument(instrument: Dict) -> Dict:
594
  log_signal_event(name, "TP2_HIT", current_price, active_signal)
595
  if CONFIG['risk']['trailing_sl']['move_to_tp1_at_tp2']:
596
  active_signal['current_sl'] = active_signal['tp1']
 
597
  print(f" 🛡️ {name}: SL moved to TP1")
598
  save_active_signals()
 
 
 
599
 
600
  # TP3
601
  if not active_signal['tp_hits'][2] and current_price <= active_signal['tp3']:
@@ -894,7 +943,8 @@ def analyze_instrument(instrument: Dict) -> Dict:
894
  "tp_hits": [False, False, False],
895
  "time": datetime.now().isoformat(),
896
  "candle_time": e_last.name.isoformat(),
897
- "category": category
 
898
  }
899
  ACTIVE_SIGNALS[name] = final_signal
900
  save_active_signals()
@@ -938,7 +988,8 @@ def analyze_instrument(instrument: Dict) -> Dict:
938
  "tp_hits": [False, False, False],
939
  "time": datetime.now().isoformat(),
940
  "candle_time": e_last.name.isoformat(),
941
- "category": category
 
942
  }
943
  ACTIVE_SIGNALS[name] = final_signal
944
  save_active_signals()
 
282
  except Exception as e:
283
  print(f" ❌ Error logging event: {e}")
284
 
285
+ def calculate_lifecycle_status(signal, has_reentry=False):
286
+ """Calculate the lifecycle status of a signal based on its current state."""
287
+ if not signal:
288
+ return None
289
+
290
+ # Check signal age (within 1 hour = "New Signal")
291
+ try:
292
+ signal_time = datetime.fromisoformat(signal['time'])
293
+ age_hours = (datetime.now() - signal_time).total_seconds() / 3600
294
+
295
+ if age_hours < 1:
296
+ return "New Signal"
297
+ except:
298
+ pass
299
+
300
+ # Check for reentry opportunity
301
+ if has_reentry:
302
+ return "Reentry Opportunity"
303
+
304
+ # Check for partial TP hits
305
+ tp_hits = signal.get('tp_hits', [False, False, False])
306
+ if any(tp_hits):
307
+ return "Partial TP Hit"
308
+
309
+ # Check for trailing SL active
310
+ current_sl = signal.get('current_sl')
311
+ initial_sl = signal.get('sl')
312
+ if current_sl is not None and initial_sl is not None and current_sl != initial_sl:
313
+ return "Trailing SL Active"
314
+
315
+ # Default active state
316
+ return "Active"
317
+
318
  # ================= STRATEGY LOGIC =================
319
  def analyze_instrument(instrument: Dict) -> Dict:
320
  symbol = instrument['symbol']
 
536
  log_signal_event(name, "TP1_HIT", current_price, active_signal)
537
  if CONFIG['risk']['trailing_sl']['move_to_breakeven_at_tp1']:
538
  active_signal['current_sl'] = active_signal['entry_price']
539
+ active_signal['lifecycle_status'] = "Trailing SL Active"
540
  print(f" 🛡️ {name}: SL moved to Breakeven")
541
  save_active_signals()
542
+ else:
543
+ active_signal['lifecycle_status'] = "Partial TP Hit"
544
+ save_active_signals()
545
 
546
  # TP2
547
  if not active_signal['tp_hits'][1] and current_price >= active_signal['tp2']:
 
550
  log_signal_event(name, "TP2_HIT", current_price, active_signal)
551
  if CONFIG['risk']['trailing_sl']['move_to_tp1_at_tp2']:
552
  active_signal['current_sl'] = active_signal['tp1']
553
+ active_signal['lifecycle_status'] = "Trailing SL Active"
554
  print(f" 🛡️ {name}: SL moved to TP1")
555
  save_active_signals()
556
+ else:
557
+ active_signal['lifecycle_status'] = "Partial TP Hit"
558
+ save_active_signals()
559
 
560
  # TP3
561
  if not active_signal['tp_hits'][2] and current_price >= active_signal['tp3']:
 
625
  log_signal_event(name, "TP1_HIT", current_price, active_signal)
626
  if CONFIG['risk']['trailing_sl']['move_to_breakeven_at_tp1']:
627
  active_signal['current_sl'] = active_signal['entry_price']
628
+ active_signal['lifecycle_status'] = "Trailing SL Active"
629
  print(f" 🛡️ {name}: SL moved to Breakeven")
630
  save_active_signals()
631
+ else:
632
+ active_signal['lifecycle_status'] = "Partial TP Hit"
633
+ save_active_signals()
634
 
635
  # TP2
636
  if not active_signal['tp_hits'][1] and current_price <= active_signal['tp2']:
 
639
  log_signal_event(name, "TP2_HIT", current_price, active_signal)
640
  if CONFIG['risk']['trailing_sl']['move_to_tp1_at_tp2']:
641
  active_signal['current_sl'] = active_signal['tp1']
642
+ active_signal['lifecycle_status'] = "Trailing SL Active"
643
  print(f" 🛡️ {name}: SL moved to TP1")
644
  save_active_signals()
645
+ else:
646
+ active_signal['lifecycle_status'] = "Partial TP Hit"
647
+ save_active_signals()
648
 
649
  # TP3
650
  if not active_signal['tp_hits'][2] and current_price <= active_signal['tp3']:
 
943
  "tp_hits": [False, False, False],
944
  "time": datetime.now().isoformat(),
945
  "candle_time": e_last.name.isoformat(),
946
+ "category": category,
947
+ "lifecycle_status": "New Signal"
948
  }
949
  ACTIVE_SIGNALS[name] = final_signal
950
  save_active_signals()
 
988
  "tp_hits": [False, False, False],
989
  "time": datetime.now().isoformat(),
990
  "candle_time": e_last.name.isoformat(),
991
+ "category": category,
992
+ "lifecycle_status": "New Signal"
993
  }
994
  ACTIVE_SIGNALS[name] = final_signal
995
  save_active_signals()