dumpykhan970

Untitled 1490

Apr 22nd, 2026
21
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 264.22 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| AIBRAIN OTC v45.4 - OVERLAP + TIMER + SR + POPUP FIXED |
  3. //| Fixed: Other pairs no overlap, text size 11, timer screen pos |
  4. //| Fixed: SR lines stable (no shift), popup notification removed |
  5. //+------------------------------------------------------------------+
  6. #property copyright "AIBRAIN ULTIMATE - KM RANA"
  7. #property version "46.2"
  8. #property strict
  9. #property indicator_chart_window
  10.  
  11. #define PFX "AIB9_"
  12. #define VER "v46.2"
  13. #define LB 18
  14. #define MAX_LEVELS 5
  15. #define MAX_REJ 50
  16. #define LOOKBACK 50
  17.  
  18. input string SPM_FILE = "KM RANA";
  19. input int HOLD_SEC = 60;
  20. input int POS_X = 4;
  21. input int POS_Y = 20;
  22. input int DASH_W = 660;
  23. input bool SHOW_TIMER = true;
  24. input bool SHOW_SR_LINES = true;
  25. input string ENTRY_MODE = "TIME";
  26. input int FLAG_MIN_SEC = 10;
  27. input int FLAG_MAX_SEC = 35;
  28. input int TIME_MIN_SEC = 10;
  29. input int TIME_MAX_SEC = 35;
  30. input int MIN_REMAINING_SEC = 15;
  31. input double REVERSAL_PCT = 30.0;
  32. input double WICK_PCT = 0.65;
  33. input int HRN_CONFIRM_BARS = 2;
  34. input bool HA_ALIGN_FILTER = false;
  35. input bool CONSENSUS_FILTER = false;
  36. input bool ENABLE_NOTIFY = true;
  37. input bool FAST_MODE = false;
  38. input int BROKER_SPREAD_THRESHOLD= 6;
  39. input double OTC_TRAP_WEIGHT = 15.0;
  40. input int CROWD_EXTREME_PCT = 70;
  41. input bool ENABLE_BROKER_KILLER = true;
  42. input bool ENABLE_SESSION_FILTER = true;
  43. input bool ENABLE_SPIKE_FILTER = true;
  44. input bool ENABLE_LAST_SEC_BLOCK = true;
  45. input bool ENABLE_RISK_CONTROL = true;
  46. input bool ENABLE_ENTRY_ZONE = true;
  47. input bool ML_ADAPTIVE = true;
  48. input bool VOLUME_PROFILE_TRAP = true;
  49. input bool MTF_CONFIRM = false;
  50. input bool ENABLE_TELEGRAM = false;
  51. input string TELEGRAM_TOKEN = "";
  52. input string TELEGRAM_CHAT_ID = "";
  53. input string TRADE_MODE = "NORMAL";
  54. input string STRATEGY_MODE = "TRAP_ONLY";
  55. input int STRATEGY_THRESHOLD = 75;
  56. input int MAX_LOSS_STREAK = 2;
  57. input double MIN_CONFIDENCE = 75.0;
  58. input bool ENABLE_RISK_LOCK = true;
  59. input double PATTERN_TOLERANCE_PIPS = 8.0;
  60. input int MIN_PATTERN_SEPARATION = 5;
  61. input bool SHOW_COMMON_POINTS = true;
  62. input double COMMON_POINT_TOL_PIPS = 15.0;
  63. input bool SHOW_WICK_REJECT_LINES = true;
  64. input int WICK_MIN_TOUCHES = 1;
  65. input double WICK_ZONE_PIPS = 15.0;
  66. input int WICK_EXPIRE_BARS = 60;
  67.  
  68. // ============================================================
  69. // BB PULLBACK SETTINGS
  70. // ============================================================
  71. input bool ENABLE_BB_PULLBACK = true;
  72. input int BB_PERIOD = 20;
  73. input double BB_DEVIATION = 2.0;
  74. input int BB_SIGNAL_EXPIRE_BARS = 5;
  75.  
  76. // ============================================================
  77. // ============================================================
  78. // NCP PRO v7.3 SETTINGS — OTC BROKER KILLER + LEARNING ENGINE
  79. // ============================================================
  80. input bool ENABLE_MTG = true;
  81. input int NCP_OpenNoiseSeconds = 3;
  82. input int NCP_KillZoneSeconds = 55;
  83. input double NCP_MaxSpreadPips = 4.0;
  84. input int NCP_ATRPeriod = 14;
  85. input double NCP_MinATRPips = 0.4; // Very low = more signals
  86. input int NCP_FastEMA = 3;
  87. input int NCP_MidEMA = 7;
  88. input int NCP_SlowEMA = 15;
  89. input bool NCP_UseM5Confirm = true;
  90. input bool NCP_UseVolume = true;
  91. input double NCP_RoundStepPips = 10.0;
  92. input double NCP_RoundTolPips = 4.0;
  93. input int NCP_MinProbMedium = 57; // Low threshold = more signals
  94. input int NCP_MinProbStrong = 63;
  95. input int NCP_MinProbExtreme = 70;
  96. input bool NCP_UseRSINorm = true;
  97. input int NCP_RSIPeriod = 10;
  98. input double NCP_RSIBase = 50.0;
  99. input double NCP_RSIM5Target = 45.0;
  100. input double NCP_RSIM15Target = 40.0;
  101. // Learning Engine
  102. input bool ENABLE_LEARNING = true;
  103. input string MEMORY_FILE_NAME = "NCP_Brain_v7.csv";
  104. input int MIN_TRADES_TO_LEARN = 15;
  105. input double LEARNING_STEP = 0.025;
  106. // Trap Detection
  107. input bool ENABLE_TRAP_DETECTION = true;
  108. input int TRAP_LOOKBACK = 30;
  109.  
  110. #define NEON_GREEN C'0,255,100'
  111. #define NEON_RED C'255,40,80'
  112. #define NEON_YELLOW C'255,220,0'
  113. #define NEON_ORANGE C'255,120,0'
  114. #define NEON_CYAN C'0,255,200'
  115. #define NEON_PURPLE C'200,0,255'
  116. #define NEON_PINK C'255,80,180'
  117. #define NEON_BLUE C'0,200,255'
  118. #define NEON_LIME C'80,255,80'
  119. #define NEON_GOLD C'255,180,0'
  120. #define DARK_BLUE_NEON C'0,100,255'
  121. #define NEON_WHITE C'220,230,255'
  122. #define CGR C'160,170,190'
  123. #define BB_GREY C'180,180,190'
  124. #define BG_DARK1 C'6,8,16'
  125. #define BG_DARK2 C'10,14,24'
  126. #define BG_DARK3 C'14,20,32'
  127. #define BG_DARK4 C'18,26,40'
  128.  
  129. struct HTFLevel{ double price; string timeframe; string type; datetime time; int strength; bool isRoundNumber; };
  130.  
  131. //+------------------------------------------------------------------+
  132. // GLOBAL VARIABLES
  133. //+------------------------------------------------------------------+
  134. string g_haM1="",g_haM5="",g_haBoth="";
  135. color g_haM1Color=clrGray,g_haM5Color=clrGray;
  136. static double NW[36];
  137. static double g_rsc=0,g_tfa=3;
  138. static datetime g_spm_t=0,g_last_bar=0;
  139. double ResLevels[MAX_LEVELS],SupLevels[MAX_LEVELS];
  140. int resCount=0,supCount=0;
  141. datetime lastSRBar=0;
  142. static double g_nearest_res=0,g_nearest_sup=0;
  143. static string g_brk_str="NO BRK";
  144. static color g_brk_color=CGR;
  145. struct RejLevel{double price;int touches;};
  146. static RejLevel g_rj[MAX_REJ];
  147. static int g_rj_cnt=0;
  148.  
  149. static double g_hrn_price=0;
  150. static bool g_hrn_is_sup=true;
  151. static int g_hrn_brk_bars=0;
  152. static datetime g_hrn_scan_bar=0;
  153. static string g_hrn_str="--";
  154. static double g_hrn_score=0;
  155. static bool g_hrn_confirmed_break=false;
  156.  
  157. static int g_otcCallPct=50,g_otcPutPct=50;
  158. static string g_pairNames[8],g_pairSigs[8];
  159. static double g_pairConfs[8];
  160. static int g_pairCount=0;
  161. static double g_frozen_gProb=50,g_frozen_rProb=50;
  162. static string g_frozen_pAction="UNCERTAIN";
  163. static color g_frozen_pColor=NEON_YELLOW;
  164. static double g_calc_gProb=50,g_calc_rProb=50;
  165. static string g_calc_pAction="UNCERTAIN";
  166. static color g_calc_pColor=NEON_YELLOW;
  167. static double g_gProb=50,g_rProb=50;
  168. static string g_pAction="UNCERTAIN";
  169. static color g_pColor=NEON_YELLOW;
  170. static datetime g_last_freeze_bar=0;
  171. static double g_accuracy=65.0;
  172. static int g_acc_correct=0,g_acc_total=0;
  173. static datetime g_acc_lastBar=0;
  174. static double g_lastPredGreen=50.0;
  175. static datetime g_lastPredBar=0;
  176. static string g_finalSignal="WAIT";
  177. static color g_finalColor=NEON_YELLOW;
  178. static datetime g_signalTime=0;
  179. static string g_lastNotified="";
  180. static string g_marketMode="RANGE",g_prevMode="";
  181. static int g_lossStreak=0;
  182. static bool g_tradingStopped=false;
  183. static string g_strategyType="NONE";
  184. static string g_strategyReason="";
  185. static double qmrA=0,qmrB=0,qmrC=0,qmrD=0;
  186. static bool qmrActive=false;
  187. static datetime qmrExpire=0;
  188. #define MAX_COMMON_PTS 5
  189. static double g_commonPrice[MAX_COMMON_PTS];
  190. static datetime g_commonTime[MAX_COMMON_PTS];
  191. static int g_commonCount=0;
  192. static datetime g_commonExpire[MAX_COMMON_PTS];
  193. static string g_commonType[MAX_COMMON_PTS];
  194. static datetime g_lastCommonScan=0;
  195. #define MAX_WICK_LINES 5
  196. static double g_wickLinePrice[MAX_WICK_LINES];
  197. static int g_wickLineTouches[MAX_WICK_LINES];
  198. static datetime g_wickLineExpire[MAX_WICK_LINES];
  199. static int g_wickLineCount=0;
  200. static datetime g_lastWickScan=0;
  201. static bool g_isSideways=false;
  202. static string g_tfa_detail="";
  203. static HTFLevel g_htfLevels[50];
  204. static int g_htfLevelCount=0;
  205. static bool g_mtfConfirmed=true;
  206. static string g_filterStatus="ALL CLEAR";
  207. static string g_adv1="WAIT KAR";
  208. static string g_symbolKey="";
  209. static double g_brokerBias=0.0;
  210.  
  211. // ============================================================
  212. // BB PULLBACK GLOBAL VARIABLES - NEW
  213. // ============================================================
  214. static string g_bbSignal = "--"; // "CALL", "PUT", "--"
  215. static color g_bbColor = CGR;
  216. static double g_bbTouchPrice = 0.0; // Jahan BB touch hua
  217. static datetime g_bbSignalTime = 0; // Signal kab aaya
  218. static int g_bbSignalBars = 0; // Kitne bars ho gaye
  219. static string g_bbDetail = ""; // Detail text
  220.  
  221. // 30-sec HA variables
  222. static double g_ha30_open = 0.0;
  223. static double g_ha30_close = 0.0;
  224. static double g_ha30_high = 0.0;
  225. static double g_ha30_low = 0.0;
  226. static datetime g_ha30_bar = 0;
  227.  
  228. // Max grey lines on chart
  229. #define MAX_BB_LINES 8
  230. static double g_bbLinePrice[MAX_BB_LINES];
  231. static datetime g_bbLineExpire[MAX_BB_LINES];
  232. static string g_bbLineType[MAX_BB_LINES];
  233. static int g_bbLineCount=0;
  234.  
  235. // ============================================================
  236. // NCP PRO GLOBALS
  237. // ============================================================
  238. static string g_mtgState = "INIT";
  239. static string g_mtgReason = "";
  240. static string g_mtgAction = "";
  241. static color g_mtgClr = CGR;
  242. static double g_mtgHype = 50.0;
  243. static double g_mtgBetrayal = 50.0;
  244. static string g_mtgPattern = "";
  245. static int g_mtgBullCount = 0;
  246. static int g_mtgBearCount = 0;
  247. static double g_mtgRecovery = 0.0;
  248. static datetime g_mtg_lastBar = 0;
  249. static double g_trapScore = 0;
  250.  
  251. // NCP Learning Engine globals
  252. static double g_callWeight = 1.0;
  253. static double g_putWeight = 1.0;
  254. static int g_totalTrades = 0;
  255. static int g_callTrades = 0;
  256. static int g_putTrades = 0;
  257. static int g_callWins = 0;
  258. static int g_putWins = 0;
  259. // Signal tracking for win/loss detection
  260. static datetime g_ncpLastSignalTime = 0;
  261. static string g_ncpLastSignalType = "";
  262. static double g_ncpLastEntryPrice = 0.0;
  263. static bool g_ncpSignalProcessed = true;
  264. // Score smoothing — gradual build, no sudden spikes
  265. static double g_smoothCallScore = 50.0;
  266. static double g_smoothPutScore = 50.0;
  267.  
  268.  
  269. //+------------------------------------------------------------------+
  270. // 30-SECOND HA CANDLE - BACKTESTING SAFE VERSION
  271. // Strategy: Current M1 candle ke current tick se live HA banao
  272. // Backtesting mein bhi kaam karta hai kyunki Time[] arrays use karta hai
  273. // elapsed time dependent NAHI hai - pure price-based hai
  274. //+------------------------------------------------------------------+
  275. void Calc30SecHA(){
  276. if(Bars < 5) return;
  277.  
  278. // Current live candle (index 0) - live tick data
  279. double cur_open = Open[0];
  280. double cur_high = High[0];
  281. double cur_low = Low[0];
  282. double cur_close = Close[0];
  283.  
  284. // Prev fully closed candle (index 1) - confirmed data
  285. double prev_open = Open[1];
  286. double prev_high = High[1];
  287. double prev_low = Low[1];
  288. double prev_close = Close[1];
  289.  
  290. // HA for prev closed candle
  291. double haC_prev = (prev_open + prev_high + prev_low + prev_close) / 4.0;
  292. double haC_prev2 = (Open[2] + High[2] + Low[2] + Close[2]) / 4.0;
  293. double haO_prev2 = (Open[3] + Close[3]) / 2.0;
  294. double haO_prev = (haO_prev2 + haC_prev2) / 2.0;
  295.  
  296. // HA for current live candle
  297. double haC_cur = (cur_open + cur_high + cur_low + cur_close) / 4.0;
  298. double haO_cur = (haO_prev + haC_prev) / 2.0; // Standard HA open
  299.  
  300. g_ha30_open = haO_cur;
  301. g_ha30_close = haC_cur;
  302. g_ha30_high = cur_high;
  303. g_ha30_low = cur_low;
  304. g_ha30_bar = Time[0];
  305. }
  306.  
  307. bool Is30SecHABull(){ return (g_ha30_close > g_ha30_open); }
  308. bool Is30SecHABear(){ return (g_ha30_close < g_ha30_open); }
  309.  
  310. //+------------------------------------------------------------------+
  311. // BB PULLBACK DETECTION - FIXED
  312. // Color logic:
  313. // CALL signal → GREEN
  314. // PUT signal → RED
  315. // No signal → YELLOW (normal/wait state)
  316. //+------------------------------------------------------------------+
  317. void DetectBBPullback(){
  318. if(!ENABLE_BB_PULLBACK || Bars < BB_PERIOD + 5) return;
  319.  
  320. Calc30SecHA();
  321.  
  322. double bbUpper1 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_UPPER, 1);
  323. double bbLower1 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_LOWER, 1);
  324. double bbUpper0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_UPPER, 0);
  325. double bbLower0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_LOWER, 0);
  326. double bbMid0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_MAIN, 0);
  327. if(bbUpper1 <= 0 || bbLower1 <= 0) return;
  328.  
  329. bool ha30Bull = Is30SecHABull();
  330. bool ha30Bear = Is30SecHABear();
  331.  
  332. // ============================================================
  333. // ADX + RSI CONFIRMATION - NEW
  334. // ============================================================
  335. double adx = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  336. double plusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  337. double minusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  338. double rsi = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  339.  
  340. bool adxBear = (minusDI > plusDI);
  341. bool adxBull = (plusDI > minusDI);
  342. bool adxActive = (adx >= 15);
  343. bool adxStrong = (adx >= 20);
  344.  
  345. bool rsiOverbought = (rsi >= 60);
  346. bool rsiOversold = (rsi <= 40);
  347. bool rsiExtremeOB = (rsi >= 70);
  348. bool rsiExtremeOS = (rsi <= 30);
  349.  
  350. // PUT confirmation score
  351. int putConfirm = 0;
  352. if(ha30Bear) putConfirm++;
  353. if(adxBear) putConfirm++;
  354. if(rsiOverbought) putConfirm++;
  355. if(rsiExtremeOB) putConfirm++;
  356.  
  357. // CALL confirmation score
  358. int callConfirm = 0;
  359. if(ha30Bull) callConfirm++;
  360. if(adxBull) callConfirm++;
  361. if(rsiOversold) callConfirm++;
  362. if(rsiExtremeOS) callConfirm++;
  363.  
  364. string adxStr = "ADX:"+DoubleToString(adx,0)+(adxStrong?"*":"");
  365. string rsiStr = "RSI:"+DoubleToString(rsi,0);
  366. string diStr = (adxBull?"DI^":adxBear?"DIv":"DI=");
  367.  
  368. // Expire check
  369. if(g_bbSignal != "--" && g_bbSignalTime > 0){
  370. int barsElapsed = (int)((TimeCurrent() - g_bbSignalTime) / (Period() * 60));
  371. if(barsElapsed > BB_SIGNAL_EXPIRE_BARS){
  372. g_bbSignal = "--"; g_bbColor = NEON_YELLOW;
  373. g_bbDetail = ""; g_bbTouchPrice = 0.0;
  374. }
  375. }
  376.  
  377. bool touchedUpper = (High[1] >= bbUpper1 * 0.9998);
  378. bool touchedLower = (Low[1] <= bbLower1 * 1.0002);
  379. bool wickUpper = (High[1] > bbUpper1 && Close[1] < bbUpper1);
  380. bool wickLower = (Low[1] < bbLower1 && Close[1] > bbLower1);
  381.  
  382. string newSignal = "--";
  383. double touchPriceNew = 0.0;
  384. string detailNew = "";
  385. color newColor = NEON_YELLOW;
  386.  
  387. // PUT: BB upper + min 2 confirmations
  388. if((touchedUpper || wickUpper) && putConfirm >= 2){
  389. newSignal = "PUT";
  390. touchPriceNew = bbUpper1;
  391. string conf = (putConfirm>=4)?"STRONG":(putConfirm>=3)?"GOOD":"WEAK";
  392. detailNew = "BB UP|"+conf+"|"+adxStr+"|"+rsiStr+"|"+diStr;
  393. newColor = (putConfirm >= 3) ? NEON_RED : NEON_ORANGE;
  394. }
  395. // CALL: BB lower + min 2 confirmations
  396. else if((touchedLower || wickLower) && callConfirm >= 2){
  397. newSignal = "CALL";
  398. touchPriceNew = bbLower1;
  399. string conf = (callConfirm>=4)?"STRONG":(callConfirm>=3)?"GOOD":"WEAK";
  400. detailNew = "BB LO|"+conf+"|"+adxStr+"|"+rsiStr+"|"+diStr;
  401. newColor = (callConfirm >= 3) ? NEON_GREEN : NEON_CYAN;
  402. }
  403.  
  404. if(newSignal != "--"){
  405. bool isNew = (newSignal != g_bbSignal || touchPriceNew != g_bbTouchPrice);
  406. if(isNew){
  407. g_bbSignal = newSignal;
  408. g_bbTouchPrice = touchPriceNew;
  409. g_bbSignalTime = TimeCurrent();
  410. g_bbDetail = detailNew;
  411. g_bbColor = newColor;
  412. AddBBLine(touchPriceNew, newSignal);
  413. Print("BB PULLBACK: ",newSignal," | ",detailNew," Conf:",(newSignal=="PUT"?putConfirm:callConfirm),"/4");
  414. }
  415. } else if(g_bbSignal == "--"){
  416. g_bbColor = NEON_YELLOW;
  417. }
  418.  
  419. DrawBBLines(bbUpper0, bbLower0, bbMid0);
  420. }
  421.  
  422. //+------------------------------------------------------------------+
  423. // BB LINES DRAW ON CHART
  424. //+------------------------------------------------------------------+
  425. void DrawBBLines(double upper, double lower, double mid){
  426. if(!ENABLE_BB_PULLBACK) return;
  427.  
  428. // Upper BB - grey dashed line
  429. string nmU = PFX+"BB_UPPER";
  430. SafeDel(nmU);
  431. if(upper > 0){
  432. ObjectCreate(0, nmU, OBJ_HLINE, 0, 0, upper);
  433. ObjectSetInteger(0, nmU, OBJPROP_COLOR, BB_GREY);
  434. ObjectSetInteger(0, nmU, OBJPROP_WIDTH, 1);
  435. ObjectSetInteger(0, nmU, OBJPROP_STYLE, STYLE_DASH);
  436. ObjectSetInteger(0, nmU, OBJPROP_BACK, false);
  437. }
  438.  
  439. // Lower BB - grey dashed line
  440. string nmL = PFX+"BB_LOWER";
  441. SafeDel(nmL);
  442. if(lower > 0){
  443. ObjectCreate(0, nmL, OBJ_HLINE, 0, 0, lower);
  444. ObjectSetInteger(0, nmL, OBJPROP_COLOR, BB_GREY);
  445. ObjectSetInteger(0, nmL, OBJPROP_WIDTH, 1);
  446. ObjectSetInteger(0, nmL, OBJPROP_STYLE, STYLE_DASH);
  447. ObjectSetInteger(0, nmL, OBJPROP_BACK, false);
  448. }
  449.  
  450. // Mid BB - lighter grey dot line
  451. string nmM = PFX+"BB_MID";
  452. SafeDel(nmM);
  453. if(mid > 0){
  454. ObjectCreate(0, nmM, OBJ_HLINE, 0, 0, mid);
  455. ObjectSetInteger(0, nmM, OBJPROP_COLOR, C'100,100,110');
  456. ObjectSetInteger(0, nmM, OBJPROP_WIDTH, 1);
  457. ObjectSetInteger(0, nmM, OBJPROP_STYLE, STYLE_DOT);
  458. ObjectSetInteger(0, nmM, OBJPROP_BACK, false);
  459. }
  460.  
  461. // Labels
  462. int dg = (int)MarketInfo(Symbol(), MODE_DIGITS);
  463. if(dg <= 0) dg = 5;
  464.  
  465. string lblU = PFX+"BB_U_LBL";
  466. SafeDel(lblU);
  467. if(upper > 0 && Bars > 2){
  468. ObjectCreate(0, lblU, OBJ_TEXT, 0, Time[2], upper);
  469. ObjectSetText(lblU, "BB("+IntegerToString(BB_PERIOD)+")", 8, "Arial", BB_GREY);
  470. ObjectSetInteger(0, lblU, OBJPROP_BACK, false);
  471. }
  472. }
  473.  
  474. //+------------------------------------------------------------------+
  475. // ADD BB TOUCH LINE — sirf 1 line at a time (latest signal wali)
  476. void AddBBLine(double price, string sigType){
  477. if(price <= 0) return;
  478. // Pehli sab purani BB touch lines delete karo
  479. for(int i = 0; i < MAX_BB_LINES; i++){
  480. SafeDel(PFX+"BB_TOUCH_"+IntegerToString(i));
  481. SafeDel(PFX+"BB_TOUCH_LBL_"+IntegerToString(i));
  482. }
  483. g_bbLineCount = 0;
  484. // Sirf naya ek line add karo
  485. g_bbLinePrice[0] = price;
  486. g_bbLineExpire[0] = TimeCurrent() + 60 * BB_SIGNAL_EXPIRE_BARS;
  487. g_bbLineType[0] = sigType;
  488. g_bbLineCount = 1;
  489. DrawAllBBTouchLines();
  490. }
  491.  
  492. void DrawAllBBTouchLines(){
  493. // Clean all slots
  494. for(int i = 0; i < MAX_BB_LINES; i++){
  495. SafeDel(PFX+"BB_TOUCH_"+IntegerToString(i));
  496. SafeDel(PFX+"BB_TOUCH_LBL_"+IntegerToString(i));
  497. }
  498. // Remove expired
  499. if(g_bbLineCount > 0 && TimeCurrent() > g_bbLineExpire[0]){
  500. g_bbLineCount = 0;
  501. return;
  502. }
  503. if(g_bbLineCount <= 0) return;
  504. int dg = (int)MarketInfo(Symbol(), MODE_DIGITS);
  505. if(dg <= 0) dg = 5;
  506. // Draw only the single active line
  507. string nm = PFX+"BB_TOUCH_0";
  508. string lbl = PFX+"BB_TOUCH_LBL_0";
  509. ObjectCreate(0, nm, OBJ_HLINE, 0, 0, g_bbLinePrice[0]);
  510. ObjectSetInteger(0, nm, OBJPROP_COLOR, BB_GREY);
  511. ObjectSetInteger(0, nm, OBJPROP_WIDTH, 2);
  512. ObjectSetInteger(0, nm, OBJPROP_STYLE, STYLE_SOLID);
  513. ObjectSetInteger(0, nm, OBJPROP_BACK, false);
  514. if(Bars > 3){
  515. string txt = (g_bbLineType[0]=="CALL") ? "BB^ CALL" : "BBv PUT";
  516. color lc = (g_bbLineType[0]=="CALL") ? NEON_GREEN : NEON_RED;
  517. ObjectCreate(0, lbl, OBJ_TEXT, 0, Time[3], g_bbLinePrice[0]);
  518. ObjectSetText(lbl, txt, 8, "Arial Bold", lc);
  519. ObjectSetInteger(0, lbl, OBJPROP_BACK, false);
  520. }
  521. }
  522.  
  523. //+------------------------------------------------------------------+
  524. // MTG AI - MOMENTUM TRAP GAUGE
  525. // 1-minute OTC ke liye: 3-5 candle patterns detect karta hai
  526. // States: SLEEPING → HYPE → SETUP → SCAM/RECOVERY
  527. //
  528. // Pattern Logic:
  529. // 1. HYPE: 3+ bull candles streak, expanding body, high volume
  530. // 2. SETUP: Current candle red banana shuru hua
  531. // 3. SCAM: Fakeout + wick trap + body reversal confirm
  532. // 4. RECOVERY: Price mid-zone tak wapas aaya = CALL signal
  533. //+------------------------------------------------------------------+
  534. // ============================================================
  535. // NCP PRO — NEXT CANDLE PREDICTOR PRO
  536. // RSI Multi-Timeframe Normalized + EMA Trend + Volume + Patterns
  537. // Runs every tick, updates g_mtgState/g_mtgHype/g_mtgBetrayal
  538. // ============================================================
  539. // ============================================================
  540. // NCP PRO v4.2 — OTC BROKER KILLER | TRAP REVERSAL
  541. // Best of v4.1 + AIBRAIN integration
  542. // Signals: CALL / PUT / PUT TRAP / CALL TRAP / WAIT
  543. // ============================================================
  544. // ============================================================
  545. // NCP PRO v7.3 — BROKER KILLER + LEARNING ENGINE
  546. // Signal smoothing: no sudden spikes, gradual build
  547. // Learning: tracks wins/losses, adjusts weights carefully
  548. // Trap: engulfing + wick patterns + crowd run
  549. // ============================================================
  550.  
  551. // --- LEARNING ENGINE FUNCTIONS ---
  552. void LoadBrainMemory(){
  553. if(!ENABLE_LEARNING) return;
  554. int h=FileOpen(MEMORY_FILE_NAME,FILE_READ|FILE_CSV|FILE_SHARE_READ);
  555. if(h!=INVALID_HANDLE){
  556. if(FileSize(h)>10){
  557. g_totalTrades=(int)FileReadNumber(h);
  558. g_callTrades =(int)FileReadNumber(h);
  559. g_putTrades =(int)FileReadNumber(h);
  560. g_callWins =(int)FileReadNumber(h);
  561. g_putWins =(int)FileReadNumber(h);
  562. g_callWeight =FileReadNumber(h);
  563. g_putWeight =FileReadNumber(h);
  564. // Guard against bad values
  565. if(g_callWeight<0.2||g_callWeight>1.8) g_callWeight=1.0;
  566. if(g_putWeight <0.2||g_putWeight >1.8) g_putWeight =1.0;
  567. Print("NCP Brain loaded: ",g_totalTrades," trades | CW:",DoubleToString(g_callWeight,3)," PW:",DoubleToString(g_putWeight,3));
  568. }
  569. FileClose(h);
  570. }
  571. }
  572. void SaveBrainMemory(){
  573. if(!ENABLE_LEARNING) return;
  574. int h=FileOpen(MEMORY_FILE_NAME,FILE_WRITE|FILE_CSV|FILE_SHARE_WRITE);
  575. if(h!=INVALID_HANDLE){
  576. FileWrite(h,g_totalTrades);FileWrite(h,g_callTrades);FileWrite(h,g_putTrades);
  577. FileWrite(h,g_callWins);FileWrite(h,g_putWins);
  578. FileWrite(h,g_callWeight);FileWrite(h,g_putWeight);
  579. FileClose(h);
  580. }
  581. }
  582. void UpdateBrain(string sig,bool win){
  583. if(!ENABLE_LEARNING) return;
  584. g_totalTrades++;
  585. if(sig=="CALL"){g_callTrades++;if(win)g_callWins++;}
  586. else if(sig=="PUT"){g_putTrades++;if(win)g_putWins++;}
  587. if(g_totalTrades < MIN_TRADES_TO_LEARN){ SaveBrainMemory(); return; }
  588. double adj=LEARNING_STEP;
  589. // Adaptive: winning = boost, losing = reduce (but carefully)
  590. if(sig=="CALL"){
  591. double wr=(g_callTrades>0)?(double)g_callWins/g_callTrades:0.5;
  592. double adaptive=adj*(wr-0.5)*2.0;
  593. if(win) g_callWeight=MathMin(1.8,g_callWeight+adj+adaptive);
  594. else g_callWeight=MathMax(0.3,g_callWeight-adj*1.5+adaptive);
  595. // Noise guard: don't let weight collapse too fast
  596. if(g_callTrades<MIN_TRADES_TO_LEARN+5) g_callWeight=MathMax(0.6,g_callWeight);
  597. } else if(sig=="PUT"){
  598. double wr=(g_putTrades>0)?(double)g_putWins/g_putTrades:0.5;
  599. double adaptive=adj*(wr-0.5)*2.0;
  600. if(win) g_putWeight=MathMin(1.8,g_putWeight+adj+adaptive);
  601. else g_putWeight=MathMax(0.3,g_putWeight-adj*1.5+adaptive);
  602. if(g_putTrades<MIN_TRADES_TO_LEARN+5) g_putWeight=MathMax(0.6,g_putWeight);
  603. }
  604. SaveBrainMemory();
  605. }
  606. void CheckPreviousResult(){
  607. if(g_ncpSignalProcessed||g_ncpLastSignalType==""||g_ncpLastSignalTime==0) return;
  608. // Check if enough time passed (at least 1 bar closed)
  609. if(Time[0]==g_ncpLastSignalTime) return;
  610.  
  611. // Use bar that closed AFTER signal (bar index 1)
  612. double sigOpen =iOpen(NULL,PERIOD_M1,1);
  613. double sigClose=iClose(NULL,PERIOD_M1,1);
  614.  
  615. // Win logic: check price movement vs entry
  616. bool win=false;
  617. if(g_ncpLastSignalType=="CALL")
  618. win=(sigClose>sigOpen); // Candle closed higher = WIN
  619. else if(g_ncpLastSignalType=="PUT")
  620. win=(sigClose<sigOpen); // Candle closed lower = WIN
  621.  
  622. // IMPORTANT: Mark as processed BEFORE UpdateBrain
  623. g_ncpSignalProcessed=true;
  624.  
  625. UpdateBrain(g_ncpLastSignalType,win);
  626. Print("NCP Result: ",g_ncpLastSignalType," ",win?"WIN":"LOSS"," | Trades:",g_totalTrades,
  627. " | CW:",DoubleToString(g_callWeight,3)," PW:",DoubleToString(g_putWeight,3));
  628. }
  629.  
  630. // --- BROKER TRAP CALCULATOR (v7.3 FIXED) ---
  631. // Uses recent bars (1-15) instead of old bars (3-30)
  632. // Includes live candle (index 0) for real-time trap detection
  633. double CalculateBrokerTrap(double callBias,double putBias){
  634. if(!ENABLE_TRAP_DETECTION||Bars<20) return 0;
  635. double trapScore=0;
  636. bool jpy=(StringFind(Symbol(),"JPY")>=0);
  637. double pip=jpy?0.01:0.0001;
  638.  
  639. // === CHECK RECENT BARS (1-15) - FIXED: was 3-30 ===
  640. for(int i=1;i<=15&&i<Bars-1;i++){
  641. // Engulfing patterns
  642. bool bullEngulf=(Close[i]>Open[i])&&(Close[i-1]<Open[i-1])&&
  643. (Close[i]>=Open[i-1])&&(Open[i]<=Close[i-1]);
  644. bool bearEngulf=(Close[i]<Open[i])&&(Close[i-1]>Open[i-1])&&
  645. (Close[i]<=Open[i-1])&&(Open[i]>=Close[i-1]);
  646.  
  647. // Star patterns (3 candle)
  648. if(i>=2){
  649. bool mornStar=(Close[i-2]<Open[i-2])&&
  650. (MathAbs(Close[i-1]-Open[i-1])<pip*3)&& // Fixed: was Point*5
  651. (Close[i]>Open[i]);
  652. bool eveStar =(Close[i-2]>Open[i-2])&&
  653. (MathAbs(Close[i-1]-Open[i-1])<pip*3)&& // Fixed: was Point*5
  654. (Close[i]<Open[i]);
  655.  
  656. // Context: prior trend
  657. if(i>=3){
  658. if(bullEngulf&&Close[i-3]<Close[i-2]) trapScore+=3.5;
  659. if(bearEngulf&&Close[i-3]>Close[i-2]) trapScore+=3.5;
  660. if(mornStar&&Close[i-3]<Close[i-2]) trapScore+=3.0;
  661. if(eveStar&&Close[i-3]>Close[i-2]) trapScore+=3.0;
  662. }
  663. }
  664.  
  665. // Large wick traps (rejection candles)
  666. double rng=High[i]-Low[i];
  667. if(rng<=0) continue;
  668. double uw=(High[i]-MathMax(Open[i],Close[i]))/rng;
  669. double lw=(MathMin(Open[i],Close[i])-Low[i])/rng;
  670.  
  671. if(uw>0.55&&Close[i]<Open[i]) trapScore+=2.0; // Upper wick rejection + bearish = trap
  672. if(lw>0.55&&Close[i]>Open[i]) trapScore+=2.0; // Lower wick rejection + bullish = trap
  673. }
  674.  
  675. // === CHECK LIVE CANDLE (index 0) - NEW: Real-time detection ===
  676. double rng0=High[0]-Low[0];
  677. if(rng0>0){
  678. double uw0=(High[0]-MathMax(Open[0],Close[0]))/rng0;
  679. double lw0=(MathMin(Open[0],Close[0])-Low[0])/rng0;
  680.  
  681. // Live rejection happening NOW = high confidence trap
  682. if(uw0>0.65&&Close[0]<Open[0]) trapScore+=4.0;
  683. if(lw0>0.65&&Close[0]>Open[0]) trapScore+=4.0;
  684.  
  685. // Live engulfing forming
  686. if(Close[0]>Open[0]&&Close[1]<Open[1]){ // Bullish engulfing forming
  687. if(Close[0]>Open[1]&&Open[0]<Close[1]) trapScore+=3.0;
  688. }
  689. if(Close[0]<Open[0]&&Close[1]>Open[1]){ // Bearish engulfing forming
  690. if(Close[0]<Open[1]&&Open[0]>Close[1]) trapScore+=3.0;
  691. }
  692. }
  693.  
  694. // Bias multiplier - but cap it to prevent extreme scores
  695. double biasStr=MathMax(callBias,putBias)-50;
  696. double biasMul=1.0+biasStr/150.0; // Fixed: was /100.0 (too aggressive)
  697. if(biasMul>1.5) biasMul=1.5; // Cap at 1.5x
  698. if(biasMul<0.5) biasMul=0.5; // Floor at 0.5x
  699.  
  700. trapScore*=biasMul;
  701. return MathMin(100,trapScore);
  702. }
  703.  
  704. // --- MAIN CALCULATE FUNCTION (v7.3 COMPLETE FIX) ---
  705. void CalculateMTG(){
  706. if(!ENABLE_MTG||Bars<50){g_mtgState="OFF";g_mtgReason="";g_mtgClr=CGR;return;}
  707. bool jpy=(StringFind(Symbol(),"JPY")>=0);
  708. double pip=jpy?0.01:0.0001;
  709.  
  710. // Check previous result on new bar
  711. CheckPreviousResult();
  712.  
  713. // Time filter
  714. int sec=(int)(TimeCurrent()-Time[0]);
  715. if(sec<NCP_OpenNoiseSeconds){g_mtgState="...";g_mtgReason="Bar open";g_mtgClr=CGR;g_mtgHype=50;g_mtgBetrayal=50;return;}
  716. if(sec>=NCP_KillZoneSeconds){g_mtgState="END";g_mtgReason="Late";g_mtgClr=NEON_ORANGE;g_mtgHype=50;g_mtgBetrayal=50;return;}
  717.  
  718. // Spread filter
  719. double spPips=MarketInfo(Symbol(),MODE_SPREAD)*Point/pip;
  720. if(spPips>NCP_MaxSpreadPips){g_mtgState="SPREAD";g_mtgReason=DoubleToString(spPips,1)+"p";g_mtgClr=NEON_ORANGE;g_mtgHype=50;g_mtgBetrayal=50;return;}
  721.  
  722. // ATR filter
  723. double atr=iATR(Symbol(),PERIOD_M1,NCP_ATRPeriod,1);
  724. double atrPips=atr/pip;
  725. if(atrPips<NCP_MinATRPips){g_mtgState="FLAT";g_mtgReason="Low ATR";g_mtgClr=CGR;g_mtgHype=50;g_mtgBetrayal=50;return;}
  726.  
  727. // === INDICATORS ===
  728. double eF=iMA(NULL,0,NCP_FastEMA,0,MODE_EMA,PRICE_CLOSE,1);
  729. double eM=iMA(NULL,0,NCP_MidEMA, 0,MODE_EMA,PRICE_CLOSE,1);
  730. double eS=iMA(NULL,0,NCP_SlowEMA,0,MODE_EMA,PRICE_CLOSE,1);
  731.  
  732. // FIXED: Proper trend detection (no double counting)
  733. bool upTrend=(eF>eM&&eM>eS);
  734. bool dnTrend=(eF<eM&&eM<eS);
  735. bool upBias=(eF>eS)&&!upTrend; // FIXED: exclude if already upTrend
  736. bool dnBias=(eF<eS)&&!dnTrend; // FIXED: exclude if already dnTrend
  737.  
  738. bool h5Up=false,h5Dn=false;
  739. if(NCP_UseM5Confirm){
  740. double h5F=iMA(NULL,PERIOD_M5,NCP_FastEMA,0,MODE_EMA,PRICE_CLOSE,1);
  741. double h5M=iMA(NULL,PERIOD_M5,NCP_MidEMA, 0,MODE_EMA,PRICE_CLOSE,1);
  742. double h5S=iMA(NULL,PERIOD_M5,NCP_SlowEMA,0,MODE_EMA,PRICE_CLOSE,1);
  743. h5Up=(h5F>h5M&&h5M>h5S);
  744. h5Dn=(h5F<h5M&&h5M<h5S);
  745. }
  746.  
  747. // FIXED: RSI Normalization - capped to prevent inflation
  748. double nM5=(NCP_RSIM5Target>0)?NCP_RSIBase/NCP_RSIM5Target:1.0;
  749. double nM15=(NCP_RSIM15Target>0)?NCP_RSIBase/NCP_RSIM15Target:1.0;
  750. if(nM5>1.3) nM5=1.3; // FIXED: cap at 1.3
  751. if(nM15>1.5) nM15=1.5; // FIXED: cap at 1.5
  752.  
  753. double r1=iRSI(NULL,0,NCP_RSIPeriod,PRICE_CLOSE,1);
  754. double r5=iRSI(NULL,PERIOD_M5,NCP_RSIPeriod,PRICE_CLOSE,1);
  755. double r15=iRSI(NULL,PERIOD_M15,NCP_RSIPeriod,PRICE_CLOSE,1);
  756. double n5v=r5*nM5;
  757. double n15v=r15*nM15;
  758.  
  759. int rBullCt=(r1>50?1:0)+(n5v>50?1:0)+(n15v>50?1:0);
  760. int rBearCt=(r1<50?1:0)+(n5v<50?1:0)+(n15v<50?1:0);
  761. bool rBull2=(rBullCt>=2);
  762. bool rBear2=(rBearCt>=2);
  763.  
  764. double stK=iStochastic(Symbol(),PERIOD_M1,5,3,3,MODE_SMA,0,MODE_MAIN,1);
  765. bool stOS=(stK<25),stOB=(stK>75);
  766.  
  767. double macdM=iMACD(NULL,0,8,17,5,PRICE_CLOSE,MODE_MAIN,1);
  768. double macdS=iMACD(NULL,0,8,17,5,PRICE_CLOSE,MODE_SIGNAL,1);
  769. double macdM2=iMACD(NULL,0,8,17,5,PRICE_CLOSE,MODE_MAIN,2);
  770. double macdS2=iMACD(NULL,0,8,17,5,PRICE_CLOSE,MODE_SIGNAL,2);
  771. bool macdBull=(macdM>macdS);
  772. bool macdBear=(macdM<macdS);
  773. bool macdCrossUp=(macdM>macdS&&macdM2<=macdS2);
  774. bool macdCrossDn=(macdM<macdS&&macdM2>=macdS2);
  775.  
  776. // === CANDLE ANALYSIS ===
  777. double body1=MathAbs(Close[1]-Open[1]);
  778. double rng1=High[1]-Low[1];
  779. if(rng1<=0){g_mtgState="WAIT";return;}
  780.  
  781. double uwPct=(High[1]-MathMax(Open[1],Close[1]))/rng1;
  782. double lwPct=(MathMin(Open[1],Close[1])-Low[1])/rng1;
  783. double bPct=body1/rng1;
  784.  
  785. bool bull1=(Close[1]>Open[1]);
  786. bool bear1=(Close[1]<Open[1]);
  787. bool sBull=(bull1&&bPct>=0.5&&body1>atr*0.3);
  788. bool sBear=(bear1&&bPct>=0.5&&body1>atr*0.3);
  789. bool rejUp=(uwPct>0.40&&bPct<0.50);
  790. bool rejDn=(lwPct>0.40&&bPct<0.50);
  791. bool doji=(bPct<0.20);
  792.  
  793. double mom1=(Close[1]-Close[2])/pip;
  794. double mom3=(Close[1]-Close[4])/pip;
  795. bool bMom=(mom1>0.8&&mom3>1.5);
  796. bool brMom=(mom1<-0.8&&mom3<-1.5);
  797.  
  798. double hh3=MathMax(High[2],MathMax(High[3],High[4]));
  799. double ll3=MathMin(Low[2], MathMin(Low[3], Low[4]));
  800. bool bkUp=(Close[1]>hh3&&body1>atr*0.25);
  801. bool bkDn=(Close[1]<ll3&&body1>atr*0.25);
  802. bool fkUp=(High[1]>hh3&&Close[1]<hh3);
  803. bool fkDn=(Low[1]<ll3&&Close[1]>ll3);
  804.  
  805. int bullRun=0,bearRun=0;
  806. for(int i=1;i<=6&&i<Bars;i++){
  807. if(Close[i]>Open[i]){if(bearRun>0)break;bullRun++;}
  808. else if(Close[i]<Open[i]){if(bullRun>0)break;bearRun++;}
  809. else break;
  810. }
  811.  
  812. double rndSt=NCP_RoundStepPips*pip;
  813. double rndTl=NCP_RoundTolPips*pip;
  814. double bs=MathFloor(Close[1]/rndSt)*rndSt;
  815. bool rrUp=false,rrDn=false;
  816. for(int k=-1;k<=1;k++){
  817. double lv=bs+k*rndSt;
  818. if(MathAbs(Close[1]-lv)<=rndTl){
  819. if(High[1]>lv&&Close[1]<lv-pip) rrUp=true;
  820. if(Low[1]<lv&&Close[1]>lv+pip) rrDn=true;
  821. break;
  822. }
  823. }
  824.  
  825. double v1=(double)Volume[1],vAvg=0;
  826. for(int i=2;i<=11;i++) vAvg+=(double)Volume[i];
  827. vAvg/=10.0;
  828. bool hiVol=(NCP_UseVolume&&vAvg>0&&v1>vAvg*1.2);
  829. bool loVol=(NCP_UseVolume&&vAvg>0&&v1<vAvg*0.85);
  830.  
  831. // === SCORING ===
  832. double cS=50.0,pS=50.0;
  833.  
  834. if(upTrend){cS+=15;pS-=8;}
  835. else if(dnTrend){pS+=15;cS-=8;}
  836. else if(upBias){cS+=7;}
  837. else if(dnBias){pS+=7;}
  838.  
  839. if(NCP_UseM5Confirm){
  840. if(h5Up){cS+=9;pS-=5;}
  841. if(h5Dn){pS+=9;cS-=5;}
  842. }
  843.  
  844. if(rBull2){cS+=10;pS-=5;}
  845. if(rBear2){pS+=10;cS-=5;}
  846. if(stOS){cS+=10;pS-=5;}
  847. if(stOB){pS+=10;cS-=5;}
  848. if(macdCrossUp){cS+=12;pS-=6;}
  849. else if(macdBull){cS+=5;}
  850. if(macdCrossDn){pS+=12;cS-=6;}
  851. else if(macdBear){pS+=5;}
  852. if(bMom){cS+=10;pS-=5;}
  853. if(brMom){pS+=10;cS-=5;}
  854. if(sBull&&!rejUp){cS+=13;pS-=7;}
  855. if(sBear&&!rejDn){pS+=13;cS-=7;}
  856. if(rejUp){pS+=12;cS-=6;}
  857. if(rejDn){cS+=12;pS-=6;}
  858. if(doji){cS-=3;pS-=3;}
  859. if(bkUp){cS+=9;pS-=4;}
  860. if(bkDn){pS+=9;cS-=4;}
  861. if(fkUp){pS+=11;cS-=5;}
  862. if(fkDn){cS+=11;pS-=5;}
  863.  
  864. if(bullRun>=3){pS+=14;cS-=7;}
  865. else if(bearRun>=3){cS+=14;pS-=7;}
  866. if(bullRun>=4){pS+=6;}
  867. if(bearRun>=4){cS+=6;}
  868. if(bullRun>=5){pS+=4;}
  869. if(bearRun>=5){cS+=4;}
  870.  
  871. if(rrUp){pS+=10;cS-=5;}
  872. if(rrDn){cS+=10;pS-=5;}
  873.  
  874. if(NCP_UseVolume){
  875. if(hiVol){
  876. if(bull1) cS+=7;
  877. if(bear1) pS+=7;
  878. }
  879. if(loVol){cS-=2;pS-=2;}
  880. }
  881.  
  882. // === NEW: Live Candle Bonus (Close[0] analysis) ===
  883. double rng0=High[0]-Low[0];
  884. if(rng0>0){
  885. double uw0=(High[0]-MathMax(Open[0],Close[0]))/rng0;
  886. double lw0=(MathMin(Open[0],Close[0])-Low[0])/rng0;
  887. bool bull0=(Close[0]>Open[0]);
  888. bool bear0=(Close[0]<Open[0]);
  889.  
  890. if(uw0>0.50&&!bull0){pS+=8;cS-=4;}
  891. if(lw0>0.50&&!bear0){cS+=8;pS-=4;}
  892.  
  893. if(bull0&&bull1) cS+=5;
  894. if(bear0&&bear1) pS+=5;
  895. }
  896.  
  897. // Trap context (recent 30 bars)
  898. double tBull=0,tBear=0;
  899. for(int t=2;t<=30&&t<Bars;t++){
  900. double r=High[t]-Low[t];
  901. if(r<=0) continue;
  902. double uw=High[t]-MathMax(Open[t],Close[t]);
  903. double lw=MathMin(Open[t],Close[t])-Low[t];
  904. if(uw>r*0.55&&Close[t]<Open[t]) tBear+=1.2;
  905. if(lw>r*0.55&&Close[t]>Open[t]) tBull+=1.2;
  906. }
  907. if(tBull>tBear+2.0) cS+=MathMin(10.0,(tBull-tBear));
  908. if(tBear>tBull+2.0) pS+=MathMin(10.0,(tBear-tBull));
  909.  
  910. // Confluence booster
  911. int bC=0,brC=0;
  912. if(upTrend||upBias) bC++;
  913. if(dnTrend||dnBias) brC++;
  914. if(h5Up) bC++;
  915. if(h5Dn) brC++;
  916. if(rBull2) bC++;
  917. if(rBear2) brC++;
  918. if(stOS) bC++;
  919. if(stOB) brC++;
  920. if(macdBull||macdCrossUp) bC++;
  921. if(macdBear||macdCrossDn) brC++;
  922. if(bMom) bC++;
  923. if(brMom) brC++;
  924. if(sBull) bC++;
  925. if(sBear) brC++;
  926. if(rejDn) bC++;
  927. if(rejUp) brC++;
  928. if(bkUp||fkDn) bC++;
  929. if(bkDn||fkUp) brC++;
  930. if(rrDn) bC++;
  931. if(rrUp) brC++;
  932. if(hiVol&&bull1) bC++;
  933. if(hiVol&&bear1) brC++;
  934. if(bearRun>=3) bC++;
  935. if(bullRun>=3) brC++;
  936.  
  937. if(bC>=5){cS+=4;}
  938. if(brC>=5){pS+=4;}
  939. if(bC>=7){cS+=3;}
  940. if(brC>=7){pS+=3;}
  941. if(bC>=9){cS+=2;}
  942. if(brC>=9){pS+=2;}
  943.  
  944. // Normalize raw score
  945. cS=MathMax(1.0,cS);
  946. pS=MathMax(1.0,pS);
  947. double tot=cS+pS;
  948. cS=(cS/tot)*100.0;
  949. pS=100.0-cS;
  950.  
  951. // === LEARNING WEIGHT (only after MIN_TRADES) ===
  952. if(ENABLE_LEARNING&&g_totalTrades>=MIN_TRADES_TO_LEARN){
  953. double wCap=MathMax(0.6,g_callWeight);
  954. double wPap=MathMax(0.6,g_putWeight);
  955. cS*=wCap;
  956. pS*=wPap;
  957. tot=cS+pS;
  958. if(tot>0){cS=(cS/tot)*100.0;pS=100.0-cS;}
  959. }
  960.  
  961. // === BROKER TRAP DETECTION ===
  962. g_trapScore=0;
  963. if(ENABLE_TRAP_DETECTION){
  964. g_trapScore=CalculateBrokerTrap(cS,pS);
  965. if(g_trapScore>70){
  966. if(cS>pS){pS+=18;cS-=10;}
  967. else{cS+=18;pS-=10;}
  968. tot=cS+pS;
  969. cS=(cS/tot)*100.0;
  970. pS=100.0-cS;
  971. }
  972. }
  973.  
  974. // === SCORE SMOOTHING — DISPLAY ONLY ===
  975. double smoothFactor=0.55;
  976. g_smoothCallScore = g_smoothCallScore*(1.0-smoothFactor) + cS*smoothFactor;
  977. g_smoothPutScore = g_smoothPutScore*(1.0-smoothFactor) + pS*smoothFactor;
  978.  
  979. double smoothTot=g_smoothCallScore+g_smoothPutScore;
  980. if(smoothTot>0){
  981. g_smoothCallScore=(g_smoothCallScore/smoothTot)*100.0;
  982. g_smoothPutScore=100.0-g_smoothCallScore;
  983. }
  984.  
  985. // Display uses SMOOTHED values
  986. g_mtgHype = NormalizeDouble(g_smoothCallScore, 1);
  987. g_mtgBetrayal = NormalizeDouble(g_smoothPutScore, 1);
  988.  
  989. // SIGNAL uses RAW values (FIXED: this was the main bug!)
  990.  
  991. // Strength based on RAW scores
  992. double edge=MathAbs(cS-pS);
  993. string str="WEAK";
  994. if(edge>=22) str="EXTREME";
  995. else if(edge>=15) str="STRONG";
  996. else if(edge>=8) str="MEDIUM";
  997.  
  998. // Save globals
  999. g_mtgPattern = str;
  1000. g_mtgBullCount = bC;
  1001. g_mtgBearCount = brC;
  1002. g_mtgRecovery = NormalizeDouble(atrPips,1);
  1003.  
  1004. string rsiStr="";
  1005. if(NCP_UseRSINorm)
  1006. rsiStr="R1:"+DoubleToString(r1,0)+" N5:"+DoubleToString(n5v,0)+" T:"+DoubleToString(g_trapScore,0);
  1007.  
  1008. // FIXED: Lowered thresholds for more signals
  1009. int minP=NCP_MinProbMedium;
  1010. if(str=="STRONG") minP=NCP_MinProbStrong-2;
  1011. if(str=="EXTREME") minP=NCP_MinProbExtreme-3;
  1012.  
  1013. // === FINAL SIGNAL (on new bar only for tracking) ===
  1014. if(Time[0]!=g_ncpLastSignalTime){
  1015. g_ncpLastSignalTime=Time[0];
  1016. g_ncpSignalProcessed=false;
  1017. }
  1018.  
  1019. bool isTrap=(ENABLE_TRAP_DETECTION&&g_trapScore>70);
  1020.  
  1021. if(cS>=minP&&cS>pS){
  1022. g_mtgState = isTrap?"PUT TRAP":"CALL";
  1023. g_mtgReason = rsiStr;
  1024. g_mtgAction = isTrap?"PUT "+DoubleToString(g_trapScore,0)+"% TRAP!":"CALL "+DoubleToString(cS,1)+"% ["+str+"]";
  1025. g_mtgClr = isTrap?NEON_PURPLE:(str=="EXTREME")?NEON_GREEN:(str=="STRONG")?NEON_LIME:NEON_CYAN;
  1026. if(!g_ncpSignalProcessed){
  1027. g_ncpLastSignalType=isTrap?"PUT":"CALL";
  1028. g_ncpLastSignalTime=Time[0];
  1029. }
  1030. }
  1031. else if(pS>=minP&&pS>cS){
  1032. g_mtgState = isTrap?"CALL TRAP":"PUT";
  1033. g_mtgReason = rsiStr;
  1034. g_mtgAction = isTrap?"CALL "+DoubleToString(g_trapScore,0)+"% TRAP!":"PUT "+DoubleToString(pS,1)+"% ["+str+"]";
  1035. g_mtgClr = isTrap?NEON_PURPLE:(str=="EXTREME")?NEON_RED:(str=="STRONG")?C'255,60,60':NEON_ORANGE;
  1036. if(!g_ncpSignalProcessed){
  1037. g_ncpLastSignalType=isTrap?"CALL":"PUT";
  1038. g_ncpLastSignalTime=Time[0];
  1039. }
  1040. }
  1041. else{
  1042. g_mtgState = "WAIT";
  1043. g_mtgReason = rsiStr;
  1044. string edgeDir=(cS>pS)?">C":">P";
  1045. g_mtgAction = edgeDir+" "+DoubleToString(MathMax(cS,pS),0)+"%";
  1046. g_mtgClr = NEON_YELLOW;
  1047. g_ncpSignalProcessed=true;
  1048. }
  1049. }
  1050.  
  1051. //+------------------------------------------------------------------+
  1052. // HA CANDLE CALCULATION (M1 + M5)
  1053. //+------------------------------------------------------------------+
  1054. void CalcHA(){
  1055. if(Bars<10) return;
  1056. ArraySetAsSeries(Open,true); ArraySetAsSeries(High,true); ArraySetAsSeries(Low,true); ArraySetAsSeries(Close,true);
  1057. static double haO1[500],haC1[500]; static datetime lb1=0;
  1058. ArraySetAsSeries(haO1,true); ArraySetAsSeries(haC1,true);
  1059. datetime c1=iTime(NULL,PERIOD_M1,0);
  1060. if(c1!=lb1){ lb1=c1; int lim=MathMin(iBars(NULL,PERIOD_M1),500);
  1061. for(int i=lim-1;i>=0;i--){ haC1[i]=(Open[i]+High[i]+Low[i]+Close[i])/4.0;
  1062. if(i==lim-1) haO1[i]=(Open[i]+Close[i])/2.0; else haO1[i]=(haO1[i+1]+haC1[i+1])/2.0; }}
  1063. double d1=MathAbs(haC1[1]-haO1[1]),r1=High[1]-Low[1];
  1064. bool dz=(r1>0&&d1<r1*0.1),b1=(!dz&&haC1[1]>haO1[1]),be1=(!dz&&haC1[1]<haO1[1]);
  1065. if(b1){g_haM1="HA BULLISH 1";g_haM1Color=clrLime;}
  1066. else if(be1){g_haM1="HA BEARISH 1";g_haM1Color=clrRed;}
  1067. else{g_haM1="HA DOJI 1";g_haM1Color=clrYellow;}
  1068. if(iBars(NULL,PERIOD_M5)>=10){
  1069. static double haO5[200],haC5[200]; static datetime lb5=0;
  1070. ArraySetAsSeries(haO5,true); ArraySetAsSeries(haC5,true);
  1071. datetime c5=iTime(NULL,PERIOD_M5,0);
  1072. if(c5!=lb5){ lb5=c5; int lim=MathMin(iBars(NULL,PERIOD_M5),200);
  1073. for(int i=lim-1;i>=0;i--){ double o=iOpen(NULL,PERIOD_M5,i),h=iHigh(NULL,PERIOD_M5,i),l=iLow(NULL,PERIOD_M5,i),c=iClose(NULL,PERIOD_M5,i);
  1074. haC5[i]=(o+h+l+c)/4.0; if(i==lim-1) haO5[i]=(o+c)/2.0; else haO5[i]=(haO5[i+1]+haC5[i+1])/2.0; }}
  1075. double d5=MathAbs(haC5[1]-haO5[1]),r5=iHigh(NULL,PERIOD_M5,1)-iLow(NULL,PERIOD_M5,1);
  1076. bool dz5=(r5>0&&d5<r5*0.1),b5=(!dz5&&haC5[1]>haO5[1]),be5=(!dz5&&haC5[1]<haO5[1]);
  1077. if(b5){g_haM5="HA BULLISH 5";g_haM5Color=clrLime;}
  1078. else if(be5){g_haM5="HA BEARISH 5";g_haM5Color=clrRed;}
  1079. else{g_haM5="HA DOJI 5";g_haM5Color=clrYellow;}
  1080. if(b1&&b5)g_haBoth="BOTH BULL"; else if(be1&&be5)g_haBoth="BOTH BEAR"; else g_haBoth="MIXED";
  1081. }else{g_haM5="5 --";g_haM5Color=clrGray;g_haBoth="HA --";}
  1082. }
  1083.  
  1084. //+------------------------------------------------------------------+
  1085. // HELPER FUNCTIONS
  1086. //+------------------------------------------------------------------+
  1087. bool IsLineNearby(double price,double pipTol=3.0){
  1088. bool jpy=(StringFind(Symbol(),"JPY")>=0); double pip=jpy?0.01:0.0001; double tol=pipTol*pip;
  1089. for(int i=ObjectsTotal()-1;i>=0;i--){ string nm=ObjectName(i);
  1090. if((int)ObjectGetInteger(0,nm,OBJPROP_TYPE)==OBJ_HLINE){
  1091. double lp=ObjectGetDouble(0,nm,OBJPROP_PRICE); if(MathAbs(lp-price)<tol) return true; }}
  1092. return false;
  1093. }
  1094. string GetCurrentSession(color &sC){
  1095. int h=TimeHour(TimeGMT());
  1096. if(h>=12&&h<16){sC=NEON_GOLD;return "LON+NY";}
  1097. if(h>=7&&h<9){sC=NEON_CYAN;return "ASI+LON";}
  1098. if(h>=7&&h<16){sC=NEON_GREEN;return "LONDON";}
  1099. if(h>=12&&h<21){sC=NEON_BLUE;return "NEW YORK";}
  1100. if(h>=0&&h<9){sC=NEON_ORANGE;return "ASIA";}
  1101. sC=C'100,100,120'; return "SYDNEY";
  1102. }
  1103. bool IsSessionActive(){int h=TimeHour(TimeGMT());return(h>=7&&h<=21);}
  1104. bool IsBigCandle(int s){if(Bars<20)return false;double a=iATR(NULL,PERIOD_M1,14,0);return(a>0&&(High[s]-Low[s])>a*1.8);}
  1105. bool CheckMTFConfirmation(){
  1106. if(!MTF_CONFIRM) return true;
  1107. bool m1Bull=(Close[1]>Open[1]);
  1108. bool m5Bull=false,m15Bull=false;
  1109. if(iBars(NULL,PERIOD_M5)>=3) m5Bull=(iClose(NULL,PERIOD_M5,1)>iOpen(NULL,PERIOD_M5,1));
  1110. if(iBars(NULL,PERIOD_M15)>=3) m15Bull=(iClose(NULL,PERIOD_M15,1)>iOpen(NULL,PERIOD_M15,1));
  1111. int bullCount=0,bearCount=0;
  1112. if(m1Bull) bullCount++; else bearCount++;
  1113. if(m5Bull) bullCount++; else bearCount++;
  1114. if(m15Bull) bullCount++; else bearCount++;
  1115. g_mtfConfirmed=(bullCount>=2 || bearCount>=2);
  1116. return g_mtfConfirmed;
  1117. }
  1118. double Tanh(double x){double e=MathExp(2.0*x);return(e-1.0)/(e+1.0);}
  1119. double CalcFractalDimension(int period){
  1120. if(period<2)return 1.5;double sum=0.0;
  1121. for(int i=1;i<period;i++){double r1=High[i]-Low[i],r2=High[i+1]-Low[i+1];if(r1<=0.0||r2<=0.0)continue;sum+=MathLog(r1/r2+0.001)*MathLog((double)i/(double)period);}
  1122. double result=2.0-sum/period;if(result<1.0)result=1.0;if(result>2.0)result=2.0;return result;
  1123. }
  1124. double DetectBrokerTrap(int lookback){
  1125. int bull=0,bear=0;for(int i=1;i<=lookback;i++){if(Close[i]>Open[i])bull++;else if(Close[i]<Open[i])bear++;}
  1126. double crowd=(bull>bear)?(double)bull/lookback:(double)bear/lookback;
  1127. double priceMove=MathAbs(Close[1]-Close[lookback])/(Close[lookback]+0.00001);
  1128. if(crowd>0.7&&priceMove<0.002)return 0.9;if(crowd>0.6&&priceMove>0.001)return 0.5;return 0.3;
  1129. }
  1130. double QuickMedian(double &arr[],int size){
  1131. double sorted[20];for(int i=0;i<size;i++)sorted[i]=arr[i];
  1132. for(int i=0;i<size-1;i++)for(int j=i+1;j<size;j++)if(sorted[i]>sorted[j]){double tmp=sorted[i];sorted[i]=sorted[j];sorted[j]=tmp;}
  1133. return(sorted[size/2]+sorted[(size-1)/2])/2.0;
  1134. }
  1135. double CalcAdvancedTFA(string &detail){
  1136. if(Bars<30){detail="Bars low";return 3.0;}
  1137. double score=0;string p="";
  1138. double adx=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);
  1139. double adxSc=(adx>=25)?1.0:(adx>=20)?0.6:(adx>=15)?0.3:0.1;
  1140. score+=adxSc;p+="ADX:"+((adxSc>=0.6)?"+":"-")+" ";
  1141. double v1=iVolume(NULL,PERIOD_M1,1),vA=0;
  1142. for(int i=2;i<=6;i++)vA+=iVolume(NULL,PERIOD_M1,i);
  1143. vA=(vA>0)?vA/5.0:1;double vr=v1/vA;
  1144. double volSc=(vr>=2.0)?1.0:(vr>=1.5)?0.7:(vr>=1.1)?0.4:0.1;
  1145. score+=volSc;p+="VOL:"+((volSc>=0.5)?"+":"-")+" ";
  1146. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  1147. double rsiSc=0.2;if(rsi>=60||rsi<=40)rsiSc=1.0;else if(rsi>=55||rsi<=45)rsiSc=0.6;
  1148. score+=rsiSc;p+="RSI:"+((rsiSc>=0.6)?"+":"-")+" ";
  1149. double atr=iATR(NULL,PERIOD_M1,14,1),cs=High[1]-Low[1],atrSc=0.2;
  1150. if(atr>0){double r=cs/atr;if(r>=0.7&&r<=1.5)atrSc=1.0;else if(r>=0.5&&r<=2.0)atrSc=0.6;else atrSc=0.3;}
  1151. score+=atrSc;p+="ATR:"+((atrSc>=0.6)?"+":"-")+" ";
  1152. double body=MathAbs(Close[1]-Open[1]),range=High[1]-Low[1],bdySc=0.2;
  1153. if(range>0){double br=body/range;if(br>=0.6)bdySc=1.0;else if(br>=0.4)bdySc=0.6;else if(br>=0.2)bdySc=0.3;}
  1154. score+=bdySc;p+="BODY:"+((bdySc>=0.6)?"+":"-")+" ";
  1155. double sesSc=IsSessionActive()?1.0:0.4;
  1156. score+=sesSc;p+="SES:"+((sesSc>=0.7)?"+":"-");
  1157. double finalScore=MathMin(6.0,score);int rounded=(int)MathRound(finalScore);
  1158. string dir="MIX";
  1159. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5")dir="STRONG UP";
  1160. else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5")dir="STRONG DOWN";
  1161. else if(g_haM1=="HA BULLISH 1")dir="UP";
  1162. else if(g_haM1=="HA BEARISH 1")dir="DOWN";
  1163. detail=p+" | "+IntegerToString(rounded)+"/6 "+dir;
  1164. g_tfa_detail=detail;return finalScore;
  1165. }
  1166. double CalcAdvancedMicroAI(){
  1167. if(Bars<20)return 50.0;double score=50.0;
  1168. for(int i=1;i<=5;i++){double r=High[i]-Low[i];if(r<=0)continue;double uw=(High[i]-MathMax(Open[i],Close[i]))/r,lw=(MathMin(Open[i],Close[i])-Low[i])/r,w=(6.0-i)/5.0;if(uw>0.60)score-=w*15;if(lw>0.60)score+=w*15;}
  1169. double pos5=0;for(int i=1;i<=5;i++){double r=High[i]-Low[i];if(r<=0)continue;pos5+=(Close[i]-Low[i])/r;}score+=(pos5/5.0-0.5)*20.0;
  1170. int gc=0,rc=0;for(int i=1;i<=8;i++){if(Close[i]>Open[i])gc++;else if(Close[i]<Open[i])rc++;}score+=(gc-rc)*2.5;
  1171. double vl1=(double)iVolume(NULL,PERIOD_M1,1),vA=0;for(int i=2;i<=6;i++)vA+=iVolume(NULL,PERIOD_M1,i);vA/=5.0;
  1172. if(vA>0){double vr=vl1/vA;if(vr>1.5&&Close[1]>Open[1])score+=8;if(vr>1.5&&Close[1]<Open[1])score-=8;}
  1173. double r1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),r5=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);
  1174. if(r1<35)score+=10;if(r1>65)score-=10;if(r1>r5&&Close[1]<Close[5])score+=6;if(r1<r5&&Close[1]>Close[5])score-=6;
  1175. return MathMax(5.0,MathMin(95.0,score));
  1176. }
  1177. double VolumeTrapScore();
  1178. int CalcAdvancedMicroTrap(){
  1179. if(Bars<20)return 0;int score=0;double r=High[1]-Low[1];
  1180. if(r>0){double uw=(High[1]-MathMax(Open[1],Close[1]))/r,lw=(MathMin(Open[1],Close[1])-Low[1])/r;if(uw>0.65)score+=30;else if(uw>0.50)score+=15;if(lw>0.65)score+=20;else if(lw>0.50)score+=10;}
  1181. double v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  1182. if(v2>0&&v1/v2>2.0)score+=25;else if(v2>0&&v1/v2>1.5)score+=12;
  1183. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);if(rsi>78||rsi<22)score+=20;else if(rsi>72||rsi<28)score+=10;
  1184. if((High[1]>g_nearest_res&&Close[1]<g_nearest_res)||(Low[1]<g_nearest_sup&&Close[1]>g_nearest_sup))score+=25;
  1185. bool aG=(Close[1]>Open[1]&&Close[2]>Open[2]&&Close[3]>Open[3]),aR=(Close[1]<Open[1]&&Close[2]<Open[2]&&Close[3]<Open[3]);
  1186. if(aG||aR)score+=15;
  1187. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);if(sp>BROKER_SPREAD_THRESHOLD*1.5)score+=10;
  1188. score+=(int)VolumeTrapScore();return MathMin(100,score);
  1189. }
  1190. double CalcAdvancedFractal(){
  1191. if(Bars<15)return 0;double score=0;
  1192. for(int i=2;i<=8&&i+2<Bars;i++){
  1193. if(High[i]>High[i-1]&&High[i]>High[i-2]&&High[i]>High[i+1]&&High[i]>High[i+2]){double d=(High[i]-Close[0])/Point;if(d<50&&d>-10)score+=35;}
  1194. if(Low[i]<Low[i-1]&&Low[i]<Low[i-2]&&Low[i]<Low[i+1]&&Low[i]<Low[i+2]){double d=(Close[0]-Low[i])/Point;if(d<50&&d>-10)score+=25;}}
  1195. int dj=0;for(int i=1;i<=6;i++){double b=MathAbs(Close[i]-Open[i]),rg=High[i]-Low[i];if(rg>0&&b<rg*0.20)dj++;}
  1196. if(dj>=3)score+=25;
  1197. double r1=High[1]-Low[1];if(r1>0){double uw1=(High[1]-MathMax(Open[1],Close[1]))/r1,lw1=(MathMin(Open[1],Close[1])-Low[1])/r1;if(uw1>0.70||lw1>0.70)score+=20;}
  1198. return MathMin(100,score);
  1199. }
  1200. string CalcAdvancedMarketBias(color &bC,double brain,double rsc){
  1201. if(Bars<30){bC=NEON_YELLOW;return "CALCULATING";}
  1202. double score=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1203. double pc=Close[0]-Close[10];
  1204. if(pc>5*pip)score+=4;else if(pc>2*pip)score+=2;else if(pc<-5*pip)score-=4;else if(pc<-2*pip)score-=2;
  1205. double hh1=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],hh2=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];
  1206. double ll1=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],ll2=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];
  1207. if(hh1>hh2&&ll1>ll2)score+=4;else if(hh1<hh2&&ll1<ll2)score-=4;else if(hh1>hh2)score+=2;else if(hh1<hh2)score-=2;
  1208. double ema20=0,ema20p=0;for(int i=0;i<20;i++)ema20+=Close[i];ema20/=20;
  1209. for(int i=1;i<=20&&i<Bars;i++)ema20p+=Close[i];ema20p/=20;
  1210. if(ema20>ema20p)score+=3;else if(ema20<ema20p)score-=3;
  1211. double ema50=0;for(int i=0;i<50&&i<Bars;i++)ema50+=Close[i];ema50/=MathMin(50,Bars);
  1212. if(ema20>ema50)score+=3;else score-=3;
  1213. int gC=0,rC=0;for(int i=1;i<=10;i++){if(Close[i]>Open[i])gC++;else if(Close[i]<Open[i])rC++;}
  1214. if(gC>=7)score+=3;else if(rC>=7)score-=3;else if(gC>=5)score+=1;else if(rC>=5)score-=1;
  1215. bool m1B=(g_haM1=="HA BULLISH 1"),m1Be=(g_haM1=="HA BEARISH 1"),m5B=(g_haM5=="HA BULLISH 5"),m5Be=(g_haM5=="HA BEARISH 5");
  1216. if(m1B&&m5B)score+=4;else if(m1Be&&m5Be)score-=4;else if(m1B)score+=2;else if(m1Be)score-=2;
  1217. double adx=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1),pDI=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_PLUSDI,1),mDI=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MINUSDI,1);
  1218. if(adx>20){if(pDI>mDI+5)score+=4;else if(mDI>pDI+5)score-=4;}
  1219. if(brain>3)score-=2;else if(brain<-3)score+=2;
  1220. if(Close[1]>Open[1])score+=1;else if(Close[1]<Open[1])score-=1;
  1221. if(rsc>2)score+=1;else if(rsc<-2)score-=1;
  1222. if(score>=12){bC=NEON_GREEN;return "STRONG BULL";}if(score>=7){bC=NEON_GREEN;return "BULLISH";}if(score>=3){bC=NEON_CYAN;return "LIGHT BULL";}
  1223. if(score<=-12){bC=NEON_RED;return "STRONG BEAR";}if(score<=-7){bC=NEON_RED;return "BEARISH";}if(score<=-3){bC=NEON_ORANGE;return "LIGHT BEAR";}
  1224. bC=NEON_YELLOW;return "NEUTRAL";
  1225. }
  1226. void CalcAdvancedPrediction(double &gP,double &rP,string &reason,color &pC){
  1227. if(Bars<50){gP=50;rP=50;reason="Bars low";pC=NEON_YELLOW;return;}
  1228. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1229. double green=0,red=0;
  1230. double body1=MathAbs(Close[1]-Open[1]),range1=High[1]-Low[1];
  1231. if(range1>0){double uw=High[1]-MathMax(Open[1],Close[1]),lw=MathMin(Open[1],Close[1])-Low[1];
  1232. if(uw>range1*0.60&&Close[1]>Open[1])red+=22;else if(lw>range1*0.60&&Close[1]<Open[1])green+=22;
  1233. else if((uw+lw)/range1>0.55){if(uw>lw)red+=12;else green+=12;}}
  1234. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),cci=iCCI(NULL,PERIOD_M1,14,PRICE_TYPICAL,1),stochK=iStochastic(NULL,PERIOD_M1,5,3,3,MODE_SMA,0,MODE_MAIN,1);
  1235. int os=0,ob=0;
  1236. if(rsi<30)os++;else if(rsi>70)ob++;if(cci<-120)os++;else if(cci>120)ob++;if(stochK<20)os++;else if(stochK>80)ob++;
  1237. if(os>=3)green+=18;else if(os>=2)green+=12;else if(ob>=3)red+=18;else if(ob>=2)red+=12;
  1238. double rsiPrev=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);
  1239. double low1=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],low2=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];
  1240. double high1=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],high2=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];
  1241. if(low1<low2&&rsi>rsiPrev&&rsi<45)green+=15;if(high1>high2&&rsi<rsiPrev&&rsi>55)red+=15;
  1242. double v1=iVolume(NULL,PERIOD_M1,1),vAvg=0;for(int i=2;i<=10;i++)vAvg+=iVolume(NULL,PERIOD_M1,i);vAvg=(vAvg>0)?vAvg/9.0:1.0;
  1243. double vol_ratio=(vAvg>0)?v1/vAvg:1.0;
  1244. if(vol_ratio>2.0){if(Close[1]>Open[1])red+=20;else green+=20;}else if(vol_ratio>1.5){if(Close[1]>Open[1])red+=12;else green+=12;}else if(vol_ratio>1.2){if(Close[1]>Open[1])green+=5;else red+=5;}
  1245. double m1_haC1=(Open[1]+High[1]+Low[1]+Close[1])/4.0,m1_haC2=(Open[2]+High[2]+Low[2]+Close[2])/4.0;
  1246. double m1_haO2=(Open[3]+Close[3])/2.0,m1_haO1=(m1_haO2+m1_haC2)/2.0;
  1247. bool haBull1=(m1_haC1>m1_haO1);
  1248. double haStr1=MathAbs(m1_haC1-m1_haO1)/(High[1]-Low[1]+0.00001);
  1249. bool haBull5=false;double haStr5=0;
  1250. if(iBars(NULL,PERIOD_M5)>=5){
  1251. double o1=iOpen(NULL,PERIOD_M5,1),h1=iHigh(NULL,PERIOD_M5,1),l1=iLow(NULL,PERIOD_M5,1),c1=iClose(NULL,PERIOD_M5,1);
  1252. double o2=iOpen(NULL,PERIOD_M5,2),h2=iHigh(NULL,PERIOD_M5,2),l2=iLow(NULL,PERIOD_M5,2),c2=iClose(NULL,PERIOD_M5,2);
  1253. double o3=iOpen(NULL,PERIOD_M5,3),c3=iClose(NULL,PERIOD_M5,3);
  1254. double haC1_5=(o1+h1+l1+c1)/4.0,haC2_5=(o2+h2+l2+c2)/4.0,haO2_5=(o3+c3)/2.0,haO1_5=(haO2_5+haC2_5)/2.0;
  1255. haBull5=(haC1_5>haO1_5);haStr5=MathAbs(haC1_5-haO1_5)/(h1-l1+0.00001);}
  1256. if(haBull1&&haBull5)green+=15;else if(!haBull1&&!haBull5)red+=15;
  1257. else if(haBull1&&haStr1>0.5)green+=8;else if(!haBull1&&haStr1>0.5)red+=8;
  1258. double adx=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1),plusDI=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_PLUSDI,1),minusDI=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MINUSDI,1);
  1259. double diDiff=plusDI-minusDI;
  1260. if(adx>25){if(diDiff>8)green+=12;else if(diDiff>4)green+=8;else if(diDiff<-8)red+=12;else if(diDiff<-4)red+=8;}
  1261. else if(adx>15){if(plusDI>minusDI)green+=4;else red+=4;}
  1262. else{if(Close[1]>Open[1])red+=3;else green+=3;}
  1263. double sup=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,20,2)],res=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,20,2)];
  1264. double dR=(res-Close[0])/pip,dS=(Close[0]-sup)/pip;
  1265. if(dR<8&&dR>=0)red+=12;else if(dR<15&&dR>=0)red+=6;
  1266. if(dS<8&&dS>=0)green+=12;else if(dS<15&&dS>=0)green+=6;
  1267. if(g_hrn_price>0){double dH=MathAbs(Close[0]-g_hrn_price)/pip;if(dH<5){if(Close[0]>g_hrn_price)red+=8;else green+=8;}}
  1268. double macdMain=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1),macdSig=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
  1269. double macdPrev=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,2),macdSigPrev=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
  1270. if(macdMain>macdSig&&macdPrev<=macdSigPrev)green+=10;else if(macdMain<macdSig&&macdPrev>=macdSigPrev)red+=10;
  1271. else if(macdMain>macdSig)green+=5;else if(macdMain<macdSig)red+=5;
  1272. if(g_otcCallPct>=75&&!haBull1)red+=15;else if(g_otcPutPct>=75&&haBull1)green+=15;
  1273. else if(g_otcCallPct>=65&&!haBull1)red+=8;else if(g_otcPutPct>=65&&haBull1)green+=8;
  1274. // BB Pullback bonus
  1275. if(ENABLE_BB_PULLBACK&&g_bbSignal=="CALL")green+=12;
  1276. else if(ENABLE_BB_PULLBACK&&g_bbSignal=="PUT")red+=12;
  1277. double diff=MathMax(-100.0,MathMin(100.0,green-red));
  1278. gP=MathMax(8.0,MathMin(92.0,50.0+diff*0.65));rP=100.0-gP;
  1279. double dom=MathMax(gP,rP);
  1280. if(dom>=82){reason=(gP>rP)?"STRONG GREEN":"STRONG RED";pC=(gP>rP)?NEON_GREEN:NEON_RED;}
  1281. else if(dom>=72){reason=(gP>rP)?"GREEN":"RED";pC=(gP>rP)?NEON_GREEN:NEON_RED;}
  1282. else if(dom>=60){reason=(gP>rP)?"WEAK GREEN":"WEAK RED";pC=(gP>rP)?NEON_CYAN:NEON_ORANGE;}
  1283. else{reason="WAIT";pC=NEON_YELLOW;}
  1284. }
  1285. string DetectMyStrategy(){
  1286. int cc=g_otcCallPct,cp=g_otcPutPct,thr=STRATEGY_THRESHOLD;
  1287. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TRAP_ONLY"){
  1288. if(cc>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TRAP";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA RED";return "PUT";}
  1289. if(cp>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TRAP";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA GREEN";return "CALL";}}
  1290. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TREND_ONLY"){
  1291. if(cc>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TREND";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA GREEN";return "CALL";}
  1292. if(cp>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TREND";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA RED";return "PUT";}}
  1293. g_strategyType="NONE";g_strategyReason="Wait "+IntegerToString(thr)+"% + HA";return "WAIT";
  1294. }
  1295. void CalcOTCCrowd(){
  1296. int cS=0,pS=0;int weights[15];
  1297. for(int w=0;w<15;w++)weights[w]=15-w*2;
  1298. for(int w=0;w<15;w++)if(weights[w]<1)weights[w]=1;
  1299. for(int i=1;i<=15&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;
  1300. double uw=(High[i]-MathMax(Open[i],Close[i]))/rg,lw=(MathMin(Open[i],Close[i])-Low[i])/rg;int wgt=weights[i-1];
  1301. if(uw>0.70&&Close[i]<Open[i])cS+=4*wgt;if(lw>0.70&&Close[i]>Open[i])pS+=4*wgt;
  1302. if(uw>0.55&&Close[i]<Open[i])cS+=2*wgt;if(lw>0.55&&Close[i]>Open[i])pS+=2*wgt;
  1303. if(Close[i]>Open[i])cS+=1*wgt;else if(Close[i]<Open[i])pS+=1*wgt;}
  1304. int tot=cS+pS;if(tot==0){g_otcCallPct=50;g_otcPutPct=50;}
  1305. else{int rC=(int)(cS*100.0/tot),rP=100-rC;
  1306. if(rC>=rP){g_otcCallPct=MathMax(50,rC);g_otcPutPct=100-g_otcCallPct;}
  1307. else{g_otcPutPct=MathMax(50,rP);g_otcCallPct=100-g_otcPutPct;}}
  1308. }
  1309. double BrainSc(int &cp){
  1310. double s=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1311. double sup=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,LB,1)],res=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,LB,1)];
  1312. double bH=MathMax(Open[1],Close[1]),bL=MathMin(Open[1],Close[1]);
  1313. if(High[1]>res&&bL<res)s-=4;if(Low[1]<sup&&bH>sup)s+=4;if(High[0]>res)s-=2;if(Low[0]<sup)s+=2;
  1314. double atr=iATR(NULL,PERIOD_M1,14,1);
  1315. if(atr!=0){double cn=(High[1]-Low[1])/atr,v1=(double)iVolume(NULL,PERIOD_M1,1),va=0;
  1316. for(int i=2;i<=10;i++)va+=iVolume(NULL,PERIOD_M1,i);va/=9.0;
  1317. double vr=va>0?v1/va:1;if(cn>1.2&&vr>1.5){if(Close[1]>Open[1])s-=3;else s+=3;}}
  1318. int c=0;for(int i=1;i<=30;i++)if(Close[i]>Open[i])c++;
  1319. cp=(int)(c*100.0/30.0);if(cp>=70)s-=5;if(cp<=30)s+=5;s*=1.5;
  1320. int dj=0;for(int i=1;i<=8;i++){double bd=MathAbs(Close[i]-Open[i]),rg=High[i]-Low[i];if(rg<=0)continue;
  1321. if(bd<rg*0.2)dj++;double uw=High[i]-MathMax(Open[i],Close[i]),lw=MathMin(Open[i],Close[i])-Low[i];
  1322. if(uw>bd*3)s+=2;if(lw>bd*3)s-=2;}if(dj>=4)s*=1.5;
  1323. int bu=0,be=0;for(int i=1;i<=8;i++){if(Close[i]>Open[i]){bu++;be=0;}else{be++;bu=0;}if(bu>=5)s-=3;if(be>=5)s+=3;}
  1324. int h=TimeHour(TimeCurrent());double mul=1.0;
  1325. if((h>=0&&h<=2)||(h>=8&&h<=11)||(h>=14&&h<=17))mul=1.20;if(h>=3&&h<=7)mul=0.90;
  1326. return s*mul;
  1327. }
  1328. double MicroWick(){double r=High[1]-Low[1];if(r<=0)return 0;return((High[1]-MathMax(Open[1],Close[1]))+(MathMin(Open[1],Close[1])-Low[1]))/r;}
  1329. double VolumeTrapScore(){
  1330. if(!VOLUME_PROFILE_TRAP||Bars<50)return 0;
  1331. double cur=Close[0],hi=High[iHighest(NULL,0,MODE_HIGH,50,1)],lo=Low[iLowest(NULL,0,MODE_LOW,50,1)];
  1332. double rg=hi-lo;if(rg<=0)return 0;int zn=5;double zs=rg/zn;double vz[5]={0,0,0,0,0};int zc[5]={0,0,0,0,0};
  1333. for(int i=1;i<=50;i++){double p=Close[i];int z=(int)((p-lo)/zs);if(z<0)z=0;if(z>=zn)z=zn-1;vz[z]+=Volume[i];zc[z]++;}
  1334. for(int z=0;z<zn;z++)if(zc[z]>0)vz[z]/=zc[z];
  1335. int cz=(int)((cur-lo)/zs);if(cz<0)cz=0;if(cz>=zn)cz=zn-1;
  1336. double vr=(double)Volume[1]/(vz[cz]+0.001);double wr=MicroWick();
  1337. if(vr>2.0&&wr>0.65)return 25;if(vr>1.8&&wr>0.55)return 15;return 0;
  1338. }
  1339. double UltimateTrapScore(){double wr=MicroWick(),v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  1340. double avg=v2>0?v2:1.0,vr=v1/avg,rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),sc=0.0;
  1341. if(wr>0.65)sc+=40;if(vr>2.0)sc+=30;if(rsi>75||rsi<25)sc+=20;
  1342. if((High[1]>g_nearest_res&&Close[1]<g_nearest_res)||(Low[1]<g_nearest_sup&&Close[1]>g_nearest_sup))sc+=25;
  1343. sc+=VolumeTrapScore();return MathMin(100.0,sc);}
  1344. bool IsBrokerForce(double dummy=0){
  1345. int sd=0;for(int i=1;i<=3;i++){if(Close[i]>Open[i])sd++;else sd--;}
  1346. double v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  1347. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);
  1348. return(MathAbs(sd)==3&&v1>v2*2.0&&sp>BROKER_SPREAD_THRESHOLD);}
  1349. double NeuralBiasFast(){double r=NeuralBias(0)*0.3+NeuralBias(1)*0.7;if(r>96)r=96;if(r<4)r=4;return r;}
  1350. double NeuralBias(int s){
  1351. double f[8];f[0]=fDoji(s)/100.0;f[1]=fWick(s)/100.0;f[2]=fVol()/100.0;f[3]=fMom(s)/100.0;
  1352. f[4]=fSpread()/100.0;f[5]=fMem(s)/100.0;f[6]=fTick(s)/100.0;f[7]=fFrac(s)/100.0;
  1353. double h[4]={0,0,0,0};for(int i=0;i<4;i++){for(int j=0;j<8;j++)h[i]+=f[j]*NW[j*4+i];h[i]=MathMax(0,h[i]-0.08);}
  1354. double o=0;for(int i=0;i<4;i++)o+=h[i]*NW[32+i];double r=50.0+o*22.0;return r>96?96:r<4?4:r;}
  1355. double fDoji(int s){int n=0;for(int i=s;i<s+5&&i<Bars;i++){double b=MathAbs(Open[i]-Close[i]),r=High[i]-Low[i];if(r>0&&b<r*0.25)n++;}return n*20.0;}
  1356. double fWick(int s){double r=High[s]-Low[s];if(r==0)return 0;double u=(High[s]-MathMax(Open[s],Close[s]))/r,l=(MathMin(Open[s],Close[s])-Low[s])/r;return(u>0.65||l>0.65)?95:(u>0.5||l>0.5)?70:0;}
  1357. double fVol(){if(Bars<11)return 0;double a=0;for(int i=1;i<=10;i++)a+=iVolume(NULL,PERIOD_M1,i);a/=10.0;if(a==0)return 0;double r=(double)iVolume(NULL,PERIOD_M1,0)/a;return r>3?95:r>2?75:r>1.5?50:0;}
  1358. double fMom(int s){if(s+3>=Bars)return 0;return((Close[s]>Open[s]&&Close[s+1]<Open[s+1])||(Close[s]<Open[s]&&Close[s+1]>Open[s+1]))?88:0;}
  1359. double fSpread(){double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);return sp>8?90:sp>5?70:sp>3?40:0;}
  1360. double fMem(int s){if(s+8>=Bars)return 0;int r=0;for(int i=s;i<s+8&&i+2<Bars;i+=2){bool vB=(Low[i]>Low[i+1]&&Low[i+2]>Low[i+1]&&Close[i+2]>Open[i+2]);bool aT=(High[i]<High[i+1]&&High[i+2]<High[i+1]&&Close[i+2]<Open[i+2]);if(vB||aT)r++;}return(double)r*12.5;}
  1361. double fTick(int s){if(s+5>=Bars)return 0;double vN=(double)iVolume(NULL,PERIOD_M1,s),vP=(double)iVolume(NULL,PERIOD_M1,s+1);if(vP==0)return 0;double vR=vN/vP,pc=MathAbs(Close[s]-Close[s+1])/Close[s+1]*100;if(vR>2.0&&pc<0.1)return 85;if(vR>1.5&&pc<0.05)return 60;return 0;}
  1362. double fFrac(int s){if(s+4>=Bars||s<2)return 0;bool b=(Low[s]<Low[s-1]&&Low[s]<Low[s-2]&&Low[s]<Low[s+1]&&Low[s]<Low[s+2]);bool t=(High[s]>High[s-1]&&High[s]>High[s-2]&&High[s]>High[s+1]&&High[s]>High[s+2]);return(b||t)?90:0;}
  1363. void CalcPrediction(double brain,double nb,int cp,double mai,int mts,double rsc,bool bf,double mw){
  1364. string ms=DetectMyStrategy();bool sA=(ms!="WAIT");
  1365. if(sA&&g_strategyType=="TRAP"){
  1366. if(ms=="PUT"){g_gProb=12.0;g_rProb=88.0;g_pAction="PUT TRAP!";g_pColor=NEON_PURPLE;g_calc_gProb=g_gProb;g_calc_rProb=g_rProb;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;return;}
  1367. if(ms=="CALL"){g_gProb=88.0;g_rProb=12.0;g_pAction="CALL TRAP!";g_pColor=NEON_PURPLE;g_calc_gProb=g_gProb;g_calc_rProb=g_rProb;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;return;}}
  1368. double tS=UltimateTrapScore();bool bF2=IsBrokerForce();
  1369. if(TRADE_MODE=="HUNT"){if(tS<85){g_gProb=50;g_rProb=50;g_pAction="HUNT: WAIT";g_pColor=NEON_YELLOW;g_calc_gProb=50;g_calc_rProb=50;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;return;}}
  1370. if(ENABLE_BROKER_KILLER&&(tS>=85||(tS>=75&&bF2))&&g_marketMode!="TREND"){g_gProb=15;g_rProb=85;g_pAction="TRAP DETECTED";g_pColor=NEON_PURPLE;g_calc_gProb=15;g_calc_rProb=85;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;return;}
  1371. double gS=0,rS=0,ab=MathMax(0.70,MathMin(1.30,g_accuracy/65.0));
  1372. if(IsSidewaysMarket()){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1373. bool nR=(g_nearest_res>0&&(g_nearest_res-Close[0])<8*pip),nS=(g_nearest_sup>0&&(Close[0]-g_nearest_sup)<8*pip);
  1374. if(!nR&&!nS){g_gProb=50;g_rProb=50;g_pAction="SIDEWAYS";g_pColor=NEON_YELLOW;g_calc_gProb=50;g_calc_rProb=50;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;return;}
  1375. if(nR)rS+=20;if(nS)gS+=20;}
  1376. if(sA&&g_strategyType=="TREND"){double sw=50.0;if(ms=="CALL"){gS+=sw*ab;g_pAction="TREND: CALL!";g_pColor=NEON_GREEN;}else if(ms=="PUT"){rS+=sw*ab;g_pAction="TREND: PUT!";g_pColor=NEON_RED;}}
  1377. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5")gS+=60;
  1378. else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5")rS+=60;
  1379. else if(g_haM1=="HA BULLISH 1")gS+=30;else if(g_haM1=="HA BEARISH 1")rS+=30;
  1380. if(HA_ALIGN_FILTER&&g_haBoth=="MIXED"){gS*=0.5;rS*=0.5;}
  1381. gS+=(nb-50)/50.0*22*ab;rS-=(nb-50)/50.0*22*ab;
  1382. double bn=MathMax(-1.0,MathMin(1.0,brain/10.0));gS+=bn*15*ab;rS-=bn*15*ab;
  1383. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1384. if(g_nearest_res>0&&(g_nearest_res-Close[0])<10*pip)rS+=12;
  1385. if(g_nearest_sup>0&&(Close[0]-g_nearest_sup)<10*pip)gS+=12;
  1386. double rsi1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);if(rsi1>70)rS+=6;if(rsi1<30)gS+=6;
  1387. double sn=MathMax(-1.0,MathMin(1.0,rsc/3.0));gS+=sn*8*ab;rS-=sn*8*ab;
  1388. double wRS=GetWickRejectSignal();gS+=(wRS>0?wRS:0);rS+=(wRS<0?MathAbs(wRS):0);
  1389. double cPS=GetCommonPointSignal();gS+=(cPS>0?cPS:0);rS+=(cPS<0?MathAbs(cPS):0);
  1390. // BB Pullback signal contribution
  1391. if(ENABLE_BB_PULLBACK){if(g_bbSignal=="CALL")gS+=18*ab;else if(g_bbSignal=="PUT")rS+=18*ab;}
  1392. int rd=DetectRSIDivergence();if(rd==1)gS+=20;if(rd==-1)rS+=20;
  1393. int cp2=DetectClassicPatterns();if(cp2==1)gS+=12;if(cp2==-1)rS+=12;
  1394. int qm=DetectQMR();if(qm==1)gS+=25;if(qm==-1)rS+=25;
  1395. int sk=StreakReversalSignal();if(sk==1)gS+=18;if(sk==-1)rS+=18;
  1396. double adxV=GetADXStrength();int adxD=GetADXDirection();if(adxV>25){if(adxD==1)gS+=12;if(adxD==-1)rS+=12;}if(adxV<15){gS*=0.7;rS*=0.7;}
  1397. int vs=SmartVolumeSignal();if(vs==1)gS+=12;if(vs==-1)rS+=12;
  1398. if(!IsVolatilityGood()){gS*=0.5;rS*=0.5;}
  1399. double diff=MathMax(-100.0,MathMin(100.0,gS-rS));
  1400. g_gProb=MathMax(8.0,MathMin(92.0,50.0+(diff/1.8)));g_rProb=100.0-g_gProb;
  1401. if(sA&&g_strategyType=="TREND"){
  1402. if(ms=="CALL"){g_gProb=MathMax(g_gProb,85.0);g_rProb=100.0-g_gProb;}
  1403. if(ms=="PUT"){g_rProb=MathMax(g_rProb,85.0);g_gProb=100.0-g_rProb;}
  1404. g_gProb=MathMin(92.0,MathMax(8.0,g_gProb));g_rProb=100.0-g_gProb;}
  1405. double dom=MathMax(g_gProb,g_rProb);
  1406. if(dom>=82){if(g_gProb>g_rProb){if(!sA){g_pAction="GREEN";g_pColor=NEON_GREEN;}}else{if(!sA){g_pAction="RED";g_pColor=NEON_RED;}}}
  1407. else if(dom>=75){if(g_gProb>g_rProb){if(!sA){g_pAction="GREEN";g_pColor=NEON_CYAN;}}else{if(!sA){g_pAction="RED";g_pColor=NEON_ORANGE;}}}
  1408. else{if(!sA){g_pAction="WAIT";g_pColor=NEON_YELLOW;}}
  1409. g_calc_gProb=g_gProb;g_calc_rProb=g_rProb;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;
  1410. }
  1411. void DetectCommonPoints(){
  1412. if(!SHOW_COMMON_POINTS){g_commonCount=0;return;}
  1413. if(Time[0]==g_lastCommonScan)return;g_lastCommonScan=Time[0];g_commonCount=0;
  1414. datetime nowBar=Time[0];int periodSec=PeriodSeconds(PERIOD_M1);
  1415. double m5h=iHigh(NULL,PERIOD_M5,iHighest(NULL,PERIOD_M5,MODE_HIGH,10,1));
  1416. double m5l=iLow(NULL,PERIOD_M5,iLowest(NULL,PERIOD_M5,MODE_LOW,10,1));
  1417. if(m5l>0&&g_commonCount<MAX_COMMON_PTS){g_commonPrice[g_commonCount]=m5l;g_commonTime[g_commonCount]=nowBar;g_commonType[g_commonCount]="CALL";g_commonExpire[g_commonCount]=nowBar+periodSec*120;g_commonCount++;}
  1418. if(g_hrn_price>0&&g_hrn_is_sup&&g_commonCount<MAX_COMMON_PTS){g_commonPrice[g_commonCount]=g_hrn_price;g_commonTime[g_commonCount]=nowBar;g_commonType[g_commonCount]="CALL";g_commonExpire[g_commonCount]=nowBar+periodSec*120;g_commonCount++;}
  1419. if(m5h>0&&g_commonCount<MAX_COMMON_PTS){g_commonPrice[g_commonCount]=m5h;g_commonTime[g_commonCount]=nowBar;g_commonType[g_commonCount]="PUT";g_commonExpire[g_commonCount]=nowBar+periodSec*120;g_commonCount++;}
  1420. if(g_hrn_price>0&&!g_hrn_is_sup&&g_commonCount<MAX_COMMON_PTS){g_commonPrice[g_commonCount]=g_hrn_price;g_commonTime[g_commonCount]=nowBar;g_commonType[g_commonCount]="PUT";g_commonExpire[g_commonCount]=nowBar+periodSec*120;g_commonCount++;}
  1421. for(int i=g_commonCount-1;i>=0;i--)if(TimeCurrent()>g_commonExpire[i]){for(int j=i;j<g_commonCount-1;j++){g_commonPrice[j]=g_commonPrice[j+1];g_commonTime[j]=g_commonTime[j+1];g_commonType[j]=g_commonType[j+1];g_commonExpire[j]=g_commonExpire[j+1];}g_commonCount--;}
  1422. }
  1423. void DrawCommonPoints(){
  1424. for(int i=0;i<MAX_COMMON_PTS;i++){SafeDel(PFX+"COMMON_"+IntegerToString(i));SafeDel(PFX+"COMMON_L_"+IntegerToString(i));SafeDel(PFX+"COMMON_HL_"+IntegerToString(i));}
  1425. if(!SHOW_COMMON_POINTS||g_commonCount==0)return;
  1426. for(int i=0;i<g_commonCount;i++){if(TimeCurrent()>g_commonExpire[i])continue;if(IsLineNearby(g_commonPrice[i],8.0))continue;
  1427. string id=PFX+"COMMON_"+IntegerToString(i),idL=PFX+"COMMON_L_"+IntegerToString(i),idH=PFX+"COMMON_HL_"+IntegerToString(i);color zc=DARK_BLUE_NEON;
  1428. ObjectCreate(0,idH,OBJ_HLINE,0,0,g_commonPrice[i]);ObjectSetInteger(0,idH,OBJPROP_COLOR,zc);ObjectSetInteger(0,idH,OBJPROP_WIDTH,3);ObjectSetInteger(0,idH,OBJPROP_STYLE,STYLE_SOLID);ObjectSetInteger(0,idH,OBJPROP_BACK,false);
  1429. ObjectCreate(0,id,OBJ_TEXT,0,Time[0],g_commonPrice[i]);ObjectSetText(id,"*",16,"Arial",zc);ObjectSetInteger(0,id,OBJPROP_BACK,false);
  1430. string lbl=(g_commonType[i]=="CALL")?"^ CALL ZONE":"v PUT ZONE";
  1431. ObjectCreate(0,idL,OBJ_TEXT,0,Time[0]+PeriodSeconds(PERIOD_M1)*3,g_commonPrice[i]);ObjectSetText(idL,lbl,10,"Arial Bold",zc);ObjectSetInteger(0,idL,OBJPROP_BACK,false);}
  1432. }
  1433. void DetectWickRejections(){
  1434. g_isSideways=IsSidewaysMarket();if(!SHOW_WICK_REJECT_LINES){g_wickLineCount=0;return;}
  1435. if(Time[0]==g_lastWickScan)return;g_lastWickScan=Time[0];
  1436. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double zone=WICK_ZONE_PIPS*pip*2;double cur=Close[0];
  1437. g_wickLineCount=0;datetime now=Time[0],expT=now+PeriodSeconds(PERIOD_M1)*WICK_EXPIRE_BARS*2;
  1438. for(int i=1;i<=40&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;
  1439. double uw=High[i]-MathMax(Open[i],Close[i]),lw=MathMin(Open[i],Close[i])-Low[i];double rl=0;bool fd=false;
  1440. if(uw>rg*0.30){rl=High[i];if(MathAbs(cur-rl)<zone*3)fd=true;}
  1441. if(lw>rg*0.30){rl=Low[i];if(MathAbs(cur-rl)<zone*3)fd=true;}
  1442. if(!fd)continue;bool ex=false;
  1443. for(int j=0;j<g_wickLineCount;j++)if(MathAbs(g_wickLinePrice[j]-rl)<zone){g_wickLineTouches[j]++;ex=true;break;}
  1444. if(!ex&&g_wickLineCount<MAX_WICK_LINES){g_wickLinePrice[g_wickLineCount]=rl;g_wickLineTouches[g_wickLineCount]=1;g_wickLineExpire[g_wickLineCount]=expT;g_wickLineCount++;}}
  1445. for(int i=g_wickLineCount-1;i>=0;i--)if(TimeCurrent()>g_wickLineExpire[i]){for(int j=i;j<g_wickLineCount-1;j++){g_wickLinePrice[j]=g_wickLinePrice[j+1];g_wickLineTouches[j]=g_wickLineTouches[j+1];g_wickLineExpire[j]=g_wickLineExpire[j+1];}g_wickLineCount--;}
  1446. }
  1447. void DrawWickRejectLines(){
  1448. for(int i=0;i<MAX_WICK_LINES;i++){SafeDel(PFX+"WICK_"+IntegerToString(i));SafeDel(PFX+"WICK_L_"+IntegerToString(i));}
  1449. if(!SHOW_WICK_REJECT_LINES||!g_isSideways)return;
  1450. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1451. for(int i=0;i<g_wickLineCount;i++){if(TimeCurrent()>g_wickLineExpire[i])continue;if(MathAbs(cur-g_wickLinePrice[i])/pip>20.0)continue;if(IsLineNearby(g_wickLinePrice[i],5.0))continue;
  1452. string id=PFX+"WICK_"+IntegerToString(i),idL=PFX+"WICK_L_"+IntegerToString(i);color lc=C'0,180,255';
  1453. ObjectCreate(0,id,OBJ_HLINE,0,0,g_wickLinePrice[i]);ObjectSetInteger(0,id,OBJPROP_COLOR,lc);ObjectSetInteger(0,id,OBJPROP_WIDTH,2);ObjectSetInteger(0,id,OBJPROP_STYLE,STYLE_DOT);ObjectSetInteger(0,id,OBJPROP_BACK,false);
  1454. string lbl="W"+IntegerToString(g_wickLineTouches[i])+"x";
  1455. ObjectCreate(0,idL,OBJ_TEXT,0,Time[0]+PeriodSeconds(PERIOD_M1)*2,g_wickLinePrice[i]);ObjectSetText(idL,lbl,9,"Arial Bold",lc);ObjectSetInteger(0,idL,OBJPROP_BACK,false);}
  1456. }
  1457. double GetWickRejectSignal(){if(!g_isSideways||g_wickLineCount==0)return 0;double sc=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1458. for(int i=0;i<g_wickLineCount;i++){double d=MathAbs(Close[0]-g_wickLinePrice[i])/pip;bool iS=(g_wickLinePrice[i]<Close[0]);
  1459. if(d<5){if(iS)sc+=15*g_wickLineTouches[i];else sc-=15*g_wickLineTouches[i];}else if(d<10){if(iS)sc+=8*g_wickLineTouches[i];else sc-=8*g_wickLineTouches[i];}}return MathMax(-40,MathMin(40,sc));}
  1460. double GetCommonPointSignal(){if(g_commonCount==0)return 0;double sc=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1461. for(int i=0;i<g_commonCount;i++){if(TimeCurrent()>g_commonExpire[i])continue;double d=MathAbs(Close[0]-g_commonPrice[i])/pip;if(d<8){if(g_commonType[i]=="CALL")sc+=25;else sc-=25;}}return MathMax(-30,MathMin(30,sc));}
  1462. void DetectHiddenLevels(){g_htfLevelCount=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double curP=Close[0];
  1463. double rU=MathCeil(curP/(10*pip))*(10*pip),rD=MathFloor(curP/(10*pip))*(10*pip);
  1464. if(MathAbs(curP-rU)/pip<=15)AddLevel(rU,"ROUND","RESISTANCE",TimeCurrent(),8,true);
  1465. if(MathAbs(curP-rD)/pip<=15)AddLevel(rD,"ROUND","SUPPORT",TimeCurrent(),8,true);
  1466. DetectHTFLevels(PERIOD_M5,"M5",3,pip);DetectHTFLevels(PERIOD_M15,"M15",5,pip);DetectHTFLevels(PERIOD_M30,"M30",7,pip);DetectHTFLevels(PERIOD_H1,"H1",9,pip);
  1467. SortLevelsByStrength();}
  1468. void DetectHTFLevels(ENUM_TIMEFRAMES tf,string tfN,int bS,double pip){
  1469. int bars=MathMin(50,iBars(NULL,tf));if(bars<10)return;double curP=Close[0];
  1470. for(int i=2;i<bars-2;i++){
  1471. if(IsSwingHigh(tf,i,2)){double d=MathAbs(curP-iHigh(NULL,tf,i))/pip;if(d<=20)AddLevel(iHigh(NULL,tf,i),tfN,"RESISTANCE",iTime(NULL,tf,i),bS+(20-(int)d)/2,false);}
  1472. if(IsSwingLow(tf,i,2)){double d=MathAbs(curP-iLow(NULL,tf,i))/pip;if(d<=20)AddLevel(iLow(NULL,tf,i),tfN,"SUPPORT",iTime(NULL,tf,i),bS+(20-(int)d)/2,false);}}}
  1473. bool IsSwingHigh(ENUM_TIMEFRAMES tf,int p,int lb){
  1474. double h=iHigh(NULL,tf,p);
  1475. for(int i=1;i<=lb;i++){if(p-i<0||p+i>=iBars(NULL,tf))return false;if(iHigh(NULL,tf,p-i)>=h||iHigh(NULL,tf,p+i)>=h)return false;}
  1476. return true;}
  1477. bool IsSwingLow(ENUM_TIMEFRAMES tf,int p,int lb){
  1478. double l=iLow(NULL,tf,p);
  1479. for(int i=1;i<=lb;i++){if(p-i<0||p+i>=iBars(NULL,tf))return false;if(iLow(NULL,tf,p-i)<=l||iLow(NULL,tf,p+i)<=l)return false;}
  1480. return true;}
  1481. void AddLevel(double price,string tf,string type,datetime time,int strength,bool isRound){
  1482. if(g_htfLevelCount>=50)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1483. for(int i=0;i<g_htfLevelCount;i++)if(MathAbs(g_htfLevels[i].price-price)/pip<5)return;
  1484. g_htfLevels[g_htfLevelCount].price=price;g_htfLevels[g_htfLevelCount].timeframe=tf;g_htfLevels[g_htfLevelCount].type=type;g_htfLevels[g_htfLevelCount].time=time;g_htfLevels[g_htfLevelCount].strength=strength;g_htfLevels[g_htfLevelCount].isRoundNumber=isRound;g_htfLevelCount++;}
  1485. void SortLevelsByStrength(){
  1486. for(int i=0;i<g_htfLevelCount-1;i++)for(int j=0;j<g_htfLevelCount-i-1;j++)if(g_htfLevels[j].strength<g_htfLevels[j+1].strength){HTFLevel tmp=g_htfLevels[j];g_htfLevels[j]=g_htfLevels[j+1];g_htfLevels[j+1]=tmp;}}
  1487. void DrawHiddenLevels(){
  1488. for(int i=0;i<50;i++){SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i));SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i)+"_LBL");}
  1489. int dc=MathMin(10,g_htfLevelCount);int drawn=0;
  1490. for(int i=0;i<dc&&drawn<5;i++){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;if(MathAbs(Close[0]-g_htfLevels[i].price)/pip>30)continue;if(IsLineNearby(g_htfLevels[i].price,5.0))continue;
  1491. string nm=PFX+"HTF_LEVEL_"+IntegerToString(drawn);color lc=g_htfLevels[i].isRoundNumber?NEON_PURPLE:(StringFind(g_htfLevels[i].type,"SUPPORT")>=0?NEON_GREEN:NEON_RED);
  1492. ObjectCreate(0,nm,OBJ_HLINE,0,0,g_htfLevels[i].price);ObjectSetInteger(0,nm,OBJPROP_COLOR,lc);ObjectSetInteger(0,nm,OBJPROP_WIDTH,1);ObjectSetInteger(0,nm,OBJPROP_STYLE,STYLE_DOT);ObjectSetInteger(0,nm,OBJPROP_BACK,false);
  1493. string ln=nm+"_LBL";ObjectCreate(0,ln,OBJ_TEXT,0,Time[0],g_htfLevels[i].price);ObjectSetText(ln,StringFormat("%s %s S:%d",g_htfLevels[i].timeframe,g_htfLevels[i].type,g_htfLevels[i].strength),9,"Arial",lc);drawn++;}}
  1494. string DetectSuddenReversal(){if(Close[1]<Open[1]&&Close[0]>Open[0]){if(iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,0)<30)return "BULL REVERSAL";}if(Close[1]>Open[1]&&Close[0]<Open[0]){if(iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,0)>70)return "BEAR REVERSAL";}return "";}
  1495. void SafeDel(string id){if(ObjectFind(0,id)>=0)ObjectDelete(0,id);}
  1496. void DrawHLine(string id,double price,color clr,int width,int style){if(price<=0)return;SafeDel(id);ObjectCreate(0,id,OBJ_HLINE,0,0,price);ObjectSetInteger(0,id,OBJPROP_COLOR,clr);ObjectSetInteger(0,id,OBJPROP_WIDTH,width);ObjectSetInteger(0,id,OBJPROP_STYLE,style);ObjectSetInteger(0,id,OBJPROP_BACK,false);}
  1497. void HideSPM(){
  1498. for(int i=ObjectsTotal()-1;i>=0;i--){
  1499. string nm=ObjectName(i);
  1500. if(StringFind(nm,PFX)==0) continue; // Our objects - skip
  1501. if(StringFind(nm,PFX+"candle_timer")>=0) continue; // Timer - keep
  1502. int tp=(int)ObjectGetInteger(0,nm,OBJPROP_TYPE);
  1503. if(tp==OBJ_LABEL||tp==OBJ_RECTANGLE_LABEL||tp==OBJ_TEXT) ObjectDelete(0,nm);
  1504. }
  1505. }
  1506. void NuclearDeleteAll(){
  1507. for(int i=ObjectsTotal()-1;i>=0;i--){
  1508. string nm=ObjectName(i);
  1509. if(StringFind(nm,PFX+"candle_timer")>=0) continue; // Keep timer
  1510. int tp=(int)ObjectGetInteger(0,nm,OBJPROP_TYPE);
  1511. if(tp==OBJ_HLINE||tp==OBJ_LABEL||tp==OBJ_RECTANGLE_LABEL||tp==OBJ_TEXT)
  1512. ObjectDelete(0,nm);
  1513. }
  1514. }
  1515. void DelDashboard(){
  1516. for(int i=ObjectsTotal()-1;i>=0;i--){
  1517. string nm=ObjectName(i);
  1518. if(StringFind(nm,PFX)!=0) continue;
  1519. if(StringFind(nm,"HRN_LINE")>=0) continue;
  1520. if(StringFind(nm,"HRN_LBL")>=0) continue;
  1521. if(StringFind(nm,"candle_timer")>=0) continue;
  1522. if(StringFind(nm,"HTF_LEVEL")>=0) continue;
  1523. if(StringFind(nm,"COMMON_")>=0) continue;
  1524. if(StringFind(nm,"WICK_")>=0) continue;
  1525. if(StringFind(nm,"BB_")>=0) continue;
  1526. ObjectDelete(nm);
  1527. }
  1528. }
  1529. void LoadMLWeights(){
  1530. if(!ML_ADAPTIVE)return;g_symbolKey=Symbol();string fn="ML_weights_"+g_symbolKey+".dat";
  1531. int h=FileOpen(fn,FILE_READ|FILE_TXT);if(h!=INVALID_HANDLE){string d=FileReadString(h);FileClose(h);g_brokerBias=StringToDouble(d);if(g_brokerBias<-0.5)g_brokerBias=-0.5;if(g_brokerBias>0.5)g_brokerBias=0.5;}else g_brokerBias=0.0;}
  1532. void SaveMLWeights(){
  1533. if(!ML_ADAPTIVE)return;string fn="ML_weights_"+g_symbolKey+".dat";
  1534. int h=FileOpen(fn,FILE_WRITE|FILE_TXT);if(h!=INVALID_HANDLE){FileWriteString(h,DoubleToString(g_brokerBias,6));FileClose(h);}}
  1535. void UpdateMLWeights(bool win){
  1536. if(!ML_ADAPTIVE)return;double lr=0.02;if(win)g_brokerBias+=lr*(g_frozen_gProb/100.0);else g_brokerBias-=lr*(g_frozen_rProb/100.0);
  1537. if(g_brokerBias>0.5)g_brokerBias=0.5;if(g_brokerBias<-0.5)g_brokerBias=-0.5;SaveMLWeights();}
  1538. void UpdateAccuracy(){if(Bars<5||Time[0]==g_acc_lastBar)return;g_acc_lastBar=Time[0];
  1539. if(g_lastPredBar==Time[1]&&MathAbs(g_lastPredGreen-50.0)>10.0){bool aG=(Close[1]>Open[1]),pG=(g_lastPredGreen>50.0),win=(aG==pG);
  1540. if(win){g_acc_correct++;g_lossStreak=0;}else g_lossStreak++;g_acc_total++;
  1541. if(g_acc_total>0)g_accuracy=MathMax(40.0,MathMin(90.0,(double)g_acc_correct/g_acc_total*100.0));
  1542. if(ML_ADAPTIVE&&g_lastPredGreen>50.0&&g_lastPredGreen<100.0)UpdateMLWeights(win);}
  1543. g_lastPredGreen=g_frozen_gProb;g_lastPredBar=Time[0];}
  1544. double ValidateHistoricalAccuracy(){if(Bars<55)return 65.0;int cr=0,tt=0;for(int i=10;i<=50&&i+1<Bars;i++){int gc=0;for(int j=i+1;j<=i+5&&j<Bars;j++)if(Close[j]>Open[j])gc++;double pG=(gc>2)?65.0:35.0;bool aG=(Close[i]>Open[i]);if((pG>55&&aG)||(pG<45&&!aG))cr++;tt++;}return tt>0?(double)cr/tt*100.0:65.0;}
  1545. void UpdateWeights(){double lr=0.01;if(g_lastPredBar==Time[1]){bool c=((Close[1]>Open[1])==(g_lastPredGreen>50));if(c){NW[32]+=lr;NW[33]-=lr;}else{NW[32]-=lr;NW[33]+=lr;}NW[32]=MathMax(0.5,MathMin(2.0,NW[32]));NW[33]=MathMin(-0.5,MathMax(-2.0,NW[33]));}}
  1546. string DetectMarketMode(double brain,double rsc,int cp,double nb){double atr=iATR(NULL,PERIOD_M1,14,1),avgR=(High[1]-Low[1]+High[2]-Low[2]+High[3]-Low[3])/3.0;bool hV=(avgR>0&&atr/avgR>1.3);if(MathAbs(brain)>=3.5&&MathAbs(rsc)>=1.5&&hV)return "TREND";if(cp>=70||cp<=30)return "REVERSAL";if(nb>=70||nb<=30)return "TREND";return "RANGE";}
  1547. bool IsSidewaysMarket(){if(Bars<30)return false;double adx=GetADXStrength();if(adx<25)return true;double h20=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,20,1)],l20=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,20,1)];double r20=h20-l20;double atr=iATR(NULL,PERIOD_M1,14,1);if(atr>0&&r20<atr*8)return true;return false;}
  1548. bool IsVolatilityGood(){if(Bars<100)return true;double dh=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,60,0)],dl=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,60,0)];double tr=dh-dl;double ac=0;for(int i=1;i<=20;i++)ac+=(High[i]-Low[i]);ac/=20.0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double mr=jpy?0.30:0.0030;if(tr<mr)return false;if(ac>0&&(High[0]-Low[0])<ac*0.3)return false;return true;}
  1549. int DetectRSIDivergence(){if(Bars<30)return 0;double rN=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),rP=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,8);double lN=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],lPv=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];if(lN<lPv&&rN>rP&&rN<40)return 1;double hN=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],hPv=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];if(hN>hPv&&rN<rP&&rN>60)return -1;return 0;}
  1550. int StreakReversalSignal(){if(Bars<15)return 0;int g=0,r=0;for(int i=1;i<Bars;i++){if(Close[i]>Open[i]){if(r>0)break;g++;}else if(Close[i]<Open[i]){if(g>0)break;r++;}else break;}if(g>=4)return -1;if(r>=4)return 1;return 0;}
  1551. double GetADXStrength(){if(Bars<20)return 0;return iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);}
  1552. int GetADXDirection(){if(Bars<20)return 0;double p=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_PLUSDI,1),m=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MINUSDI,1);if(p>m+5)return 1;if(m>p+5)return -1;return 0;}
  1553. int SmartVolumeSignal(){if(Bars<15)return 0;double v1=(double)iVolume(NULL,PERIOD_M1,1),avg=0;for(int i=2;i<=10;i++)avg+=iVolume(NULL,PERIOD_M1,i);avg/=9.0;if(avg==0)return 0;double vr=v1/avg;if(vr<1.8)return 0;bool g=(Close[1]>Open[1]);if(vr>2.5&&g)return 1;if(vr>2.5&&!g)return -1;if(vr>3.0&&g&&High[1]-Close[1]>Close[1]-Low[1])return -1;if(vr>3.0&&!g&&Close[1]-Low[1]>High[1]-Close[1])return 1;return 0;}
  1554. int DetectClassicPatterns(){if(Bars<30)return 0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double tol=PATTERN_TOLERANCE_PIPS*pip;double sh[2],sl[2];int shB[2],slB[2];int shC=0,slC=0;
  1555. for(int i=2;i<25&&(shC<2||slC<2);i++){if(High[i]>High[i-1]&&High[i]>High[i-2]&&High[i]>High[i+1]&&High[i]>High[i+2]){if(shC<2){sh[shC]=High[i];shB[shC]=i;shC++;}}if(Low[i]<Low[i-1]&&Low[i]<Low[i-2]&&Low[i]<Low[i+1]&&Low[i]<Low[i+2]){if(slC<2){sl[slC]=Low[i];slB[slC]=i;slC++;}}}
  1556. if(shC>=2){int bd=MathAbs(shB[0]-shB[1]);if(bd>=MIN_PATTERN_SEPARATION&&MathAbs(sh[0]-sh[1])<=tol){int bn=MathMin(shB[0],shB[1]),bx=MathMax(shB[0],shB[1]);double vl=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,bx-bn+1,bn)];if(Close[0]<vl)return -1;}}
  1557. if(slC>=2){int bd=MathAbs(slB[0]-slB[1]);if(bd>=MIN_PATTERN_SEPARATION&&MathAbs(sl[0]-sl[1])<=tol){int bn=MathMin(slB[0],slB[1]),bx=MathMax(slB[0],slB[1]);double ph=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,bx-bn+1,bn)];if(Close[0]>ph)return 1;}}return 0;}
  1558. int DetectQMR(){if(Bars<40)return 0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double sH[10],sL[10];int sHB[10],sLB[10];int hC=0,lC=0;
  1559. for(int i=3;i<40&&hC<10;i++)if(High[i]>High[i-1]&&High[i]>High[i-2]&&High[i]>High[i+1]&&High[i]>High[i+2]){sH[hC]=High[i];sHB[hC]=i;hC++;}
  1560. for(int i=3;i<40&&lC<10;i++)if(Low[i]<Low[i-1]&&Low[i]<Low[i-2]&&Low[i]<Low[i+1]&&Low[i]<Low[i+2]){sL[lC]=Low[i];sLB[lC]=i;lC++;}
  1561. if(hC<2||lC<2)return 0;
  1562. for(int iA=0;iA<hC;iA++)for(int iC2=0;iC2<hC;iC2++){if(iA==iC2||sHB[iA]<=sHB[iC2]||sH[iC2]>=sH[iA]-10*pip)continue;
  1563. for(int iB=0;iB<lC;iB++){if(sHB[iA]<=sLB[iB]||sLB[iB]<=sHB[iC2])continue;
  1564. for(int iD=0;iD<lC;iD++){if(iB==iD||sHB[iC2]<=sLB[iD]||sL[iD]>=sL[iB])continue;
  1565. bool m3=false;if(Bars>=8){double o1=Open[3],c1=Close[3],o2=Open[0],c2=Close[0];double b1=MathAbs(c1-o1);if(c2>o2&&c1<o1&&MathAbs(c2-o2)>b1*0.5)m3=true;}
  1566. bool m5=false;if(iBars(NULL,PERIOD_M5)>=3){double o1=iOpen(NULL,PERIOD_M5,2),c1=iClose(NULL,PERIOD_M5,2),o2=iOpen(NULL,PERIOD_M5,1),c2=iClose(NULL,PERIOD_M5,1);double b1=MathAbs(c1-o1);if(c2>o2&&c1<o1&&MathAbs(c2-o2)>b1*0.5)m5=true;}
  1567. if((m3||m5)&&Close[0]>sL[iD]&&Close[0]<sH[iC2]){qmrA=sH[iA];qmrB=sL[iB];qmrC=sH[iC2];qmrD=sL[iD];qmrActive=true;qmrExpire=TimeCurrent()+900;return 1;}}}}
  1568. for(int iA=0;iA<lC;iA++)for(int iC2=0;iC2<lC;iC2++){if(iA==iC2||sLB[iA]<=sLB[iC2]||sL[iC2]<=sL[iA]+10*pip)continue;
  1569. for(int iB=0;iB<hC;iB++){if(sLB[iA]<=sHB[iB]||sHB[iB]<=sLB[iC2])continue;
  1570. for(int iD=0;iD<hC;iD++){if(iB==iD||sLB[iC2]<=sHB[iD]||sH[iD]>=sH[iB])continue;
  1571. bool m3=false;if(Bars>=8){double o1=Open[3],c1=Close[3],o2=Open[0],c2=Close[0];double b1=MathAbs(c1-o1);if(c2<o2&&c1>o1&&MathAbs(c2-o2)>b1*0.5)m3=true;}
  1572. bool m5=false;if(iBars(NULL,PERIOD_M5)>=3){double o1=iOpen(NULL,PERIOD_M5,2),c1=iClose(NULL,PERIOD_M5,2),o2=iOpen(NULL,PERIOD_M5,1),c2=iClose(NULL,PERIOD_M5,1);double b1=MathAbs(c1-o1);if(c2<o2&&c1>o1&&MathAbs(c2-o2)>b1*0.5)m5=true;}
  1573. if((m3||m5)&&Close[0]<sH[iD]&&Close[0]>sL[iC2]){qmrA=sL[iA];qmrB=sH[iB];qmrC=sL[iC2];qmrD=sH[iD];qmrActive=true;qmrExpire=TimeCurrent()+900;return -1;}}}}
  1574. return 0;}
  1575. void AddRNLevel(double &arr[],int &cnt,double lv){if(cnt>=100)return;for(int i=0;i<cnt;i++)if(MathAbs(arr[i]-lv)<0.000001)return;arr[cnt]=lv;cnt++;}
  1576. double ScoreRoundNumber(double level,double pip){
  1577. double score=0,tol=3*pip;int touches=0,ru=0,rd=0;
  1578. for(int i=1;i<=LOOKBACK&&i<Bars;i++){double h=High[i],l=Low[i],c=Close[i],o=Open[i];
  1579. if(h>=level-tol&&l<=level+tol){touches++;
  1580. if(l<=level+tol&&c>level+tol){ru++;double bdy=MathMin(o,c),lw=bdy-l,rng=h-l;if(rng>0&&lw>rng*0.5)score+=5;}
  1581. if(h>=level-tol&&c<level-tol){rd++;double bdyH=MathMax(o,c),uw=h-bdyH,rng=h-l;if(rng>0&&uw>rng*0.5)score+=5;}}}
  1582. score+=touches*3;score+=ru*8+rd*8;
  1583. if(touches>=4)score+=15;else if(touches>=3)score+=10;else if(touches>=2)score+=5;
  1584. return score;}
  1585. double FindBestHiddenRoundNumber(double curPrice){
  1586. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;int dg=(int)MarketInfo(Symbol(),MODE_DIGITS);if(dg<=0)dg=5;
  1587. double levels[];int levelCount=0;ArrayResize(levels,100);
  1588. double b100=MathFloor(curPrice/(100*pip))*(100*pip);for(int i=-1;i<=2;i++)AddRNLevel(levels,levelCount,b100+i*100*pip);
  1589. double b50=MathFloor(curPrice/(50*pip))*(50*pip);for(int i=-2;i<=3;i++)AddRNLevel(levels,levelCount,b50+i*50*pip);
  1590. double b25=MathFloor(curPrice/(25*pip))*(25*pip);for(int i=-3;i<=4;i++)AddRNLevel(levels,levelCount,b25+i*25*pip);
  1591. double b10=MathFloor(curPrice/(10*pip))*(10*pip);for(int i=-5;i<=6;i++)AddRNLevel(levels,levelCount,b10+i*10*pip);
  1592. double b5=MathFloor(curPrice/(5*pip))*(5*pip);for(int i=-4;i<=5;i++)AddRNLevel(levels,levelCount,b5+i*5*pip);
  1593. double bestLevel=0,bestScore=-1;
  1594. for(int i=0;i<levelCount;i++){double lv=levels[i],dist=MathAbs(curPrice-lv)/pip;if(dist>60)continue;double dp=(dist>40)?20:(dist>30)?10:(dist>20)?5:0;double sc=ScoreRoundNumber(lv,pip)-dp;if(sc<5)continue;if(sc>bestScore){bestScore=sc;bestLevel=lv;}}
  1595. if(bestLevel==0){double n100=MathRound(curPrice/(100*pip))*(100*pip),n50=MathRound(curPrice/(50*pip))*(50*pip),n25=MathRound(curPrice/(25*pip))*(25*pip),n10=MathRound(curPrice/(10*pip))*(10*pip),n5=MathRound(curPrice/(5*pip))*(5*pip);
  1596. double d100=MathAbs(curPrice-n100),d50=MathAbs(curPrice-n50),d25=MathAbs(curPrice-n25),d10=MathAbs(curPrice-n10);
  1597. if(d100<30*pip)bestLevel=n100;else if(d50<20*pip)bestLevel=n50;else if(d25<15*pip)bestLevel=n25;else if(d10<10*pip)bestLevel=n10;else bestLevel=n5;}
  1598. g_hrn_score=bestScore;return NormalizeDouble(bestLevel,dg);}
  1599. void ScanHRNLevels(){g_rj_cnt=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1600. for(int i=1;i<=LOOKBACK&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;double uw=High[i]-MathMax(Open[i],Close[i]),lw=MathMin(Open[i],Close[i])-Low[i];
  1601. if(uw>rg*0.35){double lv=High[i];if(MathAbs(lv-cur)<=40*pip){bool f=false;for(int k=0;k<g_rj_cnt;k++)if(MathAbs(g_rj[k].price-lv)<8*pip){g_rj[k].touches++;f=true;break;}if(!f&&g_rj_cnt<MAX_REJ){g_rj[g_rj_cnt].price=lv;g_rj[g_rj_cnt].touches=1;g_rj_cnt++;}}}
  1602. if(lw>rg*0.35){double lv=Low[i];if(MathAbs(lv-cur)<=40*pip){bool f=false;for(int k=0;k<g_rj_cnt;k++)if(MathAbs(g_rj[k].price-lv)<8*pip){g_rj[k].touches++;f=true;break;}if(!f&&g_rj_cnt<MAX_REJ){g_rj[g_rj_cnt].price=lv;g_rj[g_rj_cnt].touches=1;g_rj_cnt++;}}}}
  1603. for(int i=0;i<g_rj_cnt-1;i++)for(int j=0;j<g_rj_cnt-i-1;j++)if(g_rj[j].touches<g_rj[j+1].touches){RejLevel tmp=g_rj[j];g_rj[j]=g_rj[j+1];g_rj[j+1]=tmp;}}
  1604. void UpdateHRN(){
  1605. if(Bars<LOOKBACK+10)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;int dg=(int)MarketInfo(Symbol(),MODE_DIGITS);if(dg<=0)dg=5;
  1606. if(Time[0]!=g_hrn_scan_bar){double nH=FindBestHiddenRoundNumber(Close[0]);if(g_hrn_price==0||MathAbs(nH-g_hrn_price)>2*pip){g_hrn_price=nH;g_hrn_is_sup=(Close[0]>g_hrn_price);g_hrn_brk_bars=0;g_hrn_confirmed_break=false;}g_hrn_scan_bar=Time[0];}
  1607. double tol=1*pip;
  1608. if(g_hrn_is_sup){if(Bars>=3){bool c1b=(Close[1]<g_hrn_price-tol),c2b=(Close[2]<g_hrn_price-tol);if(c1b&&c2b){g_hrn_confirmed_break=true;g_hrn_brk_bars=HRN_CONFIRM_BARS;}else if(c1b)g_hrn_brk_bars=1;else g_hrn_brk_bars=0;}}
  1609. else{if(Bars>=3){bool c1a=(Close[1]>g_hrn_price+tol),c2a=(Close[2]>g_hrn_price+tol);if(c1a&&c2a){g_hrn_confirmed_break=true;g_hrn_brk_bars=HRN_CONFIRM_BARS;}else if(c1a)g_hrn_brk_bars=1;else g_hrn_brk_bars=0;}}
  1610. if(g_hrn_brk_bars>=HRN_CONFIRM_BARS){double nH=FindBestHiddenRoundNumber(Close[0]);if(MathAbs(nH-g_hrn_price)>3*pip){g_hrn_price=nH;g_hrn_is_sup=(Close[0]>g_hrn_price);g_hrn_brk_bars=0;g_hrn_confirmed_break=false;}else{g_hrn_is_sup=(Close[0]>g_hrn_price);g_hrn_brk_bars=0;g_hrn_confirmed_break=false;}}
  1611. g_hrn_str=DoubleToString(NormalizeDouble(g_hrn_price,dg),dg);
  1612. color hrnColor=g_hrn_is_sup?NEON_GREEN:NEON_RED;if(g_hrn_brk_bars==1)hrnColor=NEON_YELLOW;
  1613. DrawHLine(PFX+"HRN_LINE",g_hrn_price,hrnColor,3,STYLE_SOLID);
  1614. string ln=PFX+"HRN_LBL";SafeDel(ln);string typeStr=g_hrn_is_sup?"SUP":"RES";string confStr=(g_hrn_brk_bars==1)?" [1/2]":(g_hrn_confirmed_break?" [SHIFTING]":"");
  1615. ObjectCreate(0,ln,OBJ_TEXT,0,Time[0]+Period()*60*2,g_hrn_price);ObjectSetText(ln,"HRN "+typeStr+" "+g_hrn_str+confStr,10,"Arial Bold",hrnColor);ObjectSetInteger(0,ln,OBJPROP_BACK,false);}
  1616. void AddRes(double lv){if(lv<=0)return;for(int i=0;i<resCount;i++)if(MathAbs(ResLevels[i]-lv)<10*Point)return;if(resCount>=MAX_LEVELS){for(int i=0;i<MAX_LEVELS-1;i++)ResLevels[i]=ResLevels[i+1];resCount=MAX_LEVELS-1;}ResLevels[resCount++]=lv;}
  1617. void AddSup(double lv){if(lv<=0)return;for(int i=0;i<supCount;i++)if(MathAbs(SupLevels[i]-lv)<10*Point)return;if(supCount>=MAX_LEVELS){for(int i=0;i<MAX_LEVELS-1;i++)SupLevels[i]=SupLevels[i+1];supCount=MAX_LEVELS-1;}SupLevels[supCount++]=lv;}
  1618. void DrawNearestSR(){
  1619. // Remove ALL old SR lines from chart — NO shifting lines
  1620. for(int i=0;i<20;i++){SafeDel(PFX+"RES_"+IntegerToString(i));SafeDel(PFX+"SUP_"+IntegerToString(i));}
  1621. SafeDel(PFX+"NEAREST_RES");
  1622. SafeDel(PFX+"NEAREST_SUP");
  1623. // Only calculate values for dashboard — NO chart lines drawn
  1624. double cur=Close[0]; g_nearest_res=0; g_nearest_sup=0;
  1625. double mR=999999, mS=999999;
  1626. for(int i=0;i<resCount;i++)
  1627. if(ResLevels[i]>cur && (ResLevels[i]-cur)<mR){ mR=ResLevels[i]-cur; g_nearest_res=ResLevels[i]; }
  1628. for(int i=0;i<supCount;i++)
  1629. if(SupLevels[i]<cur && (cur-SupLevels[i])<mS){ mS=cur-SupLevels[i]; g_nearest_sup=SupLevels[i]; }
  1630. // Values used only in dashboard S/R cube — chart pe koi line nahi
  1631. }
  1632. void CheckBRK(){double pC=Close[1],pO=Open[1];g_brk_str="NO BRK";g_brk_color=CGR;if(g_nearest_res>0&&pC>g_nearest_res&&pO<=g_nearest_res){g_brk_str="BRK UP!";g_brk_color=NEON_GREEN;}else if(g_nearest_sup>0&&pC<g_nearest_sup&&pO>=g_nearest_sup){g_brk_str="BRK DOWN!";g_brk_color=NEON_RED;}}
  1633. void UpdateSR(){
  1634. // Only run once per new candle
  1635. if(Time[0]==lastSRBar) return;
  1636. lastSRBar=Time[0];
  1637. if(Bars<15) return;
  1638.  
  1639. bool jpy=(StringFind(Symbol(),"JPY")>=0);
  1640. double pip=jpy?0.01:0.0001;
  1641. double cur=Close[0];
  1642.  
  1643. // Find swing highs/lows from last 10 bars as potential S/R
  1644. // Use stronger candles (body > 40% of range)
  1645. for(int i=2;i<=10;i++){
  1646. double rng=High[i]-Low[i];
  1647. double bd=MathAbs(Close[i]-Open[i]);
  1648. if(rng<=0) continue;
  1649. // Swing high candidate: higher than neighbors
  1650. if(High[i]>High[i-1]&&High[i]>High[i+1]&&High[i]>cur+5*pip){
  1651. AddRes(High[i]);
  1652. }
  1653. // Swing low candidate
  1654. if(Low[i]<Low[i-1]&&Low[i]<Low[i+1]&&Low[i]<cur-5*pip){
  1655. AddSup(Low[i]);
  1656. }
  1657. }
  1658.  
  1659. // If arrays empty, add basic levels
  1660. if(resCount<1){double nR=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,10,1)];if(nR>cur)AddRes(nR);}
  1661. if(supCount<1){double nS=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,10,1)];if(nS<cur)AddSup(nS);}
  1662.  
  1663. DrawNearestSR();
  1664. CheckBRK();
  1665. }
  1666. void UpdateTimer(){
  1667. if(!SHOW_TIMER) return;
  1668. string nm = PFX+"candle_timer";
  1669. int ps = Period() * 60;
  1670. int el = (int)(TimeCurrent() - Time[0]);
  1671. int rem = ps - el;
  1672. if(rem <= 0) rem = ps;
  1673. if(rem > ps) rem = ps;
  1674. int mm = rem / 60, ss = rem % 60;
  1675. string t = (mm > 0 ? IntegerToString(mm) + ":" : "") +
  1676. (ss < 10 ? "0" : "") + IntegerToString(ss) + "s";
  1677.  
  1678. // Chart pe candle ke right mein 8px offset
  1679. // Time = current bar close time (Time[0] + period)
  1680. // Price = High[0] ka thoda upar
  1681. double atrVal = iATR(NULL, PERIOD_M1, 14, 0);
  1682. double offset = (atrVal > 0) ? atrVal * 0.25 : Point * 30;
  1683.  
  1684. // Candle ke TIME ke thoda baad place karo = right side mein dikhega
  1685. datetime tPos = Time[0] + 8; // 8 seconds = candle ke right mein
  1686. double pPos = High[0] + offset;
  1687.  
  1688. if(ObjectFind(0, nm) < 0){
  1689. ObjectCreate(0, nm, OBJ_TEXT, 0, tPos, pPos);
  1690. ObjectSetInteger(0, nm, OBJPROP_BACK, false);
  1691. ObjectSetInteger(0, nm, OBJPROP_SELECTABLE, false);
  1692. ObjectSetInteger(0, nm, OBJPROP_HIDDEN, false);
  1693. }
  1694. ObjectSetInteger(0, nm, OBJPROP_TIME, tPos);
  1695. ObjectSetDouble (0, nm, OBJPROP_PRICE, pPos);
  1696. ObjectSetText(nm, t, 11, "Arial Bold", NEON_YELLOW);
  1697. }
  1698. void InitNW(){NW[0]=0.852;NW[1]=0.724;NW[2]=0.913;NW[3]=0.681;NW[4]=0.795;NW[5]=0.836;NW[6]=0.772;NW[7]=0.894;NW[8]=0.623;NW[9]=0.748;NW[10]=0.887;NW[11]=0.716;NW[12]=0.661;NW[13]=0.829;NW[14]=0.753;NW[15]=0.942;NW[16]=0.784;NW[17]=0.697;NW[18]=0.871;NW[19]=0.735;NW[20]=0.813;NW[21]=0.768;NW[22]=0.932;NW[23]=0.674;NW[24]=0.845;NW[25]=0.709;NW[26]=0.888;NW[27]=0.651;NW[28]=0.921;NW[29]=0.743;NW[30]=0.802;NW[31]=0.867;NW[32]=1.24;NW[33]=-0.93;NW[34]=1.15;NW[35]=-1.08;}
  1699. void Bx(string id,int x,int y,int w,int h,color bg,color bd){string n=PFX+id;if(ObjectFind(0,n)<0)ObjectCreate(0,n,OBJ_RECTANGLE_LABEL,0,0,0);ObjectSetInteger(0,n,OBJPROP_XDISTANCE,x);ObjectSetInteger(0,n,OBJPROP_YDISTANCE,y);ObjectSetInteger(0,n,OBJPROP_XSIZE,w);ObjectSetInteger(0,n,OBJPROP_YSIZE,h);ObjectSetInteger(0,n,OBJPROP_BGCOLOR,bg);ObjectSetInteger(0,n,OBJPROP_BORDER_TYPE,BORDER_FLAT);ObjectSetInteger(0,n,OBJPROP_COLOR,bd);ObjectSetInteger(0,n,OBJPROP_BACK,false);}
  1700. void Tx(string id,int x,int y,string txt,color tc,int fs,bool bold){string n=PFX+id;if(ObjectFind(0,n)<0)ObjectCreate(0,n,OBJ_LABEL,0,0,0);ObjectSetInteger(0,n,OBJPROP_XDISTANCE,x);ObjectSetInteger(0,n,OBJPROP_YDISTANCE,y);ObjectSetText(n,txt,fs,bold?"Arial Bold":"Arial",tc);ObjectSetInteger(0,n,OBJPROP_BACK,false);}
  1701. void NeonBar(string id,int x,int y,int w,int h,double pct,color fc){Bx(id+"_bg",x,y,w,h,BG_DARK4,BG_DARK4);int fw=(int)(pct/100.0*w);if(fw<4)fw=4;if(fw>w)fw=w;Bx(id+"_f",x,y,fw,h,fc,fc);}
  1702. void Cube(string id,int x,int y,int w,int h,color bc,color bg,string lbl,color lc,string val,color vc,int vfs){Bx(id,x,y,w,h,bg,bc);if(StringLen(lbl)>0)Tx(id+"_l",x+10,y+6,lbl,lc,11,true);if(StringLen(val)>0)Tx(id+"_v",x+10,y+38,val,vc,vfs,true);}
  1703. void DrawOtherPairsCube(int x,int y,int w,int h){
  1704. Bx("c20",x,y,w,h,BG_DARK2,NEON_GOLD);
  1705. Tx("c20_l",x+8,y+4,"OTHER PAIRS",NEON_CYAN,10,true);
  1706. if(g_pairCount==0){
  1707. Tx("c20_v0",x+8,y+30,"No signals yet",CGR,10,false);
  1708. Tx("c20_v1",x+8,y+52,"(need 2+ instances)",C'100,100,100',9,false);
  1709. return;
  1710. }
  1711. // NO bars at all — only text lines, 20px per entry, no overlap possible
  1712. int lY=24;
  1713. for(int i=0;i<g_pairCount&&i<5;i++){
  1714. if(lY+16 > h-4) break;
  1715. string sp=g_pairNames[i];
  1716. if(StringLen(sp)>9) sp=StringSubstr(sp,0,9);
  1717. int qScore=(int)g_pairConfs[i];
  1718. string qLabel;
  1719. if(qScore>=80) qLabel="[S]";
  1720. else if(qScore>=65) qLabel="[G]";
  1721. else if(qScore>=50) qLabel="[M]";
  1722. else qLabel="[W]";
  1723.  
  1724. // Check if NCP signal matches this pair's signal
  1725. bool ncpMatch = (ENABLE_MTG &&
  1726. (g_mtgState=="CALL"||g_mtgState=="PUT"||
  1727. g_mtgState=="CALL TRAP"||g_mtgState=="PUT TRAP") &&
  1728. g_pairSigs[i]==StringSubstr(g_mtgState,0,4)); // "CALL" or "PUT_"
  1729. // More precise match check
  1730. bool isCallSig = (g_mtgState=="CALL"||g_mtgState=="CALL TRAP");
  1731. bool isPutSig = (g_mtgState=="PUT" ||g_mtgState=="PUT TRAP");
  1732. ncpMatch = ((isCallSig && g_pairSigs[i]=="CALL") ||
  1733. (isPutSig && g_pairSigs[i]=="PUT"));
  1734.  
  1735. // Color: purple if NCP matches, else normal green/red
  1736. color dc;
  1737. string prefix="";
  1738. if(ncpMatch){
  1739. dc = NEON_PURPLE; // NCP condition matched = purple
  1740. prefix = "* "; // Star marker
  1741. } else {
  1742. dc = (g_pairSigs[i]=="CALL") ? NEON_GREEN : NEON_RED;
  1743. }
  1744. string txt=prefix+sp+" "+g_pairSigs[i]+" "+IntegerToString(qScore)+qLabel;
  1745. Tx("c20_p"+IntegerToString(i),x+8,y+lY,txt,dc,10,true);
  1746. lY+=20;
  1747. }
  1748. }
  1749. void DrawOTCQuickCube(int x,int y,int w,int h){
  1750. Bx("c_quick",x,y,w,h,BG_DARK2,NEON_LIME);Tx("c_quick_l",x+8,y+2,"OTC QUICK",NEON_CYAN,10,true);
  1751. int age=(int)(TimeCurrent()-Time[0]);int ps=Period()*60;int rem=ps-age;if(rem<0)rem=0;
  1752. int mm=rem/60,ss2=rem%60;string tl=(mm>0?IntegerToString(mm)+"m ":"")+IntegerToString(ss2)+"s";
  1753. Tx("c_quick_t",x+8,y+20,"Time: "+tl,NEON_GOLD,11,true);
  1754. double spread=MarketInfo(Symbol(),MODE_SPREAD);color spC=(spread<=BROKER_SPREAD_THRESHOLD)?NEON_GREEN:NEON_ORANGE;
  1755. Tx("c_quick_s",x+8,y+38,"Spread: "+DoubleToString(spread,0),spC,10,true);
  1756. // VOLUME FIX: Bar[1]=last closed candle vs Bar[2]=candle before it (1 candle comparison only)
  1757. double volLast = (double)iVolume(NULL,PERIOD_M1,1); // Last fully closed candle
  1758. double volPrev = (double)iVolume(NULL,PERIOD_M1,2); // Candle just before last
  1759. double volRatio = (volPrev > 0) ? volLast / volPrev : 1.0;
  1760. bool candleBull = (Close[1] > Open[1]);
  1761. bool candleBear = (Close[1] < Open[1]);
  1762. color vColor; string vText;
  1763. if(candleBull){
  1764. if(volRatio>=2.0){vColor=NEON_GREEN;vText="UP "+DoubleToString(volRatio,1)+"x HI";}
  1765. else if(volRatio>=1.3){vColor=NEON_LIME;vText="UP "+DoubleToString(volRatio,1)+"x MD";}
  1766. else if(volRatio>=0.8){vColor=NEON_CYAN;vText="UP "+DoubleToString(volRatio,1)+"x LO";}
  1767. else{vColor=NEON_YELLOW;vText="UP "+DoubleToString(volRatio,1)+"x WK";}
  1768. } else if(candleBear){
  1769. if(volRatio>=2.0){vColor=NEON_RED;vText="DN "+DoubleToString(volRatio,1)+"x HI";}
  1770. else if(volRatio>=1.3){vColor=NEON_ORANGE;vText="DN "+DoubleToString(volRatio,1)+"x MD";}
  1771. else if(volRatio>=0.8){vColor=NEON_PINK;vText="DN "+DoubleToString(volRatio,1)+"x LO";}
  1772. else{vColor=NEON_YELLOW;vText="DN "+DoubleToString(volRatio,1)+"x WK";}
  1773. } else{vColor=NEON_YELLOW;vText="DOJI "+DoubleToString(volRatio,1)+"x";}
  1774. Tx("c_quick_v",x+8,y+56,vText,vColor,11,true);
  1775. string es=GetEntryStatus();color eC=GetEntryColor();Tx("c_quick_e",x+8,y+76,es,eC,10,true);}
  1776. string GetEntryStatus(){
  1777. int age=(int)(TimeCurrent()-Time[0]);int ps=Period()*60;int rem=ps-age;if(rem<0)rem=0;string ms=DetectMyStrategy();
  1778. if(ms!="WAIT"){string type=(g_strategyType=="TRAP")?"TRAP":"TREND";
  1779. if(ENTRY_MODE=="FLAG"){if(age<FLAG_MIN_SEC)return type+" WAIT "+IntegerToString(FLAG_MIN_SEC-age)+"s";if(rem<MIN_REMAINING_SEC)return type+" EXPIRED";if(age>FLAG_MAX_SEC)return type+" LATE";return type+" OPEN "+IntegerToString(rem)+"s";}
  1780. if(age<TIME_MIN_SEC)return type+" WAIT "+IntegerToString(TIME_MIN_SEC-age)+"s";if(age>TIME_MAX_SEC)return type+" CLOSED";return type+" OPEN "+IntegerToString(age)+"s";}
  1781. if(ENTRY_MODE=="FLAG"){if(age<FLAG_MIN_SEC)return "WAIT "+IntegerToString(FLAG_MIN_SEC-age)+"s";if(rem<MIN_REMAINING_SEC)return "EXPIRED";if(age>FLAG_MAX_SEC)return "LATE";return "OPEN "+IntegerToString(rem)+"s left";}
  1782. if(age<TIME_MIN_SEC)return "WAIT "+IntegerToString(TIME_MIN_SEC-age)+"s";if(age>TIME_MAX_SEC)return "CLOSED";return "OPEN "+IntegerToString(age)+"s";}
  1783. color GetEntryColor(){string s=GetEntryStatus();if(StringFind(s,"TRAP OPEN")>=0)return NEON_PURPLE;if(StringFind(s,"TREND OPEN")>=0)return NEON_LIME;if(StringFind(s,"OPEN")>=0)return NEON_GREEN;if(StringFind(s,"WAIT")>=0)return NEON_YELLOW;if(StringFind(s,"LATE")>=0||StringFind(s,"EXPIRED")>=0||StringFind(s,"CLOSED")>=0)return NEON_ORANGE;return NEON_RED;}
  1784. bool DirectionOK(){if(!ENABLE_ENTRY_ZONE)return true;double cur=Close[0],lo=Low[0],hi=High[0],rg=hi-lo;if(rg==0)return true;double pos=(cur-lo)/rg*100.0;if(MathMax(g_calc_gProb,g_calc_rProb)>=85)return true;string ms=DetectMyStrategy();if(ms=="CALL")return(pos>REVERSAL_PCT);if(ms=="PUT")return(pos<(100.0-REVERSAL_PCT));if(StringFind(g_adv1,"CALL")>=0)return(pos>REVERSAL_PCT&&(hi-cur)<rg*WICK_PCT);if(StringFind(g_adv1,"PUT")>=0)return(pos<(100.0-REVERSAL_PCT)&&(cur-lo)<rg*WICK_PCT);return true;}
  1785. bool EntryPrecisionOK(){if(TRADE_MODE=="GOD")return true;if(g_calc_gProb>80&&Close[0]<Open[0])return false;if(g_calc_rProb>80&&Close[0]>Open[0])return false;return true;}
  1786. bool ConsensusOK(){if(!CONSENSUS_FILTER)return true;string ms=DetectMyStrategy();if(ms=="WAIT")return true;bool haAgree=false;if(ms=="CALL"&&(g_haM1=="HA BULLISH 1"||g_haBoth=="BOTH BULL"))haAgree=true;if(ms=="PUT"&&(g_haM1=="HA BEARISH 1"||g_haBoth=="BOTH BEAR"))haAgree=true;return haAgree;}
  1787. void SendTelegramAlert(string message){
  1788. if(!ENABLE_TELEGRAM)return;if(StringLen(TELEGRAM_TOKEN)<10||StringLen(TELEGRAM_CHAT_ID)<5)return;
  1789. char post[];char result_data[];string result_headers;int timeout=5000;
  1790. int res=WebRequest("GET",url,"",timeout,post,result_data,result_headers);
  1791. if(res==-1)Print("Telegram failed: ",GetLastError());}
  1792.  
  1793. //+------------------------------------------------------------------+
  1794. // MAIN DRAW FUNCTION
  1795. //+------------------------------------------------------------------+
  1796. void Draw(){
  1797. HideSPM(); DelDashboard();
  1798. int W=DASH_W,G=8,CW=(W-2*G)/3,CH=110,cx=POS_X+G,yy=POS_Y;
  1799. int cp=0;double brain=BrainSc(cp);double nb=NeuralBiasFast();double mw=MicroWick();bool bf=IsBrokerForce();
  1800. g_marketMode=DetectMarketMode(brain,g_rsc,cp,nb);
  1801. if(StringLen(g_prevMode)>0&&g_prevMode!=g_marketMode){g_signalTime=0;g_finalSignal="WAIT";}
  1802. g_prevMode=g_marketMode;
  1803. CalcOTCCrowd();UpdateAccuracy();UpdateWeights();
  1804. double mai=CalcAdvancedMicroAI();int mts=CalcAdvancedMicroTrap();
  1805. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  1806. UpdateRiskControl();CheckMTFConfirmation();
  1807. double tfaScore=CalcAdvancedTFA(g_tfa_detail);
  1808. color ha1C=(g_haM1=="HA BULLISH 1")?NEON_GREEN:(g_haM1=="HA BEARISH 1")?NEON_RED:NEON_YELLOW;
  1809. color ha5C=(g_haM5=="HA BULLISH 5")?NEON_GREEN:(g_haM5=="HA BEARISH 5")?NEON_RED:NEON_YELLOW;
  1810. color biasClr;string biasStr=CalcAdvancedMarketBias(biasClr,brain,g_rsc);
  1811. // Strategy cube: g_strategyType already set in CalcPrediction() via DetectMyStrategy()
  1812. // Re-call se side-effects avoid karo — cached values use karo
  1813. string strategyMS = g_finalSignal; // Use already computed final signal direction
  1814. // But for strategy label, use g_strategyType and g_strategyReason directly
  1815. color stratC = (g_strategyType=="TRAP") ? NEON_PURPLE : (g_strategyType=="TREND") ? NEON_GREEN : CGR;
  1816. string stratSig = "";
  1817. if(g_strategyType=="TRAP" || g_strategyType=="TREND"){
  1818. // g_strategyReason has: "CALL xx% + HA GREEN" or "PUT xx% + HA RED"
  1819. if(StringFind(g_strategyReason,"CALL")>=0) stratSig = "CALL";
  1820. else if(StringFind(g_strategyReason,"PUT")>=0) stratSig = "PUT";
  1821. }
  1822. string stratLabel = (g_strategyType=="TRAP") ? "TRAP " + stratSig :
  1823. (g_strategyType=="TREND") ? "TREND " + stratSig : "NO STRATEGY";
  1824.  
  1825. // ============================================================
  1826. // SIGNAL CUBE - BB PULLBACK INTEGRATED - FIXED COLORS
  1827. // ============================================================
  1828. string finalSig = g_finalSignal;
  1829. color sigC = NEON_YELLOW; // Default = YELLOW (WAIT)
  1830.  
  1831. // BB signal integration: agar regular WAIT hai aur BB signal active hai
  1832. if(ENABLE_BB_PULLBACK && g_bbSignal != "--" && (finalSig == "WAIT" || finalSig == "")){
  1833. finalSig = "BB " + g_bbSignal;
  1834. sigC = g_bbColor; // GREEN for CALL, RED for PUT, YELLOW for no signal
  1835. } else {
  1836. if(finalSig=="CALL") sigC = NEON_GREEN;
  1837. else if(finalSig=="PUT") sigC = NEON_RED;
  1838. else if(finalSig=="WEAK CALL") sigC = NEON_CYAN;
  1839. else if(finalSig=="WEAK PUT") sigC = NEON_ORANGE;
  1840. else sigC = NEON_YELLOW; // WAIT = always YELLOW
  1841. }
  1842.  
  1843. // ROW 1 - PAIR, SIGNAL, NEXT CANDLE (CH=110)
  1844. Cube("c1",cx,yy,CW,CH,NEON_CYAN,BG_DARK2,"PAIR",NEON_CYAN,Symbol(),NEON_BLUE,13);
  1845. Bx("c2",cx+CW+G,yy,CW,CH,BG_DARK2,sigC);
  1846. Tx("c2_l",cx+CW+G+10,yy+4,"SIGNAL",NEON_CYAN,10,true);
  1847. Tx("c2_v",cx+CW+G+10,yy+26,finalSig,sigC,13,true);
  1848. if(ENABLE_BB_PULLBACK){
  1849. string ha30Txt=Is30SecHABull()?"HA30:BULL":Is30SecHABear()?"HA30:BEAR":"HA30:DOJI";
  1850. color ha30C=Is30SecHABull()?NEON_GREEN:Is30SecHABear()?NEON_RED:NEON_YELLOW;
  1851. Tx("c2_ha30",cx+CW+G+10,yy+60,ha30Txt,ha30C,9,false);
  1852. if(g_bbSignal!="--")Tx("c2_bb",cx+CW+G+10,yy+78,g_bbDetail,g_bbColor,9,false);
  1853. }
  1854. double gP,rP;string rsn;color pClr;
  1855. CalcAdvancedPrediction(gP,rP,rsn,pClr);
  1856. g_frozen_gProb=gP;g_frozen_rProb=rP;g_frozen_pAction=rsn;g_frozen_pColor=pClr;g_last_freeze_bar=Time[0];
  1857. Bx("c_pred",cx+2*(CW+G),yy,CW,CH,BG_DARK2,pClr);
  1858. Tx("c_pred_l",cx+2*(CW+G)+10,yy+4,"NEXT CANDLE",NEON_CYAN,10,true);
  1859. int bW=CW-20,gBW=(int)(gP/100.0*bW),rBW=(int)(rP/100.0*bW);
  1860. if(gBW<3)gBW=3;if(rBW<3)rBW=3;
  1861. Bx("c_pred_gbg",cx+2*(CW+G)+10,yy+22,bW,7,BG_DARK4,BG_DARK4);
  1862. Bx("c_pred_gfg",cx+2*(CW+G)+10,yy+22,gBW,7,NEON_GREEN,NEON_GREEN);
  1863. Tx("c_pred_gp",cx+2*(CW+G)+10,yy+31,"G:"+DoubleToString(gP,0)+"%",NEON_GREEN,11,true);
  1864. Bx("c_pred_rbg",cx+2*(CW+G)+10,yy+48,bW,7,BG_DARK4,BG_DARK4);
  1865. Bx("c_pred_rfg",cx+2*(CW+G)+10,yy+48,rBW,7,NEON_RED,NEON_RED);
  1866. Tx("c_pred_rp",cx+2*(CW+G)+10,yy+57,"R:"+DoubleToString(rP,0)+"%",NEON_RED,11,true);
  1867. Tx("c_pred_a",cx+2*(CW+G)+10,yy+76,rsn,pClr,11,true);
  1868. NeonBar("c_pred_b",cx+2*(CW+G)+10,yy+92,bW,7,MathMax(gP,rP),pClr);
  1869. yy+=CH+G;
  1870.  
  1871. // ROW 2 - NEURAL AI, CROWD, SESSION (CH=110)
  1872. Cube("c7",cx,yy,CW,CH,NEON_CYAN,BG_DARK2,"NEURAL AI",NEON_CYAN,DoubleToString(nb,1)+"%",(nb>72)?NEON_ORANGE:(nb<35)?NEON_CYAN:NEON_GREEN,16);
  1873. NeonBar("c7b",cx+12,yy+92,CW-24,7,nb,(nb>72)?NEON_ORANGE:(nb<35)?NEON_CYAN:NEON_GREEN);
  1874. Bx("c8",cx+CW+G,yy,CW,CH,BG_DARK2,NEON_CYAN);
  1875. Tx("c8_l",cx+CW+G+10,yy+4,"CROWD",NEON_CYAN,10,true);
  1876. Tx("c8_c",cx+CW+G+10,yy+24,"CALL "+IntegerToString(g_otcCallPct)+"%",NEON_RED,14,true);
  1877. Tx("c8_p",cx+CW+G+10,yy+54,"PUT "+IntegerToString(g_otcPutPct)+"%",NEON_GREEN,14,true);
  1878. Tx("c8_b",cx+CW+G+10,yy+86,"OTC Crowd",NEON_GOLD,9,false);
  1879. color sClr=CGR;string sName=GetCurrentSession(sClr);bool isAct=IsSessionActive();
  1880. Bx("c_ses",cx+2*(CW+G),yy,CW,CH,BG_DARK2,sClr);
  1881. Tx("c_ses_l",cx+2*(CW+G)+10,yy+4,"SESSION",NEON_CYAN,10,true);
  1882. Tx("c_ses_v",cx+2*(CW+G)+10,yy+22,sName,sClr,14,true);
  1883. int gH=TimeHour(TimeGMT()),gMn=TimeMinute(TimeGMT());
  1884. Tx("c_ses_t",cx+2*(CW+G)+10,yy+54,"GMT "+(gH<10?"0":"")+IntegerToString(gH)+":"+(gMn<10?"0":"")+IntegerToString(gMn),NEON_WHITE,11,false);
  1885. Tx("c_ses_s",cx+2*(CW+G)+10,yy+76,isAct?"ACTIVE":"LOW VOL",isAct?NEON_GREEN:NEON_ORANGE,11,true);
  1886. yy+=CH+G;
  1887.  
  1888. // ROW 3 - OTHER PAIRS, OTC QUICK, BRAIN (Strategy hata diya)
  1889. DrawOtherPairsCube(cx,yy,CW,CH);
  1890. DrawOTCQuickCube(cx+CW+G,yy,CW,CH);
  1891. // BRAIN cube yahan (Row 3, position 3)
  1892. Bx("c_brain_r3",cx+2*(CW+G),yy,CW,CH,BG_DARK3,NEON_CYAN);
  1893. Tx("c_brain_r3_l",cx+2*(CW+G)+10,yy+5,"BRAIN",NEON_CYAN,10,true);
  1894. color brainClr3=(brain>0)?NEON_GREEN:NEON_RED;
  1895. Tx("c_brain_r3_v",cx+2*(CW+G)+10,yy+28,(brain>=0?"+":"")+DoubleToString(brain,1),brainClr3,22,true);
  1896. // Strategy info as small text below brain value
  1897. Tx("c_brain_r3_s",cx+2*(CW+G)+10,yy+72,stratLabel,stratC,10,false);
  1898. NeonBar("c_brain_r3_b",cx+2*(CW+G)+12,yy+92,CW-24,7,MathMin(100,MathAbs(brain)*8+50),(brain>0)?NEON_GREEN:NEON_RED);
  1899. yy+=CH+G;
  1900.  
  1901. // ROW 4 - MICRO AI, MICRO TRAP, TFA (CH4=120 - taller)
  1902. int CH4=120;
  1903. color maC;
  1904. if(mai>72)maC=NEON_ORANGE;else if(mai>55)maC=NEON_YELLOW;else if(mai>45)maC=NEON_CYAN;else maC=NEON_GREEN;
  1905. Cube("c_mai",cx,yy,CW,CH4,maC,BG_DARK2,"MICRO AI",NEON_CYAN,DoubleToString(mai,0)+"%",maC,16);
  1906. NeonBar("c_mai_b",cx+12,yy+104,CW-24,7,mai,maC);
  1907. color mtsC=(mts>=90)?NEON_PURPLE:(mts>=75)?NEON_RED:(mts>=50)?NEON_ORANGE:NEON_GREEN;
  1908. Cube("c_mts",cx+CW+G,yy,CW,CH4,mtsC,BG_DARK2,"MICRO TRAP",NEON_CYAN,"SCORE "+IntegerToString(mts),mtsC,14);
  1909. NeonBar("c_mts_b",cx+CW+G+12,yy+104,CW-24,7,MathMin(100,mts),mtsC);
  1910. // TFA SCORE - fixed, no overlap
  1911. int tfaInt=(int)tfaScore;color tfaColor;string tfaLabel;
  1912. if(tfaInt>=5){tfaLabel="STRONG";tfaColor=NEON_GREEN;}
  1913. else if(tfaInt>=4){tfaLabel="GOOD";tfaColor=NEON_LIME;}
  1914. else if(tfaInt>=3){tfaLabel="MEDIUM";tfaColor=NEON_YELLOW;}
  1915. else if(tfaInt>=2){tfaLabel="WEAK";tfaColor=NEON_ORANGE;}
  1916. else{tfaLabel="POOR";tfaColor=C'100,100,120';}
  1917. color tfaBC=tfaColor;string dirTxt="";
  1918. if(g_haM1=="HA BEARISH 1"){tfaBC=NEON_RED;dirTxt="[DOWN]";}
  1919. else if(g_haM1=="HA BULLISH 1"){tfaBC=NEON_GREEN;dirTxt="[UP]";}
  1920. else dirTxt="[MIX]";
  1921. Bx("c_tfa",cx+2*(CW+G),yy,CW,CH4,BG_DARK3,tfaBC);
  1922. Tx("c_tfa_l",cx+2*(CW+G)+10,yy+4,"TFA SCORE",NEON_CYAN,10,true);
  1923. Tx("c_tfa_v",cx+2*(CW+G)+10,yy+22,IntegerToString(tfaInt)+"/6 "+tfaLabel,tfaBC,12,true);
  1924. Tx("c_tfa_dir",cx+2*(CW+G)+10,yy+44,dirTxt,tfaBC,11,true);
  1925. // Split TFA detail into 2 short lines
  1926. string tdet=g_tfa_detail;
  1927. int pp=StringFind(tdet," | ");
  1928. string td1=(pp>=0)?StringSubstr(tdet,0,pp):tdet;
  1929. string td2=(pp>=0)?StringSubstr(tdet,pp+3):"";
  1930. if(StringLen(td1)>24)td1=StringSubstr(td1,0,23);
  1931. if(StringLen(td2)>24)td2=StringSubstr(td2,0,23);
  1932. Tx("c_tfa_d1",cx+2*(CW+G)+10,yy+62,td1,CGR,8,false);
  1933. if(StringLen(td2)>0)Tx("c_tfa_d2",cx+2*(CW+G)+10,yy+76,td2,CGR,8,false);
  1934. int tfaFW=(int)((double)tfaInt/6.0*(CW-20));if(tfaFW<2)tfaFW=2;if(tfaFW>CW-20)tfaFW=CW-20;
  1935. Bx("c_tfa_bb",cx+2*(CW+G)+10,yy+104,CW-20,7,BG_DARK4,BG_DARK4);
  1936. Bx("c_tfa_bf",cx+2*(CW+G)+10,yy+104,tfaFW,7,tfaBC,tfaBC);
  1937. yy+=CH4+G;
  1938.  
  1939. // ROW 5 - NCP PRO (replaces FRACTAL), MARKET BIAS, S/R LEVELS (CH5=120)
  1940. int CH5=120;
  1941. // NCP PRO v4.2 CUBE
  1942. if(ENABLE_MTG){
  1943. bool isTrap=(g_mtgState=="PUT TRAP"||g_mtgState=="CALL TRAP");
  1944. color mtgC=g_mtgClr;
  1945. if(mtgC==CGR||mtgC==clrGray) mtgC=C'55,65,85';
  1946. // Trap = purple flashing border
  1947. color borderC = isTrap ? NEON_PURPLE : mtgC;
  1948.  
  1949. Bx("c_mtg",cx,yy,CW,CH5,BG_DARK2,borderC);
  1950.  
  1951. // Title: NCP + run count
  1952. string runStr="";
  1953. if(g_mtgBullCount>=2) runStr=" "+IntegerToString(g_mtgBullCount)+"G-RUN";
  1954. else if(g_mtgBearCount>=2) runStr=" "+IntegerToString(g_mtgBearCount)+"R-RUN";
  1955. Tx("c_mtg_l",cx+10,yy+4,"NCP PRO"+runStr,NEON_PURPLE,9,true);
  1956.  
  1957. // State (big) — TRAP gets special size
  1958. int stateFS = isTrap ? 11 : 13;
  1959. Tx("c_mtg_s",cx+10,yy+20,g_mtgState,mtgC,stateFS,true);
  1960.  
  1961. // CALL% bar + text
  1962. int cW=(int)(g_mtgHype/100.0*(CW-22));if(cW<2)cW=2;if(cW>CW-22)cW=CW-22;
  1963. color cBarC=(g_mtgHype>=g_mtgBetrayal)?NEON_GREEN:C'35,70,35';
  1964. Bx("c_mtg_cbb",cx+10,yy+42,CW-22,6,BG_DARK4,BG_DARK4);
  1965. Bx("c_mtg_cbf",cx+10,yy+42,cW,6,cBarC,cBarC);
  1966. Tx("c_mtg_ct",cx+10,yy+50,"CALL: "+DoubleToString(g_mtgHype,1)+"%",NEON_GREEN,10,true);
  1967.  
  1968. // PUT% bar + text
  1969. int pW=(int)(g_mtgBetrayal/100.0*(CW-22));if(pW<2)pW=2;if(pW>CW-22)pW=CW-22;
  1970. color pBarC=(g_mtgBetrayal>g_mtgHype)?NEON_RED:C'70,25,25';
  1971. Bx("c_mtg_pbb",cx+10,yy+66,CW-22,6,BG_DARK4,BG_DARK4);
  1972. Bx("c_mtg_pbf",cx+10,yy+66,pW,6,pBarC,pBarC);
  1973. Tx("c_mtg_pt",cx+10,yy+74,"PUT: "+DoubleToString(g_mtgBetrayal,1)+"%",NEON_RED,10,true);
  1974.  
  1975. // Action — trap gets purple, else normal
  1976. string actD=g_mtgAction;if(StringLen(actD)>24)actD=StringSubstr(actD,0,23);
  1977. color actC=isTrap?NEON_PURPLE:(g_mtgState=="CALL")?NEON_GREEN:(g_mtgState=="PUT")?NEON_RED:NEON_YELLOW;
  1978. Tx("c_mtg_a",cx+10,yy+88,actD,actC,10,true);
  1979.  
  1980. // Strength + RSI
  1981. string rsnD=(StringLen(g_mtgReason)>0)?g_mtgReason:g_mtgPattern;
  1982. if(StringLen(rsnD)>24)rsnD=StringSubstr(rsnD,0,23);
  1983. Tx("c_mtg_r",cx+10,yy+104,rsnD,CGR,8,false);
  1984.  
  1985. } else {
  1986. double frac=CalcAdvancedFractal();
  1987. color frC=(frac>=75)?NEON_RED:(frac>=50)?NEON_ORANGE:(frac>=25)?NEON_YELLOW:NEON_GREEN;
  1988. string frT=(frac>=75)?"TRAP ZONE!":(frac>=50)?"WARNING":(frac>=25)?"CAUTION":"CLEAR";
  1989. Cube("c_frc",cx,yy,CW,CH5,frC,BG_DARK2,"FRACTAL",NEON_CYAN,frT,frC,13);
  1990. NeonBar("c_frc_b",cx+12,yy+104,CW-24,7,frac,frC);
  1991. }
  1992. // MARKET BIAS - fixed spacing
  1993. Bx("c_bias",cx+CW+G,yy,CW,CH5,BG_DARK2,biasClr);
  1994. Tx("c_bias_l",cx+CW+G+10,yy+4,"MARKET BIAS",NEON_CYAN,10,true);
  1995. string bStr=biasStr;if(StringLen(bStr)>13)bStr=StringSubstr(bStr,0,12);
  1996. Tx("c_bias_v",cx+CW+G+10,yy+22,bStr,biasClr,13,true);
  1997. Tx("c_bias_m",cx+CW+G+10,yy+52,"MODE: "+g_marketMode,(g_marketMode=="TREND")?NEON_GREEN:(g_marketMode=="REVERSAL")?NEON_RED:NEON_YELLOW,10,false);
  1998. color accC=(g_accuracy>=70)?NEON_GREEN:(g_accuracy>=60)?NEON_YELLOW:NEON_RED;
  1999. Tx("c_bias_a",cx+CW+G+10,yy+72,"ACC: "+DoubleToString(g_accuracy,1)+"%",accC,10,false);
  2000. NeonBar("c_bias_b",cx+CW+G+10,yy+88,CW-20,7,g_accuracy,accC);
  2001. // S/R LEVELS
  2002. int dg_sr=(int)MarketInfo(Symbol(),MODE_DIGITS);if(dg_sr<=0)dg_sr=5;
  2003. Bx("c18",cx+2*(CW+G),yy,CW,CH5,BG_DARK2,NEON_CYAN);
  2004. Tx("c18_l",cx+2*(CW+G)+10,yy+4,"S/R LEVELS",NEON_CYAN,10,true);
  2005. Tx("c18_r",cx+2*(CW+G)+10,yy+24,"R: "+(g_nearest_res>0?DoubleToString(g_nearest_res,dg_sr):"--"),NEON_RED,13,true);
  2006. Tx("c18_s",cx+2*(CW+G)+10,yy+54,"S: "+(g_nearest_sup>0?DoubleToString(g_nearest_sup,dg_sr):"--"),NEON_GREEN,13,true);
  2007. Tx("c18_b",cx+2*(CW+G)+10,yy+86,g_brk_str,g_brk_color,11,true);
  2008. yy+=CH5+G;
  2009.  
  2010. // ROW 6 - WICKS, HA CANDLES, REVERSAL (CH6=120)
  2011. int CH6=120;
  2012. color wickC=(mw>0.70)?NEON_RED:(mw>0.50)?NEON_ORANGE:NEON_GREEN;
  2013. Bx("c15",cx,yy,CW,CH6,BG_DARK2,wickC);
  2014. Tx("c15_l",cx+10,yy+4,"WICKS",NEON_CYAN,10,true);
  2015. Tx("c15_v",cx+10,yy+26,DoubleToString(mw,2),wickC,18,true);
  2016. Tx("c15_b",cx+10,yy+62,(mw>0.70)?"KILL SHOT":(mw>0.50)?"HIGH":"LOW",wickC,11,false);
  2017. NeonBar("c15_bar",cx+10,yy+80,CW-20,7,mw*100,wickC);
  2018. // HA CANDLES - fixed M5 cutoff
  2019. Bx("c_ha",cx+CW+G,yy,CW,CH6,BG_DARK2,ha1C);
  2020. Tx("c_ha_l",cx+CW+G+10,yy+4,"HA CANDLES",NEON_CYAN,10,true);
  2021. string haM1txt=g_haM1,haM5txt=g_haM5;
  2022. if(StringLen(haM1txt)>16)haM1txt=StringSubstr(haM1txt,3);
  2023. if(StringLen(haM5txt)>16)haM5txt=StringSubstr(haM5txt,3);
  2024. Tx("c_ha_m1",cx+CW+G+10,yy+24,"M1: "+haM1txt,ha1C,11,true);
  2025. Tx("c_ha_m5",cx+CW+G+10,yy+50,"M5: "+haM5txt,ha5C,11,true);
  2026. string haComb="";color haCombC=NEON_YELLOW;
  2027. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5"){haComb="BOTH BULL";haCombC=NEON_GREEN;}
  2028. else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5"){haComb="BOTH BEAR";haCombC=NEON_RED;}
  2029. else if(g_haBoth=="MIXED")haComb="MIXED";
  2030. else haComb=g_haBoth;
  2031. Tx("c_ha_c",cx+CW+G+10,yy+76,haComb,haCombC,11,true);
  2032. // REVERSAL
  2033. string revReason=DetectSuddenReversal();color revColor=NEON_YELLOW;
  2034. if(StringFind(revReason,"BULL")>=0)revColor=NEON_GREEN;
  2035. if(StringFind(revReason,"BEAR")>=0)revColor=NEON_RED;
  2036. Bx("c_reason",cx+2*(CW+G),yy,CW,CH6,BG_DARK2,revColor);
  2037. Tx("c_reason_l",cx+2*(CW+G)+10,yy+4,"REVERSAL",NEON_CYAN,10,true);
  2038. Tx("c_reason_v",cx+2*(CW+G)+10,yy+36,(StringLen(revReason)>0?revReason:"No reversal"),revColor,10,false);
  2039. yy+=CH6+G;
  2040.  
  2041. // BB PULLBACK ROW - with ADX+RSI confirmation display
  2042. if(ENABLE_BB_PULLBACK){
  2043. int bbRowH=52;
  2044. color bbRowBorder = (g_bbSignal=="CALL")?NEON_GREEN:(g_bbSignal=="PUT")?NEON_RED:BB_GREY;
  2045. Bx("c_bb_row",cx,yy,W-2*G,bbRowH,BG_DARK3,bbRowBorder);
  2046. // Line 1: BB info + HA30 + bands
  2047. string ha30Str=Is30SecHABull()?"BULL":Is30SecHABear()?"BEAR":"DOJI";
  2048. color ha30C2=Is30SecHABull()?NEON_GREEN:Is30SecHABear()?NEON_RED:NEON_YELLOW;
  2049. double adxD=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);
  2050. double rsiD=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  2051. double bbU2=iBands(NULL,PERIOD_M1,BB_PERIOD,BB_DEVIATION,0,PRICE_CLOSE,MODE_UPPER,0);
  2052. double bbL2=iBands(NULL,PERIOD_M1,BB_PERIOD,BB_DEVIATION,0,PRICE_CLOSE,MODE_LOWER,0);
  2053. int dg3=(int)MarketInfo(Symbol(),MODE_DIGITS);if(dg3<=0)dg3=5;
  2054. Tx("c_bb_row_l",cx+10,yy+4,"BB("+IntegerToString(BB_PERIOD)+") PULLBACK",BB_GREY,9,true);
  2055. Tx("c_bb_ha2",cx+190,yy+4,"HA30:"+ha30Str,ha30C2,9,true);
  2056. Tx("c_bb_adx",cx+310,yy+4,"ADX:"+DoubleToString(adxD,0),adxD>=20?NEON_LIME:CGR,9,false);
  2057. Tx("c_bb_rsi",cx+390,yy+4,"RSI:"+DoubleToString(rsiD,0),(rsiD>=70||rsiD<=30)?NEON_RED:CGR,9,false);
  2058. if(bbU2>0&&bbL2>0){
  2059. Tx("c_bb_u2",cx+470,yy+4,"U:"+DoubleToString(bbU2,dg3),NEON_ORANGE,8,false);
  2060. Tx("c_bb_d2",cx+570,yy+4,"L:"+DoubleToString(bbL2,dg3),NEON_CYAN,8,false);
  2061. }
  2062. // Line 2: Signal + full detail
  2063. if(g_bbSignal!="--"){
  2064. string sigTxt = "BB "+g_bbSignal+" | "+g_bbDetail;
  2065. if(StringLen(sigTxt)>60) sigTxt=StringSubstr(sigTxt,0,58)+"..";
  2066. Tx("c_bb_sig2",cx+10,yy+22,sigTxt,g_bbColor,10,true);
  2067. } else {
  2068. Tx("c_bb_sig2",cx+10,yy+22,"Waiting for BB touch + 2+ confirmations (HA30+ADX+RSI)...",CGR,9,false);
  2069. }
  2070. yy+=bbRowH+G;
  2071. }
  2072.  
  2073. // BOTTOM ROW - HRN, SPM, STRATEGY
  2074. int bottomW=(W-2*G-2*G)/3,bottomH=106;yy+=4;int x1=cx;
  2075. // HRN LEVEL
  2076. Bx("c_hrn",x1,yy,bottomW,bottomH,BG_DARK2,NEON_PINK);
  2077. Tx("c_hrn_l",x1+8,yy+4,"HRN LEVEL",NEON_PINK,10,true);
  2078. Tx("c_hrn_v",x1+8,yy+22,g_hrn_str,NEON_PINK,13,true);
  2079. Tx("c_hrn_t",x1+8,yy+46,g_hrn_is_sup?"SUPPORT":"RESISTANCE",NEON_PINK,10,false);
  2080. color scoreC=(g_hrn_score>=30)?NEON_GREEN:(g_hrn_score>=15)?NEON_YELLOW:NEON_ORANGE;
  2081. Tx("c_hrn_sc",x1+8,yy+62,"Score: "+DoubleToString(g_hrn_score,0),scoreC,10,false);
  2082. Tx("c_hrn_b",x1+8,yy+78,"Brk "+IntegerToString(g_hrn_brk_bars)+"/"+IntegerToString(HRN_CONFIRM_BARS),NEON_PINK,10,false);
  2083. Tx("c_hrn_s",x1+8,yy+94,g_isSideways?"SIDEWAYS":"TRENDING",g_isSideways?NEON_YELLOW:NEON_GREEN,9,false);
  2084. int x2=x1+bottomW+G;
  2085. // SPM
  2086. Bx("c5",x2,yy,bottomW,bottomH,BG_DARK3,NEON_CYAN);
  2087. Tx("c5_l",x2+10,yy+12,"SPM",NEON_CYAN,10,true);
  2088. Tx("c5_v",x2+10,yy+44,(g_rsc>=0?"+":"")+DoubleToString(g_rsc,1),(g_rsc>0)?NEON_GREEN:NEON_RED,22,true);
  2089. int x3=x2+bottomW+G;
  2090. // STRATEGY
  2091. Bx("c_strat_b",x3,yy,bottomW,bottomH,BG_DARK3,stratC);
  2092. Tx("c_strat_b_l",x3+10,yy+6,"STRATEGY",NEON_CYAN,10,true);
  2093. Tx("c_strat_b_v",x3+10,yy+30,stratLabel,stratC,11,true);
  2094. Tx("c_strat_b_r",x3+10,yy+60,g_strategyReason,CGR,8,false);
  2095. yy+=bottomH+5;
  2096.  
  2097. // FILTER STATUS BAR
  2098. int fsW=W-2*G;
  2099. color fsC=(g_filterStatus=="ALL CLEAR")?NEON_GREEN:(StringFind(g_filterStatus,"ACTIVE")>=0)?NEON_LIME:(StringFind(g_filterStatus,"LOCK")>=0)?NEON_RED:NEON_ORANGE;
  2100. Bx("c_filter",cx,yy,fsW,22,BG_DARK3,fsC);
  2101. Tx("c_filter_v",cx+10,yy+4,"FILTER: "+g_filterStatus,fsC,11,true);
  2102. HideSPM();
  2103. ChartRedraw();
  2104. }
  2105.  
  2106. void UpdateRiskControl(){
  2107. static datetime llt=0;
  2108. if(ENABLE_RISK_LOCK){if(g_lossStreak>=MAX_LOSS_STREAK){g_tradingStopped=true;llt=TimeCurrent();g_filterStatus="RISK LOCK "+IntegerToString(g_lossStreak)+"L";}if(g_tradingStopped&&TimeCurrent()-llt>3600){g_tradingStopped=false;g_lossStreak=0;g_filterStatus="RISK UNLOCK";}}}
  2109.  
  2110. void FinalSignalEngine(double brain,int cp,int mts){
  2111. g_filterStatus="ALL CLEAR";
  2112. if(ENABLE_RISK_CONTROL&&g_tradingStopped){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="RISK LOCKED";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2113. if(ENABLE_SESSION_FILTER&&!IsSessionActive()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="SESSION OFF";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2114. if(MTF_CONFIRM&&!g_mtfConfirmed){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="MTF NO CONF";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2115. if(ENABLE_SPIKE_FILTER&&IsBigCandle(1)){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="SPIKE BLOCK";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2116. if(ENABLE_LAST_SEC_BLOCK){int age=(int)(TimeCurrent()-Time[0]);int ps=Period()*60;int rem=ps-age;if(rem<0)rem=0;if(rem<MIN_REMAINING_SEC){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="LAST SEC BLOCK";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}}
  2117. double conf=MathMax(g_calc_gProb,g_calc_rProb);
  2118. if(conf<MIN_CONFIDENCE){g_finalSignal="WAIT";g_finalColor=NEON_YELLOW;g_filterStatus="LOW CONF "+DoubleToString(conf,0)+"%";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2119. bool reverseSignal=(g_accuracy<45.0&&g_acc_total>20);
  2120. if(reverseSignal)g_filterStatus="REVERSE MODE";
  2121. string ms=DetectMyStrategy();
  2122. if(ms!="WAIT"){
  2123. if(CONSENSUS_FILTER&&!ConsensusOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="NO CONSENSUS";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2124. if(ENABLE_ENTRY_ZONE&&!DirectionOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="BAD ENTRY ZONE";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2125. if(!EntryPrecisionOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="PRECISION FAIL";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2126. if(reverseSignal){if(ms=="CALL")ms="PUT";else if(ms=="PUT")ms="CALL";}
  2127. g_finalSignal=ms;g_finalColor=(ms=="CALL")?NEON_GREEN:NEON_RED;g_filterStatus=(g_strategyType=="TRAP")?"TRAP ACTIVE":"TREND ACTIVE";
  2128. if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT"){g_finalSignal=ms;g_signalTime=TimeCurrent();}
  2129. SendSignalNotification(ms,conf);return;}
  2130. double th=FAST_MODE?70:68;string rawSignal="WAIT";
  2131. if(g_calc_gProb>th)rawSignal="CALL";else if(g_calc_rProb>th)rawSignal="PUT";
  2132. else if(g_calc_gProb>60)rawSignal="WEAK CALL";else if(g_calc_rProb>60)rawSignal="WEAK PUT";
  2133. if(reverseSignal){if(rawSignal=="CALL")rawSignal="PUT";else if(rawSignal=="PUT")rawSignal="CALL";else if(rawSignal=="WEAK CALL")rawSignal="WEAK PUT";else if(rawSignal=="WEAK PUT")rawSignal="WEAK CALL";}
  2134. g_finalSignal=rawSignal;
  2135. if((g_finalSignal=="CALL"||g_finalSignal=="PUT")&&ENABLE_ENTRY_ZONE&&!DirectionOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="BAD ENTRY ZONE";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2136. if((g_finalSignal=="CALL"||g_finalSignal=="PUT")&&!EntryPrecisionOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="PRECISION FAIL";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}
  2137. if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT")g_signalTime=TimeCurrent();
  2138. if(g_finalSignal=="CALL"){g_finalColor=NEON_GREEN;g_filterStatus="GREEN SIGNAL";SendSignalNotification("CALL",conf);}
  2139. else if(g_finalSignal=="PUT"){g_finalColor=NEON_RED;g_filterStatus="RED SIGNAL";SendSignalNotification("PUT",conf);}
  2140. else if(g_finalSignal=="WEAK CALL"){g_finalColor=NEON_CYAN;g_filterStatus="WEAK GREEN";}
  2141. else if(g_finalSignal=="WEAK PUT"){g_finalColor=NEON_ORANGE;g_filterStatus="WEAK RED";}
  2142. else g_finalColor=NEON_YELLOW;}
  2143.  
  2144. void SendSignalNotification(string sig,double conf){
  2145. if(!ENABLE_NOTIFY) return;
  2146. string msg=Symbol()+" | "+sig+" | "+DoubleToString(conf,0)+"% | "+g_filterStatus;
  2147. if(msg==g_lastNotified) return;
  2148. g_lastNotified=msg;
  2149. // NO popup Alert, NO SendNotification (mobile push) - removed as requested
  2150. // Only Telegram if enabled, and Print to log
  2151. if(ENABLE_TELEGRAM) SendTelegramAlert("AIBRAIN\n"+Symbol()+"\n"+sig+"\nConf: "+DoubleToString(conf,0)+"%\n"+g_filterStatus);
  2152. Print(msg);
  2153. }
  2154.  
  2155. void BroadcastSignal(){
  2156. double conf=MathMax(g_frozen_gProb,g_frozen_rProb);if(conf<55)return;
  2157. string fn="OTC_Signals_Master.txt",pair=Symbol();string sig=(g_frozen_gProb>g_frozen_rProb)?"CALL":"PUT";
  2158. int exp=(int)(TimeCurrent()+65);string line=pair+"|"+sig+"|"+DoubleToString(conf,0)+"|"+IntegerToString((int)TimeCurrent())+"|"+IntegerToString(exp);
  2159. int h=FileOpen(fn,FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);
  2160. if(h==INVALID_HANDLE){h=FileOpen(fn,FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);if(h==INVALID_HANDLE)return;FileWriteString(h,line+"\n");FileClose(h);return;}
  2161. string content="";while(!FileIsEnding(h)){string ex=FileReadString(h);if(StringLen(ex)>10&&StringFind(ex,pair+"|")!=0)content+=ex+"\n";}
  2162. content=line+"\n"+content;string lns[];int cnt=0,pos=0;int f=StringFind(content,"\n",pos);
  2163. while(f!=-1&&cnt<100){ArrayResize(lns,cnt+1);lns[cnt]=StringSubstr(content,pos,f-pos);cnt++;pos=f+1;f=StringFind(content,"\n",pos);}
  2164. if(pos<StringLen(content)){ArrayResize(lns,cnt+1);lns[cnt]=StringSubstr(content,pos);cnt++;}
  2165. if(cnt>50){content="";for(int i=0;i<50;i++)if(StringLen(lns[i])>10)content+=lns[i]+"\n";}
  2166. FileSeek(h,0,SEEK_SET);FileWriteString(h,content);FileFlush(h);FileClose(h);}
  2167.  
  2168. // ============================================================
  2169. // PAIR QUALITY SCORE - 1 MINUTE OTC OPTIMIZED
  2170. // Har pair ko 0-100 score deta hai based on:
  2171. // 1. HA M1 alignment (25 pts)
  2172. // 2. HA M5 alignment (15 pts)
  2173. // 3. RSI position (20 pts)
  2174. // 4. Volume spike (15 pts)
  2175. // 5. Momentum - consecutive candles (15 pts)
  2176. // 6. Spread check (10 pts)
  2177. // Returns score aur signal direction
  2178. // ============================================================
  2179. int CalcPairQuality(string pair, string &outSig, string &outDetail){
  2180. // HA M1 calculation
  2181. double o1=iOpen(pair,PERIOD_M1,1), h1=iHigh(pair,PERIOD_M1,1);
  2182. double l1=iLow(pair,PERIOD_M1,1), c1=iClose(pair,PERIOD_M1,1);
  2183. double o2=iOpen(pair,PERIOD_M1,2), h2=iHigh(pair,PERIOD_M1,2);
  2184. double l2=iLow(pair,PERIOD_M1,2), c2=iClose(pair,PERIOD_M1,2);
  2185. double o3=iOpen(pair,PERIOD_M1,3), c3=iClose(pair,PERIOD_M1,3);
  2186. if(o1==0||c1==0||o2==0||c2==0||o3==0||c3==0){ outSig="WAIT"; return 0; }
  2187.  
  2188. double haC1=(o1+h1+l1+c1)/4.0, haC2=(o2+h2+l2+c2)/4.0;
  2189. double haO2=(o3+c3)/2.0, haO1=(haO2+haC2)/2.0;
  2190. bool haM1Bull=(haC1>haO1), haM1Bear=(haC1<haO1);
  2191. double haBody=MathAbs(haC1-haO1), haRange=(h1-l1);
  2192. double haStrength=(haRange>0)? haBody/haRange : 0;
  2193.  
  2194. // HA M5
  2195. double m5_o1=iOpen(pair,PERIOD_M5,1), m5_h1=iHigh(pair,PERIOD_M5,1);
  2196. double m5_l1=iLow(pair,PERIOD_M5,1), m5_c1=iClose(pair,PERIOD_M5,1);
  2197. double m5_o2=iOpen(pair,PERIOD_M5,2), m5_h2=iHigh(pair,PERIOD_M5,2);
  2198. double m5_l2=iLow(pair,PERIOD_M5,2), m5_c2=iClose(pair,PERIOD_M5,2);
  2199. double m5_o3=iOpen(pair,PERIOD_M5,3), m5_c3=iClose(pair,PERIOD_M5,3);
  2200. bool haM5Bull=false, haM5Bear=false;
  2201. if(m5_o1>0&&m5_c1>0&&m5_o2>0&&m5_c2>0&&m5_o3>0&&m5_c3>0){
  2202. double m5haC1=(m5_o1+m5_h1+m5_l1+m5_c1)/4.0, m5haC2=(m5_o2+m5_h2+m5_l2+m5_c2)/4.0;
  2203. double m5haO2=(m5_o3+m5_c3)/2.0, m5haO1=(m5haO2+m5haC2)/2.0;
  2204. haM5Bull=(m5haC1>m5haO1); haM5Bear=(m5haC1<m5haO1);
  2205. }
  2206.  
  2207. // RSI M1
  2208. double rsi=iRSI(pair,PERIOD_M1,14,PRICE_CLOSE,1);
  2209.  
  2210. // Volume ratio (bar1 vs bar2)
  2211. double vol1=(double)iVolume(pair,PERIOD_M1,1);
  2212. double vol2=(double)iVolume(pair,PERIOD_M1,2);
  2213. double volRatio=(vol2>0)?vol1/vol2:1.0;
  2214.  
  2215. // Spread
  2216. double spread=(double)MarketInfo(pair,MODE_SPREAD);
  2217.  
  2218. // Momentum: consecutive candles same direction
  2219. int bullStreak=0, bearStreak=0;
  2220. for(int i=1;i<=4;i++){
  2221. double ci=iClose(pair,PERIOD_M1,i), oi=iOpen(pair,PERIOD_M1,i);
  2222. if(ci==0||oi==0) break;
  2223. if(ci>oi){ if(bearStreak>0)break; bullStreak++; }
  2224. else if(ci<oi){ if(bullStreak>0)break; bearStreak++; }
  2225. else break;
  2226. }
  2227.  
  2228. // ============================================================
  2229. // DETERMINE SIGNAL DIRECTION
  2230. // Priority: HA M1 + M5 both agree = strong
  2231. // ============================================================
  2232. bool bullDir=false, bearDir=false;
  2233. if(haM1Bull&&haM5Bull) { bullDir=true; }
  2234. else if(haM1Bear&&haM5Bear) { bearDir=true; }
  2235. else if(haM1Bull&&rsi<45) { bullDir=true; } // M1 bull + oversold
  2236. else if(haM1Bear&&rsi>55) { bearDir=true; } // M1 bear + overbought
  2237. else { outSig="WAIT"; return 0; }
  2238.  
  2239. // ============================================================
  2240. // SCORING
  2241. // ============================================================
  2242. int score=0;
  2243. string det="";
  2244.  
  2245. // 1. HA M1 (25 pts)
  2246. if(bullDir&&haM1Bull){ int s=(haStrength>0.6)?25:(haStrength>0.4)?18:12; score+=s; det+="HA1+"; }
  2247. if(bearDir&&haM1Bear){ int s=(haStrength>0.6)?25:(haStrength>0.4)?18:12; score+=s; det+="HA1+"; }
  2248.  
  2249. // 2. HA M5 alignment (15 pts)
  2250. if(bullDir&&haM5Bull){ score+=15; det+="HA5+"; }
  2251. else if(bearDir&&haM5Bear){ score+=15; det+="HA5+"; }
  2252. else det+="HA5-";
  2253.  
  2254. // 3. RSI zone (20 pts)
  2255. if(bullDir){
  2256. if(rsi<30){ score+=20; det+="RSI_OS "; }
  2257. else if(rsi<40){ score+=14; det+="RSI_low "; }
  2258. else if(rsi<50){ score+=8; det+="RSI_mid "; }
  2259. else{ score+=2; det+="RSI_hi "; }
  2260. } else {
  2261. if(rsi>70){ score+=20; det+="RSI_OB "; }
  2262. else if(rsi>60){ score+=14; det+="RSI_hi "; }
  2263. else if(rsi>50){ score+=8; det+="RSI_mid "; }
  2264. else{ score+=2; det+="RSI_lo "; }
  2265. }
  2266.  
  2267. // 4. Volume (15 pts)
  2268. if(volRatio>=2.0){ score+=15; det+="VOL_HI "; }
  2269. else if(volRatio>=1.5){ score+=10; det+="VOL_MD "; }
  2270. else if(volRatio>=1.1){ score+=5; det+="VOL_LO "; }
  2271.  
  2272. // 5. Momentum streak (15 pts)
  2273. int streak=(bullDir)?bullStreak:bearStreak;
  2274. if(streak>=3){ score+=15; det+="STK"+IntegerToString(streak); }
  2275. else if(streak>=2){ score+=10; det+="STK"+IntegerToString(streak); }
  2276. else if(streak>=1){ score+=5; det+="STK1"; }
  2277.  
  2278. // 6. Spread (10 pts) — low spread = good
  2279. if(spread<=3){ score+=10; }
  2280. else if(spread<=6){ score+=6; }
  2281. else if(spread<=10){ score+=3; }
  2282. // else 0 pts
  2283.  
  2284. outSig = bullDir ? "CALL" : "PUT";
  2285. outDetail = det;
  2286. return MathMin(100, score);
  2287. }
  2288.  
  2289. string GetPairHADirection(string pair){
  2290. double o1=iOpen(pair,PERIOD_M1,1),h1=iHigh(pair,PERIOD_M1,1),l1=iLow(pair,PERIOD_M1,1),c1=iClose(pair,PERIOD_M1,1);
  2291. double o2=iOpen(pair,PERIOD_M1,2),h2=iHigh(pair,PERIOD_M1,2),l2=iLow(pair,PERIOD_M1,2),c2=iClose(pair,PERIOD_M1,2);
  2292. double o3=iOpen(pair,PERIOD_M1,3),c3=iClose(pair,PERIOD_M1,3);
  2293. if(o1==0||h1==0||l1==0||c1==0||o2==0||c2==0||o3==0||c3==0)return "NEUTRAL";
  2294. double haC1=(o1+h1+l1+c1)/4.0,haC2=(o2+h2+l2+c2)/4.0,haO2=(o3+c3)/2.0,haO1=(haO2+haC2)/2.0;
  2295. if(haC1>haO1)return "BULL";else if(haC1<haO1)return "BEAR";return "NEUTRAL";}
  2296.  
  2297. double GetPairVolumeRatio(string pair){double v1=iVolume(pair,PERIOD_M1,1),v2=iVolume(pair,PERIOD_M1,2);if(v2<=0)return 1.0;return v1/v2;}
  2298.  
  2299. double CalculateCorrelation(string sym1,string sym2){
  2300. int bars=MathMin(iBars(sym1,PERIOD_M1),iBars(sym2,PERIOD_M1));if(bars<50)return 0;
  2301. double sum1=0,sum2=0,sum1sq=0,sum2sq=0,sum12=0;
  2302. for(int i=0;i<bars;i++){double c1=iClose(sym1,PERIOD_M1,i),c2=iClose(sym2,PERIOD_M1,i);if(c1==0||c2==0)continue;sum1+=c1;sum2+=c2;sum1sq+=c1*c1;sum2sq+=c2*c2;sum12+=c1*c2;}
  2303. int n=bars;double num=n*sum12-sum1*sum2;double den=MathSqrt((n*sum1sq-sum1*sum1)*(n*sum2sq-sum2*sum2));if(den==0)return 0;return num/den;}
  2304.  
  2305. // ============================================================
  2306. // SCAN SIGNALS - QUALITY SCORED VERSION
  2307. // File se broadcast signals read karo
  2308. // Phir har pair ka quality score check karo
  2309. // Sirf high-quality pairs dikhao
  2310. // ============================================================
  2311. void ScanSignals(){
  2312. g_pairCount=0;
  2313. string fn="OTC_Signals_Master.txt", cur=Symbol();
  2314. int h=FileOpen(fn,FILE_READ|FILE_TXT|FILE_SHARE_READ);
  2315. if(h==INVALID_HANDLE) return;
  2316.  
  2317. datetime now=TimeCurrent();
  2318.  
  2319. // Temp arrays for sorting by quality score
  2320. string tmpNames[8]; string tmpSigs[8];
  2321. double tmpConfs[8]; int tmpScores[8];
  2322. string tmpDetails[8];
  2323. int tmpCount=0;
  2324.  
  2325. while(!FileIsEnding(h) && tmpCount<8){
  2326. string ln=FileReadString(h);
  2327. if(StringLen(ln)<15) continue;
  2328. int p1=StringFind(ln,"|"),p2=StringFind(ln,"|",p1+1),p3=StringFind(ln,"|",p2+1),p4=StringFind(ln,"|",p3+1);
  2329. if(p1<0||p2<0||p3<0||p4<0) continue;
  2330.  
  2331. string pair=StringSubstr(ln,0,p1);
  2332. if(pair==cur) continue;
  2333.  
  2334. string sig=StringSubstr(ln,p1+1,p2-p1-1);
  2335. double conf=StringToDouble(StringSubstr(ln,p2+1,p3-p2-1));
  2336. int expTime=(int)StringToInteger(StringSubstr(ln,p4+1));
  2337.  
  2338. // Basic filters
  2339. if(conf<60) continue;
  2340. if(expTime<(int)(now-20)) continue; // 20 sec grace period
  2341.  
  2342. // Quality score calculate karo
  2343. string qSig="WAIT", qDetail="";
  2344. int qScore=CalcPairQuality(pair, qSig, qDetail);
  2345.  
  2346. // Signal direction must match broadcast
  2347. if(qSig != sig) continue;
  2348. // Minimum quality threshold
  2349. if(qScore < 45) continue;
  2350.  
  2351. tmpNames[tmpCount] = pair;
  2352. tmpSigs[tmpCount] = sig;
  2353. tmpConfs[tmpCount] = conf;
  2354. tmpScores[tmpCount] = qScore;
  2355. tmpDetails[tmpCount] = qDetail;
  2356. tmpCount++;
  2357. }
  2358. FileClose(h);
  2359.  
  2360. // Sort by quality score (descending) - bubble sort
  2361. for(int i=0;i<tmpCount-1;i++){
  2362. for(int j=0;j<tmpCount-i-1;j++){
  2363. if(tmpScores[j]<tmpScores[j+1]){
  2364. // Swap
  2365. string ts=tmpNames[j]; tmpNames[j]=tmpNames[j+1]; tmpNames[j+1]=ts;
  2366. ts=tmpSigs[j]; tmpSigs[j]=tmpSigs[j+1]; tmpSigs[j+1]=ts;
  2367. ts=tmpDetails[j]; tmpDetails[j]=tmpDetails[j+1]; tmpDetails[j+1]=ts;
  2368. double td=tmpConfs[j]; tmpConfs[j]=tmpConfs[j+1]; tmpConfs[j+1]=td;
  2369. int ti=tmpScores[j]; tmpScores[j]=tmpScores[j+1]; tmpScores[j+1]=ti;
  2370. }
  2371. }
  2372. }
  2373.  
  2374. // Copy top 5 to global arrays (already sorted best first)
  2375. g_pairCount=MathMin(5,tmpCount);
  2376. for(int i=0;i<g_pairCount;i++){
  2377. g_pairNames[i] = tmpNames[i];
  2378. g_pairSigs[i] = tmpSigs[i];
  2379. // Store quality score in conf field for display (combined)
  2380. // Format: "score|broadcast_conf"
  2381. g_pairConfs[i] = tmpScores[i]; // Show quality score
  2382. }
  2383.  
  2384. // Boost logic: agar 2+ high-quality pairs same direction mein
  2385. string curPred=(g_frozen_gProb>g_frozen_rProb)?"CALL":"PUT";
  2386. int sameDir=0;
  2387. double boostTotal=0;
  2388. for(int i=0;i<g_pairCount;i++){
  2389. if(g_pairSigs[i]==curPred && tmpScores[i]>=60){
  2390. sameDir++;
  2391. boostTotal+=tmpScores[i];
  2392. }
  2393. }
  2394. if(sameDir>=2 && !g_tradingStopped){
  2395. double boost=MathMin(12.0, boostTotal/50.0);
  2396. if(curPred=="CALL"){ g_calc_gProb+=boost; g_calc_rProb-=boost; }
  2397. else { g_calc_rProb+=boost; g_calc_gProb-=boost; }
  2398. g_calc_gProb=MathMax(8.0,MathMin(92.0,g_calc_gProb));
  2399. g_calc_rProb=100.0-g_calc_gProb;
  2400. if(StringFind(g_filterStatus,"BOOST")<0)
  2401. g_filterStatus="PAIR BOOST+"+DoubleToString(boost,0)+"%";
  2402. }
  2403. }
  2404.  
  2405. void ReadSPM(){
  2406. datetime cb=Time[0];if(cb==g_last_bar&&TimeCurrent()-g_spm_t<5)return;
  2407. double v1=iCustom(NULL,PERIOD_M1,SPM_FILE,0,0),v2=iCustom(NULL,PERIOD_M1,SPM_FILE,1,0);
  2408. if(v1!=EMPTY_VALUE&&v1!=0){double sm=0.7;g_rsc=g_rsc*(1.0-sm)+v1*sm;}
  2409. if(v2!=EMPTY_VALUE&&v2!=0){double sm=0.7;g_tfa=g_tfa*(1.0-sm)+v2*sm;}
  2410. g_last_bar=cb;g_spm_t=TimeCurrent();}
  2411.  
  2412. //+------------------------------------------------------------------+
  2413. // ONINIT
  2414. //+------------------------------------------------------------------+
  2415. int OnInit(){
  2416. InitNW();NuclearDeleteAll();
  2417. resCount=0;supCount=0;lastSRBar=0;g_hrn_price=0;g_hrn_brk_bars=0;g_hrn_scan_bar=0;
  2418. g_hrn_score=0;g_hrn_confirmed_break=false;g_nearest_res=0;g_nearest_sup=0;g_rj_cnt=0;g_pairCount=0;
  2419. g_marketMode="RANGE";g_prevMode="";g_last_freeze_bar=0;g_lastNotified="";
  2420. g_accuracy=ValidateHistoricalAccuracy();g_acc_correct=0;g_acc_total=0;g_acc_lastBar=0;
  2421. g_lastPredGreen=50.0;g_lastPredBar=0;g_lossStreak=0;g_tradingStopped=false;
  2422. g_strategyType="NONE";g_frozen_gProb=50;g_frozen_rProb=50;g_calc_gProb=50;g_calc_rProb=50;
  2423. g_commonCount=0;g_wickLineCount=0;g_tfa_detail="";g_htfLevelCount=0;g_mtfConfirmed=true;g_filterStatus="ALL CLEAR";
  2424. g_symbolKey="";g_brokerBias=0.0;
  2425. // BB variables init
  2426. g_bbSignal="--";g_bbColor=NEON_YELLOW;g_bbTouchPrice=0.0;g_bbSignalTime=0;g_bbSignalBars=0;g_bbDetail="";
  2427. // MTG/NCP init
  2428. g_mtgHype=50;g_mtgBetrayal=50;g_mtgRecovery=0;
  2429. g_mtgState="INIT";g_mtgReason="";g_mtgAction="";
  2430. g_mtgClr=CGR;g_mtg_lastBar=0;g_mtgPattern="";
  2431. g_mtgBullCount=0;g_mtgBearCount=0;g_trapScore=0;
  2432. g_smoothCallScore=50.0;g_smoothPutScore=50.0;
  2433. g_callWeight=1.0;g_putWeight=1.0;
  2434. g_totalTrades=0;g_callTrades=0;g_putTrades=0;
  2435. g_callWins=0;g_putWins=0;
  2436. g_ncpLastSignalTime=0;g_ncpLastSignalType="";
  2437. g_ncpLastEntryPrice=0.0;g_ncpSignalProcessed=true;
  2438. if(ENABLE_LEARNING) LoadBrainMemory();
  2439. g_ha30_open=0;g_ha30_close=0;g_ha30_high=0;g_ha30_low=0;g_ha30_bar=0;
  2440. g_bbLineCount=0;
  2441. LoadMLWeights();EventSetTimer(1);
  2442. Print("===========================================");
  2443. Print("AIBRAIN v46.2 - NCP v7.3 LEARNING+SMOOTH+TIMER");
  2444. Print("BB Color: YELLOW=no signal, GREEN=CALL, RED=PUT");
  2445. Print("Volume: Bar[1] vs Bar[2] - single candle ratio");
  2446. Print("30sec HA: Backtesting safe (no elapsed time)");
  2447. Print("Strategy: Cached values, no double-call");
  2448. Print("BB Pullback: ", ENABLE_BB_PULLBACK?"ON":"OFF", " BB(",BB_PERIOD,",",DoubleToString(BB_DEVIATION,1),")");
  2449. Print("===========================================");
  2450. return INIT_SUCCEEDED;}
  2451.  
  2452. //+------------------------------------------------------------------+
  2453. // ONDEINIT
  2454. //+------------------------------------------------------------------+
  2455. void OnDeinit(const int r){
  2456. EventKillTimer();Comment("");
  2457. // Remove all indicator objects including timer
  2458. for(int i=ObjectsTotal()-1;i>=0;i--){
  2459. string nm=ObjectName(i);
  2460. if(StringFind(nm,PFX)==0)ObjectDelete(nm);
  2461. }
  2462. }
  2463.  
  2464. //+------------------------------------------------------------------+
  2465. // ONTIMER
  2466. //+------------------------------------------------------------------+
  2467. void OnTimer(){
  2468. if(Bars<50)return;
  2469. static datetime last5min=0;
  2470. if(Time[0]/300!=last5min/300){DetectHiddenLevels();last5min=Time[0];}
  2471. ReadSPM();CalcHA();UpdateHRN();UpdateSR();
  2472. DetectCommonPoints();DetectWickRejections();
  2473. DrawCommonPoints();DrawWickRejectLines();DrawHiddenLevels();DrawNearestSR();
  2474. if(ENABLE_BB_PULLBACK) DetectBBPullback();
  2475. if(ENABLE_MTG) CalculateMTG();
  2476. int cp=0;double brain=BrainSc(cp);double nb=NeuralBiasFast();double mw=MicroWick();bool bf=IsBrokerForce();
  2477. double mai=CalcAdvancedMicroAI();int mts=CalcAdvancedMicroTrap();
  2478. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  2479. ScanSignals();FinalSignalEngine(brain,cp,mts);BroadcastSignal();
  2480. UpdateTimer(); // Timer update — har second
  2481. Draw();
  2482. }
  2483.  
  2484. //+------------------------------------------------------------------+
  2485. // ONCALCULATE
  2486. //+------------------------------------------------------------------+
  2487. int OnCalculate(const int rates_total,const int prev_calculated,
  2488. const datetime &time[],const double &open[],
  2489. const double &high[],const double &low[],const double &close[],
  2490. const long &tick_volume[],const long &volume[],const int &spread[]){
  2491. return(rates_total);}
  2492. //+------------------------------------------------------------------+
RAW Paste Data Copied