1. //+------------------------------------------------------------------+
  2. //| AIBRAIN OTC v46.3 - NCP PRO v8.0 ADVANCED INTEGRATED |
  3. //| Fixed: S/R draw, CalcHA, NCP Engine, Dashboard Layout |
  4. //| Removed: HRN/SPM/Strategy cubes, Added ShortPrice, Purple Theme |
  5. //| Updated: MTF ANALYSIS replaced with PSYCHE BREAKER v3.0 |
  6. //| ALL ERRORS FIXED - CLEAN VERSION |
  7. //+------------------------------------------------------------------+
  8. #property copyright "AIBRAIN ULTIMATE - KM RANA"
  9. #property version "46.3"
  10. #property strict
  11. #property indicator_chart_window
  12.  
  13. #define PFX "AIB9_"
  14. #define VER "v46.3"
  15. #define LB 18
  16. #define MAX_LEVELS 5
  17. #define MAX_REJ 50
  18. #define LOOKBACK 50
  19.  
  20. // ============================================================
  21. // WEIGHTED PATTERN STRUCT & LOGIC (PRO FORMULA)
  22. // ============================================================
  23. struct WPattern {
  24. string sig; // CALL, PUT, 50/50, WAIT
  25. double conf; // 0 - 100
  26. bool isTrap; // Broker trap detected?
  27. string pat; // Current Pattern e.g. GGR
  28. string str; // STRONG, WEAK, MEDIUM
  29. double trapScr;// 0 - 100
  30. };
  31.  
  32. WPattern GetWeightedPattern(int pSize, int &colArr[], int totalCandles) {
  33. WPattern r;
  34. r.sig="WAIT"; r.conf=50; r.isTrap=false; r.pat=""; r.str="NONE"; r.trapScr=0;
  35. if(totalCandles < 30) return r;
  36.  
  37. r.pat = "";
  38. for(int j=0; j<pSize; j++) r.pat += (colArr[j]==1) ? "G" : "R";
  39.  
  40. int cw=0, pw=0, tw=0, cm=0, pm=0, tm=0;
  41.  
  42. for(int i=2; i<=totalCandles-pSize; i++) {
  43. string hp="";
  44. for(int j=0; j<pSize; j++) hp += (colArr[i+j]==1) ? "G" : "R";
  45.  
  46. if(hp == r.pat) {
  47. double rw = 1.0 + ((double)(totalCandles - i) / totalCandles) * 3.0;
  48. double sw = 1.0;
  49. if(i-1 >= 0) {
  50. double nb = MathAbs(Close[i-1]-Open[i-1]);
  51. double nr = High[i-1]-Low[i-1];
  52. if(nr>0) {
  53. double br = nb/nr;
  54. if(br>0.65) sw=2.0;
  55. else if(br>0.40) sw=1.5;
  56. else if(br>0.15) sw=1.0;
  57. else sw=0.5;
  58. }
  59. }
  60. double fw = rw * sw;
  61. tm++; tw += (int)fw;
  62. if(colArr[i-1]==1) { cm++; cw+=(int)fw; }
  63. else { pm++; pw+=(int)fw; }
  64. }
  65. }
  66.  
  67. if(tw > 0) {
  68. double domPct = (double)MathMax(cw, pw) / tw * 100.0;
  69. int wk=0;
  70. for(int i=0; i<MathMin(3, totalCandles); i++) {
  71. double b=MathAbs(Close[i]-Open[i]), rng=High[i]-Low[i];
  72. if(rng>0 && (b/rng)<0.35) wk++;
  73. }
  74. r.trapScr = domPct * 0.6 + ((double)wk/3.0)*100.0*0.4;
  75. if(r.trapScr > 100) r.trapScr = 100;
  76. r.isTrap = (domPct > 78.0 && wk >= 2);
  77. }
  78.  
  79. if(tm < 3) return r;
  80. double cP = (tw>0) ? ((double)cw/tw)*100.0 : 50.0;
  81. double pP = (tw>0) ? ((double)pw/tw)*100.0 : 50.0;
  82.  
  83. if(r.isTrap && cP > 80.0 && cP > pP) { r.sig="PUT"; r.conf=MathMin(95.0, cP); }
  84. else if(r.isTrap && pP > 80.0 && pP > cP) { r.sig="CALL"; r.conf=MathMin(95.0, pP); }
  85. else if(cP >= 55.0 && cP > pP) { r.sig="CALL"; r.conf=cP; }
  86. else if(pP >= 55.0 && pP > cP) { r.sig="PUT"; r.conf=pP; }
  87. else { r.sig="50/50"; r.conf=50.0; }
  88.  
  89. if(r.conf >= 85.0) r.str="V.STRONG";
  90. else if(r.conf >= 72.0) r.str="STRONG";
  91. else if(r.conf >= 60.0) r.str="MEDIUM";
  92. else r.str="WEAK";
  93.  
  94. return r;
  95. }
  96.  
  97. input string SPM_FILE = "KM RANA";
  98. input int HOLD_SEC = 60;
  99. input int POS_X = 4;
  100. input int POS_Y = 20;
  101. input int DASH_W = 660;
  102. input bool SHOW_TIMER = true;
  103. input bool SHOW_SR_LINES = true;
  104. input string ENTRY_MODE = "TIME";
  105. input int FLAG_MIN_SEC = 10;
  106. input int FLAG_MAX_SEC = 35;
  107. input int TIME_MIN_SEC = 10;
  108. input int TIME_MAX_SEC = 35;
  109. input int MIN_REMAINING_SEC = 15;
  110. input double REVERSAL_PCT = 30.0;
  111. input double WICK_PCT = 0.65;
  112. input int HRN_CONFIRM_BARS = 2;
  113. input bool SHOW_HRN_LINE = true;
  114. input bool HA_ALIGN_FILTER = false;
  115. input bool CONSENSUS_FILTER = false;
  116. input bool ENABLE_NOTIFY = true;
  117. input bool FAST_MODE = false;
  118. input int BROKER_SPREAD_THRESHOLD= 6;
  119. input double OTC_TRAP_WEIGHT = 15.0;
  120. input int CROWD_EXTREME_PCT = 70;
  121. input bool ENABLE_BROKER_KILLER = true;
  122. input bool ENABLE_SESSION_FILTER = true;
  123. input bool ENABLE_SPIKE_FILTER = true;
  124. input bool ENABLE_LAST_SEC_BLOCK = true;
  125. input bool ENABLE_RISK_CONTROL = true;
  126. input bool ENABLE_ENTRY_ZONE = true;
  127. input bool ML_ADAPTIVE = true;
  128. input bool VOLUME_PROFILE_TRAP = true;
  129. input bool MTF_CONFIRM = false;
  130. input bool ENABLE_TELEGRAM = false;
  131. input string TELEGRAM_TOKEN = "";
  132. input string TELEGRAM_CHAT_ID = "";
  133. input string TRADE_MODE = "NORMAL";
  134. input string STRATEGY_MODE = "TRAP_ONLY";
  135. input int STRATEGY_THRESHOLD = 75;
  136. input int MAX_LOSS_STREAK = 2;
  137. input double MIN_CONFIDENCE = 75.0;
  138. input bool ENABLE_RISK_LOCK = true;
  139. input double PATTERN_TOLERANCE_PIPS = 8.0;
  140. input int MIN_PATTERN_SEPARATION = 5;
  141. input bool SHOW_COMMON_POINTS = true;
  142. input double COMMON_POINT_TOL_PIPS = 15.0;
  143. input bool SHOW_WICK_REJECT_LINES = true;
  144. input int WICK_MIN_TOUCHES = 1;
  145. input double WICK_ZONE_PIPS = 15.0;
  146. input int WICK_EXPIRE_BARS = 60;
  147.  
  148. // ============================================================
  149. // BB PULLBACK SETTINGS
  150. // ============================================================
  151. input bool ENABLE_BB_PULLBACK = true;
  152. input int BB_PERIOD = 20;
  153. input double BB_DEVIATION = 2.0;
  154. input int BB_SIGNAL_EXPIRE_BARS = 5;
  155.  
  156.  
  157.  
  158. // ============================================================
  159. // ORDER BLOCK FORMULA v5.0 - SMC INSTITUTIONAL GRADE
  160. // ============================================================
  161. input string OB_SECTION = "=== ORDER BLOCK v5.0 SMC ===";
  162. input int OB_LookBack = 20;
  163. input int OB_MinScore = 65;
  164. input int OB_PremiumScore = 82;
  165. input double OB_DecayRate = 0.70;
  166. input double OB_TimeDecay = 0.04;
  167. input bool OB_EnableSessionFilter= true;
  168. input bool OB_EnableSignals = true;
  169.  
  170. // ============================================================
  171. // NCP PRO v8.0 ADVANCED SETTINGS
  172. // ============================================================
  173. input bool ENABLE_MTG = true;
  174. input int NCP_OpenNoiseSeconds = 3;
  175. input int NCP_KillZoneSeconds = 55;
  176. input double NCP_MaxSpreadPips = 4.0;
  177. input int NCP_ATRPeriod = 14;
  178. input double NCP_MinATRPips = 0.4;
  179. input int NCP_FastEMA = 3;
  180. input int NCP_MidEMA = 7;
  181. input int NCP_SlowEMA = 15;
  182. input bool NCP_UseM5Confirm = true;
  183. input bool NCP_UseVolume = true;
  184. input double NCP_RoundStepPips = 10.0;
  185. input double NCP_RoundTolPips = 4.0;
  186. input int NCP_MinProbMedium = 57;
  187. input int NCP_MinProbStrong = 63;
  188. input int NCP_MinProbExtreme = 70;
  189. input bool NCP_UseRSINorm = true;
  190. input int NCP_RSIPeriod = 10;
  191. input double NCP_RSIBase = 50.0;
  192. input double NCP_RSIM5Target = 45.0;
  193. input double NCP_RSIM15Target = 40.0;
  194. input bool ENABLE_LEARNING = true;
  195. input string MEMORY_FILE_NAME = "NCP_Brain_v8.csv";
  196. input int MIN_TRADES_TO_LEARN = 15;
  197. input double LEARNING_STEP = 0.025;
  198. input bool ENABLE_TRAP_DETECTION = true;
  199. input int TRAP_LOOKBACK = 30;
  200.  
  201. double GetUniversalPip(){
  202. double pt=MarketInfo(Symbol(),MODE_POINT);
  203. if(pt<=0) pt=0.00001;
  204. int dg=(int)MarketInfo(Symbol(),MODE_DIGITS);
  205. if(dg==6) return pt*100;
  206. if(dg==5||dg==3||dg==1) return pt*10;
  207. return pt;
  208. }
  209. double GetAutoBoxMin(){double a=iATR(Symbol(),0,14,1);double p=GetUniversalPip();return p>0?MathMax(3.0,a/p*0.3):4.0;}
  210. double GetAutoBoxMax(){double a=iATR(Symbol(),0,14,1);double p=GetUniversalPip();return p>0?MathMax(20.0,a/p*3.0):30.0;}
  211.  
  212. // ===== NEW NCP v8.0 ADVANCED SETTINGS =====
  213. input string NCP8_SECTION = "=== NCP v8.0 ADVANCED ===";
  214. input bool NCP8_UseM15Trend = true;
  215. input bool NCP8_UseDynamicSR = true;
  216. input bool NCP8_UsePatterns = true;
  217. input bool NCP8_UseMultiTF = true;
  218. input bool NCP8_UseColorPattern = true;
  219. input bool NCP8_UseHRNReject = true;
  220. input bool NCP8_UseOrderBlocks = true;
  221. input bool NCP8_UseDivergence = true;
  222. input int NCP8_MinConfidence = 65;
  223.  
  224. static bool g_m30Bull = false;
  225.  
  226. // ============================================================
  227. // PSYCHE BREAKER CONSTANTS
  228. // ============================================================
  229. #define PSYCHE_DOJI_THRESHOLD 0.10
  230. #define PSYCHE_WICK_THRESHOLD 0.55
  231. #define PSYCHE_WICK_CANDLES 12
  232. #define PSYCHE_VOL_SPIKE_MULT 3.0
  233. #define PSYCHE_SR_CLOSE_PIPS 3.0
  234. #define PSYCHE_SR_NEAR_PIPS 8.0
  235. #define PSYCHE_SR_FAR_PIPS 15.0
  236. #define PSYCHE_STRONG_THRESHOLD 80
  237.  
  238. #define NEON_GREEN C'0,255,100'
  239. #define NEON_RED C'255,40,80'
  240. #define NEON_YELLOW C'255,220,0'
  241. #define NEON_ORANGE C'255,120,0'
  242. #define NEON_CYAN C'0,255,200'
  243. #define NEON_PURPLE C'200,0,255'
  244. #define NEON_PINK C'255,80,180'
  245. #define NEON_BLUE C'0,200,255'
  246. #define NEON_LIME C'80,255,80'
  247. #define NEON_GOLD C'255,180,0'
  248. #define DARK_BLUE_NEON C'0,100,255'
  249. #define NEON_WHITE C'220,230,255'
  250. #define CGR C'160,170,190'
  251. #define BB_GREY C'180,180,190'
  252. #define BG_DARK1 C'6,8,16'
  253. #define BG_DARK2 C'10,14,24'
  254. #define BG_DARK3 C'14,20,32'
  255. #define BG_DARK4 C'18,26,40'
  256.  
  257. struct HTFLevel{ double price; string timeframe; string type; datetime time; int strength; bool isRoundNumber; };
  258.  
  259. struct NCPPatternResult {
  260. string name;
  261. int type;
  262. int strength;
  263. string category;
  264. };
  265.  
  266. struct NCPSRZone {
  267. double price;
  268. string type;
  269. int touches;
  270. int strength;
  271. bool isRoundNum;
  272. bool isOrderBlock;
  273. datetime lastTouch;
  274. };
  275.  
  276. struct NCPTrendInfo {
  277. string m15Direction;
  278. int m15Strength;
  279. int m1Bias;
  280. string m5Direction;
  281. int m5Strength;
  282. bool aligned;
  283. };
  284.  
  285.  
  286.  
  287. struct NCPMultiTF {
  288. double rsi_m1;
  289. double rsi_m5;
  290. double rsi_m15;
  291. int cci_m1;
  292. int cci_m5;
  293. int macd_m1_dir;
  294. int macd_m5_dir;
  295. double atr_m1;
  296. double atr_m5;
  297. bool ha_m1_bull;
  298. bool ha_m1_bear;
  299. bool ha_m5_bull;
  300. bool ha_m5_bear;
  301. int bullCount;
  302. int bearCount;
  303. };
  304.  
  305. //+------------------------------------------------------------------+
  306. // GLOBAL VARIABLES
  307. //+------------------------------------------------------------------+
  308. string g_haM1="",g_haM5="",g_haBoth="";
  309. color g_haM1Color=clrGray,g_haM5Color=clrGray;
  310. static double NW[36];
  311. static double g_rsc=0,g_tfa=3;
  312. static datetime g_spm_t=0,g_last_bar=0;
  313. static double g_nearest_res=0,g_nearest_sup=0;
  314. static string g_brk_str="NO BRK";
  315. static color g_brk_color=CGR;
  316. struct RejLevel{double price;int touches;};
  317. static RejLevel g_rj[MAX_REJ];
  318. static int g_rj_cnt=0;
  319.  
  320. static double g_hrn_price=0;
  321. static bool g_hrn_is_sup=true;
  322. static int g_hrn_brk_bars=0;
  323. static datetime g_hrn_scan_bar=0;
  324. static string g_hrn_str="--";
  325. static double g_hrn_score=0;
  326. static bool g_hrn_confirmed_break=false;
  327.  
  328. static int g_otcCallPct=50,g_otcPutPct=50;
  329. static string g_pairNames[8],g_pairSigs[8];
  330. static double g_pairConfs[8];
  331. static int g_pairCount=0;
  332. static double g_frozen_gProb=50,g_frozen_rProb=50;
  333. static string g_frozen_pAction="UNCERTAIN";
  334. static color g_frozen_pColor=NEON_YELLOW;
  335. static double g_calc_gProb=50,g_calc_rProb=50;
  336. static string g_calc_pAction="UNCERTAIN";
  337. static color g_calc_pColor=NEON_YELLOW;
  338. static double g_gProb=50,g_rProb=50;
  339. static string g_pAction="UNCERTAIN";
  340. static color g_pColor=NEON_YELLOW;
  341. static datetime g_last_freeze_bar=0;
  342. static double g_accuracy=65.0;
  343. static int g_acc_correct=0,g_acc_total=0;
  344. static datetime g_acc_lastBar=0;
  345. static double g_lastPredGreen=50.0;
  346. static datetime g_lastPredBar=0;
  347. static string g_finalSignal="WAIT";
  348. static color g_finalColor=NEON_YELLOW;
  349. static datetime g_signalTime=0;
  350. static string g_lastNotified="";
  351. static string g_marketMode="RANGE",g_prevMode="";
  352. static int g_lossStreak=0;
  353. static bool g_tradingStopped=false;
  354. static string g_strategyType="NONE";
  355. static string g_strategyReason="";
  356. static double qmrA=0,qmrB=0,qmrC=0,qmrD=0;
  357. static bool qmrActive=false;
  358. static datetime qmrExpire=0;
  359. #define MAX_COMMON_PTS 5
  360. static double g_commonPrice[MAX_COMMON_PTS];
  361. static datetime g_commonTime[MAX_COMMON_PTS];
  362. static int g_commonCount=0;
  363. static datetime g_commonExpire[MAX_COMMON_PTS];
  364. static string g_commonType[MAX_COMMON_PTS];
  365. static datetime g_lastCommonScan=0;
  366. #define MAX_WICK_LINES 5
  367. static double g_wickLinePrice[MAX_WICK_LINES];
  368. static int g_wickLineTouches[MAX_WICK_LINES];
  369. static datetime g_wickLineExpire[MAX_WICK_LINES];
  370. static int g_wickLineCount=0;
  371. static datetime g_lastWickScan=0;
  372. static bool g_isSideways=false;
  373. static string g_tfa_detail="";
  374. static HTFLevel g_htfLevels[50];
  375. static int g_htfLevelCount=0;
  376. static bool g_mtfConfirmed=true;
  377. static string g_filterStatus="ALL CLEAR";
  378. static string g_adv1="WAIT KAR";
  379. static string g_symbolKey="";
  380. static double g_brokerBias=0.0;
  381.  
  382. double g_stableRes[10];
  383. double g_stableSup[10];
  384. int g_stableResCount = 0;
  385. int g_stableSupCount = 0;
  386. datetime g_lastSRScan = 0;
  387. int g_srScanInterval = 5;
  388.  
  389. static double g_fibBestScore = 0;
  390. static double g_fibBestProb = 0;
  391. static string g_fibBestDir = "WAIT";
  392. static int g_fibConfidence = 0;
  393. static double g_fibBestPrice = 0;
  394. static string g_fibPattern = "";
  395. static string g_fibFibLevel = "";
  396. static string g_bbSignal = "--";
  397. static color g_bbColor = CGR;
  398. static double g_bbTouchPrice = 0.0;
  399. static datetime g_bbSignalTime = 0;
  400. static int g_bbSignalBars = 0;
  401. static string g_bbDetail = "";
  402.  
  403. static double g_ha30_open = 0.0;
  404. static double g_ha30_close = 0.0;
  405. static double g_ha30_high = 0.0;
  406. static double g_ha30_low = 0.0;
  407. static datetime g_ha30_bar = 0;
  408.  
  409. // === OTC FIB v3.0 NEW GLOBALS ===
  410. static double g_otcFibPrice = 0;
  411. static string g_otcFibDir = "WAIT";
  412. static int g_otcFibStrength = 0;
  413. static string g_otcFibPattern = "--";
  414. static string g_otcFibLevel = "--";
  415. static datetime g_otcFibSignalTime = 0;
  416. static bool g_otcHasSignal = false;
  417. double g_gannBoxHigh = 0;
  418. double g_gannBoxLow = 0;
  419.  
  420. static int g_buyCountCache = 0;
  421. static int g_sellCountCache = 0;
  422. static datetime g_cacheConfluenceTime = 0;
  423.  
  424.  
  425.  
  426. static int g_otcPreCloseWindow = 20; // 3 se 20 seconds ki window // 3 seconds before close
  427.  
  428.  
  429.  
  430. static datetime g_last_m5_bar = 0; // FOR STABLE M5 S/R
  431.  
  432. #define MAX_BB_LINES 8
  433. static double g_bbLinePrice[MAX_BB_LINES];
  434. static datetime g_bbLineExpire[MAX_BB_LINES];
  435. static string g_bbLineType[MAX_BB_LINES];
  436. static int g_bbLineCount=0;
  437.  
  438. // NCP PRO v8.0 GLOBALS
  439. static string g_mtgState = "INIT";
  440. static string g_mtgReason = "";
  441. static string g_mtgAction = "";
  442. static color g_mtgClr = CGR;
  443. static double g_mtgHype = 50.0;
  444. static double g_mtgBetrayal = 50.0;
  445. static string g_mtgPattern = "";
  446. static int g_mtgBullCount = 0;
  447. static int g_mtgBearCount = 0;
  448. static double g_mtgRecovery = 0.0;
  449. static datetime g_mtg_lastBar = 0;
  450. static double g_trapScore = 0;
  451. static double g_ncpADX = 0.0;
  452. static double g_ncpPlusDI = 0.0;
  453. static double g_ncpMinusDI = 0.0;
  454.  
  455. static double g_callWeight = 1.0;
  456. static double g_putWeight = 1.0;
  457. static int g_totalTrades = 0;
  458. static int g_callTrades = 0;
  459. static int g_putTrades = 0;
  460. static int g_callWins = 0;
  461. static int g_putWins = 0;
  462. static datetime g_ncpLastSignalTime = 0;
  463. static string g_ncpLastSignalType = "";
  464. static double g_ncpLastEntryPrice = 0.0;
  465. static bool g_ncpSignalProcessed = true;
  466. static double g_smoothCallScore = 50.0;
  467. static double g_smoothPutScore = 50.0;
  468.  
  469. static NCPTrendInfo g_ncpTrend;
  470. static NCPMultiTF g_ncpMultiTF;
  471. static NCPSRZone g_ncpSRZones[20];
  472. static int g_ncpSRCount = 0;
  473. static string g_ncpDetailLine1 = "";
  474. static string g_ncpDetailLine2 = "";
  475. static string g_ncpDetailLine3 = "";
  476. static int g_ncpPatternBullScore = 0;
  477. static int g_ncpPatternBearScore = 0;
  478. static string g_ncpMainPattern = "";
  479.  
  480. // ============================================================
  481. // OTC NEXT CANDLE PREDICTOR (REPLACES PSYCHE BREAKER)
  482. // ============================================================
  483. static string g_otpSignal = "WAIT";
  484. static double g_otpConf = 50.0;
  485. static string g_otpRsi = "--";
  486. static string g_otpWick = "--";
  487. static string g_otpReason = "";
  488. static color g_otpColor = NEON_YELLOW;
  489.  
  490. // ============================================================
  491. // MHI-QUANTUM v9.1 PRO GLOBALS
  492. // ============================================================
  493. static string g_qSignal = "WAIT";
  494. static double g_qConf = 50.0;
  495. static string g_qPattern = "---";
  496. static string g_qStatus = "SCANNING";
  497. static color g_qColor = NEON_YELLOW;
  498. static string g_qReason = "";
  499. static bool g_qIsTrap = false;
  500. static double g_qCallScore = 50.0;
  501. static double g_qPutScore = 50.0;
  502. static string g_qConfluence = "";
  503. static int g_qHistoryScore = 0;
  504. static double g_qTimeProb = 50.0;
  505. static double g_qSpreadPenalty = 0.0;
  506.  
  507. // Auto-learning globals
  508. static double g_qWeightMHI = 0.25;
  509. static double g_qWeightNCP = 0.20;
  510. static double g_qWeightTrap = 0.15;
  511. static double g_qWeightFib = 0.15;
  512. static double g_qWeightBB = 0.10;
  513. static double g_qWeightNeural = 0.10;
  514. static double g_qWeightHA = 0.05;
  515. static int g_qTotalTrades = 0;
  516. static int g_qWinTrades = 0;
  517. static string g_qLearnFile = "Quantum_Learn_v91.dat";
  518.  
  519. // 150-candle history
  520. static int g_histPatterns[150];
  521. static int g_histResults[150];
  522. static int g_histCount = 0;
  523.  
  524. // Multi-pair MHI
  525. static string g_bgMHI_Signal[8];
  526. static double g_bgMHI_Conf[8];
  527. static int g_bgMHI_Count = 0;
  528.  
  529. static string g_bgMHI_Pair[8];
  530.  
  531.  
  532. // Time phase
  533. static string g_qTimePhase = "EARLY";
  534.  
  535. // Tracking
  536. static string g_qLastSignal = "";
  537. static datetime g_qLastSignalBar = 0;
  538.  
  539.  
  540. // BACKGROUND PAIRS NEXT CANDLE DATA
  541. static string g_bgOtpSignal[8];
  542. static int g_bgOtpCount = 0;
  543. static string g_bgOB_Pair[8];
  544. static string g_bgOB_Signal[8];
  545. static int g_bgOB_Count = 0;
  546.  
  547. // ============================================================
  548. // PSYCHE BREAKER v3.0 GLOBALS
  549. // ============================================================
  550. static double g_psycheScore = 0;
  551. static string g_psycheDir = "NONE";
  552. static string g_psycheWick = "--";
  553. static string g_psycheVol = "--";
  554. static string g_psycheHA = "--";
  555. static string g_psycheSR = "--";
  556. static string g_psycheSignal = "SCANNING";
  557. static color g_psycheClr = CGR;
  558. static int g_psycheUpperWick= 0;
  559. static int g_psycheLowerWick= 0;
  560.  
  561. // ============================================================
  562. // OTC BROKER KILLER v4.0 - PROFESSIONAL TRAP ENGINE
  563. // ============================================================
  564. struct VisualTrapPro {
  565. string boxStatus, gannStatus, m30Dir, m1Seq, verdict;
  566. color gannColor, verdictColor;
  567. double callBias, putBias, trapScore;
  568. bool buySignal, sellSignal;
  569. };
  570. VisualTrapPro g_visualTrapPro;
  571. static datetime g_lastTrapArrowTime = 0;
  572.  
  573. //+------------------------------------------------------------------+
  574. // SHORT PRICE FUNCTION - For chart S/R labels
  575. //+------------------------------------------------------------------+
  576. string ShortPrice(double price){
  577. int dg = (int)MarketInfo(Symbol(), MODE_DIGITS);
  578. if(dg < 2) dg = 5;
  579. return DoubleToString(price, dg);
  580. }
  581. string SRPrice(double price){ return ShortPrice(price); }
  582.  
  583. //+------------------------------------------------------------------+
  584. // DYNAMIC ADX COLOR FUNCTION
  585. //+------------------------------------------------------------------+
  586. color GetADXColor(double adx, double plusDI, double minusDI){
  587. if(adx < 15) return CGR;
  588. if(adx < 22) return NEON_ORANGE;
  589. double diDiff = plusDI - minusDI;
  590. if(diDiff > 10) return NEON_GREEN;
  591. if(diDiff > 5) return NEON_LIME;
  592. if(diDiff < -10) return NEON_RED;
  593. if(diDiff < -5) return C'255,100,100';
  594. return NEON_YELLOW;
  595. }
  596.  
  597. //+------------------------------------------------------------------+
  598. // M30 HA CANDLE (FIXED NAME)
  599. //+------------------------------------------------------------------+
  600. void CalcM30HACandle(){
  601. if(Bars < 5) return;
  602. if(iBars(NULL, PERIOD_M30) >= 2){
  603. g_ha30_open = iOpen(NULL, PERIOD_M30, 1);
  604. g_ha30_close = iClose(NULL, PERIOD_M30, 1);
  605. g_ha30_high = iHigh(NULL, PERIOD_M30, 1);
  606. g_ha30_low = iLow(NULL, PERIOD_M30, 1);
  607. g_ha30_bar = iTime(NULL, PERIOD_M30, 1);
  608. }
  609. else {
  610. g_ha30_open = Open[0];
  611. g_ha30_close = Close[0];
  612. g_ha30_high = High[0];
  613. g_ha30_low = Low[0];
  614. g_ha30_bar = Time[0];
  615. }
  616. }
  617.  
  618. bool IsM30HABull(){ return (g_ha30_close > g_ha30_open); }
  619. bool IsM30HABear(){ return (g_ha30_close < g_ha30_open); }
  620.  
  621. //+------------------------------------------------------------------+
  622. // UPGRADED BB PULLBACK - M30 HA PHOOLBAG ENGINE (FIXED REFS)
  623. //+------------------------------------------------------------------+
  624. void DetectBBPullback(){
  625. if(!ENABLE_BB_PULLBACK || Bars < BB_PERIOD + 5) return;
  626. CalcM30HACandle();
  627. double bbUpper0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_UPPER, 0);
  628. double bbLower0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_LOWER, 0);
  629. double bbMid0 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_MAIN, 0);
  630. double bbUpper1 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_UPPER, 1);
  631. double bbLower1 = iBands(NULL, PERIOD_M1, BB_PERIOD, BB_DEVIATION, 0, PRICE_CLOSE, MODE_LOWER, 1);
  632. if(bbUpper1 <= 0 || bbLower1 <= 0) return;
  633.  
  634. bool isHaM30Bull = IsM30HABull();
  635. bool isHaM30Bear = IsM30HABear();
  636. bool topPhoolbag = (g_ha30_high >= bbUpper0 * 0.9998) && (g_ha30_close < bbUpper0) && isHaM30Bear;
  637. bool bottomPhoolbag = (g_ha30_low <= bbLower0 * 1.0002) && (g_ha30_close > bbLower0) && isHaM30Bull;
  638.  
  639. double adx = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  640. double plusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  641. double minusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  642. double rsi = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  643. bool adxValid = (adx >= 18);
  644. bool diBull = (plusDI > minusDI);
  645. bool diBear = (minusDI > plusDI);
  646.  
  647. double ha30Range = g_ha30_high - g_ha30_low;
  648. double topWick = (ha30Range > 0) ? (g_ha30_high - MathMax(g_ha30_open, g_ha30_close)) / ha30Range : 0;
  649. double bottomWick = (ha30Range > 0) ? (MathMin(g_ha30_open, g_ha30_close) - g_ha30_low) / ha30Range : 0;
  650.  
  651. bool isStrongTopPhoolbag = topPhoolbag && (topWick > 0.50);
  652. bool isStrongBottomPhoolbag = bottomPhoolbag && (bottomWick > 0.50);
  653.  
  654. int putConfirm = 0;
  655. if(isStrongTopPhoolbag) putConfirm += 3;
  656. else if(topPhoolbag) putConfirm += 2;
  657. if(diBear) putConfirm++;
  658. if(rsi > 55) putConfirm++;
  659. if(rsi > 65) putConfirm++;
  660.  
  661. int callConfirm = 0;
  662. if(isStrongBottomPhoolbag) callConfirm += 3;
  663. else if(bottomPhoolbag) callConfirm += 2;
  664. if(diBull) callConfirm++;
  665. if(rsi < 45) callConfirm++;
  666. if(rsi < 35) callConfirm++;
  667.  
  668. if(!adxValid) { putConfirm = 0; callConfirm = 0; }
  669.  
  670. string adxStr = "ADX:"+DoubleToString(adx,0)+(adxValid?"*":"");
  671. string rsiStr = "RSI:"+DoubleToString(rsi,0);
  672. string diStr = (diBull?"DI^":diBear?"DIv":"DI=");
  673. string haStr = isHaM30Bull?"HA30:BULL":isHaM30Bear?"HA30:BEAR":"HA30:DOJI";
  674.  
  675. if(g_bbSignal != "--" && g_bbSignalTime > 0){
  676. int barsElapsed = (int)((TimeCurrent() - g_bbSignalTime) / (Period() * 60));
  677. if(barsElapsed > BB_SIGNAL_EXPIRE_BARS){
  678. g_bbSignal = "--"; g_bbColor = NEON_YELLOW;
  679. g_bbDetail = ""; g_bbTouchPrice = 0.0;
  680. }
  681. }
  682.  
  683. string newSignal = "--";
  684. double touchPriceNew = 0.0;
  685. string detailNew = "";
  686. color newColor = NEON_YELLOW;
  687.  
  688. if(putConfirm >= 2){
  689. newSignal = "PUT";
  690. touchPriceNew = bbUpper0;
  691. string conf = (putConfirm>=4)?"STRONG PB":(putConfirm>=3)?"GOOD PB":"WEAK PB";
  692. detailNew = "PB UP|"+conf+"|"+adxStr+"|"+rsiStr+"|"+diStr+"|"+haStr;
  693. newColor = (putConfirm >= 3) ? NEON_RED : NEON_ORANGE;
  694. }
  695. else if(callConfirm >= 2){
  696. newSignal = "CALL";
  697. touchPriceNew = bbLower0;
  698. string conf = (callConfirm>=4)?"STRONG PB":(callConfirm>=3)?"GOOD PB":"WEAK PB";
  699. detailNew = "PB LO|"+conf+"|"+adxStr+"|"+rsiStr+"|"+diStr+"|"+haStr;
  700. newColor = (callConfirm >= 3) ? NEON_GREEN : NEON_CYAN;
  701. }
  702.  
  703. if(newSignal != "--"){
  704. bool isNew = (newSignal != g_bbSignal || touchPriceNew != g_bbTouchPrice);
  705. if(isNew){
  706. g_bbSignal = newSignal;
  707. g_bbTouchPrice = touchPriceNew;
  708. g_bbSignalTime = TimeCurrent();
  709. g_bbDetail = detailNew;
  710. g_bbColor = newColor;
  711. AddBBLine(touchPriceNew, newSignal);
  712. Print("BB PHOOLBAG: ",newSignal," | ",detailNew," Conf:",(newSignal=="PUT"?putConfirm:callConfirm),"/5");
  713. }
  714. } else if(g_bbSignal == "--"){
  715. g_bbColor = NEON_YELLOW;
  716. }
  717. DrawBBLines(bbUpper0, bbLower0, bbMid0);
  718. }
  719.  
  720. void DrawBBLines(double upper, double lower, double mid){
  721. if(!ENABLE_BB_PULLBACK) return;
  722. string nmU = PFX+"BB_UPPER"; SafeDel(nmU);
  723. if(upper > 0){ ObjectCreate(0, nmU, OBJ_HLINE, 0, 0, upper); ObjectSetInteger(0, nmU, OBJPROP_COLOR, BB_GREY); ObjectSetInteger(0, nmU, OBJPROP_WIDTH, 1); ObjectSetInteger(0, nmU, OBJPROP_STYLE, STYLE_DASH); ObjectSetInteger(0, nmU, OBJPROP_BACK, false); }
  724. string nmL = PFX+"BB_LOWER"; SafeDel(nmL);
  725. if(lower > 0){ ObjectCreate(0, nmL, OBJ_HLINE, 0, 0, lower); ObjectSetInteger(0, nmL, OBJPROP_COLOR, BB_GREY); ObjectSetInteger(0, nmL, OBJPROP_WIDTH, 1); ObjectSetInteger(0, nmL, OBJPROP_STYLE, STYLE_DASH); ObjectSetInteger(0, nmL, OBJPROP_BACK, false); }
  726. string nmM = PFX+"BB_MID"; SafeDel(nmM);
  727. if(mid > 0){ ObjectCreate(0, nmM, OBJ_HLINE, 0, 0, mid); ObjectSetInteger(0, nmM, OBJPROP_COLOR, C'100,100,110'); ObjectSetInteger(0, nmM, OBJPROP_WIDTH, 1); ObjectSetInteger(0, nmM, OBJPROP_STYLE, STYLE_DOT); ObjectSetInteger(0, nmM, OBJPROP_BACK, false); }
  728. int dg = (int)MarketInfo(Symbol(), MODE_DIGITS); if(dg <= 0) dg = 5;
  729. string lblU = PFX+"BB_U_LBL"; SafeDel(lblU);
  730. if(upper > 0 && Bars > 2){ ObjectCreate(0, lblU, OBJ_TEXT, 0, Time[2], upper); ObjectSetText(lblU, "BB("+IntegerToString(BB_PERIOD)+")", 8, "Arial", BB_GREY); ObjectSetInteger(0, lblU, OBJPROP_BACK, false); }
  731. }
  732.  
  733. void AddBBLine(double price, string sigType){
  734. if(price <= 0) return;
  735. for(int i = 0; i < MAX_BB_LINES; i++){ SafeDel(PFX+"BB_TOUCH_"+IntegerToString(i)); SafeDel(PFX+"BB_TOUCH_LBL_"+IntegerToString(i)); }
  736. g_bbLineCount = 0; g_bbLinePrice[0] = price; g_bbLineExpire[0] = TimeCurrent() + 60 * BB_SIGNAL_EXPIRE_BARS; g_bbLineType[0] = sigType; g_bbLineCount = 1;
  737. DrawAllBBTouchLines();
  738. }
  739.  
  740. void DrawAllBBTouchLines(){
  741. for(int i = 0; i < MAX_BB_LINES; i++){ SafeDel(PFX+"BB_TOUCH_"+IntegerToString(i)); SafeDel(PFX+"BB_TOUCH_LBL_"+IntegerToString(i)); }
  742. if(g_bbLineCount > 0 && TimeCurrent() > g_bbLineExpire[0]){ g_bbLineCount = 0; return; }
  743. if(g_bbLineCount <= 0) return;
  744. string nm = PFX+"BB_TOUCH_0";
  745. string lbl = PFX+"BB_TOUCH_LBL_0";
  746. color lineColor = (g_bbLineType[0]=="CALL") ? NEON_GREEN : NEON_RED;
  747. ObjectCreate(0, nm, OBJ_HLINE, 0, 0, g_bbLinePrice[0]);
  748. ObjectSetInteger(0, nm, OBJPROP_COLOR, lineColor);
  749. ObjectSetInteger(0, nm, OBJPROP_WIDTH, 2);
  750. ObjectSetInteger(0, nm, OBJPROP_STYLE, STYLE_SOLID);
  751. ObjectSetInteger(0, nm, OBJPROP_BACK, false);
  752. if(Bars > 3){
  753. string txt = (g_bbLineType[0]=="CALL") ? "PB^ CALL" : "PBv PUT";
  754. ObjectCreate(0, lbl, OBJ_TEXT, 0, Time[3], g_bbLinePrice[0]);
  755. ObjectSetText(lbl, txt, 8, "Arial Bold", lineColor);
  756. ObjectSetInteger(0, lbl, OBJPROP_BACK, false);
  757. }
  758. }
  759.  
  760. // ============================================================
  761. // BROKER TRAP CALCULATOR
  762. // ============================================================
  763. double CalculateBrokerTrap(double callBias,double putBias){
  764. if(!ENABLE_TRAP_DETECTION||Bars<20) return 0;
  765. double trapScore=0;
  766. bool jpy=(StringFind(Symbol(),"JPY")>=0);
  767. double pip=jpy?0.01:0.0001;
  768. for(int i=1;i<=15&&i<Bars-1;i++){
  769. bool bullEngulf=(Close[i]>Open[i])&&(Close[i-1]<Open[i-1])&&(Close[i]>=Open[i-1])&&(Open[i]<=Close[i-1]);
  770. bool bearEngulf=(Close[i]<Open[i])&&(Close[i-1]>Open[i-1])&&(Close[i]<=Open[i-1])&&(Open[i]>=Close[i-1]);
  771. if(i>=2){
  772. bool mornStar=(Close[i-2]<Open[i-2])&&(MathAbs(Close[i-1]-Open[i-1])<pip*3)&&(Close[i]>Open[i]);
  773. bool eveStar =(Close[i-2]>Open[i-2])&&(MathAbs(Close[i-1]-Open[i-1])<pip*3)&&(Close[i]<Open[i]);
  774. if(i>=3){
  775. if(bullEngulf&&Close[i-3]<Close[i-2]) trapScore+=3.5;
  776. if(bearEngulf&&Close[i-3]>Close[i-2]) trapScore+=3.5;
  777. if(mornStar&&Close[i-3]<Close[i-2]) trapScore+=3.0;
  778. if(eveStar&&Close[i-3]>Close[i-2]) trapScore+=3.0;
  779. }
  780. }
  781. double rng=High[i]-Low[i];
  782. if(rng<=0) continue;
  783. double uw=(High[i]-MathMax(Open[i],Close[i]))/rng;
  784. double lw=(MathMin(Open[i],Close[i])-Low[i])/rng;
  785. if(uw>0.55&&Close[i]<Open[i]) trapScore+=2.0;
  786. if(lw>0.55&&Close[i]>Open[i]) trapScore+=2.0;
  787. }
  788. double rng0=High[0]-Low[0];
  789. if(rng0>0){
  790. double uw0=(High[0]-MathMax(Open[0],Close[0]))/rng0;
  791. double lw0=(MathMin(Open[0],Close[0])-Low[0])/rng0;
  792. if(uw0>0.65&&Close[0]<Open[0]) trapScore+=4.0;
  793. if(lw0>0.65&&Close[0]>Open[0]) trapScore+=4.0;
  794. if(Close[0]>Open[0]&&Close[1]<Open[1]){
  795. if(Close[0]>Open[1]&&Open[0]<Close[1]) trapScore+=3.0;
  796. }
  797. if(Close[0]<Open[0]&&Close[1]>Open[1]){
  798. if(Close[0]<Open[1]&&Open[0]>Close[1]) trapScore+=3.0;
  799. }
  800. }
  801. double biasStr=MathMax(callBias,putBias)-50;
  802. double biasMul=1.0+biasStr/150.0;
  803. if(biasMul>1.5) biasMul=1.5;
  804. if(biasMul<0.5) biasMul=0.5;
  805. trapScore*=biasMul;
  806. return MathMin(100,trapScore);
  807. }
  808.  
  809. // ============================================================
  810. // HA CANDLE CALCULATION (FIXED ARRAY BOUNDS)
  811. // ============================================================
  812. void CalcHA(){
  813. // ✅ FIX: ArraySetAsSeries REMOVED - Ye global arrays corrupt karta tha!
  814. if(Bars<10) return;
  815.  
  816. static double haO1[500], haC1[500];
  817. static datetime lb1=0;
  818. ArraySetAsSeries(haO1,true); ArraySetAsSeries(haC1,true);
  819. datetime c1=iTime(NULL,PERIOD_M1,0);
  820. if(c1!=lb1){
  821. lb1=c1;
  822. int lim=MathMin(iBars(NULL,PERIOD_M1), 499); // FIX: Use 499
  823. for(int i=lim; i>=0; i--){ // FIX: Start from lim
  824. haC1[i] = (Open[i]+High[i]+Low[i]+Close[i])/4.0;
  825. if(i==lim) haO1[i] = (Open[i]+Close[i])/2.0; // FIX: Use lim
  826. else haO1[i] = (haO1[i+1] + haC1[i+1])/2.0;
  827. }
  828. }
  829.  
  830. double d1=MathAbs(haC1[1]-haO1[1]), r1=High[1]-Low[1];
  831. bool dz=(r1>0 && d1<r1*0.1), b1=(!dz && haC1[1]>haO1[1]), be1=(!dz && haC1[1]<haO1[1]);
  832. if(b1){ g_haM1="HA BULLISH 1"; g_haM1Color=clrLime; }
  833. else if(be1){ g_haM1="HA BEARISH 1"; g_haM1Color=clrRed; }
  834. else{ g_haM1="HA DOJI 1"; g_haM1Color=clrYellow; }
  835.  
  836. if(iBars(NULL,PERIOD_M5)>=10){
  837. static double haO5[200], haC5[200];
  838. static datetime lb5=0;
  839. ArraySetAsSeries(haO5,true); ArraySetAsSeries(haC5,true);
  840. datetime c5=iTime(NULL,PERIOD_M5,0);
  841. if(c5!=lb5){
  842. lb5=c5;
  843. int lim5=MathMin(iBars(NULL,PERIOD_M5), 199); // FIX: Use 199
  844. for(int i=lim5; i>=0; i--){ // FIX: Start from lim5
  845. double o=iOpen(NULL,PERIOD_M5,i), h=iHigh(NULL,PERIOD_M5,i),
  846. l=iLow(NULL,PERIOD_M5,i), c=iClose(NULL,PERIOD_M5,i);
  847. haC5[i]=(o+h+l+c)/4.0;
  848. if(i==lim5) haO5[i]=(o+c)/2.0; // FIX: Use lim5
  849. else haO5[i]=(haO5[i+1]+haC5[i+1])/2.0;
  850. }
  851. }
  852. double d5=MathAbs(haC5[1]-haO5[1]), r5=iHigh(NULL,PERIOD_M5,1)-iLow(NULL,PERIOD_M5,1);
  853. bool dz5=(r5>0 && d5<r5*0.1), b5=(!dz5 && haC5[1]>haO5[1]), be5=(!dz5 && haC5[1]<haO5[1]);
  854. if(b5){ g_haM5="HA BULLISH 5"; g_haM5Color=clrLime; }
  855. else if(be5){ g_haM5="HA BEARISH 5"; g_haM5Color=clrRed; }
  856. else{ g_haM5="HA DOJI 5"; g_haM5Color=clrYellow; }
  857. if(b1&&b5) g_haBoth="BOTH BULL";
  858. else if(be1&&be5) g_haBoth="BOTH BEAR";
  859. else g_haBoth="MIXED";
  860. } else {
  861. g_haM5="5 --"; g_haM5Color=clrGray; g_haBoth="HA --";
  862. }
  863.  
  864. // ✅ FIX: Array restore removed - MQL4 mein default series hi hoti hai
  865. }
  866.  
  867. // ============================================================
  868. // HELPERS
  869. // ============================================================
  870. bool IsLineNearby(double price,double pipTol=3.0){
  871. bool jpy=(StringFind(Symbol(),"JPY")>=0); double pip=jpy?0.01:0.0001; double tol=pipTol*pip;
  872. for(int i=ObjectsTotal()-1;i>=0;i--){ string nm=ObjectName(i);
  873. if((int)ObjectGetInteger(0,nm,OBJPROP_TYPE)==OBJ_HLINE){
  874. double lp=ObjectGetDouble(0,nm,OBJPROP_PRICE); if(MathAbs(lp-price)<tol) return true; }}
  875. return false;
  876. }
  877.  
  878. string GetCurrentSession(color &sC){
  879. int h=TimeHour(TimeGMT());
  880. if(h>=12&&h<16){sC=NEON_GOLD;return "LON+NY";}
  881. if(h>=7&&h<9){sC=NEON_CYAN;return "ASI+LON";}
  882. if(h>=7&&h<16){sC=NEON_GREEN;return "LONDON";}
  883. if(h>=12&&h<21){sC=NEON_BLUE;return "NEW YORK";}
  884. if(h>=0&&h<9){sC=NEON_ORANGE;return "ASIA";}
  885. sC=C'100,100,120'; return "SYDNEY";
  886. }
  887.  
  888. bool IsSessionActive(){int h=TimeHour(TimeGMT());return(h>=7&&h<=21);}
  889. 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);}
  890.  
  891. bool CheckMTFConfirmation(){
  892. if(!MTF_CONFIRM) return true;
  893. bool m1Bull=(Close[1]>Open[1]);
  894. bool m5Bull=false,m15Bull=false;
  895. if(iBars(NULL,PERIOD_M5)>=3) m5Bull=(iClose(NULL,PERIOD_M5,1)>iOpen(NULL,PERIOD_M5,1));
  896. if(iBars(NULL,PERIOD_M15)>=3) m15Bull=(iClose(NULL,PERIOD_M15,1)>iOpen(NULL,PERIOD_M15,1));
  897. int bullCount=0,bearCount=0;
  898. if(m1Bull) bullCount++; else bearCount++;
  899. if(m5Bull) bullCount++; else bearCount++;
  900. if(m15Bull) bullCount++; else bearCount++;
  901. g_mtfConfirmed=(bullCount>=2 || bearCount>=2);
  902. return g_mtfConfirmed;
  903. }
  904.  
  905. double Tanh(double x){double e=MathExp(2.0*x);return(e-1.0)/(e+1.0);}
  906.  
  907. double CalcAdvancedMicroAI(){
  908. if(Bars<20) return 50.0;
  909. double score=50.0;
  910. bool jpy=(StringFind(Symbol(),"JPY")>=0);
  911. double pip=jpy?0.01:0.0001;
  912. for(int i=1;i<=5;i++){
  913. double r=High[i]-Low[i]; if(r<=0) continue;
  914. double uw=(High[i]-MathMax(Open[i],Close[i]))/r;
  915. double lw=(MathMin(Open[i],Close[i])-Low[i])/r;
  916. double w=(6.0-i)/5.0;
  917. if(uw>0.65) score-=w*18; else if(uw>0.50) score-=w*10;
  918. if(lw>0.65) score+=w*18; else if(lw>0.50) score+=w*10;
  919. }
  920. double pos5=0;
  921. for(int i=1;i<=5;i++){double r=High[i]-Low[i]; if(r<=0) continue; pos5+=(Close[i]-Low[i])/r; }
  922. score+=(pos5/5.0-0.5)*25.0;
  923. double gc=0,rc=0;
  924. for(int i=1;i<=8;i++){double w=(9.0-i)/8.0; if(Close[i]>Open[i]) gc+=w; else if(Close[i]<Open[i]) rc+=w;}
  925. score+=(gc-rc)*4.0;
  926. double vl1=(double)iVolume(NULL,PERIOD_M1,1),vA=0;
  927. for(int i=2;i<=6;i++) vA+=iVolume(NULL,PERIOD_M1,i);
  928. vA/=5.0;
  929. if(vA>0){double vr=vl1/vA; if(vr>2.0&&Close[1]>Open[1]) score+=12; else if(vr>2.0&&Close[1]<Open[1]) score-=12; else if(vr>1.5&&Close[1]>Open[1]) score+=7; else if(vr>1.5&&Close[1]<Open[1]) score-=7;}
  930. double r1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  931. double r5=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);
  932. if(r1<30) score+=15; else if(r1<40) score+=7;
  933. if(r1>70) score-=15; else if(r1>60) score-=7;
  934. if(r1>r5&&Close[1]<Close[5]) score+=8;
  935. if(r1<r5&&Close[1]>Close[5]) score-=8;
  936. if(g_nearest_res>0){double distR=(g_nearest_res-Close[0])/pip; if(distR<5) score-=12; else if(distR<10) score-=6;}
  937. if(g_nearest_sup>0){double distS=(Close[0]-g_nearest_sup)/pip; if(distS<5) score+=12; else if(distS<10) score+=6;}
  938. if(Close[0]>Open[0]&&Close[1]>Open[1]) score+=5;
  939. if(Close[0]<Open[0]&&Close[1]<Open[1]) score-=5;
  940. return MathMax(5.0,MathMin(95.0,score));
  941. }
  942.  
  943. // ============================================================
  944. // BAYESIAN PROBABILITY ENGINE (MQL4)
  945. // ============================================================
  946. struct BayesianResult
  947. {
  948. double posteriorReal;
  949. double posteriorTrap;
  950. bool isTrap;
  951. string recommendation;
  952. };
  953.  
  954. BayesianResult CalcBayesianProbability(
  955. int direction,
  956. double priorReal,
  957. double priorTrap,
  958. bool hasPinBar,
  959. bool hasDoji,
  960. bool hasEngulf,
  961. bool hasFakeBreakout,
  962. bool hasWickReversal,
  963. bool hasLiquiditySweep
  964. )
  965. {
  966. BayesianResult result;
  967.  
  968. double pinBarLikelyReal, pinBarLikelyTrap;
  969. double dojiLikelyReal, dojiLikelyTrap;
  970. double engulfLikelyReal, engulfLikelyTrap;
  971. double fakeLikelyReal, fakeLikelyTrap;
  972. double wickLikelyReal, wickLikelyTrap;
  973. double liqLikelyReal, liqLikelyTrap;
  974.  
  975. if(direction == 0) // CALL
  976. {
  977. pinBarLikelyReal = 0.15; pinBarLikelyTrap = 0.55;
  978. dojiLikelyReal = 0.08; dojiLikelyTrap = 0.42;
  979. engulfLikelyReal = 0.32; engulfLikelyTrap = 0.12;
  980. fakeLikelyReal = 0.05; fakeLikelyTrap = 0.35;
  981. wickLikelyReal = 0.22; wickLikelyTrap = 0.52;
  982. liqLikelyReal = 0.18; liqLikelyTrap = 0.62;
  983. }
  984. else // PUT
  985. {
  986. pinBarLikelyReal = 0.18; pinBarLikelyTrap = 0.58;
  987. dojiLikelyReal = 0.10; dojiLikelyTrap = 0.45;
  988. engulfLikelyReal = 0.30; engulfLikelyTrap = 0.10;
  989. fakeLikelyReal = 0.06; fakeLikelyTrap = 0.38;
  990. wickLikelyReal = 0.25; wickLikelyTrap = 0.55;
  991. liqLikelyReal = 0.20; liqLikelyTrap = 0.65;
  992. }
  993.  
  994. double likeReal = 1.0;
  995. double likeTrap = 1.0;
  996.  
  997. likeReal *= hasPinBar ? pinBarLikelyReal : (1 - pinBarLikelyReal);
  998. likeTrap *= hasPinBar ? pinBarLikelyTrap : (1 - pinBarLikelyTrap);
  999.  
  1000. likeReal *= hasDoji ? dojiLikelyReal : (1 - dojiLikelyReal);
  1001. likeTrap *= hasDoji ? dojiLikelyTrap : (1 - dojiLikelyTrap);
  1002.  
  1003. likeReal *= hasEngulf ? engulfLikelyReal : (1 - engulfLikelyReal);
  1004. likeTrap *= hasEngulf ? engulfLikelyTrap : (1 - engulfLikelyTrap);
  1005.  
  1006. likeReal *= hasFakeBreakout ? fakeLikelyReal : (1 - fakeLikelyReal);
  1007. likeTrap *= hasFakeBreakout ? fakeLikelyTrap : (1 - fakeLikelyTrap);
  1008.  
  1009. likeReal *= hasWickReversal ? wickLikelyReal : (1 - wickLikelyReal);
  1010. likeTrap *= hasWickReversal ? wickLikelyTrap : (1 - wickLikelyTrap);
  1011.  
  1012. likeReal *= hasLiquiditySweep ? liqLikelyReal : (1 - liqLikelyReal);
  1013. likeTrap *= hasLiquiditySweep ? liqLikelyTrap : (1 - liqLikelyTrap);
  1014.  
  1015. double evidence = (likeReal * priorReal) + (likeTrap * priorTrap);
  1016.  
  1017. if(evidence <= 0)
  1018. {
  1019. result.posteriorReal = 0.5;
  1020. result.posteriorTrap = 0.5;
  1021. result.isTrap = false;
  1022. result.recommendation = "WAIT";
  1023. return result;
  1024. }
  1025.  
  1026. result.posteriorReal = (likeReal * priorReal) / evidence;
  1027. result.posteriorTrap = (likeTrap * priorTrap) / evidence;
  1028.  
  1029. if(result.posteriorReal < 0.30)
  1030. {
  1031. result.isTrap = true;
  1032. result.recommendation = (direction == 0) ? "PUT" : "CALL";
  1033. }
  1034. else if(result.posteriorReal > 0.70)
  1035. {
  1036. result.isTrap = false;
  1037. result.recommendation = (direction == 0) ? "CALL" : "PUT";
  1038. }
  1039. else
  1040. {
  1041. result.isTrap = false;
  1042. result.recommendation = "WAIT";
  1043. }
  1044.  
  1045. return result;
  1046. }
  1047.  
  1048. double g_priorReal = 0.35;
  1049. double g_priorTrap = 0.65;
  1050.  
  1051. void UpdateBayesianPrior(bool tradeWon, bool wasTrapSignal)
  1052. {
  1053. if(wasTrapSignal)
  1054. {
  1055. g_priorTrap = tradeWon ? MathMin(g_priorTrap + 0.02, 0.90) : MathMax(g_priorTrap - 0.02, 0.10);
  1056. g_priorReal = 1.0 - g_priorTrap;
  1057. }
  1058. else
  1059. {
  1060. g_priorReal = tradeWon ? MathMin(g_priorReal + 0.02, 0.90) : MathMax(g_priorReal - 0.02, 0.10);
  1061. g_priorTrap = 1.0 - g_priorReal;
  1062. }
  1063. }
  1064.  
  1065. // === FIB v2.0 GLOBALS ===
  1066. static double g_fibV2_BestScore = 0;
  1067. static double g_fibV2_BestProb = 0;
  1068. static string g_fibV2_BestDir = "WAIT";
  1069. static int g_fibV2_Confidence = 0;
  1070. static double g_fibV2_BestPrice = 0;
  1071. static string g_fibV2_Pattern = "--";
  1072. static string g_fibV2_FibLevel = "--";
  1073. static string g_fibV2_Reason = "";
  1074. static datetime g_fibV2_SignalTime = 0;
  1075. static int g_fibV2_SignalBars = 0;
  1076.  
  1077. struct SwingPoint {
  1078. double price;
  1079. int index;
  1080. bool isHigh;
  1081. int strength;
  1082. };
  1083.  
  1084. struct FibSignal {
  1085. string direction;
  1086. double price;
  1087. double score;
  1088. string pattern;
  1089. string level;
  1090. double confidence;
  1091. string reason;
  1092. int swingPair;
  1093. };
  1094.  
  1095. int CalcSwingStrength(int index, bool isHigh)
  1096. {
  1097. if(index < 2 || index >= Bars - 2) return 1;
  1098. int strength = 5;
  1099. double range = High[index] - Low[index];
  1100. if(range <= 0) return 1;
  1101. int startIdx = MathMax(0, index - 4);
  1102. if(isHigh) {
  1103. double leftRange = High[index] - MathMax(High[index-1], High[index-2]);
  1104. double rightRange = High[index] - MathMax(High[index+1], High[index+2]);
  1105. if(leftRange > range * 0.3) strength += 2;
  1106. if(rightRange > range * 0.3) strength += 2;
  1107. if(index == iHighest(NULL, PERIOD_M1, MODE_HIGH, 10, startIdx)) strength += 3;
  1108. } else {
  1109. double leftRange = MathMin(Low[index-1], Low[index-2]) - Low[index];
  1110. double rightRange = MathMin(Low[index+1], Low[index+2]) - Low[index];
  1111. if(leftRange > range * 0.3) strength += 2;
  1112. if(rightRange > range * 0.3) strength += 2;
  1113. if(index == iLowest(NULL, PERIOD_M1, MODE_LOW, 10, startIdx)) strength += 3;
  1114. }
  1115. return MathMin(10, strength);
  1116. }
  1117.  
  1118. double GetPatternWeight(string pattern)
  1119. {
  1120. if(pattern == "PIN") return 1.0;
  1121. if(pattern == "ENGULF") return 1.2;
  1122. if(pattern == "FAKE") return 1.1;
  1123. if(pattern == "EXHAUST") return 0.9;
  1124. if(pattern == "DOJI") return 0.7;
  1125. if(pattern == "WICK") return 0.6;
  1126. if(pattern == "LIQSWEEP") return 1.3;
  1127. return 0.8;
  1128. }
  1129.  
  1130. //+------------------------------------------------------------------+
  1131. //| OTC FIB REJECTION v4.3 - HIGH SIGNAL FREQUENCY EDITION |
  1132. //| Relaxed Timing + Gann Buffer + Better Swing Detection |
  1133. //+------------------------------------------------------------------+
  1134. void CalcFibRejectionHunter()
  1135. {
  1136. // ==========================================================
  1137. // SECTION 1: RESET ALL GLOBALS
  1138. // ==========================================================
  1139. g_fibV2_BestScore = 0; g_fibV2_BestProb = 0; g_fibV2_BestDir = "WAIT";
  1140. g_fibV2_Confidence = 0; g_fibV2_BestPrice = 0; g_fibV2_Pattern = "--";
  1141. g_fibV2_FibLevel = "--"; g_fibV2_Reason = "";
  1142.  
  1143. g_fibBestScore = 0; g_fibBestDir = "WAIT"; g_fibBestPrice = 0;
  1144. g_fibPattern = "--"; g_fibFibLevel = "--"; g_fibConfidence = 0;
  1145.  
  1146. // ==========================================================
  1147. // SECTION 2: EXPIRE OLD SIGNAL (60 seconds max life)
  1148. // ==========================================================
  1149. if(g_otcFibSignalTime > 0) {
  1150. int secElapsed = (int)(TimeCurrent() - g_otcFibSignalTime);
  1151. if(secElapsed > 180) { // ✅ 3 minutes tak signal rahega
  1152. g_otcFibSignalTime = 0;
  1153. g_otcHasSignal = false;
  1154. DeleteOTCFibLine();
  1155. }
  1156. }
  1157.  
  1158. // ==========================================================
  1159. // SECTION 3: TIMING WINDOW (FIX: 20 seconds before close)
  1160. // ==========================================================
  1161. int candleAge = (int)(TimeCurrent() - Time[0]);
  1162. int candleTotal = Period() * 60;
  1163. int remaining = candleTotal - candleAge;
  1164.  
  1165. // ✅ FIX: 3 second ki jagah 20 second ki window rakh di
  1166. if(remaining > 45 || remaining < 0) {
  1167. if(g_otcHasSignal) DrawOTCFibLine();
  1168. return;
  1169. }
  1170.  
  1171. if(Bars < 15) return;
  1172.  
  1173. // ==========================================================
  1174. // SECTION 4: BASIC CALCULATIONS
  1175. // ==========================================================
  1176. double pip = GetUniversalPip();
  1177. if(pip <= 0) pip = 0.0001;
  1178.  
  1179. double atrVal = iATR(NULL, 0, 14, 1);
  1180. if(atrVal <= 0) atrVal = pip * 8;
  1181.  
  1182. // ==========================================================
  1183. // SECTION 5: GANN BOX OR FRACTAL SWING COLLECTION
  1184. // ==========================================================
  1185. double sHighs[3]; sHighs[0]=0; sHighs[1]=0; sHighs[2]=0;
  1186. double sLows[3]; sLows[0]=0; sLows[1]=0; sLows[2]=0;
  1187. int hIdx[3]; hIdx[0]=0; hIdx[1]=0; hIdx[2]=0;
  1188. int lIdx[3]; lIdx[0]=0; lIdx[1]=0; lIdx[2]=0;
  1189. int hCnt = 0, lCnt = 0;
  1190.  
  1191. bool useGannBox = false;
  1192.  
  1193. // ✅ FIX: Minimum Box Size Filter (Chhote box = Noise, Fib nahi banani chahiye)
  1194. double gannBuffer = pip * 3;
  1195. double boxRange = g_gannBoxHigh - g_gannBoxLow;
  1196. double minBoxSizeForFib = pip * 12; // Box kam se kam 12 pips ka hona chahiye
  1197.  
  1198. if(g_gannBoxHigh > 0 && g_gannBoxLow > 0 && boxRange >= minBoxSizeForFib) {
  1199. // Box bada hai, aur candle andar hai
  1200. if(Close[0] <= (g_gannBoxHigh + gannBuffer) && Close[0] >= (g_gannBoxLow - gannBuffer)) {
  1201. useGannBox = true;
  1202. }
  1203. }
  1204.  
  1205. if(useGannBox) {
  1206. sHighs[0] = g_gannBoxHigh;
  1207. sLows[0] = g_gannBoxLow;
  1208. hIdx[0] = iHighest(Symbol(), PERIOD_M1, MODE_HIGH, 25, 1);
  1209. lIdx[0] = iLowest(Symbol(), PERIOD_M1, MODE_LOW, 25, 1);
  1210. hCnt = 1;
  1211. lCnt = 1;
  1212. }
  1213. else {
  1214. // Box chhota hai ya candle bahar hai, toh Fractal/Swing logic use karo
  1215. int maxLB = MathMin(60, Bars - 3);
  1216. double swingDupTol = atrVal * 0.4;
  1217. int i;
  1218.  
  1219. for(i = 2; i < maxLB && hCnt < 3; i++) {
  1220. if(High[i] > High[i-1] && High[i] > High[i-2] &&
  1221. High[i] > High[i+1] && High[i] > High[i+2]) {
  1222.  
  1223. bool dup = false;
  1224. for(int k = 0; k < hCnt; k++) {
  1225. if(MathAbs(High[i] - sHighs[k]) < swingDupTol) { dup = true; break; }
  1226. }
  1227.  
  1228. if(!dup && (High[i] - Low[i]) > atrVal * 0.3) {
  1229. sHighs[hCnt] = High[i]; hIdx[hCnt] = i; hCnt++;
  1230. }
  1231. }
  1232. }
  1233.  
  1234. for(i = 2; i < maxLB && lCnt < 3; i++) {
  1235. if(Low[i] < Low[i-1] && Low[i] < Low[i-2] &&
  1236. Low[i] < Low[i+1] && Low[i] < Low[i+2]) {
  1237.  
  1238. bool dup2 = false;
  1239. for(int k2 = 0; k2 < lCnt; k2++) {
  1240. if(MathAbs(Low[i] - sLows[k2]) < swingDupTol) { dup2 = true; break; }
  1241. }
  1242.  
  1243. if(!dup2 && (High[i] - Low[i]) > pip * 2) {
  1244. sLows[lCnt] = Low[i]; lIdx[lCnt] = i; lCnt++;
  1245. }
  1246. }
  1247. }
  1248. }
  1249.  
  1250. if(hCnt < 1 || lCnt < 1) return;
  1251.  
  1252. // ==========================================================
  1253. // SECTION 6: INDICATORS
  1254. // ==========================================================
  1255. double ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
  1256. double ema20 = iMA(NULL, 0, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
  1257. bool emaBull = (ema10 > ema20);
  1258. bool emaBear = (ema10 < ema20);
  1259. bool emaFlat = (MathAbs(ema10 - ema20) < atrVal * 0.15);
  1260.  
  1261. double rsi0 = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
  1262. double rsi1 = iRSI(NULL, 0, 14, PRICE_CLOSE, 1);
  1263.  
  1264. double adx = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  1265. double pDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  1266. double mDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  1267. double diGap = MathAbs(pDI - mDI);
  1268.  
  1269. // ==========================================================
  1270. // SECTION 7: FIB LEVELS SETUP
  1271. // ==========================================================
  1272. double touchTol = MathMax(atrVal * 0.25, pip * 8); // ✅ FIX: 0.20->0.25 | 6->8 pips (Wider touch)
  1273. double fibLvls[4];
  1274. fibLvls[0] = 0.886; fibLvls[1] = 0.500; fibLvls[2] = 0.618; fibLvls[3] = 0.786;
  1275.  
  1276. string fibNms[4];
  1277. fibNms[0] = "88.6%"; fibNms[1] = "50.0%"; fibNms[2] = "61.8%"; fibNms[3] = "78.6%";
  1278.  
  1279. // ==========================================================
  1280. // SECTION 8: BEST SIGNAL TRACKER
  1281. // ==========================================================
  1282. double bestScore = 0;
  1283. double bestPrice = 0;
  1284. string bestPat = "NONE";
  1285. string bestLvl = "--";
  1286. string bestDir = "WAIT";
  1287.  
  1288. // ==========================================================
  1289. // SECTION 9: MAIN COMBINATION LOOP
  1290. // ==========================================================
  1291. for(int h = 0; h < hCnt; h++) {
  1292. for(int l = 0; l < lCnt; l++) {
  1293.  
  1294. double swingH = sHighs[h];
  1295. double swingL = sLows[l];
  1296. int idxH = hIdx[h];
  1297. int idxL = lIdx[l];
  1298.  
  1299. if(idxH == idxL && !useGannBox) continue;
  1300.  
  1301. double range = MathAbs(swingH - swingL);
  1302. if(range <= pip * 2 || range > pip * 500) continue; // 3->2 min, 400->500 max
  1303.  
  1304. string dir;
  1305. // ✅ FIX: Use Close[1] instead of Close[0] to lock direction.
  1306. // Close[0] changes every tick causing flicker. Close[1] is stable.
  1307. double refPrice = Close[1];
  1308. double pricePos = (refPrice - MathMin(swingH, swingL)) / range;
  1309. pricePos = MathMax(0.0, MathMin(1.0, pricePos));
  1310.  
  1311. bool highFirst = (idxH < idxL);
  1312.  
  1313. if(useGannBox) {
  1314. // ✅ FIX: Direction locked based on last completed candle
  1315. if(Close[1] > Open[1]) dir = "CALL"; // Pichli candle Green thi toh CALL pullback
  1316. else if(Close[1] < Open[1]) dir = "PUT"; // Pichli candle Red thi toh PUT pullback
  1317. else dir = (pricePos < 0.50) ? "CALL" : "PUT"; // Doji ho toh price position se
  1318. } else {
  1319. if(highFirst) {
  1320. dir = (pricePos < 0.65) ? "CALL" : "PUT";
  1321. } else {
  1322. dir = (pricePos > 0.35) ? "PUT" : "CALL";
  1323. }
  1324. }
  1325.  
  1326. if(!emaFlat) {
  1327. if(emaBull && dir == "PUT" && pricePos < 0.25) dir = "CALL";
  1328. if(emaBear && dir == "CALL" && pricePos > 0.75) dir = "PUT";
  1329. }
  1330.  
  1331. if(adx > 25) {
  1332. if(emaBull && dir == "PUT" && pricePos < 0.35) dir = "CALL";
  1333. if(emaBear && dir == "CALL" && pricePos > 0.65) dir = "PUT";
  1334. }
  1335.  
  1336. // ==========================================================
  1337. // SECTION 10: FIB LEVEL LOOP
  1338. // ==========================================================
  1339. for(int f = 0; f < 4; f++) {
  1340. double fibPrice;
  1341.  
  1342. if(dir == "CALL") {
  1343. fibPrice = swingH - (range * fibLvls[f]);
  1344. } else {
  1345. fibPrice = swingL + (range * fibLvls[f]);
  1346. }
  1347.  
  1348. // ==========================================================
  1349. // SECTION 11: TOUCH CHECK [0]
  1350. // ==========================================================
  1351. double touchQ = 0;
  1352. bool touched = false;
  1353.  
  1354. if(dir == "CALL") {
  1355. double d = Low[0] - fibPrice;
  1356. if(d <= touchTol && d >= -touchTol * 2.0) { // 1.5 -> 2.0 (More forgiving)
  1357. touched = true;
  1358. touchQ = MathMax(0, 100 - MathAbs(d) / touchTol * 100);
  1359. double rng = High[0] - Low[0];
  1360. if(rng > 0) {
  1361. double lw = MathMin(Open[0], Close[0]) - Low[0];
  1362. if(lw / rng > 0.25) touchQ += 12; // 0.30 -> 0.25
  1363. }
  1364. }
  1365. } else {
  1366. double d2 = fibPrice - High[0];
  1367. if(d2 <= touchTol && d2 >= -touchTol * 2.0) { // 1.5 -> 2.0
  1368. touched = true;
  1369. touchQ = MathMax(0, 100 - MathAbs(d2) / touchTol * 100);
  1370. double rng2 = High[0] - Low[0];
  1371. if(rng2 > 0) {
  1372. double uw = High[0] - MathMax(Open[0], Close[0]);
  1373. if(uw / rng2 > 0.25) touchQ += 12; // 0.30 -> 0.25
  1374. }
  1375. }
  1376. }
  1377.  
  1378. // ==========================================================
  1379. // SECTION 12: TOUCH CHECK [1]
  1380. // ==========================================================
  1381. if(!touched) {
  1382. if(dir == "CALL") {
  1383. double d3 = Low[1] - fibPrice;
  1384. if(d3 <= touchTol && d3 >= -touchTol * 1.5) { // More forgiving
  1385. touched = true;
  1386. touchQ = MathMax(0, 75 - MathAbs(d3) / touchTol * 50);
  1387. if(Close[1] >= fibPrice - touchTol * 0.5) touchQ += 10;
  1388. }
  1389. } else {
  1390. double d4 = fibPrice - High[1];
  1391. if(d4 <= touchTol && d4 >= -touchTol * 1.5) { // More forgiving
  1392. touched = true;
  1393. touchQ = MathMax(0, 75 - MathAbs(d4) / touchTol * 50);
  1394. if(Close[1] <= fibPrice + touchTol * 0.5) touchQ += 10;
  1395. }
  1396. }
  1397. }
  1398.  
  1399. if(!touched) continue;
  1400.  
  1401. // ==========================================================
  1402. // SECTION 13: PATTERN DETECTION
  1403. // ==========================================================
  1404. string pat = "NONE";
  1405. double rng0 = High[0] - Low[0];
  1406.  
  1407. if(rng0 > 0) {
  1408. double bdy0 = MathAbs(Close[0] - Open[0]);
  1409. double uw0 = High[0] - MathMax(Open[0], Close[0]);
  1410. double lw0 = MathMin(Open[0], Close[0]) - Low[0];
  1411.  
  1412. if(dir == "CALL" && lw0 > bdy0 * 1.0 && lw0 / rng0 > 0.40) pat = "PIN"; // 1.2->1.0 | 0.45->0.40
  1413. else if(dir == "PUT" && uw0 > bdy0 * 1.0 && uw0 / rng0 > 0.40) pat = "PIN";
  1414.  
  1415. double bdy1 = MathAbs(Close[1] - Open[1]);
  1416. if(bdy1 > 0) {
  1417. if(dir == "CALL" && Close[0] > Open[0] && Close[1] < Open[1] && bdy0 > bdy1 * 1.0) pat = "ENGULF"; // 1.1->1.0
  1418. else if(dir == "PUT" && Close[0] < Open[0] && Close[1] > Open[1] && bdy0 > bdy1 * 1.0) pat = "ENGULF";
  1419. }
  1420.  
  1421. if(dir == "CALL" && Low[0] < fibPrice && Close[0] > fibPrice) pat = "FAKE";
  1422. else if(dir == "PUT" && High[0] > fibPrice && Close[0] < fibPrice) pat = "FAKE";
  1423.  
  1424. if(bdy0 / rng0 < 0.15 && rng0 > pip * 4) pat = "DOJI"; // 0.12->0.15 | 5->4
  1425.  
  1426. if(pat == "NONE") {
  1427. if(dir == "CALL" && lw0 > uw0 && lw0 / rng0 > 0.25) pat = "WICK"; // 0.30->0.25
  1428. else if(dir == "PUT" && uw0 > lw0 && uw0 / rng0 > 0.25) pat = "WICK";
  1429. }
  1430. }
  1431.  
  1432. // ==========================================================
  1433. // SECTION 14: BAYESIAN TRAP DETECTION
  1434. // ==========================================================
  1435. bool isTrap = false;
  1436. double trapPenalty = 0;
  1437. string trapReason = "";
  1438.  
  1439. if(dir == "CALL" && rsi0 < 35 && rsi1 > rsi0 && Low[0] < Low[1]) {
  1440. isTrap = true; trapReason = "RSI_DIV";
  1441. }
  1442. if(dir == "PUT" && rsi0 > 65 && rsi1 < rsi0 && High[0] > High[1]) {
  1443. isTrap = true; trapReason = "RSI_DIV";
  1444. }
  1445.  
  1446. if(adx < 18) {
  1447. if(dir == "CALL" && mDI > pDI + 8) { isTrap = true; trapReason = "ADX_WEAK"; }
  1448. if(dir == "PUT" && pDI > mDI + 8) { isTrap = true; trapReason = "ADX_WEAK"; }
  1449. }
  1450.  
  1451. double v0 = (double)iVolume(NULL, PERIOD_M1, 0);
  1452. double vAvg = 0;
  1453. for(int vi = 1; vi <= 5; vi++) vAvg += (double)iVolume(NULL, PERIOD_M1, vi);
  1454. vAvg /= 5.0;
  1455. if(vAvg > 0 && v0 > vAvg * 2.0) {
  1456. double rngV = High[0] - Low[0];
  1457. if(rngV > 0 && MathAbs(Close[0] - Open[0]) / rngV < 0.30) {
  1458. isTrap = true; trapReason = "VOL_SPIKE";
  1459. }
  1460. }
  1461.  
  1462. bool threeGreen = (Close[2] > Open[2] && Close[1] > Open[1] && Close[0] > Open[0]);
  1463. bool threeRed = (Close[2] < Open[2] && Close[1] < Open[1] && Close[0] < Open[0]);
  1464. if(dir == "CALL" && threeGreen) { isTrap = true; trapReason = "3GREEN"; }
  1465. if(dir == "PUT" && threeRed) { isTrap = true; trapReason = "3RED"; }
  1466.  
  1467. if(isTrap) {
  1468. trapPenalty = 10; // 15 -> 10 (Less penalty so lines still appear)
  1469. pat = "TRAP_" + trapReason + "_" + pat;
  1470. }
  1471.  
  1472. // ==========================================================
  1473. // SECTION 15: SCORING
  1474. // ==========================================================
  1475. double score = touchQ * 0.50; // 0.45 -> 0.50 (More weight to touch)
  1476.  
  1477. if(pat == "PIN" || StringFind(pat, "PIN") >= 0) score += 22;
  1478. else if(pat == "ENGULF" || StringFind(pat, "ENGULF") >= 0) score += 20;
  1479. else if(pat == "FAKE" || StringFind(pat, "FAKE") >= 0) score += 18;
  1480. else if(pat == "DOJI" || StringFind(pat, "DOJI") >= 0) score += 14;
  1481. else if(pat == "WICK" || StringFind(pat, "WICK") >= 0) score += 12;
  1482.  
  1483. if(f == 0) score += 10; // 88.6% (Deep trap, high bonus!)
  1484. else if(f == 1) score += 5; // 50.0%
  1485. else if(f == 2) score += 12; // 61.8% (Golden Ratio)
  1486. else if(f == 3) score += 6; // 78.6%
  1487.  
  1488. if(useGannBox) score += 10; // 8 -> 10
  1489.  
  1490. if(dir == "CALL" && emaBull) score += 10;
  1491. if(dir == "PUT" && emaBear) score += 10;
  1492.  
  1493. if(!isTrap) {
  1494. if(dir == "CALL" && rsi0 < 28) score += 14;
  1495. else if(dir == "CALL" && rsi0 < 35) score += 8;
  1496. if(dir == "PUT" && rsi0 > 72) score += 14;
  1497. else if(dir == "PUT" && rsi0 > 65) score += 8;
  1498. }
  1499.  
  1500. if(adx > 22 && diGap > 10) score += 5;
  1501.  
  1502. score -= trapPenalty;
  1503. score = MathMax(0, MathMin(100, score));
  1504.  
  1505. // ==========================================================
  1506. // SECTION 16: SAVE BEST
  1507. // ==========================================================
  1508. if(score > bestScore) {
  1509. bestScore = score;
  1510. bestPrice = fibPrice;
  1511. bestPat = pat;
  1512. bestLvl = fibNms[f];
  1513. bestDir = dir;
  1514. }
  1515. }
  1516. }
  1517. }
  1518.  
  1519. // ==========================================================
  1520. // SECTION 17: FINAL SIGNAL SET (Threshold 35)
  1521. // ==========================================================
  1522. if(bestScore >= 55 && bestPrice > 0 && bestPat != "NONE") { // ✅ FIX: 35 se 55 kiya aur NONE pattern block kiya
  1523. g_otcFibPrice = bestPrice;
  1524. g_otcFibDir = bestDir;
  1525. g_otcFibStrength = (int)bestScore;
  1526. g_otcFibPattern = bestPat;
  1527. g_otcFibLevel = bestLvl;
  1528. g_otcFibSignalTime = TimeCurrent();
  1529. g_otcHasSignal = true;
  1530.  
  1531. g_fibV2_BestScore = bestScore;
  1532. g_fibV2_BestProb = bestScore;
  1533. g_fibV2_BestDir = bestDir;
  1534. g_fibV2_Confidence = (int)bestScore;
  1535. g_fibV2_Pattern = bestPat;
  1536. g_fibV2_FibLevel = bestLvl;
  1537. g_fibV2_BestPrice = bestPrice;
  1538. g_fibV2_Reason = (useGannBox ? "GANB_v4.3_" : "FRC_v4.3_") + bestPat;
  1539.  
  1540. g_fibBestScore = bestScore;
  1541. g_fibBestProb = bestScore;
  1542. g_fibBestDir = bestDir;
  1543. g_fibConfidence = (int)bestScore;
  1544. g_fibBestPrice = bestPrice;
  1545. g_fibPattern = bestPat;
  1546. g_fibFibLevel = bestLvl;
  1547.  
  1548. DrawOTCFibLine();
  1549.  
  1550. Print("OTC FIB v4.3 ✅ ", bestDir, " | ", bestLvl,
  1551. " | Score:", (int)bestScore, "% | Pat:", bestPat,
  1552. " | Mode:", (useGannBox ? "GANN_BOX" : "FRACTAL"),
  1553. " | Trap:", (StringFind(bestPat, "TRAP_") >= 0 ? "YES" : "NO"));
  1554. }
  1555. }
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562. void DrawFibRejectionLine()
  1563. {
  1564. DrawOTCFibLine();
  1565. }
  1566.  
  1567. //+------------------------------------------------------------------+
  1568. //| DRAW OTC FIB LINE (v3.5 - 200px RIGHT - PERFECT SPACING) |
  1569. //+------------------------------------------------------------------+
  1570. void DrawOTCFibLine()
  1571. {
  1572. if(!g_otcHasSignal || g_otcFibPrice <= 0) {
  1573. DeleteOTCFibLine();
  1574. return;
  1575. }
  1576.  
  1577. string lineName = PFX + "OTC_FIB_LINE";
  1578. string lblName = PFX + "OTC_FIB_LBL";
  1579.  
  1580. // --- LINE COLOR ---
  1581. color lineColor;
  1582. if(g_otcFibDir == "CALL")
  1583. lineColor = (g_otcFibStrength >= 70) ? C'0,255,100' : C'0,200,150';
  1584. else
  1585. lineColor = (g_otcFibStrength >= 70) ? C'255,50,50' : C'255,100,100';
  1586.  
  1587. // --- DRAW DOTTED LINE ---
  1588. SafeDel(lineName);
  1589. ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, g_otcFibPrice);
  1590. ObjectSetInteger(0, lineName, OBJPROP_COLOR, lineColor);
  1591. ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 1);
  1592. ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_DOT);
  1593. ObjectSetInteger(0, lineName, OBJPROP_BACK, false);
  1594.  
  1595. // --- LABEL (200px RIGHT - PERFECT BALANCE) ---
  1596. SafeDel(lblName);
  1597.  
  1598. int subWindow = 0;
  1599. int xCurr = 0, yCurr = 0;
  1600. datetime labelTime = 0;
  1601. double labelPrice = 0;
  1602. bool posOK = false;
  1603.  
  1604. // Current candle position se start karo
  1605. if(ChartTimePriceToXY(0, subWindow, Time[0], g_otcFibPrice, xCurr, yCurr)) {
  1606. int xLabel = xCurr + 200; // ✅ 200px RIGHT (Pehle 275 tha, ab 200 perfect rahega)
  1607. if(ChartXYToTimePrice(0, xLabel, yCurr, subWindow, labelTime, labelPrice)) {
  1608. posOK = true;
  1609. }
  1610. }
  1611.  
  1612. // Fallback (Agar pixels se time na mile toh 4 candle aage lagao)
  1613. if(!posOK && Bars > 3) {
  1614. int candleShift = 4;
  1615. labelTime = Time[0] + PeriodSeconds() * candleShift;
  1616. posOK = true;
  1617. }
  1618.  
  1619. if(posOK) {
  1620. string lblText = "FIB " + g_otcFibLevel + " | " + g_otcFibDir + " " +
  1621. IntegerToString(g_otcFibStrength) + "% [" + g_otcFibPattern + "]";
  1622. color lblColor = C'255,50,255';
  1623.  
  1624. ObjectCreate(0, lblName, OBJ_TEXT, 0, labelTime, g_otcFibPrice);
  1625. ObjectSetText(lblName, lblText, 12, "Arial Bold", lblColor);
  1626. ObjectSetInteger(0, lblName, OBJPROP_BACK, false);
  1627. }
  1628. }
  1629.  
  1630. //+------------------------------------------------------------------+
  1631. //| DELETE OTC FIB LINE |
  1632. //+------------------------------------------------------------------+
  1633. void DeleteOTCFibLine()
  1634. {
  1635. SafeDel(PFX + "OTC_FIB_LINE");
  1636. SafeDel(PFX + "OTC_FIB_LBL");
  1637. }
  1638.  
  1639. void CalcAdvancedPrediction(double &gP,double &rP,string &reason,color &pC){
  1640. if(Bars<50){gP=50;rP=50;reason="Bars low";pC=NEON_YELLOW;return;}
  1641. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1642. double green=0,red=0;
  1643. double body1=MathAbs(Close[1]-Open[1]),range1=High[1]-Low[1];
  1644. if(range1>0){double uw=High[1]-MathMax(Open[1],Close[1]),lw=MathMin(Open[1],Close[1])-Low[1];
  1645. if(uw>range1*0.60&&Close[1]>Open[1])red+=22;else if(lw>range1*0.60&&Close[1]<Open[1])green+=22;
  1646. else if((uw+lw)/range1>0.55){if(uw>lw)red+=12;else green+=12;}}
  1647. 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);
  1648. int os=0,ob=0;
  1649. 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++;
  1650. if(os>=3)green+=18;else if(os>=2)green+=12;else if(ob>=3)red+=18;else if(ob>=2)red+=12;
  1651. double rsiPrev=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);
  1652. double low1=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],low2=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];
  1653. double high1=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],high2=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];
  1654. if(low1<low2&&rsi>rsiPrev&&rsi<45)green+=15;if(high1>high2&&rsi<rsiPrev&&rsi>55)red+=15;
  1655. 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;
  1656. double vol_ratio=(vAvg>0)?v1/vAvg:1.0;
  1657. 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;}
  1658. 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;
  1659. double m1_haO2=(Open[3]+Close[3])/2.0,m1_haO1=(m1_haO2+m1_haC2)/2.0;
  1660. bool haBull1=(m1_haC1>m1_haO1);
  1661. double haStr1=MathAbs(m1_haC1-m1_haO1)/(High[1]-Low[1]+0.00001);
  1662. bool haBull5=false;double haStr5=0;
  1663. if(iBars(NULL,PERIOD_M5)>=5){
  1664. 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);
  1665. 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);
  1666. double o3=iOpen(NULL,PERIOD_M5,3),c3=iClose(NULL,PERIOD_M5,3);
  1667. 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;
  1668. haBull5=(haC1_5>haO1_5);haStr5=MathAbs(haC1_5-haO1_5)/(h1-l1+0.00001);}
  1669. if(haBull1&&haBull5)green+=15;else if(!haBull1&&!haBull5)red+=15;
  1670. else if(haBull1&&haStr1>0.5)green+=8;else if(!haBull1&&haStr1>0.5)red+=8;
  1671. 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);
  1672. double diDiff=plusDI-minusDI;
  1673. 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;}
  1674. else if(adx>15){if(plusDI>minusDI)green+=4;else red+=4;}
  1675. double sup=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,20,2)],res=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,20,2)];
  1676. double dR=(res-Close[0])/pip,dS=(Close[0]-sup)/pip;
  1677. if(dR<8&&dR>=0)red+=12;else if(dR<15&&dR>=0)red+=6;
  1678. if(dS<8&&dS>=0)green+=12;else if(dS<15&&dS>=0)green+=6;
  1679. 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;}}
  1680. 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);
  1681. 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);
  1682. if(macdMain>macdSig&&macdPrev<=macdSigPrev)green+=10;else if(macdMain<macdSig&&macdPrev>=macdSigPrev)red+=10;
  1683. else if(macdMain>macdSig)green+=5;else if(macdMain<macdSig)red+=5;
  1684. if(ENABLE_BB_PULLBACK&&g_bbSignal=="CALL")green+=12; else if(ENABLE_BB_PULLBACK&&g_bbSignal=="PUT")red+=12;
  1685. double diff=MathMax(-100.0,MathMin(100.0,green-red));
  1686. gP=MathMax(8.0,MathMin(92.0,50.0+diff*0.65));rP=100.0-gP;
  1687. double dom=MathMax(gP,rP);
  1688. if(dom>=82){reason=(gP>rP)?"STRONG GREEN":"STRONG RED";pC=(gP>rP)?NEON_GREEN:NEON_RED;}
  1689. else if(dom>=72){reason=(gP>rP)?"GREEN":"RED";pC=(gP>rP)?NEON_GREEN:NEON_RED;}
  1690. else if(dom>=60){reason=(gP>rP)?"WEAK GREEN":"WEAK RED";pC=(gP>rP)?NEON_CYAN:NEON_ORANGE;}
  1691. else{reason="WAIT";pC=NEON_YELLOW;}
  1692. }
  1693.  
  1694. string DetectMyStrategy(){
  1695. int cc=g_otcCallPct,cp=g_otcPutPct,thr=STRATEGY_THRESHOLD;
  1696. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TRAP_ONLY"){
  1697. if(cc>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TRAP";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA RED";return "PUT";}
  1698. if(cp>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TRAP";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA GREEN";return "CALL";}}
  1699. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TREND_ONLY"){
  1700. if(cc>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TREND";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA GREEN";return "CALL";}
  1701. if(cp>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TREND";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA RED";return "PUT";}}
  1702. g_strategyType="NONE";g_strategyReason="Wait "+IntegerToString(thr)+"% + HA";return "WAIT";
  1703. }
  1704.  
  1705. void CalcOTCCrowd(){
  1706. int cS=0,pS=0;int weights[15];
  1707. for(int w=0;w<15;w++)weights[w]=15-w*2;
  1708. for(int w=0;w<15;w++)if(weights[w]<1)weights[w]=1;
  1709. for(int i=1;i<=15&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;
  1710. double uw=(High[i]-MathMax(Open[i],Close[i]))/rg,lw=(MathMin(Open[i],Close[i])-Low[i])/rg;int wgt=weights[i-1];
  1711. if(uw>0.70&&Close[i]<Open[i])cS+=4*wgt;if(lw>0.70&&Close[i]>Open[i])pS+=4*wgt;
  1712. if(uw>0.55&&Close[i]<Open[i])cS+=2*wgt;if(lw>0.55&&Close[i]>Open[i])pS+=2*wgt;
  1713. if(Close[i]>Open[i])cS+=1*wgt;else if(Close[i]<Open[i])pS+=1*wgt;}
  1714. int tot=cS+pS;if(tot==0){g_otcCallPct=50;g_otcPutPct=50;}
  1715. else{int rC=(int)(cS*100.0/tot),rP=100-rC;
  1716. if(rC>=rP){g_otcCallPct=MathMax(50,rC);g_otcPutPct=100-g_otcCallPct;}
  1717. else{g_otcPutPct=MathMax(50,rP);g_otcCallPct=100-g_otcPutPct;}}
  1718. }
  1719.  
  1720. // ============================================================
  1721. // BRAIN AI
  1722. // ============================================================
  1723. double BrainSc(int &cp){
  1724. double s=0; bool jpy=(StringFind(Symbol(),"JPY")>=0); double pip=jpy?0.01:0.0001;
  1725. if(g_nearest_res>0){double dR=(g_nearest_res-Close[0])/pip; if(dR<5) s-=3; else if(dR<15) s-=1;}
  1726. if(g_nearest_sup>0){double dS=(Close[0]-g_nearest_sup)/pip; if(dS<5) s+=3; else if(dS<15) s+=1;}
  1727. if(g_nearest_res>0&&High[1]>g_nearest_res){double bodyH=MathMax(Open[1],Close[1]); if(bodyH>g_nearest_res+2*pip) s-=4; else s+=2;}
  1728. if(g_nearest_sup>0&&Low[1]<g_nearest_sup){double bodyL=MathMin(Open[1],Close[1]); if(bodyL<g_nearest_sup-2*pip) s+=4; else s-=2;}
  1729. double atr=iATR(NULL,PERIOD_M1,14,1);
  1730. if(atr>0){double cn=(High[1]-Low[1])/atr; double v1=(double)iVolume(NULL,PERIOD_M1,1); double va=0; for(int i=2;i<=10;i++) va+=iVolume(NULL,PERIOD_M1,i); va/=9.0; double vr=(va>0)?v1/va:1.0; if(cn>1.3&&vr>1.8){if(Close[1]>Open[1]) s-=2; else s+=2;}}
  1731. int c=0; for(int i=1;i<=30;i++) if(Close[i]>Open[i]) c++; cp=(int)(c*100.0/30.0);
  1732. if(cp>=80) s-=4; else if(cp>=75) s-=2; else if(cp<=20) s+=4; else if(cp<=25) s+=2;
  1733. int dj=0; double wickBull=0,wickBear=0;
  1734. for(int i=1;i<=8;i++){double bd=MathAbs(Close[i]-Open[i]); double rg=High[i]-Low[i]; if(rg<=0) continue; if(bd<rg*0.2) dj++; double uw=High[i]-MathMax(Open[i],Close[i]); double lw=MathMin(Open[i],Close[i])-Low[i]; double w=(9.0-i)/8.0; if(uw>bd*2) wickBear+=w; if(lw>bd*2) wickBull+=w;}
  1735. if(wickBull>wickBear+1.0) s+=3; else if(wickBear>wickBull+1.0) s-=3;
  1736. if(dj>=4) s*=0.8;
  1737. int bu=0,be=0; for(int i=1;i<=8;i++){if(Close[i]>Open[i]){bu++;be=0;} else if(Close[i]<Open[i]){be++;bu=0;} else {bu=0;be=0;} if(bu>=6) s-=3; if(be>=6) s+=3;}
  1738. double ema10=0; for(int i=0;i<10&&i<Bars;i++) ema10+=Close[i]; ema10/=10.0; double ema20=0; for(int i=0;i<20&&i<Bars;i++) ema20+=Close[i]; ema20/=20.0;
  1739. if(ema10>ema20&&ema10-ema20>3*pip&&s<-2) s*=0.7; if(ema10<ema20&&ema20-ema10>3*pip&&s>2) s*=0.7;
  1740. int h=TimeHour(TimeCurrent()); double mul=1.0; if((h>=8&&h<=11)||(h>=14&&h<=17)) mul=1.1; if(h>=3&&h<=7) mul=0.9; return s*mul;
  1741. }
  1742.  
  1743. // ============================================================
  1744. // STABLE M5 SUPPORT & RESISTANCE (FIXED FLICKERING)
  1745. // ============================================================
  1746. void UpdateStableM5SR(){
  1747. // Sirf tab update karo jab naya 5-minute candle ban jaaye
  1748. if(iTime(NULL, PERIOD_M5, 0) == g_last_m5_bar) return;
  1749. g_last_m5_bar = iTime(NULL, PERIOD_M5, 0);
  1750.  
  1751. if(iBars(NULL, PERIOD_M5) < 20) return;
  1752.  
  1753. double cur = Close[0];
  1754.  
  1755. // Last 10 completed M5 candles me se sabse bada High (Resistance) aur sabse chhota Low (Support) dhundho
  1756. int highestIdx = iHighest(NULL, PERIOD_M5, MODE_HIGH, 10, 1);
  1757. int lowestIdx = iLowest(NULL, PERIOD_M5, MODE_LOW, 10, 1);
  1758.  
  1759. double potential_res = iHigh(NULL, PERIOD_M5, highestIdx);
  1760. double potential_sup = iLow(NULL, PERIOD_M5, lowestIdx);
  1761.  
  1762. // Sirf wahi line set karo jo current price ke upper ya neeche ho
  1763. g_nearest_res = (potential_res > cur) ? potential_res : 0;
  1764. g_nearest_sup = (potential_sup < cur) ? potential_sup : 0;
  1765.  
  1766. // Drawing call karo
  1767. DrawStableM5Lines();
  1768. CheckBRK(); // Breakout check normal rahega
  1769. }
  1770.  
  1771. void DrawStableM5Lines(){
  1772. return; // ✅ S/R LINES BAND - Order Blocks replace karenge
  1773. // Purani lines delete karo
  1774. SafeDel(PFX+"NEAREST_RES"); SafeDel(PFX+"NEAREST_SUP");
  1775. SafeDel(PFX+"NEAREST_RES_LBL"); SafeDel(PFX+"NEAREST_SUP_LBL");
  1776.  
  1777.  
  1778.  
  1779. // sirf tab draw karo agar lines exist karti hon aur chart par jagah ho (Bars > 5)
  1780. if(g_nearest_res > 0 && Bars > 5){
  1781. DrawHLine(PFX+"NEAREST_RES", g_nearest_res, C'50,80,255', 2, STYLE_DASH); // Strict Blue Color
  1782. ObjectCreate(0, PFX+"NEAREST_RES_LBL", OBJ_TEXT, 0, Time[5], g_nearest_res);
  1783. ObjectSetText(PFX+"NEAREST_RES_LBL", "M5 RES " + ShortPrice(g_nearest_res), 9, "Arial Bold", C'50,80,255');
  1784. ObjectSetInteger(0, PFX+"NEAREST_RES_LBL", OBJPROP_BACK, false);
  1785. }
  1786. if(g_nearest_sup > 0 && Bars > 5){
  1787. DrawHLine(PFX+"NEAREST_SUP", g_nearest_sup, C'50,80,255', 2, STYLE_DASH); // Strict Blue Color
  1788. ObjectCreate(0, PFX+"NEAREST_SUP_LBL", OBJ_TEXT, 0, Time[5], g_nearest_sup);
  1789. ObjectSetText(PFX+"NEAREST_SUP_LBL", "M5 SUP " + ShortPrice(g_nearest_sup), 9, "Arial Bold", C'50,80,255');
  1790. ObjectSetInteger(0, PFX+"NEAREST_SUP_LBL", OBJPROP_BACK, false);
  1791. }
  1792. }
  1793.  
  1794.  
  1795. 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;}}
  1796.  
  1797. // ============================================================
  1798. // MICRO WICK & VOLUME TRAP
  1799. // ============================================================
  1800. 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;}
  1801.  
  1802. double VolumeTrapScore(){
  1803. if(!VOLUME_PROFILE_TRAP||Bars<50)return 0;
  1804. double cur=Close[0],hi=High[iHighest(NULL,0,MODE_HIGH,50,1)],lo=Low[iLowest(NULL,0,MODE_LOW,50,1)];
  1805. 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};
  1806. 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]++;}
  1807. for(int z=0;z<zn;z++)if(zc[z]>0)vz[z]/=zc[z];
  1808. int cz=(int)((cur-lo)/zs);if(cz<0)cz=0;if(cz>=zn)cz=zn-1;
  1809. double vr=(double)Volume[1]/(vz[cz]+0.001);double wr=MicroWick();
  1810. if(vr>2.0&&wr>0.65)return 25;if(vr>1.8&&wr>0.55)return 15;return 0;
  1811. }
  1812.  
  1813. double UltimateTrapScore(){double wr=MicroWick(),v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  1814. double avg=v2>0?v2:1.0,vr=v1/avg,rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),sc=0.0;
  1815. if(wr>0.65)sc+=40;if(vr>2.0)sc+=30;if(rsi>75||rsi<25)sc+=20;
  1816. if((High[1]>g_nearest_res&&Close[1]<g_nearest_res)||(Low[1]<g_nearest_sup&&Close[1]>g_nearest_sup))sc+=25;
  1817. sc+=VolumeTrapScore();return MathMin(100.0,sc);}
  1818.  
  1819. bool IsBrokerForce(double dummy=0){
  1820. int sd=0;for(int i=1;i<=3;i++){if(Close[i]>Open[i])sd++;else sd--;}
  1821. double v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  1822. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);
  1823. return(MathAbs(sd)==3&&v1>v2*2.0&&sp>BROKER_SPREAD_THRESHOLD);
  1824. }
  1825.  
  1826. // ============================================================
  1827. // NEURAL AI (FIXED fVol AND fTick DUPLICATES)
  1828. // ============================================================
  1829. double NeuralBiasFast(){double r=NeuralBias(0)*0.3+NeuralBias(1)*0.7;if(r>96)r=96;if(r<4)r=4;return r;}
  1830.  
  1831. double NeuralBias(int s){
  1832. double f[8];
  1833. f[0]=fDoji(s);f[1]=fWick(s);f[2]=fVol();f[3]=fMom(s);f[4]=fSpread();f[5]=fMem(s);f[6]=fTick(s);f[7]=fFrac(s);
  1834. for(int i=0;i<8;i++) f[i]/=100.0;
  1835. double h[4];
  1836. for(int i=0;i<4;i++){
  1837. double sum=0;for(int j=0;j<8;j++) sum+=f[j]*NW[j*4+i];
  1838. h[i]=MathMax(0,sum-0.06);
  1839. }
  1840. double out=0;for(int i=0;i<4;i++) out+=h[i]*NW[32+i];
  1841. double result=50.0+out*22.0;
  1842. return(result>96)?96:((result<4)?4:result);
  1843. }
  1844.  
  1845. double fDoji(int s){
  1846. if(s+8>=Bars)return 0;double score=0;int streak=0;
  1847. for(int i=s;i<s+8&&i<Bars;i++){
  1848. double body=MathAbs(Open[i]-Close[i]);double range=High[i]-Low[i];if(range<=0)continue;
  1849. double ratio=body/range;double w=(9.0-(i-s))/8.0;
  1850. if(ratio<0.15){score+=18*w;streak++;
  1851. double lw=MathMin(Open[i],Close[i])-Low[i];if(lw>range*0.55)score+=12*w;
  1852. double uw=High[i]-MathMax(Open[i],Close[i]);if(uw>range*0.55)score-=8*w;}
  1853. else if(ratio<0.25){score+=8*w;streak++;}}
  1854. if(streak>=3)score+=15;else if(streak>=2)score+=8;
  1855. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1856. if(g_nearest_res>0&&(g_nearest_res-cur)<8*pip)score+=12;
  1857. if(g_nearest_sup>0&&(cur-g_nearest_sup)<8*pip)score+=12;
  1858. if(g_hrn_price>0&&MathAbs(cur-g_hrn_price)<5*pip)score+=10;
  1859. return MathMin(100,score);
  1860. }
  1861.  
  1862. double fWick(int s){
  1863. double range=High[s]-Low[s];if(range==0)return 0;
  1864. double u=(High[s]-MathMax(Open[s],Close[s]))/range;double l=(MathMin(Open[s],Close[s])-Low[s])/range;
  1865. bool closedRed=(Close[s]<Open[s]);double score=0;
  1866. if(u>0.65&&!closedRed)score=95;else if(u>0.50&&!closedRed)score=70;
  1867. else if(l>0.65&&closedRed)score=90;else if(l>0.50&&closedRed)score=65;
  1868. else if(u>0.65||l>0.65)score=50;else if(u>0.50||l>0.50)score=30;
  1869. if(score>=50){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1870. if(g_nearest_res>0&&(g_nearest_res-cur)<10*pip)score+=15;
  1871. if(g_nearest_sup>0&&(cur-g_nearest_sup)<10*pip)score+=15;}
  1872. return MathMin(100,score);
  1873. }
  1874.  
  1875. // FIX: Removed duplicate avg==0 check
  1876. double fVol(){
  1877. if(Bars<15)return 0;
  1878. double cur=(double)iVolume(NULL,PERIOD_M1,0);double avg=0;
  1879. for(int i=1;i<=10;i++)avg+=iVolume(NULL,PERIOD_M1,i);
  1880. if(avg==0)return 0;
  1881. avg/=10.0; // Clean division
  1882. double ratio=cur/avg;double score=0;
  1883. if(ratio>3.0)score=95;else if(ratio>2.5)score=85;else if(ratio>2.0)score=75;
  1884. else if(ratio>1.5)score=50;else if(ratio>1.2)score=30;
  1885. if(ratio<0.7)score=MathMin(score,15);
  1886. return MathMin(100,score);
  1887. }
  1888.  
  1889. double fMom(int s){
  1890. if(s+5>=Bars)return 0;
  1891. double m1=(Close[s]-Close[s+1]);double m2=(Close[s+1]-Close[s+2]);double m3=(Close[s+2]-Close[s+3]);
  1892. bool shift=false;if(m1>0&&m2<0&&m3<0)shift=true;if(m1<0&&m2>0&&m3>0)shift=true;
  1893. double accel=MathAbs(m1)-MathAbs(m2);
  1894. double body1=MathAbs(Close[s]-Open[s]);double body2=MathAbs(Close[s+1]-Open[s+1]);double body3=MathAbs(Close[s+2]-Open[s+2]);
  1895. double score=0;
  1896. if(shift){score=88;if(accel>0)score+=12;}
  1897. if(body1<body2&&body2<body3&&body3>0){score=60;if(body1<body2*0.5)score=80;}
  1898. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1899. if(MathAbs(m2)>3*pip&&MathAbs(m1)<pip)score=75;
  1900. return MathMin(100,score);
  1901. }
  1902.  
  1903. double fSpread(){
  1904. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);double typical=3.0;double ratio=sp/typical;double score=0;
  1905. if(ratio>3.0)score=95;else if(ratio>2.5)score=85;else if(ratio>2.0)score=75;
  1906. else if(ratio>1.5)score=50;else if(ratio>1.2)score=30;
  1907. int h=TimeHour(TimeGMT());if((h>=0&&h<=3)||(h>=12&&h<=14))score+=10;
  1908. return MathMin(100,score);
  1909. }
  1910.  
  1911. double fMem(int s){
  1912. if(s+10>=Bars)return 0;
  1913. bool bullEng=(Close[s]>Open[s])&&(Close[s+1]<Open[s+1])&&(Close[s]>=Open[s+1])&&(Open[s]<=Close[s+1]);
  1914. bool bearEng=(Close[s]<Open[s])&&(Close[s+1]>Open[s+1])&&(Close[s]<=Open[s+1])&&(Open[s]>=Close[s+1]);
  1915. double range=High[s]-Low[s];if(range<=0)return 0;
  1916. double body=MathAbs(Close[s]-Open[s]);double upperW=High[s]-MathMax(Open[s],Close[s]);double lowerW=MathMin(Open[s],Close[s])-Low[s];
  1917. bool pinUp=(body<range*0.3&&lowerW>range*0.5);bool pinDn=(body<range*0.3&&upperW>range*0.5);
  1918. double score=0;
  1919. if(bullEng)score=85;else if(bearEng)score=85;else if(pinUp)score=80;else if(pinDn)score=80;
  1920. if(score>=50){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1921. if(g_nearest_res>0&&(g_nearest_res-cur)<10*pip)score+=15;
  1922. if(g_nearest_sup>0&&(cur-g_nearest_sup)<10*pip)score+=15;}
  1923. return MathMin(100,score);
  1924. }
  1925.  
  1926. // FIX: Removed duplicate volP==0 check
  1927. double fTick(int s){
  1928. if(s+5>=Bars)return 0;
  1929. double volN=(double)iVolume(NULL,PERIOD_M1,s);double volP=(double)iVolume(NULL,PERIOD_M1,s+1);
  1930. if(volP==0)return 0; // Single clean check
  1931. double volRatio=volN/volP;double priceMove=MathAbs(Close[s]-Close[s+1]);
  1932. double pricePct=Close[s+1]>0?priceMove/Close[s+1]*100:0;double score=0;
  1933. if(volRatio>2.0&&pricePct<0.1)score=85;else if(volRatio>1.5&&pricePct<0.05)score=60;else if(volRatio>1.2&&pricePct<0.03)score=35;
  1934. return MathMin(100,score);
  1935. }
  1936.  
  1937. double fFrac(int s){
  1938. if(s+4>=Bars||s<2)return 0;
  1939. bool fHigh=(High[s]>High[s-1]&&High[s]>High[s-2]&&High[s]>High[s+1]&&High[s]>High[s+2]);
  1940. bool fLow=(Low[s]<Low[s-1]&&Low[s]<Low[s-2]&&Low[s]<Low[s+1]&&Low[s]<Low[s+2]);
  1941. if(!fHigh&&!fLow)return 0;double score=0;
  1942. if(fHigh){score=70;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double dist=MathAbs(High[s]-Close[0])/pip;
  1943. if(dist<15)score+=20;else if(dist<30)score+=10;if(Close[s]<High[s])score+=15;}
  1944. if(fLow){score=70;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double dist=MathAbs(Low[s]-Close[0])/pip;
  1945. if(dist<15)score+=20;else if(dist<30)score+=10;if(Close[s]>Low[s])score+=15;}
  1946. return MathMin(100,score);
  1947. }
  1948.  
  1949. void InitNW(){NW[0]=0.80;NW[1]=0.90;NW[2]=0.40;NW[3]=0.85;NW[4]=0.70;NW[5]=0.60;NW[6]=0.30;NW[7]=0.20;NW[8]=0.30;NW[9]=0.60;NW[10]=0.70;NW[11]=0.40;NW[12]=0.50;NW[13]=0.65;NW[14]=0.25;NW[15]=0.35;NW[16]=0.75;NW[17]=0.70;NW[18]=0.50;NW[19]=0.90;NW[20]=0.30;NW[21]=0.20;NW[22]=0.60;NW[23]=0.40;NW[24]=0.85;NW[25]=0.65;NW[26]=0.30;NW[27]=0.80;NW[28]=0.25;NW[29]=0.30;NW[30]=0.70;NW[31]=0.35;NW[32]=1.30;NW[33]=-0.80;NW[34]=1.10;NW[35]=-0.95;}
  1950.  
  1951. // ============================================================
  1952. // COMMON POINTS & WICK REJECTIONS
  1953. // ============================================================
  1954. void DetectCommonPoints(){
  1955. if(!SHOW_COMMON_POINTS){g_commonCount=0;return;}
  1956. if(Time[0]==g_lastCommonScan)return;g_lastCommonScan=Time[0];g_commonCount=0;
  1957. datetime nowBar=Time[0];int periodSec=PeriodSeconds(PERIOD_M1);
  1958. double m5h=iHigh(NULL,PERIOD_M5,iHighest(NULL,PERIOD_M5,MODE_HIGH,10,1));
  1959. double m5l=iLow(NULL,PERIOD_M5,iLowest(NULL,PERIOD_M5,MODE_LOW,10,1));
  1960. 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++;}
  1961. 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++;}
  1962. 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++;}
  1963. 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++;}
  1964. 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--;}
  1965. }
  1966.  
  1967. void DrawCommonPoints(){
  1968. return; // BLUE CALL/PUT ZONE LINES REMOVED
  1969. 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));}
  1970. if(!SHOW_COMMON_POINTS||g_commonCount==0)return;
  1971. for(int i=0;i<g_commonCount;i++){
  1972. if(TimeCurrent()>g_commonExpire[i])continue;if(IsLineNearby(g_commonPrice[i],8.0))continue;
  1973. string id=PFX+"COMMON_"+IntegerToString(i),idL=PFX+"COMMON_L_"+IntegerToString(i),idH=PFX+"COMMON_HL_"+IntegerToString(i);color zc=DARK_BLUE_NEON;
  1974. 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);
  1975. ObjectCreate(0,id,OBJ_TEXT,0,Time[0],g_commonPrice[i]);ObjectSetText(id,"*",16,"Arial",zc);ObjectSetInteger(0,id,OBJPROP_BACK,false);
  1976. string lbl=(g_commonType[i]=="CALL")?"^ CALL ZONE":"v PUT ZONE";
  1977. 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);
  1978. }
  1979. }
  1980.  
  1981.  
  1982.  
  1983.  
  1984. int CalcAdvancedMicroTrap(){
  1985. if(Bars<20) return 0;
  1986. int score=0;
  1987. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  1988. double r1=High[1]-Low[1];
  1989. if(r1>0){
  1990. double uw1=(High[1]-MathMax(Open[1],Close[1]))/r1;
  1991. double lw1=(MathMin(Open[1],Close[1])-Low[1])/r1;
  1992. if(uw1>0.65) score+=35; else if(uw1>0.50) score+=18;
  1993. if(lw1>0.65) score+=25; else if(lw1>0.50) score+=12;
  1994. }
  1995. double r0=High[0]-Low[0];
  1996. if(r0>0){double uw0=(High[0]-MathMax(Open[0],Close[0]))/r0; double lw0=(MathMin(Open[0],Close[0])-Low[0])/r0; if(uw0>0.60) score+=15; if(lw0>0.60) score+=10;}
  1997. double v1=(double)iVolume(NULL,PERIOD_M1,1); double v2=(double)iVolume(NULL,PERIOD_M1,2); double v3=(double)iVolume(NULL,PERIOD_M1,3); double vAvg=(v2+v3)/2.0;
  1998. if(vAvg>0){double vr=v1/vAvg; if(vr>3.0) score+=30; else if(vr>2.0) score+=22; else if(vr>1.5) score+=12;}
  1999. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  2000. if(rsi>82||rsi<18) score+=25; else if(rsi>75||rsi<25) score+=18; else if(rsi>70||rsi<30) score+=10;
  2001. if(g_nearest_res>0 && (High[1]>g_nearest_res && Close[1]<g_nearest_res)) score+=30;
  2002. if(g_nearest_sup>0 && (Low[1]<g_nearest_sup && Close[1]>g_nearest_sup)) score+=30;
  2003. bool aG=(Close[1]>Open[1] && Close[2]>Open[2] && Close[3]>Open[3]);
  2004. bool aR=(Close[1]<Open[1] && Close[2]<Open[2] && Close[3]<Open[3]);
  2005. if(aG||aR) score+=18;
  2006. if(Bars>=5){bool aG4=(aG && Close[4]>Open[4]); bool aR4=(aR && Close[4]<Open[4]); if(aG4||aR4) score+=10;}
  2007. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);
  2008. if(sp>BROKER_SPREAD_THRESHOLD*2.0) score+=15; else if(sp>BROKER_SPREAD_THRESHOLD*1.5) score+=8;
  2009. if(g_hrn_price>0){double distH=MathAbs(Close[0]-g_hrn_price)/pip; if(distH<3) score+=20; else if(distH<7) score+=10;}
  2010. return MathMin(100,score);
  2011. }
  2012.  
  2013. double CalcAdvancedTFA(string &detail){
  2014. if(Bars<30){detail="Bars low";return 3.0;}
  2015. double score=0;string p="";
  2016. double adx=iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);
  2017. double adxSc=(adx>=25)?1.0:(adx>=20)?0.6:(adx>=15)?0.3:0.1;
  2018. score+=adxSc;p+="ADX:"+((adxSc>=0.6)?"+":"-")+" ";
  2019. double v1=iVolume(NULL,PERIOD_M1,1),vA=0;
  2020. for(int i=2;i<=6;i++)vA+=iVolume(NULL,PERIOD_M1,i);
  2021. vA=(vA>0)?vA/5.0:1;double vr=v1/vA;
  2022. double volSc=(vr>=2.0)?1.0:(vr>=1.5)?0.7:(vr>=1.1)?0.4:0.1;
  2023. score+=volSc;p+="VOL:"+((volSc>=0.5)?"+":"-")+" ";
  2024. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  2025. double rsiSc=0.2;if(rsi>=60||rsi<=40)rsiSc=1.0;else if(rsi>=55||rsi<=45)rsiSc=0.6;
  2026. score+=rsiSc;p+="RSI:"+((rsiSc>=0.6)?"+":"-")+" ";
  2027. double atr=iATR(NULL,PERIOD_M1,14,1),cs=High[1]-Low[1],atrSc=0.2;
  2028. 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;}
  2029. score+=atrSc;p+="ATR:"+((atrSc>=0.6)?"+":"-")+" ";
  2030. double body=MathAbs(Close[1]-Open[1]),range=High[1]-Low[1],bdySc=0.2;
  2031. 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;}
  2032. score+=bdySc;p+="BODY:"+((bdySc>=0.6)?"+":"-")+" ";
  2033. double sesSc=IsSessionActive()?1.0:0.4;
  2034. score+=sesSc;p+="SES:"+((sesSc>=0.7)?"+":"-");
  2035. double finalScore=MathMin(6.0,score);int rounded=(int)MathRound(finalScore);
  2036. string dir="MIX";
  2037. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5")dir="STRONG UP";
  2038. else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5")dir="STRONG DOWN";
  2039. else if(g_haM1=="HA BULLISH 1")dir="UP";
  2040. else if(g_haM1=="HA BEARISH 1")dir="DOWN";
  2041. detail=p+" | "+IntegerToString(rounded)+"/6 "+dir;
  2042. g_tfa_detail=detail;return finalScore;
  2043. }
  2044.  
  2045. string CalcAdvancedMarketBias(color &bC,double brain,double rsc){
  2046. if(Bars<30){bC=NEON_YELLOW;return "CALCULATING";}
  2047. double score=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  2048. double pc=Close[0]-Close[10];
  2049. 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;
  2050. double hh1=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],hh2=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];
  2051. double ll1=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],ll2=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];
  2052. 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;
  2053. double ema20=0,ema20p=0;for(int i=0;i<20;i++)ema20+=Close[i];ema20/=20;
  2054. for(int i=1;i<=20&&i<Bars;i++)ema20p+=Close[i];ema20p/=20;
  2055. if(ema20>ema20p)score+=3;else if(ema20<ema20p)score-=3;
  2056. double ema50=0;for(int i=0;i<50&&i<Bars;i++)ema50+=Close[i];ema50/=MathMin(50,Bars);
  2057. if(ema20>ema50)score+=3;else score-=3;
  2058. 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++;}
  2059. if(gC>=7)score+=3;else if(rC>=7)score-=3;else if(gC>=5)score+=1;else if(rC>=5)score-=1;
  2060. 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");
  2061. if(m1B&&m5B)score+=4;else if(m1Be&&m5Be)score-=4;else if(m1B)score+=2;else if(m1Be)score-=2;
  2062. 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);
  2063. if(adx>20){if(pDI>mDI+5)score+=4;else if(mDI>pDI+5)score-=4;}
  2064. if(brain>3)score-=2;else if(brain<-3)score+=2;
  2065. if(Close[1]>Open[1])score+=1;else if(Close[1]<Open[1])score-=1;
  2066. if(rsc>2)score+=1;else if(rsc<-2)score-=1;
  2067. 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";}
  2068. 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";}
  2069. bC=NEON_YELLOW;return "NEUTRAL";
  2070. }
  2071.  
  2072.  
  2073.  
  2074. // ============================================================
  2075. // OTC BROKER KILLER v4.0 - FIXED GANN STATIC TRAP ENGINE
  2076. // ============================================================
  2077. void CalcVisualTrapPro(){
  2078. // --- INITIAL RESETS ---
  2079. g_visualTrapPro.boxStatus = "NO BOX";
  2080. g_visualTrapPro.gannStatus = "--";
  2081. g_visualTrapPro.gannColor = CGR;
  2082. g_visualTrapPro.m30Dir = "M30:NOT NEEDED";
  2083. g_visualTrapPro.m1Seq = "M1:--";
  2084. g_visualTrapPro.verdict = "SCANNING";
  2085. g_visualTrapPro.verdictColor = CGR;
  2086. g_visualTrapPro.callBias = 50;
  2087. g_visualTrapPro.putBias = 50;
  2088. g_visualTrapPro.trapScore = 0;
  2089. g_visualTrapPro.buySignal = false;
  2090. g_visualTrapPro.sellSignal = false;
  2091.  
  2092. SafeDel(PFX+"VBOX");
  2093. SafeDel(PFX+"VMID");
  2094. SafeDel(PFX+"VSIG");
  2095.  
  2096. int m1B = 0, m1R = 0;
  2097. for(int i = 1; i <= 8 && i < Bars; i++){
  2098. if(Close[i] > Open[i]) m1B++;
  2099. else if(Close[i] < Open[i]) m1R++;
  2100. }
  2101. int total = m1B + m1R;
  2102. if(total > 0){
  2103. g_visualTrapPro.callBias = NormalizeDouble((double)m1B / total * 100.0, 1);
  2104. g_visualTrapPro.putBias = NormalizeDouble((double)m1R / total * 100.0, 1);
  2105. }
  2106.  
  2107. string seq = "";
  2108. for(int i = 1; i <= 8 && i < Bars; i++){
  2109. if(Close[i] > Open[i]) seq += "G";
  2110. else if(Close[i] < Open[i]) seq += "R";
  2111. else seq += "D";
  2112. }
  2113. g_visualTrapPro.m1Seq = "M1:" + seq;
  2114.  
  2115. double pip = GetUniversalPip();
  2116. if(pip <= 0) pip = Point;
  2117. if(pip <= 0) pip = 0.0001;
  2118.  
  2119. static double s_boxHigh = 0;
  2120. static double s_boxLow = 0;
  2121. static datetime s_boxTime = 0;
  2122.  
  2123. int hI = iHighest(Symbol(), PERIOD_M1, MODE_HIGH, 25, 1);
  2124. int lI = iLowest(Symbol(), PERIOD_M1, MODE_LOW, 25, 1);
  2125.  
  2126. if(hI >= 0 && lI >= 0 && Bars > 25) {
  2127. double currentHigh = High[hI];
  2128. double currentLow = Low[lI];
  2129.  
  2130. bool newExtreme = (s_boxHigh == 0) ||
  2131. (currentHigh > s_boxHigh) ||
  2132. (currentLow < s_boxLow) ||
  2133. (Time[0] - s_boxTime > 900);
  2134.  
  2135. if(newExtreme) {
  2136. s_boxHigh = currentHigh;
  2137. s_boxLow = currentLow;
  2138. s_boxTime = Time[0];
  2139. }
  2140.  
  2141. // ✅ NEW: Gann Box boundaries ko global mein save karo Fib ke liye
  2142. g_gannBoxHigh = s_boxHigh;
  2143. g_gannBoxLow = s_boxLow;
  2144.  
  2145. double bH = s_boxHigh;
  2146. double bL = s_boxLow;
  2147. double bHt = bH - bL;
  2148. double bP = bHt / pip;
  2149.  
  2150. g_visualTrapPro.boxStatus = "GANN:" + DoubleToString(bP,1) + "p";
  2151.  
  2152. double mid = bL + (bHt / 2.0);
  2153. double dist = MathAbs(Close[0] - mid) / pip;
  2154. double pricePos = ((Close[0] - bL) / bHt) * 100.0;
  2155. double body = MathAbs(Close[0] - Open[0]) / pip;
  2156.  
  2157. g_visualTrapPro.trapScore = 0;
  2158.  
  2159. if(dist < 1.5) g_visualTrapPro.trapScore += 30;
  2160. else if(dist < 3.0) g_visualTrapPro.trapScore += 15;
  2161. else g_visualTrapPro.trapScore += 5;
  2162.  
  2163. if(pricePos > 40 && pricePos < 60) g_visualTrapPro.trapScore += 20;
  2164.  
  2165. if(bP < 10) g_visualTrapPro.trapScore += 20;
  2166. else if(bP < 18) g_visualTrapPro.trapScore += 10;
  2167.  
  2168. if(body < 1.0) g_visualTrapPro.trapScore += 15;
  2169. else if(body < 2.0) g_visualTrapPro.trapScore += 10;
  2170.  
  2171. double upperWick = (High[0] - MathMax(Open[0], Close[0])) / pip;
  2172. double lowerWick = (MathMin(Open[0], Close[0]) - Low[0]) / pip;
  2173. if(upperWick > 1.0 && lowerWick > 1.0) g_visualTrapPro.trapScore += 15;
  2174.  
  2175. if(g_visualTrapPro.trapScore > 100) g_visualTrapPro.trapScore = 100;
  2176.  
  2177. if(g_visualTrapPro.trapScore >= 70) {
  2178. g_visualTrapPro.gannStatus = "[TRAP " + DoubleToString(g_visualTrapPro.trapScore,0) + "%]";
  2179. g_visualTrapPro.gannColor = NEON_YELLOW;
  2180. }
  2181. else if(g_visualTrapPro.trapScore >= 45) {
  2182. g_visualTrapPro.gannStatus = "[WEAK " + DoubleToString(g_visualTrapPro.trapScore,0) + "%]";
  2183. g_visualTrapPro.gannColor = NEON_ORANGE;
  2184. }
  2185. else {
  2186. g_visualTrapPro.gannStatus = "[GANN " + DoubleToString(g_visualTrapPro.trapScore,0) + "%]";
  2187. g_visualTrapPro.gannColor = NEON_CYAN;
  2188. }
  2189.  
  2190. datetime tStart = Time[25];
  2191. datetime tEnd = Time[0] + 60;
  2192.  
  2193. ObjectCreate(0, PFX+"VBOX", OBJ_RECTANGLE, 0, tStart, bH, tEnd, bL);
  2194. ObjectSetInteger(0, PFX+"VBOX", OBJPROP_COLOR, clrWhite);
  2195. ObjectSetInteger(0, PFX+"VBOX", OBJPROP_WIDTH, 2);
  2196. ObjectSetInteger(0, PFX+"VBOX", OBJPROP_BACK, false);
  2197.  
  2198. ObjectCreate(0, PFX+"VMID", OBJ_HLINE, 0, 0, mid);
  2199. ObjectSetInteger(0, PFX+"VMID", OBJPROP_COLOR, clrYellow);
  2200. ObjectSetInteger(0, PFX+"VMID", OBJPROP_WIDTH, 1);
  2201. ObjectSetInteger(0, PFX+"VMID", OBJPROP_BACK, false);
  2202.  
  2203. } else {
  2204. g_visualTrapPro.boxStatus = "GANN:NO DATA";
  2205. g_gannBoxHigh = 0; // ✅ No box, reset globals
  2206. g_gannBoxLow = 0;
  2207. }
  2208.  
  2209. if(g_visualTrapPro.trapScore >= 70) {
  2210. if(g_visualTrapPro.callBias >= 60) {
  2211. g_visualTrapPro.verdict = "PUT " + DoubleToString(g_visualTrapPro.putBias,0) + "%";
  2212. g_visualTrapPro.verdictColor = NEON_RED;
  2213. g_visualTrapPro.sellSignal = true;
  2214. g_visualTrapPro.buySignal = false;
  2215. }
  2216. else if(g_visualTrapPro.putBias >= 60) {
  2217. g_visualTrapPro.verdict = "CALL " + DoubleToString(g_visualTrapPro.callBias,0) + "%";
  2218. g_visualTrapPro.verdictColor = NEON_GREEN;
  2219. g_visualTrapPro.buySignal = true;
  2220. g_visualTrapPro.sellSignal = false;
  2221. }
  2222. else {
  2223. g_visualTrapPro.verdict = "TRAP " + DoubleToString(g_visualTrapPro.trapScore,0) + "%";
  2224. g_visualTrapPro.verdictColor = NEON_YELLOW;
  2225. }
  2226. }
  2227. else if(g_visualTrapPro.callBias >= 65) {
  2228. g_visualTrapPro.verdict = "CALL " + DoubleToString(g_visualTrapPro.callBias,0) + "%";
  2229. g_visualTrapPro.verdictColor = NEON_GREEN;
  2230. g_visualTrapPro.buySignal = true;
  2231. }
  2232. else if(g_visualTrapPro.putBias >= 65) {
  2233. g_visualTrapPro.verdict = "PUT " + DoubleToString(g_visualTrapPro.putBias,0) + "%";
  2234. g_visualTrapPro.verdictColor = NEON_RED;
  2235. g_visualTrapPro.sellSignal = true;
  2236. }
  2237. else {
  2238. g_visualTrapPro.verdict = "WAIT " + DoubleToString(g_visualTrapPro.callBias,0) + "/" + DoubleToString(g_visualTrapPro.putBias,0);
  2239. g_visualTrapPro.verdictColor = CGR;
  2240. }
  2241.  
  2242. if((g_visualTrapPro.buySignal || g_visualTrapPro.sellSignal) && g_lastTrapArrowTime != Time[0]){
  2243. g_lastTrapArrowTime = Time[0];
  2244. double arrowY = g_visualTrapPro.buySignal ? Low[0] - pip*5 : High[0] + pip*5;
  2245. ObjectCreate(0, PFX+"VSIG", OBJ_ARROW, 0, Time[0], arrowY);
  2246. ObjectSetInteger(0, PFX+"VSIG", OBJPROP_ARROWCODE, g_visualTrapPro.buySignal ? 233 : 234);
  2247. ObjectSetInteger(0, PFX+"VSIG", OBJPROP_COLOR, g_visualTrapPro.buySignal ? NEON_GREEN : NEON_RED);
  2248. ObjectSetInteger(0, PFX+"VSIG", OBJPROP_WIDTH, 3);
  2249. ObjectSetInteger(0, PFX+"VSIG", OBJPROP_BACK, false);
  2250. }
  2251. }
  2252.  
  2253. // ============================================================
  2254. // HIDDEN LEVELS & HELPERS
  2255. // ============================================================
  2256. void DetectHiddenLevels(){
  2257. g_htfLevelCount=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double curP=Close[0];
  2258. double rU=MathCeil(curP/(10*pip))*(10*pip),rD=MathFloor(curP/(10*pip))*(10*pip);
  2259. if(MathAbs(curP-rU)/pip<=15)AddLevel(rU,"ROUND","RESISTANCE",TimeCurrent(),8,true);
  2260. if(MathAbs(curP-rD)/pip<=15)AddLevel(rD,"ROUND","SUPPORT",TimeCurrent(),8,true);
  2261. DetectHTFLevels(PERIOD_M5,"M5",3,pip);DetectHTFLevels(PERIOD_M15,"M15",5,pip);
  2262. DetectHTFLevels(PERIOD_M30,"M30",7,pip);DetectHTFLevels(PERIOD_H1,"H1",9,pip);
  2263. SortLevelsByStrength();
  2264. }
  2265.  
  2266. void DetectHTFLevels(ENUM_TIMEFRAMES tf,string tfN,int bS,double pip){
  2267. int bars=MathMin(50,iBars(NULL,tf));if(bars<10)return;double curP=Close[0];
  2268. for(int i=2;i<bars-2;i++){
  2269. 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);}
  2270. 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);}
  2271. }
  2272. }
  2273.  
  2274. bool IsSwingHigh(ENUM_TIMEFRAMES tf,int p,int lb){double h=iHigh(NULL,tf,p);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;}return true;}
  2275. bool IsSwingLow(ENUM_TIMEFRAMES tf,int p,int lb){double l=iLow(NULL,tf,p);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;}return true;}
  2276.  
  2277. void AddLevel(double price,string tf,string type,datetime time,int strength,bool isRound){
  2278. if(g_htfLevelCount>=50)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  2279. for(int i=0;i<g_htfLevelCount;i++)if(MathAbs(g_htfLevels[i].price-price)/pip<5)return;
  2280. g_htfLevels[g_htfLevelCount].price=price;g_htfLevels[g_htfLevelCount].timeframe=tf;g_htfLevels[g_htfLevelCount].type=type;
  2281. g_htfLevels[g_htfLevelCount].time=time;g_htfLevels[g_htfLevelCount].strength=strength;g_htfLevels[g_htfLevelCount].isRoundNumber=isRound;g_htfLevelCount++;
  2282. }
  2283.  
  2284. void SortLevelsByStrength(){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;}}
  2285.  
  2286. void DrawHiddenLevels(){
  2287. for(int i=0;i<50;i++){SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i));SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i)+"_LBL");}
  2288. int dc=MathMin(10,g_htfLevelCount);int drawn=0;
  2289. for(int i=0;i<dc&&drawn<5;i++){
  2290. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  2291. if(MathAbs(Close[0]-g_htfLevels[i].price)/pip>30)continue;if(IsLineNearby(g_htfLevels[i].price,5.0))continue;
  2292. string nm=PFX+"HTF_LEVEL_"+IntegerToString(drawn);
  2293. color lc=g_htfLevels[i].isRoundNumber?NEON_PURPLE:(StringFind(g_htfLevels[i].type,"SUPPORT")>=0?NEON_GREEN:NEON_RED);
  2294. 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);
  2295. string ln=nm+"_LBL";ObjectCreate(0,ln,OBJ_TEXT,0,Time[0],g_htfLevels[i].price);
  2296. ObjectSetText(ln,g_htfLevels[i].timeframe+" "+g_htfLevels[i].type+" S:"+IntegerToString(g_htfLevels[i].strength),9,"Arial",lc);drawn++;
  2297. }
  2298. }
  2299.  
  2300. string DetectSuddenReversal(){
  2301. if(Bars<10)return "";bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  2302. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);double atr=iATR(NULL,PERIOD_M1,14,1);if(atr<=0)return "";
  2303. double rng1=High[1]-Low[1];double body1=MathAbs(Close[1]-Open[1]);
  2304. double uw1=(rng1>0)?(High[1]-MathMax(Open[1],Close[1]))/rng1:0;double lw1=(rng1>0)?(MathMin(Open[1],Close[1])-Low[1])/rng1:0;
  2305. bool bull1=(Close[1]>Open[1]);bool bear1=(Close[1]<Open[1]);
  2306. int bullScore=0,bearScore=0;
  2307. if(lw1>0.60&&body1<rng1*0.30&&rsi<48){bullScore+=30;}
  2308. if(uw1>0.60&&body1<rng1*0.30&&rsi>52){bearScore+=30;}
  2309. if(bull1&&body1>MathAbs(Close[2]-Open[2])*1.1&&Close[2]<Open[2]&&body1>atr*0.35){bullScore+=25;}
  2310. if(bear1&&body1>MathAbs(Close[2]-Open[2])*1.1&&Close[2]>Open[2]&&body1>atr*0.35){bearScore+=25;}
  2311. if(rsi<30&&bullScore>0){bullScore+=15;}if(rsi>70&&bearScore>0){bearScore+=15;}
  2312. if(g_nearest_sup>0&&(Close[0]-g_nearest_sup)/pip<8&&bullScore>0){bullScore+=15;}
  2313. if(g_nearest_res>0&&(g_nearest_res-Close[0])/pip<8&&bearScore>0){bearScore+=15;}
  2314. if(bullScore>=40&&bullScore>bearScore){return "BULL REVERSAL";}
  2315. else if(bearScore>=40&&bearScore>bullScore){return "BEAR REVERSAL";}
  2316. return "";
  2317. }
  2318.  
  2319. void SafeDel(string id){if(ObjectFind(0,id)>=0)ObjectDelete(0,id);}
  2320. 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_DOT);ObjectSetInteger(0,id,OBJPROP_BACK,false);}
  2321. void HideSPM(){for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX)==0)continue;if(StringFind(nm,PFX+"candle_timer")>=0)continue;if(StringFind(nm,"_R")>=0||StringFind(nm,"_M")>=0||StringFind(nm,"_L")>=0||StringFind(nm,"_X")>=0||StringFind(nm,"MOB3_")>=0||StringFind(nm,"SWTL_")>=0||StringFind(nm,"DYN_SHIFT")>=0)continue;int tp=(int)ObjectGetInteger(0,nm,OBJPROP_TYPE);if(tp==OBJ_LABEL||tp==OBJ_RECTANGLE_LABEL||tp==OBJ_TEXT)ObjectDelete(0,nm);}}
  2322. void NuclearDeleteAll(){for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX+"candle_timer")>=0)continue;int tp=(int)ObjectGetInteger(0,nm,OBJPROP_TYPE);if(tp==OBJ_HLINE||tp==OBJ_LABEL||tp==OBJ_RECTANGLE_LABEL||tp==OBJ_TEXT)ObjectDelete(0,nm);}}
  2323. void DelDashboard(){
  2324. for(int i=ObjectsTotal()-1;i>=0;i--){
  2325. string nm=ObjectName(i);
  2326. if(StringFind(nm,PFX)!=0)continue;
  2327. if(StringFind(nm,"OBP3_")>=0)continue;
  2328. if(StringFind(nm,"HRN_LINE")>=0)continue;
  2329. if(StringFind(nm,"candle_timer")>=0)continue;
  2330. if(StringFind(nm,"HTF_LEVEL")>=0)continue;
  2331. if(StringFind(nm,"COMMON_")>=0)continue;
  2332. if(StringFind(nm,"WICK_")>=0)continue;
  2333. if(StringFind(nm,"BB_")>=0)continue;
  2334. if(StringFind(nm,"NEAREST_RES")>=0)continue;
  2335. if(StringFind(nm,"NEAREST_SUP")>=0)continue;
  2336. if(StringFind(nm,"VBOX")>=0)continue;
  2337. if(StringFind(nm,"VMID")>=0)continue;
  2338. if(StringFind(nm,"VSIG")>=0)continue;
  2339. if(StringFind(nm,"FIB_REJ")>=0)continue;
  2340. if(StringFind(nm,"OTC_FIB")>=0)continue; // ✅ FIB LINE FIX
  2341. if(StringFind(nm,"DYN_SHIFT")>=0)continue; // ✅ DYNAMIC SR FIX
  2342. ObjectDelete(nm);
  2343. }
  2344. }
  2345.  
  2346.  
  2347.  
  2348. // ============================================================
  2349. // ML & ACCURACY
  2350. // ============================================================
  2351. void LoadMLWeights(){if(!ML_ADAPTIVE)return;g_symbolKey=Symbol();string fn="ML_weights_"+g_symbolKey+".dat";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;}
  2352. void SaveMLWeights(){if(!ML_ADAPTIVE)return;string fn="ML_weights_"+g_symbolKey+".dat";int h=FileOpen(fn,FILE_WRITE|FILE_TXT);if(h!=INVALID_HANDLE){FileWriteString(h,DoubleToString(g_brokerBias,6));FileClose(h);}}
  2353. void UpdateMLWeights(bool win){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);if(g_brokerBias>0.5)g_brokerBias=0.5;if(g_brokerBias<-0.5)g_brokerBias=-0.5;SaveMLWeights();}
  2354. void UpdateAccuracy(){if(Bars<5||Time[0]==g_acc_lastBar)return;g_acc_lastBar=Time[0];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);if(win){g_acc_correct++;g_lossStreak=0;}else g_lossStreak++;g_acc_total++;bool wasFibTrap=(g_fibV2_Pattern=="FAKE"||g_fibV2_Pattern=="EXHAUST");UpdateBayesianPrior(win,wasFibTrap);if(g_acc_total>0)g_accuracy=MathMax(40.0,MathMin(90.0,(double)g_acc_correct/g_acc_total*100.0));if(ML_ADAPTIVE&&g_lastPredGreen>50.0&&g_lastPredGreen<100.0)UpdateMLWeights(win);}g_lastPredGreen=g_frozen_gProb;g_lastPredBar=Time[0];}
  2355. 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;}
  2356. 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]));}}
  2357.  
  2358. 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";}
  2359. 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;}
  2360. 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;}
  2361. 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;}
  2362. 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;}
  2363. double GetADXStrength(){if(Bars<20)return 0;return iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);}
  2364. 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;}
  2365. 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;}
  2366. 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;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++;}}}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;}}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;}
  2367.  
  2368. // ============================================================
  2369. // HRN LEVEL (Kept for chart line only, cube removed)
  2370. // ============================================================
  2371. double FindBestHiddenRoundNumber(double curPrice){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;double levels[];int levelCount=0;ArrayResize(levels,100);double b100=MathFloor(curPrice/(100*pip))*(100*pip);for(int i=-1;i<=2;i++)AddRNLevel(levels,levelCount,b100+i*100*pip);double b50=MathFloor(curPrice/(50*pip))*(50*pip);for(int i=-2;i<=3;i++)AddRNLevel(levels,levelCount,b50+i*50*pip);double b25=MathFloor(curPrice/(25*pip))*(25*pip);for(int i=-3;i<=4;i++)AddRNLevel(levels,levelCount,b25+i*25*pip);double b10=MathFloor(curPrice/(10*pip))*(10*pip);for(int i=-5;i<=6;i++)AddRNLevel(levels,levelCount,b10+i*10*pip);double b5=MathFloor(curPrice/(5*pip))*(5*pip);for(int i=-4;i<=5;i++)AddRNLevel(levels,levelCount,b5+i*5*pip);double bestLevel=0,bestScore=-1;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;}}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);double d100=MathAbs(curPrice-n100),d50=MathAbs(curPrice-n50),d25=MathAbs(curPrice-n25),d10=MathAbs(curPrice-n10);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;}g_hrn_score=bestScore;return NormalizeDouble(bestLevel,dg);}
  2372. 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++;}
  2373. double ScoreRoundNumber(double level,double pip){double score=0,tol=3*pip;int touches=0,ru=0,rd=0;for(int i=1;i<=LOOKBACK&&i<Bars;i++){double h=High[i],l=Low[i],c=Close[i],o=Open[i];if(h>=level-tol&&l<=level+tol){touches++;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;}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;}}}score+=touches*3;score+=ru*8+rd*8;if(touches>=4)score+=15;else if(touches>=3)score+=10;else if(touches>=2)score+=5;return score;}
  2374.  
  2375. void ScanHRNLevels(){g_rj_cnt=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];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];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++;}}}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++;}}}}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;}}
  2376.  
  2377. void UpdateHRN(){
  2378. if(Bars<LOOKBACK+10)return;
  2379. 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;
  2380. 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];}
  2381. double tol=1*pip;
  2382. 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;}}
  2383. 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;}}
  2384. 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;}}
  2385. g_hrn_str=DoubleToString(NormalizeDouble(g_hrn_price,dg),dg);
  2386. color hrnColor=g_hrn_is_sup?NEON_GREEN:NEON_RED;if(g_hrn_brk_bars==1)hrnColor=NEON_YELLOW;
  2387. if(SHOW_HRN_LINE){
  2388. DrawHLine(PFX+"HRN_LINE",g_hrn_price,hrnColor,3,STYLE_SOLID);
  2389. 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?" [SHIFT]":"");
  2390. ObjectCreate(0,ln,OBJ_TEXT,0,Time[0]+Period()*60*3,g_hrn_price);ObjectSetText(ln,"HRN "+ShortPrice(g_hrn_price)+" "+typeStr+confStr,9,"Arial Bold",hrnColor);ObjectSetInteger(0,ln,OBJPROP_BACK,false);
  2391. } else {
  2392. SafeDel(PFX+"HRN_LINE");
  2393. SafeDel(PFX+"HRN_LBL");
  2394. }
  2395. }
  2396.  
  2397. // ============================================================
  2398. // CANDLE PATTERN RECOGNITION (FIXED BUG)
  2399. // ============================================================
  2400. NCPPatternResult NCPCheckSinglePattern(int shift){
  2401. NCPPatternResult r; r.name=""; r.type=0; r.strength=0; r.category="SINGLE";
  2402. if(shift+1>=Bars || shift < 0) return r;
  2403. double o=Open[shift],c=Close[shift],h=High[shift],l=Low[shift];
  2404. double body=MathAbs(c-o); double range=h-l;if(range<=0 || o==0 || c==0) return r;
  2405. double upperWick=h-MathMax(o,c); double lowerWick=MathMin(o,c)-l;
  2406. double bodyRatio=body/range; double uwRatio=upperWick/range; double lwRatio=lowerWick/range;
  2407. bool isBull=(c>o); bool isBear=(c<o);
  2408. if(bodyRatio<0.10){
  2409. if(uwRatio>0.35&&lwRatio>0.35){r.name="DOJI"; r.type=0; r.strength=2;if(uwRatio>0.60&&lwRatio<0.15){r.name="GRAVESTONE DOJI";r.type=-1;r.strength=4;}else if(lwRatio>0.60&&uwRatio<0.15){r.name="DRAGONFLY DOJI";r.type=1;r.strength=4;}else if(uwRatio>0.40&&lwRatio>0.40){r.name="LONG-LEG DOJI";r.type=0;r.strength=3;}return r;}
  2410. if(body<range*0.05&&upperWick<range*0.10&&lowerWick<range*0.10){r.name="4-PRICE DOJI";r.type=0;r.strength=1;return r;}}
  2411. if(lwRatio>0.55&&uwRatio<0.15&&bodyRatio>0.15&&bodyRatio<0.40){r.name="HAMMER";r.type=1;r.strength=4;if(g_nearest_sup>0&&MathAbs(l-g_nearest_sup)<5*(StringFind(Symbol(),"JPY")>=0?0.01:0.0001)){r.strength=5;r.name="HAMMER@SUP";}return r;}
  2412. if(uwRatio>0.55&&lwRatio<0.15&&isBull&&bodyRatio>0.10&&bodyRatio<0.35){r.name="INV HAMMER";r.type=1;r.strength=3;return r;}
  2413. if(uwRatio>0.55&&lwRatio<0.15&&isBear&&bodyRatio>0.15&&bodyRatio<0.40){r.name="SHOOT STAR";r.type=-1;r.strength=4;if(g_nearest_res>0&&MathAbs(h-g_nearest_res)<5*(StringFind(Symbol(),"JPY")>=0?0.01:0.0001)){r.strength=5;r.name="SHOOT@RES";}return r;}
  2414. if(lwRatio>0.55&&uwRatio<0.15&&isBear&&bodyRatio>0.15&&bodyRatio<0.40){r.name="HANG MAN";r.type=-1;r.strength=3;return r;}
  2415. if(bodyRatio>0.10&&bodyRatio<0.30&&uwRatio>0.25&&lwRatio>0.25){r.name="SPIN TOP";r.type=0;r.strength=1;return r;}
  2416. if(bodyRatio>0.75){if(isBull){r.name="BULL MARUBOZU";r.type=1;r.strength=5;}else{r.name="BEAR MARUBOZU";r.type=-1;r.strength=5;}return r;}
  2417. if(bodyRatio<0.20&&uwRatio>0.30&&lwRatio>0.30){r.name="HIGH WAVE";r.type=0;r.strength=2;return r;}
  2418. return r;
  2419. }
  2420.  
  2421. NCPPatternResult NCPCheckDualPattern(int shift){
  2422. NCPPatternResult r; r.name=""; r.type=0; r.strength=0; r.category="DUAL";
  2423. if(shift+2>=Bars || shift < 0) return r;
  2424. if(Open[shift]==0||Close[shift]==0||Open[shift+1]==0||Close[shift+1]==0) return r;
  2425. double o1=Open[shift],c1=Close[shift],h1=High[shift],l1=Low[shift];
  2426. double o2=Open[shift+1],c2=Close[shift+1],h2=High[shift+1],l2=Low[shift+1];
  2427. double body1=MathAbs(c1-o1); double body2=MathAbs(c2-o2); if(body1<=0||body2<=0) return r;
  2428. bool bull1=(c1>o1), bear1=(c1<o1); bool bull2=(c2>o2), bear2=(c2<o2);
  2429. if(bull1&&bear2&&c1>=o2&&o1<=c2&&body1>body2){r.name="BULL ENGULF";r.type=1;r.strength=5;if(g_nearest_sup>0){double pip=(StringFind(Symbol(),"JPY")>=0)?0.01:0.0001;if(MathAbs(l1-g_nearest_sup)<8*pip||MathAbs(l2-g_nearest_sup)<8*pip){r.strength=6;r.name="BULL ENGULF@SUP";}}return r;}
  2430. if(bear1&&bull2&&c1<=o2&&o1>=c2&&body1>body2){r.name="BEAR ENGULF";r.type=-1;r.strength=5;if(g_nearest_res>0){double pip=(StringFind(Symbol(),"JPY")>=0)?0.01:0.0001;if(MathAbs(h1-g_nearest_res)<8*pip||MathAbs(h2-g_nearest_res)<8*pip){r.strength=6;r.name="BEAR ENGULF@RES";}}return r;}
  2431. double tol=(h1+l1)*0.001;
  2432. if(bull1&&bear2&&MathAbs(h1-h2)<tol&&h1>c1&&h2>c2){r.name="TWEEZER TOP";r.type=-1;r.strength=4;return r;}
  2433. if(bear1&&bull2&&MathAbs(l1-l2)<tol&&l1<c1&&l2<c2){r.name="TWEEZER BTM";r.type=1;r.strength=4;return r;}
  2434. if(bull1&&bear2&&o1<l2&&c1>(o2+c2)/2.0&&c1<o2){r.name="PIERCING";r.type=1;r.strength=3;return r;}
  2435. if(bear1&&bull2&&o1>h2&&c1<(o2+c2)/2.0&&c1>o2){r.name="DARK CLOUD";r.type=-1;r.strength=3;return r;}
  2436. if(h1<h2&&l1>l2){if(bull1&&bear2){r.name="BULL HARAMI";r.type=1;r.strength=2;}else if(bear1&&bull2){r.name="BEAR HARAMI";r.type=-1;r.strength=2;}return r;}
  2437. return r;
  2438. }
  2439.  
  2440. NCPPatternResult NCPCheckTriplePattern(int shift){
  2441. NCPPatternResult r; r.name=""; r.type=0; r.strength=0; r.category="TRIPLE";
  2442. if(shift+3>=Bars || shift < 0) return r;
  2443. double o1=Open[shift],c1=Close[shift],h1=High[shift],l1=Low[shift];
  2444. double o2=Open[shift+1],c2=Close[shift+1],h2=High[shift+1],l2=Low[shift+1];
  2445. double o3=Open[shift+2],c3=Close[shift+2],h3=High[shift+2],l3=Low[shift+2];
  2446. double body1=MathAbs(c1-o1),body2=MathAbs(c2-o2),body3=MathAbs(c3-o3);double rng2=h2-l2;
  2447. bool bull1=(c1>o1),bear1=(c1<o1),bull3=(c3>o3),bear3=(c3<o3);
  2448. if(bull1&&bear3&&rng2>0&&body2<rng2*0.30&&c1>(o3+c3)/2.0){r.name="MORN STAR";r.type=1;r.strength=5;if(g_nearest_sup>0){double pip=(StringFind(Symbol(),"JPY")>=0)?0.01:0.0001;if(MathAbs(l2-g_nearest_sup)<8*pip){r.strength=6;r.name="MORN STAR@SUP";}}return r;}
  2449. if(bear1&&bull3&&rng2>0&&body2<rng2*0.30&&c1<(o3+c3)/2.0){r.name="EVE STAR";r.type=-1;r.strength=5;if(g_nearest_res>0){double pip=(StringFind(Symbol(),"JPY")>=0)?0.01:0.0001;if(MathAbs(h2-g_nearest_res)<8*pip){r.strength=6;r.name="EVE STAR@RES";}}return r;}
  2450. if(bull1&&c2>o2&&bull3&&c1>c2&&c2>c3&&o1>o2&&o2>o3){r.name="3 SOLDIERS";r.type=1;r.strength=5;if(body1<body2){r.strength=4;r.name="3 SOLDIERS-W";}return r;}
  2451. if(bear1&&c2<o2&&bear3&&c1<c2&&c2<c3&&o1<o2&&o2<o3){r.name="3 CROWS";r.type=-1;r.strength=5;if(body1<body2){r.strength=4;r.name="3 CROWS-W";}return r;}
  2452. if(bull1&&bear3&&h1<h2&&l1>l2&&c1>c2&&c2>c3){r.name="3 IN UP";r.type=1;r.strength=3;return r;}
  2453. if(bear1&&bull3&&h1<h2&&l1<l2&&c1<c2&&c2<c3){r.name="3 IN DN";r.type=-1;r.strength=3;return r;}
  2454. if(bull1&&bear3&&h1>h2&&l1<l2&&c1>o2){r.name="3 OUT UP";r.type=1;r.strength=4;return r;}
  2455. if(bear1&&bull3&&h1>h2&&l1<l2&&c1<o2){r.name="3 OUT DN";r.type=-1;r.strength=4;return r;}
  2456. return r;
  2457. }
  2458.  
  2459. NCPPatternResult NCPCheckColorPattern(){
  2460. NCPPatternResult r; r.name=""; r.type=0; r.strength=0; r.category="MULTI";if(Bars<8)return r;
  2461. int bullRun=0,bearRun=0;
  2462. for(int i=1;i<=6&&i<Bars;i++){if(Close[i]>Open[i]){if(bearRun>0)break;bullRun++;}else if(Close[i]<Open[i]){if(bullRun>0)break;bearRun++;}else break;}
  2463. if(bullRun>=3){r.name="3G-RUN";r.type=-1;r.strength=3;double b1=MathAbs(Close[1]-Open[1]),b2=MathAbs(Close[2]-Open[2]),b3=MathAbs(Close[3]-Open[3]);if(b1<b2&&b2<b3){r.strength=4;r.name="3G-WEAK";}}
  2464. else if(bearRun>=3){r.name="3R-RUN";r.type=1;r.strength=3;double b1=MathAbs(Close[1]-Open[1]),b2=MathAbs(Close[2]-Open[2]),b3=MathAbs(Close[3]-Open[3]);if(b1<b2&&b2<b3){r.strength=4;r.name="3R-WEAK";}}
  2465. if(bullRun>=4){r.name="4G-RUN";r.type=-1;r.strength=4;}else if(bearRun>=4){r.name="4R-RUN";r.type=1;r.strength=4;}
  2466. if(bullRun>=5){r.name="5G-EXHAUST";r.type=-1;r.strength=5;double v1=(double)iVolume(NULL,PERIOD_M1,1),v5=(double)iVolume(NULL,PERIOD_M1,5);if(v5>0&&v1<v5*0.6){r.strength=6;r.name="5G-EXH+VOL";}}
  2467. else if(bearRun>=5){r.name="5R-EXHAUST";r.type=1;r.strength=5;double v1=(double)iVolume(NULL,PERIOD_M1,1),v5=(double)iVolume(NULL,PERIOD_M1,5);if(v5>0&&v1<v5*0.6){r.strength=6;r.name="5R-EXH+VOL";}}
  2468. return r;
  2469. }
  2470.  
  2471. void NCPScanAllPatterns(){g_ncpPatternBullScore=0;g_ncpPatternBearScore=0;g_ncpMainPattern="";int bestStrength=0;
  2472. for(int i=0;i<=2;i++){NCPPatternResult p=NCPCheckSinglePattern(i);if(p.type!=0&&p.strength>0){int weight=(i==0)?3:(i==1)?2:1;if(p.type==1)g_ncpPatternBullScore+=p.strength*weight;else if(p.type==-1)g_ncpPatternBearScore+=p.strength*weight;if(p.strength*weight>bestStrength){bestStrength=p.strength*weight;g_ncpMainPattern=p.name;}}}
  2473. for(int i=0;i<=1;i++){NCPPatternResult p=NCPCheckDualPattern(i);if(p.type!=0&&p.strength>0){int weight=(i==0)?4:2;if(p.type==1)g_ncpPatternBullScore+=p.strength*weight;else if(p.type==-1)g_ncpPatternBearScore+=p.strength*weight;if(p.strength*weight>bestStrength){bestStrength=p.strength*weight;g_ncpMainPattern=p.name;}}}
  2474. NCPPatternResult p3=NCPCheckTriplePattern(0);if(p3.type!=0&&p3.strength>0){if(p3.type==1)g_ncpPatternBullScore+=p3.strength*5;else if(p3.type==-1)g_ncpPatternBearScore+=p3.strength*5;if(p3.strength*5>bestStrength){bestStrength=p3.strength*5;g_ncpMainPattern=p3.name;}}
  2475. if(NCP8_UseColorPattern){NCPPatternResult pc=NCPCheckColorPattern();if(pc.type!=0&&pc.strength>0){if(pc.type==1)g_ncpPatternBullScore+=pc.strength*3;else if(pc.type==-1)g_ncpPatternBearScore+=pc.strength*3;if(pc.strength*3>bestStrength&&bestStrength<15){g_ncpMainPattern=pc.name;}}}
  2476. g_ncpPatternBullScore=MathMin(100,g_ncpPatternBullScore);g_ncpPatternBearScore=MathMin(100,g_ncpPatternBearScore);
  2477. }
  2478.  
  2479. int NCPCheckHRNRejection(){if(g_hrn_price<=0)return 0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];double dist=MathAbs(cur-g_hrn_price)/pip;if(dist>10)return 0;int score=0;if(g_hrn_is_sup){if(Low[1]<=g_hrn_price+2*pip&&Close[1]>g_hrn_price){score+=15;double rng=High[1]-Low[1];if(rng>0){double lw=MathMin(Open[1],Close[1])-Low[1];if(lw/rng>0.50)score+=10;}}if(Close[1]<g_hrn_price-2*pip)score-=12;}else{if(High[1]>=g_hrn_price-2*pip&&Close[1]<g_hrn_price){score-=15;double rng=High[1]-Low[1];if(rng>0){double uw=High[1]-MathMax(Open[1],Close[1]);if(uw/rng>0.50)score-=10;}}if(Close[1]>g_hrn_price+2*pip)score+=12;}return score;}
  2480. int NCPCheckDivergence(){if(!NCP8_UseDivergence||Bars<30)return 0;int score=0;double rsi1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);double rsi5=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);double low5=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)];double low10=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,10,6)];if(low5<low10&&rsi1>rsi5&&rsi1<45)score+=20;double high5=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)];double high10=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,10,6)];if(high5>high10&&rsi1<rsi5&&rsi1>55)score-=20;double macd1=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);double macd5=iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,5);if(macd1>macd5&&Close[1]<Close[5]&&macd1<0)score+=12;if(macd1<macd5&&Close[1]>Close[5]&&macd1>0)score-=12;return score;}
  2481.  
  2482. // ============================================================
  2483. // PSYCHE BREAKER v4.3 - FINAL POLISH EDITION
  2484. // ============================================================
  2485. void CalcOTCNextCandlePredictor(){
  2486. g_otpSignal = "WAIT"; g_otpConf = 50.0; g_otpColor = NEON_YELLOW;
  2487. g_otpRsi = "--"; g_otpWick = "--"; g_otpReason = "";
  2488. if(Bars < 15) return;
  2489.  
  2490. // 1. RSI CHECK
  2491. double rsi = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  2492. g_otpRsi = DoubleToString(rsi, 1);
  2493. int score = 0;
  2494. string reasons = "";
  2495.  
  2496. // 2. WICK REJECTION
  2497. double rng = High[1] - Low[1];
  2498. if(rng > 0){
  2499. double uw = (High[1] - MathMax(Open[1], Close[1])) / rng;
  2500. double lw = (MathMin(Open[1], Close[1]) - Low[1]) / rng;
  2501. if(uw > 0.65) { score -= 15; g_otpWick = "UP TRAP"; reasons += "WickUp "; }
  2502. if(lw > 0.65) { score += 15; g_otpWick = "DN TRAP"; reasons += "WickDn "; }
  2503. }
  2504.  
  2505. // 3. CANDLE PATTERNS
  2506. double body = MathAbs(Close[1] - Open[1]);
  2507. double pBody = MathAbs(Close[2] - Open[2]);
  2508. bool bull = (Close[1] > Open[1]), bear = (Close[1] < Open[1]);
  2509.  
  2510. if(bull && pBody < body && Close[1] >= Open[2] && Open[1] <= Close[2]) { score += 25; reasons += "BullEngulf "; }
  2511. if(bear && pBody < body && Close[1] <= Open[2] && Open[1] >= Close[2]) { score -= 25; reasons += "BearEngulf "; }
  2512. if(rng > 0 && (MathMin(Open[1],Close[1])-Low[1])/rng > 0.60 && body < rng*0.30 && bear) { score += 20; reasons += "Hammer "; }
  2513. if(rng > 0 && (High[1]-MathMax(Open[1],Close[1]))/rng > 0.60 && body < rng*0.30 && bull) { score -= 20; reasons += "ShootingSt "; }
  2514.  
  2515. // 4. RSI CONFIRM
  2516. if(rsi < 30) { score += 20; reasons += "RSI_OS "; }
  2517. else if(rsi < 40) { score += 10; reasons += "RSI_Low "; }
  2518. else if(rsi > 70) { score -= 20; reasons += "RSI_OB "; }
  2519. else if(rsi > 60) { score -= 10; reasons += "RSI_High "; }
  2520.  
  2521. // 5. MOMENTUM TRAP
  2522. if(Close[1]>Open[1] && Close[2]>Open[2] && Close[3]>Open[3]) { score -= 10; reasons += "3GreenTrap "; }
  2523. if(Close[1]<Open[1] && Close[2]<Open[2] && Close[3]<Open[3]) { score += 10; reasons += "3RedTrap "; }
  2524.  
  2525. // ✅ FIXED LOGIC (MathAbs use kiya)
  2526. double absScore = MathAbs(score);
  2527. g_otpConf = MathMax(5.0, MathMin(95.0, 50.0 + absScore));
  2528. g_otpReason = (reasons == "") ? "NO CONFIRMATION" : reasons;
  2529.  
  2530. if(g_otpConf >= 72){
  2531. g_otpSignal = (score > 0) ? "CALL" : "PUT";
  2532. g_otpColor = (score > 0) ? NEON_GREEN : NEON_RED;
  2533. } else if(g_otpConf >= 60){
  2534. g_otpSignal = (score > 0) ? "WEAK CALL" : "WEAK PUT";
  2535. g_otpColor = (score > 0) ? NEON_CYAN : NEON_ORANGE;
  2536. } else {
  2537. g_otpSignal = "WAIT";
  2538. g_otpColor = NEON_YELLOW;
  2539. }
  2540. }
  2541.  
  2542. // ============================================================
  2543. // NCP PRO v8.0 CORE ENGINE
  2544. // ============================================================
  2545. NCPTrendInfo NCPDetectTrend(){NCPTrendInfo t;t.m15Direction="FLAT";t.m15Strength=0;t.m1Bias=0;t.m5Direction="FLAT";t.m5Strength=0;t.aligned=false;if(Bars<20)return t;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;int greenCount=0,redCount=0;double greenBody=0,redBody=0;for(int i=1;i<=15&&i<Bars;i++){double body=Close[i]-Open[i];double absBody=MathAbs(body);if(body>0){greenCount++;greenBody+=absBody;}else if(body<0){redCount++;redBody+=absBody;}}double netBody=greenBody-redBody;t.m1Bias=(int)((netBody/pip)*2);if(t.m1Bias>100)t.m1Bias=100;if(t.m1Bias<-100)t.m1Bias=-100;if(iBars(NULL,PERIOD_M15)>=3){double m15Open=iOpen(NULL,PERIOD_M15,1);double m15Close=iClose(NULL,PERIOD_M15,1);double m15High=iHigh(NULL,PERIOD_M15,1);double m15Low=iLow(NULL,PERIOD_M15,1);double m15Body=m15Close-m15Open;double m15Range=m15High-m15Low;if(m15Body>5*pip){t.m15Direction="UP";t.m15Strength=(m15Range>0)?(int)(MathAbs(m15Body)/m15Range*10):5;}else if(m15Body<-5*pip){t.m15Direction="DOWN";t.m15Strength=(m15Range>0)?(int)(MathAbs(m15Body)/m15Range*10):5;}else{t.m15Direction="FLAT";t.m15Strength=3;}for(int j=1;j<=3;j++){double m5c=iClose(NULL,PERIOD_M5,j);double m5o=iOpen(NULL,PERIOD_M5,j);if(m5c>m5o)t.m5Strength++;else if(m5c<m5o)t.m5Strength--;}t.m5Direction=(t.m5Strength>1)?"UP":(t.m5Strength<-1)?"DOWN":"FLAT";}else{if(greenCount>=10){t.m15Direction="UP";t.m15Strength=greenCount-7;}else if(redCount>=10){t.m15Direction="DOWN";t.m15Strength=redCount-7;}t.m5Direction=(greenCount>redCount+2)?"UP":(redCount>greenCount+2)?"DOWN":"FLAT";}bool m1Up=(greenCount>redCount+2);bool m1Dn=(redCount>greenCount+2); bool m5Up=(t.m5Direction=="UP"); bool m5Dn=(t.m5Direction=="DOWN"); bool m15Up=(t.m15Direction=="UP"); bool m15Dn=(t.m15Direction=="DOWN"); t.aligned=(m1Up&&m5Up&&m15Up)||(m1Dn&&m5Dn&&m15Dn); return t;}
  2546.  
  2547. void NCPDetectDynamicSR(){g_ncpSRCount=0;if(Bars<30)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];for(int i=3;i<=30&&i<Bars-2&&g_ncpSRCount<15;i++){if(High[i]>High[i-1]&&High[i]>High[i-2]&&High[i]>High[i+1]&&High[i]>High[i+2]){double lv=High[i];if(MathAbs(lv-cur)/pip<40)NCPAddSRZone(lv,"RESISTANCE",1,false,false,iTime(NULL,PERIOD_M1,i));}if(Low[i]<Low[i-1]&&Low[i]<Low[i-2]&&Low[i]<Low[i+1]&&Low[i]<Low[i+2]){double lv=Low[i];if(MathAbs(lv-cur)/pip<40)NCPAddSRZone(lv,"SUPPORT",1,false,false,iTime(NULL,PERIOD_M1,i));}}double rndSteps[]={5,10,25,50,100};for(int s=0;s<5;s++){double step=rndSteps[s]*pip;double nearRound=MathRound(cur/step)*step;for(int k=-1;k<=1;k++){double lv=nearRound+k*step;if(MathAbs(lv-cur)/pip<30){string type=(lv>cur)?"RESISTANCE":"SUPPORT";NCPAddSRZone(lv,type,(s<2)?3:(s<3)?2:1,true,false,TimeCurrent());}}}if(NCP8_UseOrderBlocks)NCPDetectOrderBlocks();}
  2548.  
  2549. void NCPAddSRZone(double price,string type,int strength,bool isRound,bool isOB,datetime lastTouch){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double tol=5*pip;for(int i=0;i<g_ncpSRCount;i++){if(MathAbs(g_ncpSRZones[i].price-price)<tol&&g_ncpSRZones[i].type==type){g_ncpSRZones[i].touches++;g_ncpSRZones[i].strength=MathMin(10,g_ncpSRZones[i].strength+1);g_ncpSRZones[i].lastTouch=lastTouch;if(isRound)g_ncpSRZones[i].isRoundNum=true;if(isOB)g_ncpSRZones[i].isOrderBlock=true;return;}}if(g_ncpSRCount<20){g_ncpSRZones[g_ncpSRCount].price=price;g_ncpSRZones[g_ncpSRCount].type=type;g_ncpSRZones[g_ncpSRCount].touches=1;g_ncpSRZones[g_ncpSRCount].strength=strength;g_ncpSRZones[g_ncpSRCount].isRoundNum=isRound;g_ncpSRZones[g_ncpSRCount].isOrderBlock=isOB;g_ncpSRZones[g_ncpSRCount].lastTouch=lastTouch;g_ncpSRCount++;}}
  2550.  
  2551. // FIX: Removed Array Access Risk
  2552. void NCPDetectOrderBlocks(){
  2553. if(Bars<30)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  2554. for(int i=2;i<=20&&i<Bars&&g_ncpSRCount<18;i++){
  2555. int startJ = MathMax(1, i-3); // FIX: Ensure startJ doesn't go below 1
  2556. if(Close[i]<Open[i]){
  2557. double move=0;for(int j=i-1;j>=startJ;j--){if(Close[j]>Open[j])move+=Close[j]-Open[j];else break;}
  2558. if(move>8*pip){double obLow=Low[i];if(MathAbs(obLow-cur)/pip<20&&cur>obLow)NCPAddSRZone(obLow,"SUPPORT",4,false,true,iTime(NULL,PERIOD_M1,i));}
  2559. }
  2560. if(Close[i]>Open[i]){
  2561. double move=0;for(int j=i-1;j>=startJ;j--){if(Close[j]<Open[j])move+=Open[j]-Close[j];else break;}
  2562. if(move>8*pip){double obHigh=High[i];if(MathAbs(obHigh-cur)/pip<20&&cur<obHigh)NCPAddSRZone(obHigh,"RESISTANCE",4,false,true,iTime(NULL,PERIOD_M1,i));}
  2563. }
  2564. }
  2565. }
  2566.  
  2567. int NCPGetSRSignal(){if(g_ncpSRCount==0)return 0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];int score=0;for(int i=0;i<g_ncpSRCount;i++){double dist=MathAbs(g_ncpSRZones[i].price-cur)/pip;if(dist>15)continue;int weight=g_ncpSRZones[i].strength;if(g_ncpSRZones[i].isRoundNum)weight+=2;if(g_ncpSRZones[i].isOrderBlock)weight+=3;if(g_ncpSRZones[i].touches>=3)weight+=2;if(g_ncpSRZones[i].type=="SUPPORT"){if(dist<3)score+=weight*3;else if(dist<7)score+=weight*2;else if(dist<12)score+=weight;if(Low[1]<g_ncpSRZones[i].price+2*pip&&Close[1]>g_ncpSRZones[i].price)score+=weight*2;}else if(g_ncpSRZones[i].type=="RESISTANCE"){if(dist<3)score-=weight*3;else if(dist<7)score-=weight*2;else if(dist<12)score-=weight;if(High[1]>g_ncpSRZones[i].price-2*pip&&Close[1]<g_ncpSRZones[i].price)score-=weight*2;}}return MathMax(-50,MathMin(50,score));}
  2568.  
  2569. string NCPGetSRSummary(){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];double nearSup=0,nearRes=0;int supStr=0,resStr=0;for(int i=0;i<g_ncpSRCount;i++){double dist=MathAbs(g_ncpSRZones[i].price-cur)/pip;if(dist>15)continue;if(g_ncpSRZones[i].type=="SUPPORT"&&(nearSup==0||dist<MathAbs(nearSup-cur)/pip)){nearSup=g_ncpSRZones[i].price;supStr=g_ncpSRZones[i].strength;}if(g_ncpSRZones[i].type=="RESISTANCE"&&(nearRes==0||dist<MathAbs(nearRes-cur)/pip)){nearRes=g_ncpSRZones[i].price;resStr=g_ncpSRZones[i].strength;}}string result="";if(nearSup>0)result+="S:"+ShortPrice(nearSup)+"["+IntegerToString(supStr)+"] ";if(nearRes>0)result+="R:"+ShortPrice(nearRes)+"["+IntegerToString(resStr)+"]";if(result=="")result="No nearby SR";return result;}
  2570.  
  2571. // ============================================================
  2572. // LEARNING ENGINE v8.0
  2573. // ============================================================
  2574. void LoadBrainMemory(){if(!ENABLE_LEARNING) return;int h = FileOpen(MEMORY_FILE_NAME, FILE_READ|FILE_TXT|FILE_SHARE_READ);if(h != INVALID_HANDLE){if(FileSize(h) > 10){string line = FileReadString(h);StringReplace(line, " ", "");int p1 = StringFind(line, ","), p2 = StringFind(line, ",", p1+1);int p3 = StringFind(line, ",", p2+1), p4 = StringFind(line, ",", p3+1);int p5 = StringFind(line, ",", p4+1), p6 = StringFind(line, ",", p5+1);if(p6 > 0){g_totalTrades = (int)StringToInteger(StringSubstr(line, 0, p1));g_callTrades = (int)StringToInteger(StringSubstr(line, p1+1, p2-p1-1));g_putTrades = (int)StringToInteger(StringSubstr(line, p2+1, p3-p2-1));g_callWins = (int)StringToInteger(StringSubstr(line, p3+1, p4-p3-1));g_putWins = (int)StringToInteger(StringSubstr(line, p4+1, p5-p4-1));g_callWeight = StringToDouble(StringSubstr(line, p5+1, p6-p5-1));g_putWeight = StringToDouble(StringSubstr(line, p6+1));if(g_callWeight<0.2 || g_callWeight>1.8) g_callWeight = 1.0;if(g_putWeight<0.2 || g_putWeight>1.8) g_putWeight = 1.0;}}FileClose(h);} else g_brokerBias = 0.0;}
  2575. void SaveBrainMemory(){if(!ENABLE_LEARNING)return;int h=FileOpen(MEMORY_FILE_NAME,FILE_WRITE|FILE_TXT|FILE_SHARE_WRITE);if(h!=INVALID_HANDLE){string line=IntegerToString(g_totalTrades)+","+IntegerToString(g_callTrades)+","+IntegerToString(g_putTrades)+","+IntegerToString(g_callWins)+","+IntegerToString(g_putWins)+","+DoubleToString(g_callWeight,6)+","+DoubleToString(g_putWeight,6);FileWriteString(h,line);FileFlush(h);FileClose(h);}}
  2576. void UpdateBrain(string sig,bool win){if(!ENABLE_LEARNING)return;g_totalTrades++;if(sig=="CALL"){g_callTrades++;if(win)g_callWins++;}else if(sig=="PUT"){g_putTrades++;if(win)g_putWins++;}if(g_totalTrades<MIN_TRADES_TO_LEARN){SaveBrainMemory();return;}double adj=LEARNING_STEP;if(sig=="CALL"){double wr=(g_callTrades>0)?(double)g_callWins/g_callTrades:0.5;double adaptive=adj*(wr-0.5)*2.0;if(win)g_callWeight=MathMin(1.8,g_callWeight+adj+adaptive);else g_callWeight=MathMax(0.3,g_callWeight-adj*1.5+adaptive);if(g_callTrades<MIN_TRADES_TO_LEARN+5)g_callWeight=MathMax(0.6,g_callWeight);}else if(sig=="PUT"){double wr=(g_putTrades>0)?(double)g_putWins/g_putTrades:0.5;double adaptive=adj*(wr-0.5)*2.0;if(win)g_putWeight=MathMin(1.8,g_putWeight+adj+adaptive);else g_putWeight=MathMax(0.3,g_putWeight-adj*1.5+adaptive);if(g_putTrades<MIN_TRADES_TO_LEARN+5)g_putWeight=MathMax(0.6,g_putWeight);}SaveBrainMemory();}
  2577. void CheckPreviousResult(){if(g_ncpSignalProcessed||g_ncpLastSignalType==""||g_ncpLastSignalTime==0)return;if(Time[0]==g_ncpLastSignalTime)return;double sigOpen=iOpen(NULL,PERIOD_M1,1);double sigClose=iClose(NULL,PERIOD_M1,1);bool win=false;if(g_ncpLastSignalType=="CALL")win=(sigClose>sigOpen);else if(g_ncpLastSignalType=="PUT")win=(sigClose<sigOpen);g_ncpSignalProcessed=true;UpdateBrain(g_ncpLastSignalType,win);}
  2578.  
  2579. // ============================================================
  2580. // NCP PRO v8.0 MTG ENGINE
  2581. // ============================================================
  2582. void CalculateMTG(){
  2583. if(!ENABLE_MTG || Bars < 50){g_mtgState="OFF"; g_mtgReason=""; g_mtgClr=CGR; return;}
  2584. double pip=GetUniversalPip();
  2585. CheckPreviousResult();
  2586. int sec = (int)(TimeCurrent() - Time[0]);
  2587. if(sec < NCP_OpenNoiseSeconds){g_mtgState="..."; g_mtgReason="Bar open"; g_mtgClr=CGR; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2588. if(sec >= NCP_KillZoneSeconds){g_mtgState="END"; g_mtgReason="Late"; g_mtgClr=NEON_ORANGE; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2589. double spPips = MarketInfo(Symbol(), MODE_SPREAD) * Point / pip;
  2590. if(spPips > NCP_MaxSpreadPips){g_mtgState="SPREAD"; g_mtgReason=DoubleToString(spPips,1)+"p"; g_mtgClr=NEON_ORANGE; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2591. double vol1 = (double)iVolume(NULL, PERIOD_M1, 1); double volAvg = 0;
  2592. for(int v = 2; v <= 21; v++) volAvg += (double)iVolume(NULL, PERIOD_M1, v); volAvg /= 20.0;
  2593. if(volAvg > 0 && vol1 < volAvg * 0.7){g_mtgState="WAIT"; g_mtgReason="LOW VOL"; g_mtgClr=NEON_ORANGE; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2594. double atr = iATR(Symbol(), PERIOD_M1, NCP_ATRPeriod, 1); double atrPips = atr / pip;
  2595. if(atrPips < NCP_MinATRPips){g_mtgState="FLAT"; g_mtgReason="Low ATR"; g_mtgClr=CGR; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2596. g_ncpADX = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  2597. double plusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  2598. double minusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  2599. g_ncpPlusDI = plusDI; g_ncpMinusDI = minusDI;
  2600. if(g_ncpADX < 22){g_mtgState="WAIT"; g_mtgReason="ADX LOW"; g_mtgClr=NEON_ORANGE; g_mtgHype=50; g_mtgBetrayal=50; return;}
  2601. if(NCP8_UseM15Trend) g_ncpTrend = NCPDetectTrend();
  2602. if(NCP8_UseDynamicSR) NCPDetectDynamicSR();
  2603. if(NCP8_UsePatterns) NCPScanAllPatterns();
  2604. double macd_main = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);
  2605. double macd_sig = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 1);
  2606. bool closedBull = (Close[1] > Open[1]); bool closedBear = (Close[1] < Open[1]);
  2607. double body1 = MathAbs(Close[1] - Open[1]); double rng1 = High[1] - Low[1];
  2608. double dynamicBodyThresh = atr * (0.35 + (g_ncpADX - 20) / 100.0);
  2609. int callConfluence = 0, putConfluence = 0;
  2610. double weightMultiplier = 1.0 + (g_ncpADX - 20) / 50.0;
  2611. if(NCP8_UseM15Trend){if(g_ncpTrend.m15Direction == "UP") callConfluence += (int)(3 * weightMultiplier); else if(g_ncpTrend.m15Direction == "DOWN") putConfluence += (int)(3 * weightMultiplier);}
  2612. double eF = iMA(NULL, 0, NCP_FastEMA, 0, MODE_EMA, PRICE_CLOSE, 1), eM = iMA(NULL, 0, NCP_MidEMA, 0, MODE_EMA, PRICE_CLOSE, 1), eS = iMA(NULL, 0, NCP_SlowEMA, 0, MODE_EMA, PRICE_CLOSE, 1), eF_prev = iMA(NULL, 0, NCP_FastEMA, 0, MODE_EMA, PRICE_CLOSE, 2);
  2613. if(eF > eM && eM > eS && eF > eF_prev) callConfluence += (int)(2.5 * weightMultiplier); else if(eF < eM && eM < eS && eF < eF_prev) putConfluence += (int)(2.5 * weightMultiplier);
  2614. if(g_haM1 == "HA BULLISH 1") callConfluence += 1; else if(g_haM1 == "HA BEARISH 1") putConfluence += 1;
  2615. if(g_haM5 == "HA BULLISH 5") callConfluence += 1; else if(g_haM5 == "HA BEARISH 5") putConfluence += 1;
  2616. if(plusDI > minusDI + 5) callConfluence += 2; else if(minusDI > plusDI + 5) putConfluence += 2;
  2617. double rsi1 = iRSI(NULL, 0, NCP_RSIPeriod, PRICE_CLOSE, 1), rsi2 = iRSI(NULL, 0, NCP_RSIPeriod, PRICE_CLOSE, 2);
  2618. if(rsi1 > 55 && rsi1 > rsi2) callConfluence++; else if(rsi1 < 45 && rsi1 < rsi2) putConfluence++;
  2619. double bodyPct = (rng1 > 0) ? (body1 / rng1) * 100.0 : 0;
  2620. if(closedBull && body1 > dynamicBodyThresh && bodyPct > 60) callConfluence += 3; else if(closedBear && body1 > dynamicBodyThresh && bodyPct > 60) putConfluence += 3;
  2621. bool nearSup = (g_nearest_sup > 0 && MathAbs(Close[1] - g_nearest_sup) < atrPips * 1.2);
  2622. bool nearRes = (g_nearest_res > 0 && MathAbs(Close[1] - g_nearest_res) < atrPips * 1.2);
  2623. if(nearSup) callConfluence += 2; if(nearRes) putConfluence += 2;
  2624. if(g_ncpPatternBullScore > g_ncpPatternBearScore + 15 && vol1 > volAvg * 1.2) callConfluence += 2; else if(g_ncpPatternBearScore > g_ncpPatternBullScore + 15 && vol1 > volAvg * 1.2) putConfluence += 2;
  2625. if(macd_main > macd_sig && macd_main > 0) callConfluence += 1; if(macd_main < macd_sig && macd_main < 0) putConfluence += 1;
  2626. double totalPossible = callConfluence + putConfluence;
  2627. double callStrength = totalPossible > 0 ? (double)callConfluence / totalPossible * 100 : 50;
  2628. double smoothFactor = MathMin(0.65, 0.45 + (atrPips / 20.0) * 0.1);
  2629. g_smoothCallScore = g_smoothCallScore * (1.0 - smoothFactor) + callStrength * smoothFactor;
  2630. g_smoothPutScore = 100.0 - g_smoothCallScore;
  2631. g_mtgHype = NormalizeDouble(g_smoothCallScore, 1); g_mtgBetrayal = NormalizeDouble(g_smoothPutScore, 1);
  2632. string lockReason = ""; int minConfluence = 8;
  2633. if(callConfluence < minConfluence && putConfluence < minConfluence) lockReason = "LOW CONF";
  2634. if(lockReason != ""){g_mtgState = "WAIT"; g_mtgReason = lockReason; g_mtgAction = lockReason; g_mtgClr = NEON_YELLOW; g_mtgBullCount = callConfluence; g_mtgBearCount = putConfluence; g_mtgRecovery = NormalizeDouble(atrPips, 1); g_mtgPattern = "LOCK"; g_ncpSignalProcessed = true; return;}
  2635. int minP = 62; double edge = MathAbs(g_smoothCallScore - 50);
  2636. string str = "WEAK"; if(edge >= 28) str = "EXTREME"; else if(edge >= 22) str = "STRONG"; else if(edge >= 15) str = "MEDIUM";
  2637. if(Time[0] != g_ncpLastSignalTime){g_ncpLastSignalTime = Time[0]; g_ncpSignalProcessed = false;}
  2638. if(g_smoothCallScore >= minP && g_smoothCallScore > g_smoothPutScore){g_mtgState = "CALL"; g_mtgReason = g_ncpDetailLine1; g_mtgAction = "CALL " + DoubleToString(g_smoothCallScore, 1) + "% [" + str + "]"; g_mtgClr = (str == "EXTREME") ? NEON_GREEN : NEON_LIME; if(!g_ncpSignalProcessed){ g_ncpLastSignalType = "CALL"; g_ncpLastSignalTime = Time[0]; }}
  2639. else if(g_smoothPutScore >= minP && g_smoothPutScore > g_smoothCallScore){g_mtgState = "PUT"; g_mtgReason = g_ncpDetailLine1; g_mtgAction = "PUT " + DoubleToString(g_smoothPutScore, 1) + "% [" + str + "]"; g_mtgClr = (str == "EXTREME") ? NEON_RED : C'255,60,60'; if(!g_ncpSignalProcessed){ g_ncpLastSignalType = "PUT"; g_ncpLastSignalTime = Time[0]; }}
  2640. else{g_mtgState = "WAIT"; g_mtgReason = g_ncpDetailLine1; string edgeDir = (g_smoothCallScore > g_smoothPutScore) ? ">C" : ">P"; g_mtgAction = edgeDir + " " + DoubleToString(MathMax(g_smoothCallScore, g_smoothPutScore), 0) + "%"; g_mtgClr = NEON_YELLOW; g_ncpSignalProcessed = true;}
  2641. g_mtgBullCount = callConfluence; g_mtgBearCount = putConfluence; g_mtgRecovery = NormalizeDouble(atrPips, 1); g_mtgPattern = str; g_trapScore = 0;
  2642. }
  2643.  
  2644. // ============================================================
  2645. // DASHBOARD DRAWING HELPERS
  2646. // ============================================================
  2647. void DrawOtherPairsCube(int x,int y,int w,int h){
  2648. Bx("c20",x,y,w,h,BG_DARK2,NEON_GOLD);Tx("c20_l",x+8,y+4,"OTHER PAIRS",NEON_CYAN,10,true);
  2649.  
  2650. int lY=24;
  2651.  
  2652. // --- 1. CURRENT PAIR SIGNAL (WHITE) ---
  2653. if(g_otpSignal != "WAIT" && g_otpSignal != "") {
  2654. string curPairName = Symbol();
  2655. if(StringLen(curPairName) > 9) curPairName = StringSubstr(curPairName, 0, 9);
  2656. string mySigText = "* " + curPairName + " " + g_otpSignal + " " + DoubleToString(g_otpConf, 0) + "%";
  2657. Tx("c20_my_sig", x+8, y+lY, mySigText, NEON_WHITE, 10, true);
  2658. lY += 20;
  2659. }
  2660.  
  2661. // --- 2. MHI BACKGROUND PAIRS (WHITE COLOR) ---
  2662. for(int i=0; i<g_bgMHI_Count; i++){
  2663. if(lY+16>h-4) break;
  2664. string sp = g_bgMHI_Pair[i];
  2665. if(StringLen(sp) > 9) sp = StringSubstr(sp, 0, 9);
  2666. string mhiText = sp + " " + g_bgMHI_Signal[i] + " " + DoubleToString(g_bgMHI_Conf[i], 0) + "%";
  2667. Tx("c20_mhi_bg"+IntegerToString(i), x+8, y+lY, mhiText, NEON_WHITE, 10, true);
  2668. lY += 20;
  2669. }
  2670.  
  2671. // --- 3. NORMAL NCP PAIRS + VIP (GOLD + CALL/PUT) ---
  2672. if(g_pairCount==0){
  2673. if(lY+16>h-4) return;
  2674. if(StringFind(g_visualTrapPro.boxStatus, "[") >= 0){Tx("c20_v0",x+8,y+lY,g_visualTrapPro.boxStatus,NEON_WHITE,11,true);return;}
  2675. Tx("c20_v0",x+8,y+lY,"No signals yet",CGR,10,false);return;
  2676. }
  2677.  
  2678. for(int i=0;i<g_pairCount&&i<5;i++){
  2679. if(lY+16>h-4)break;
  2680. string sp=g_pairNames[i];if(StringLen(sp)>9)sp=StringSubstr(sp,0,9);
  2681. int qScore=(int)g_pairConfs[i];string qLabel;if(qScore>=80)qLabel="[S]";else if(qScore>=65)qLabel="[G]";else if(qScore>=50)qLabel="[M]";else qLabel="[W]";
  2682. bool isCallSig=(g_mtgState=="CALL");bool isPutSig=(g_mtgState=="PUT");
  2683. bool isMicroOB=(qScore==97);bool isPremOB=(qScore>=82&&qScore<97);bool isVIP=isMicroOB||isPremOB;
  2684.  
  2685. if(isVIP){
  2686. bool isCallDir=(g_pairSigs[i]=="CALL");
  2687. string pairPart="* "+sp+" ";
  2688. string sigPart="";
  2689. color sigClr;
  2690.  
  2691. if(isMicroOB){
  2692. sigPart=isCallDir?"CALL":"PUT";
  2693. sigClr=isCallDir?NEON_GREEN:NEON_RED;
  2694. } else {
  2695. sigPart=(isCallDir?"CALL ":"PUT ")+IntegerToString(qScore)+"%";
  2696. sigClr=isCallDir?NEON_GREEN:NEON_RED;
  2697. }
  2698.  
  2699. // Pair name in GOLDEN
  2700. Tx("c20_p"+IntegerToString(i),x+8,y+lY,pairPart,NEON_GOLD,10,true);
  2701. // CALL/PUT in GREEN/RED
  2702. int sigX=x+8+StringLen(pairPart)*9;
  2703. Tx("c20_p"+IntegerToString(i)+"s",sigX,y+lY,sigPart,sigClr,10,true);
  2704. }
  2705. else{
  2706. bool ncpMatch=((isCallSig&&g_pairSigs[i]=="CALL")||(isPutSig&&g_pairSigs[i]=="PUT"));
  2707. color dc;string prefix="";string txt="";
  2708. if(ncpMatch){dc=NEON_PURPLE;prefix="* ";}else{dc=(g_pairSigs[i]=="CALL")?NEON_GREEN:NEON_RED;}
  2709. txt=prefix+sp+" "+g_pairSigs[i]+" "+IntegerToString(qScore)+qLabel;
  2710. Tx("c20_p"+IntegerToString(i),x+8,y+lY,txt,dc,10,true);
  2711. }
  2712. lY+=20;
  2713. }
  2714. }
  2715.  
  2716.  
  2717.  
  2718. string GetEntryStatus(){int age=(int)(TimeCurrent()-Time[0]);int ps=Period()*60;int rem=ps-age;if(rem<0)rem=0;string ms=DetectMyStrategy();if(ms!="WAIT"){string type=(g_strategyType=="TRAP")?"TRAP":"TREND";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";}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";}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";}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";}
  2719. 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;return NEON_ORANGE;}
  2720. 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));return true;}
  2721. 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;}
  2722. 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;}
  2723. void SendTelegramAlert(string message){if(!ENABLE_TELEGRAM)return;if(StringLen(TELEGRAM_TOKEN)<10||StringLen(TELEGRAM_CHAT_ID)<5)return;string url="https://api.telegram.org/bot"+TELEGRAM_TOKEN+"/sendMessage?chat_id="+TELEGRAM_CHAT_ID+"&text="+message;char post[];char result_data[];string result_headers;int timeout=5000;int res=WebRequest("GET",url,"",timeout,post,result_data,result_headers);if(res==-1)Print("Telegram failed: ",GetLastError());}
  2724.  
  2725. 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);}
  2726. 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);}
  2727. 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);}
  2728. 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);}
  2729. // ============================================================
  2730. // MAIN PREDICTION ENGINE
  2731. // ============================================================
  2732. void CalcPrediction(double brain,double nb,int cp,double mai,int mts,double rsc,bool bf,double mw){
  2733. string ms=DetectMyStrategy();bool sA=(ms!="WAIT");
  2734. if(sA&&g_strategyType=="TRAP"){
  2735. 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;}
  2736. 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;}
  2737. }
  2738. double tS=UltimateTrapScore();bool bF2=IsBrokerForce();
  2739. 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;}}
  2740. 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;}
  2741. double gS=0,rS=0,ab=MathMax(0.70,MathMin(1.30,g_accuracy/65.0));
  2742. g_isSideways = IsSidewaysMarket();if(g_isSideways){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;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);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;}if(nR)rS+=20;if(nS)gS+=20;}
  2743. 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;}}
  2744. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5")gS+=60;else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5")rS+=60;else if(g_haM1=="HA BULLISH 1")gS+=30;else if(g_haM1=="HA BEARISH 1")rS+=30;
  2745. if(HA_ALIGN_FILTER&&g_haBoth=="MIXED"){gS*=0.5;rS*=0.5;}
  2746. gS+=(nb-50)/50.0*22*ab;rS-=(nb-50)/50.0*22*ab;
  2747. double bn=MathMax(-1.0,MathMin(1.0,brain/10.0));gS+=bn*15*ab;rS-=bn*15*ab;
  2748. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  2749. if(g_nearest_res>0&&(g_nearest_res-Close[0])<10*pip)rS+=12;if(g_nearest_sup>0&&(Close[0]-g_nearest_sup)<10*pip)gS+=12;
  2750. double rsi1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);if(rsi1>70)rS+=6;if(rsi1<30)gS+=6;
  2751. double sn=MathMax(-1.0,MathMin(1.0,rsc/3.0));gS+=sn*8*ab;rS-=sn*8*ab;
  2752. if(ENABLE_BB_PULLBACK){if(g_bbSignal=="CALL")gS+=18*ab;else if(g_bbSignal=="PUT")rS+=18*ab;}
  2753. int rd=DetectRSIDivergence();if(rd==1)gS+=20;if(rd==-1)rS+=20;
  2754. int sk=StreakReversalSignal();if(sk==1)gS+=18;if(sk==-1)rS+=18;
  2755. 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;}
  2756. int vs=SmartVolumeSignal();if(vs==1)gS+=12;if(vs==-1)rS+=12;
  2757. if(!IsVolatilityGood()){gS*=0.5;rS*=0.5;}
  2758. double diff=MathMax(-100.0,MathMin(100.0,gS-rS));
  2759. g_gProb=MathMax(8.0,MathMin(92.0,50.0+(diff/1.8)));g_rProb=100.0-g_gProb;
  2760. double dom=MathMax(g_gProb,g_rProb);
  2761. 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;}}}
  2762. 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;}}}
  2763. else{if(!sA){g_pAction="WAIT";g_pColor=NEON_YELLOW;}}
  2764. g_calc_gProb=g_gProb;g_calc_rProb=g_rProb;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;
  2765. }
  2766.  
  2767. // --- Safe Division Helper ---
  2768. double Div(double a, double b) {
  2769. if(b == 0) return 0;
  2770. return a / b;
  2771. }
  2772.  
  2773. // --- MAIN FORMULA: Buy aur Sell Count (ULTIMATE STABLE VERSION) ---
  2774. void GetConfluenceScores(int &buyCount, int &sellCount) {
  2775.  
  2776. // Caching
  2777. if(Time[0] == g_cacheConfluenceTime) {
  2778. buyCount = g_buyCountCache;
  2779. sellCount = g_sellCountCache;
  2780. return;
  2781. }
  2782.  
  2783. g_cacheConfluenceTime = Time[0];
  2784. buyCount = 0;
  2785. sellCount = 0;
  2786.  
  2787. int i = 1;
  2788. if(i >= Bars) return;
  2789.  
  2790. double atr = iATR(NULL, 0, 14, i);
  2791. if(atr <= 0) atr = Point * 10;
  2792.  
  2793. // ========== 22 INDICATORS (STRICT +1 LOGIC) ==========
  2794.  
  2795. // 1. RSI
  2796. double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, i);
  2797. if(rsi < 30) buyCount++;
  2798. else if(rsi > 70) sellCount++;
  2799. else if(rsi < 50 && rsi > iRSI(NULL, 0, 14, PRICE_CLOSE, i+1)) buyCount++;
  2800. else if(rsi > 50 && rsi < iRSI(NULL, 0, 14, PRICE_CLOSE, i+1)) sellCount++;
  2801.  
  2802. // 2. Stochastic
  2803. double stoch = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, i);
  2804. double stochSig = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, i);
  2805. if(stoch < 20 && stoch > stochSig) buyCount++;
  2806. else if(stoch > 80 && stoch < stochSig) sellCount++;
  2807.  
  2808. // 3. MACD
  2809. double macd = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i);
  2810. double macdSig = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, i);
  2811. if(macd > macdSig && macd < 0) buyCount++;
  2812. else if(macd < macdSig && macd > 0) sellCount++;
  2813.  
  2814. // 4. MA Crossover (5 EMA vs 10 EMA)
  2815. double ma5 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, i);
  2816. double ma10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, i);
  2817. if(ma5 > ma10) buyCount++;
  2818. else if(ma5 < ma10) sellCount++;
  2819.  
  2820. // 5. Triple MA (5 > 10 > 20)
  2821. double ma20 = iMA(NULL, 0, 20, 0, MODE_EMA, PRICE_CLOSE, i);
  2822. if(ma5 > ma10 && ma10 > ma20) buyCount++;
  2823. else if(ma5 < ma10 && ma10 < ma20) sellCount++;
  2824.  
  2825. // 6. Bollinger Bands
  2826. double bbLow = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, i);
  2827. double bbUpp = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, i);
  2828. if(Close[i] <= bbLow) buyCount++;
  2829. else if(Close[i] >= bbUpp) sellCount++;
  2830.  
  2831. // 7. ADX
  2832. double adx = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, i);
  2833. double pdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_PLUSDI, i);
  2834. double mdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MINUSDI, i);
  2835. if(adx > 20) {
  2836. if(pdi > mdi) buyCount++;
  2837. else if(mdi > pdi) sellCount++;
  2838. }
  2839.  
  2840. // 8. CCI
  2841. double cci = iCCI(NULL, 0, 14, PRICE_TYPICAL, i);
  2842. if(cci < -100) buyCount++;
  2843. else if(cci > 100) sellCount++;
  2844.  
  2845. // 9. Parabolic SAR
  2846. double sar = iSAR(NULL, 0, 0.02, 0.2, i);
  2847. if(sar < Low[i]) buyCount++;
  2848. else if(sar > High[i]) sellCount++;
  2849.  
  2850. // 10. MFI
  2851. double mfi = iMFI(NULL, 0, 14, i);
  2852. if(mfi < 20) buyCount++;
  2853. else if(mfi > 80) sellCount++;
  2854.  
  2855. // 11. Williams %R
  2856. double wpr = iWPR(NULL, 0, 14, i);
  2857. if(wpr < -80) buyCount++;
  2858. else if(wpr > -20) sellCount++;
  2859.  
  2860. // 12. Momentum
  2861. double mom = iMomentum(NULL, 0, 10, PRICE_CLOSE, i);
  2862. if(mom > 100) buyCount++;
  2863. else if(mom < 100) sellCount++;
  2864.  
  2865. // 13. ATR Breakout
  2866. if(Close[i] > Open[i] + atr * 0.5) buyCount++;
  2867. else if(Close[i] < Open[i] - atr * 0.5) sellCount++;
  2868.  
  2869. // 14. Envelopes
  2870. double envLow = iEnvelopes(NULL, 0, 10, MODE_EMA, 0, PRICE_CLOSE, 0.002, MODE_LOWER, i);
  2871. double envUpp = iEnvelopes(NULL, 0, 10, MODE_EMA, 0, PRICE_CLOSE, 0.002, MODE_UPPER, i);
  2872. if(Close[i] <= envLow) buyCount++;
  2873. else if(Close[i] >= envUpp) sellCount++;
  2874.  
  2875. // 15. OsMA
  2876. double osma = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, i);
  2877. if(osma > 0) buyCount++;
  2878. else if(osma < 0) sellCount++;
  2879.  
  2880. // 16. Force Index
  2881. double force = iForce(NULL, 0, 13, MODE_SMA, PRICE_CLOSE, i);
  2882. if(force > 0) buyCount++;
  2883. else if(force < 0) sellCount++;
  2884.  
  2885. // 17. Volume (FIXED AVERAGE LOGIC - NO HARD CODE)
  2886. double vol = (double)Volume[i];
  2887. double volMA = 0; int volCnt = 0;
  2888. for(int v = i; v < i+20 && v < Bars; v++) { volMA += (double)Volume[v]; volCnt++; }
  2889. if(volCnt > 0) volMA /= volCnt;
  2890. if(vol > volMA && Close[i] > Open[i]) buyCount++;
  2891. else if(vol > volMA && Close[i] < Open[i]) sellCount++;
  2892.  
  2893. // 18. Price Action (Candle Pattern)
  2894. double range = High[i] - Low[i];
  2895. double body = MathAbs(Close[i] - Open[i]);
  2896. double lowWick = MathMin(Close[i], Open[i]) - Low[i];
  2897. double upWick = High[i] - MathMax(Close[i], Open[i]);
  2898. if(body > range * 0.6 && Close[i] > Open[i]) buyCount++;
  2899. else if(body > range * 0.6 && Close[i] < Open[i]) sellCount++;
  2900. else if(lowWick > body * 2 && upWick < body) buyCount++;
  2901. else if(upWick > body * 2 && lowWick < body) sellCount++;
  2902.  
  2903. // 19. Price vs MA20
  2904. if(Close[i] > ma20) buyCount++;
  2905. else if(Close[i] < ma20) sellCount++;
  2906.  
  2907. // 20. Candle Streak (Exhaustion)
  2908. int green = 0, red = 0;
  2909. for(int j = i; j < i+3 && j < Bars; j++) {
  2910. if(Close[j] > Open[j]) green++;
  2911. else if(Close[j] < Open[j]) red++;
  2912. }
  2913. if(green >= 3) sellCount++;
  2914. if(red >= 3) buyCount++;
  2915.  
  2916. // 21. Dynamic S/R Position (STABLE 25%/75% ZONES)
  2917. int highIdx = iHighest(NULL, 0, MODE_HIGH, 20, i);
  2918. int lowIdx = iLowest(NULL, 0, MODE_LOW, 20, i);
  2919. if(highIdx >= 0 && lowIdx >= 0) {
  2920. double high20 = High[highIdx];
  2921. double low20 = Low[lowIdx];
  2922. double pos = Div(Close[i] - low20, high20 - low20);
  2923. if(pos < 0.25) buyCount++;
  2924. else if(pos > 0.75) sellCount++;
  2925. }
  2926.  
  2927. // 22. Current Candle Body vs Previous
  2928. double prevBody = MathAbs(Close[i+1] - Open[i+1]);
  2929. if(body > prevBody * 1.5 && Close[i] > Open[i]) buyCount++;
  2930. else if(body > prevBody * 1.5 && Close[i] < Open[i]) sellCount++;
  2931.  
  2932. // Cache store
  2933. g_buyCountCache = buyCount;
  2934. g_sellCountCache = sellCount;
  2935. }
  2936.  
  2937. //+------------------------------------------------------------------+
  2938. //| CONFLUENCE SCORE CUBE (FIXED OVERLAP - CLEAN LAYOUT) |
  2939. //+------------------------------------------------------------------+
  2940. void DrawConfluenceCube(int x, int y, int w, int h) {
  2941. int buyCount, sellCount;
  2942. GetConfluenceScores(buyCount, sellCount);
  2943.  
  2944. int total = buyCount + sellCount;
  2945. if(total == 0) total = 1;
  2946.  
  2947. double buyPct = (double)buyCount / total * 100.0;
  2948. double sellPct = (double)sellCount / total * 100.0;
  2949.  
  2950. // Background
  2951. Bx("conf_bg", x, y, w, h, BG_DARK2, NEON_PURPLE);
  2952. Tx("conf_title", x+10, y+4, "CONFLUENCE SCORE", NEON_CYAN, 10, true);
  2953.  
  2954. // --- BUY SECTION ---
  2955. Tx("conf_buy_t", x+10, y+24, "BUY", NEON_GREEN, 11, true);
  2956. Tx("conf_buy_v", x+w-70, y+22, DoubleToString(buyPct,0)+"%",
  2957. (buyPct>=60)?NEON_GREEN:(buyPct>=40)?NEON_YELLOW:NEON_RED, 14, true); // Right side pe %
  2958.  
  2959. int barW = w - 24;
  2960. int buyFill = (int)(buyPct/100.0 * barW);
  2961. if(buyFill < 2) buyFill = 2;
  2962. Bx("conf_buy_bg", x+10, y+42, barW, 8, BG_DARK4, BG_DARK4); // 6 se 8 moti kiya
  2963. Bx("conf_buy_fill", x+10, y+42, buyFill, 8, NEON_GREEN, NEON_GREEN);
  2964.  
  2965. // --- SELL SECTION ---
  2966. Tx("conf_sell_t", x+10, y+60, "SELL", NEON_RED, 11, true);
  2967. Tx("conf_sell_v", x+w-70, y+58, DoubleToString(sellPct,0)+"%",
  2968. (sellPct>=60)?NEON_RED:(sellPct>=40)?NEON_YELLOW:NEON_GREEN, 14, true); // Right side pe %
  2969.  
  2970. int sellFill = (int)(sellPct/100.0 * barW);
  2971. if(sellFill < 2) sellFill = 2;
  2972. Bx("conf_sell_bg", x+10, y+78, barW, 8, BG_DARK4, BG_DARK4); // 6 se 8 moti kiya
  2973. Bx("conf_sell_fill", x+10, y+78, sellFill, 8, NEON_RED, NEON_RED);
  2974.  
  2975. // --- TOTAL INDICATORS (Bada aur Saaf Dikhega) ---
  2976. Tx("conf_total", x+10, y+96, IntegerToString(total)+"/22 Indicators", NEON_PURPLE, 12, true); // Size 9 se 12 badha diya
  2977.  
  2978. // --- SIGNAL TEXT ---
  2979. string signal = "";
  2980. color sigColor = NEON_YELLOW;
  2981. if(buyCount >= 14 && buyCount > sellCount) { signal = "STRONG BUY SIGNAL"; sigColor = NEON_GREEN; }
  2982. else if(sellCount >= 14 && buyCount > sellCount) { signal = "STRONG SELL SIGNAL"; sigColor = NEON_RED; }
  2983. else if(buyCount >= 10 && buyCount > sellCount) { signal = "BUY SIGNAL"; sigColor = NEON_CYAN; }
  2984. else if(sellCount >= 10 && sellCount > buyCount) { signal = "SELL SIGNAL"; sigColor = NEON_ORANGE; }
  2985. else if(buyCount > sellCount) { signal = "WEAK BUY BIAS"; sigColor = NEON_YELLOW; }
  2986. else if(sellCount > buyCount) { signal = "WEAK SELL BIAS"; sigColor = NEON_YELLOW; }
  2987. else { signal = "NO CLEAR SIGNAL"; sigColor = NEON_RED; }
  2988.  
  2989. Tx("conf_sig", x+10, y+118, signal, sigColor, 10, true);
  2990. }
  2991.  
  2992. // ============================================================
  2993. // AUTO-LEARNING: Load/Save/Update
  2994. // ============================================================
  2995. void QuantumLoadLearning()
  2996. {
  2997. int h = FileOpen(g_qLearnFile, FILE_READ|FILE_TXT|FILE_COMMON);
  2998. if(h != INVALID_HANDLE)
  2999. {
  3000. string line = FileReadString(h);
  3001. FileClose(h);
  3002.  
  3003. int p1 = StringFind(line, ",");
  3004. int p2 = StringFind(line, ",", p1+1);
  3005. int p3 = StringFind(line, ",", p2+1);
  3006. int p4 = StringFind(line, ",", p3+1);
  3007. int p5 = StringFind(line, ",", p4+1);
  3008. int p6 = StringFind(line, ",", p5+1);
  3009. int p7 = StringFind(line, ",", p6+1);
  3010. int p8 = StringFind(line, ",", p7+1);
  3011.  
  3012. if(p8 > 0)
  3013. {
  3014. g_qWeightMHI = StringToDouble(StringSubstr(line, 0, p1));
  3015. g_qWeightNCP = StringToDouble(StringSubstr(line, p1+1, p2-p1-1));
  3016. g_qWeightTrap = StringToDouble(StringSubstr(line, p2+1, p3-p2-1));
  3017. g_qWeightFib = StringToDouble(StringSubstr(line, p3+1, p4-p3-1));
  3018. g_qWeightBB = StringToDouble(StringSubstr(line, p4+1, p5-p4-1));
  3019. g_qWeightNeural = StringToDouble(StringSubstr(line, p5+1, p6-p5-1));
  3020. g_qWeightHA = StringToDouble(StringSubstr(line, p6+1, p7-p6-1));
  3021. g_qTotalTrades = (int)StringToInteger(StringSubstr(line, p7+1, p8-p7-1));
  3022. g_qWinTrades = (int)StringToInteger(StringSubstr(line, p8+1));
  3023. }
  3024. }
  3025. }
  3026.  
  3027. void QuantumSaveLearning()
  3028. {
  3029. int h = FileOpen(g_qLearnFile, FILE_WRITE|FILE_TXT|FILE_COMMON);
  3030. if(h != INVALID_HANDLE)
  3031. {
  3032. string line = DoubleToString(g_qWeightMHI,4) + "," +
  3033. DoubleToString(g_qWeightNCP,4) + "," +
  3034. DoubleToString(g_qWeightTrap,4) + "," +
  3035. DoubleToString(g_qWeightFib,4) + "," +
  3036. DoubleToString(g_qWeightBB,4) + "," +
  3037. DoubleToString(g_qWeightNeural,4) + "," +
  3038. DoubleToString(g_qWeightHA,4) + "," +
  3039. IntegerToString(g_qTotalTrades) + "," +
  3040. IntegerToString(g_qWinTrades);
  3041. FileWriteString(h, line);
  3042. FileClose(h);
  3043. }
  3044. }
  3045.  
  3046. void QuantumUpdateLearning(bool win)
  3047. {
  3048. g_qTotalTrades++;
  3049. if(win) g_qWinTrades++;
  3050.  
  3051. double lr = 0.015;
  3052. double adj = win ? lr : -lr * 1.5;
  3053.  
  3054. if(g_qIsTrap) g_qWeightTrap += adj;
  3055. else {
  3056. if(g_qPattern == "GGG" || g_qPattern == "RRR" || g_qPattern == "GGR" || g_qPattern == "RRG")
  3057. g_qWeightMHI += adj;
  3058. if(g_otcHasSignal) g_qWeightFib += adj;
  3059. }
  3060.  
  3061. g_qWeightMHI = MathMax(0.05, MathMin(0.50, g_qWeightMHI));
  3062. g_qWeightNCP = MathMax(0.05, MathMin(0.50, g_qWeightNCP));
  3063. g_qWeightTrap = MathMax(0.05, MathMin(0.50, g_qWeightTrap));
  3064. g_qWeightFib = MathMax(0.05, MathMin(0.50, g_qWeightFib));
  3065. g_qWeightBB = MathMax(0.05, MathMin(0.50, g_qWeightBB));
  3066. g_qWeightNeural = MathMax(0.05, MathMin(0.50, g_qWeightNeural));
  3067. g_qWeightHA = MathMax(0.05, MathMin(0.50, g_qWeightHA));
  3068.  
  3069. double totalW = g_qWeightMHI + g_qWeightNCP + g_qWeightTrap +
  3070. g_qWeightFib + g_qWeightBB + g_qWeightNeural + g_qWeightHA;
  3071. if(totalW > 0)
  3072. {
  3073. g_qWeightMHI /= totalW;
  3074. g_qWeightNCP /= totalW;
  3075. g_qWeightTrap /= totalW;
  3076. g_qWeightFib /= totalW;
  3077. g_qWeightBB /= totalW;
  3078. g_qWeightNeural /= totalW;
  3079. g_qWeightHA /= totalW;
  3080. }
  3081.  
  3082. QuantumSaveLearning();
  3083. }
  3084.  
  3085. // ============================================================
  3086. // 150 CANDLE HISTORY: Build & Analyze
  3087. // ============================================================
  3088. void QuantumBuildHistory()
  3089. {
  3090. g_histCount = 0;
  3091. int maxBars = MathMin(150, Bars - 5);
  3092.  
  3093. for(int i = 5; i < 5 + maxBars && i < Bars; i++)
  3094. {
  3095. int c1 = (Close[i] > Open[i]) ? 1 : (Close[i] < Open[i]) ? -1 : 0;
  3096. int c2 = (Close[i+1] > Open[i+1]) ? 1 : (Close[i+1] < Open[i+1]) ? -1 : 0;
  3097. int c3 = (Close[i+2] > Open[i+2]) ? 1 : (Close[i+2] < Open[i+2]) ? -1 : 0;
  3098.  
  3099. int result = (Close[i-1] > Open[i-1]) ? 1 : (Close[i-1] < Open[i-1]) ? -1 : 0;
  3100.  
  3101. int patternCode = (c3+1)*100 + (c2+1)*10 + (c1+1);
  3102.  
  3103. g_histPatterns[g_histCount] = patternCode;
  3104. g_histResults[g_histCount] = result;
  3105. g_histCount++;
  3106. }
  3107. }
  3108.  
  3109. int QuantumCheckHistory(int currentPattern)
  3110. {
  3111. if(g_histCount < 20) return 0;
  3112.  
  3113. int matchCall = 0, matchPut = 0, totalMatch = 0;
  3114.  
  3115. for(int i = 0; i < g_histCount; i++)
  3116. {
  3117. if(g_histPatterns[i] == currentPattern)
  3118. {
  3119. totalMatch++;
  3120. if(g_histResults[i] == 1) matchCall++;
  3121. else if(g_histResults[i] == -1) matchPut++;
  3122. }
  3123. }
  3124.  
  3125. if(totalMatch < 3) return 0;
  3126.  
  3127. double callPct = (double)matchCall / totalMatch * 100.0;
  3128. double putPct = (double)matchPut / totalMatch * 100.0;
  3129.  
  3130. g_qHistoryScore = totalMatch;
  3131.  
  3132. if(callPct >= 60) return (int)callPct;
  3133. else if(putPct >= 60) return -(int)putPct;
  3134. return 0;
  3135. }
  3136.  
  3137. // ============================================================
  3138. // TIME-BASED PROBABILITY
  3139. // ============================================================
  3140. void QuantumCalcTimeProb()
  3141. {
  3142. int age = (int)(TimeCurrent() - Time[0]);
  3143. int ps = Period() * 60;
  3144. double pct = (double)age / ps * 100.0;
  3145.  
  3146. if(pct < 20)
  3147. {
  3148. g_qTimePhase = "EARLY";
  3149. g_qTimeProb = 45.0;
  3150. }
  3151. else if(pct < 50)
  3152. {
  3153. g_qTimePhase = "MID";
  3154. g_qTimeProb = 65.0;
  3155. }
  3156. else if(pct < 80)
  3157. {
  3158. g_qTimePhase = "LATE";
  3159. g_qTimeProb = 75.0;
  3160. }
  3161. else
  3162. {
  3163. g_qTimePhase = "LAST";
  3164. g_qTimeProb = 55.0;
  3165. }
  3166.  
  3167. double spread = (double)MarketInfo(Symbol(), MODE_SPREAD);
  3168. double pip = GetUniversalPip();
  3169. double spreadPips = spread * Point / pip;
  3170.  
  3171. if(spreadPips > 5) g_qSpreadPenalty = 15.0;
  3172. else if(spreadPips > 3) g_qSpreadPenalty = 8.0;
  3173. else g_qSpreadPenalty = 0.0;
  3174. }
  3175.  
  3176. // ============================================================
  3177. // BACKGROUND PAIRS MHI SCAN
  3178. // ============================================================
  3179. void QuantumScanBackgroundMHI()
  3180. {
  3181. g_bgMHI_Count = 0;
  3182.  
  3183. if(g_pairCount == 0) return;
  3184.  
  3185. for(int i = 0; i < g_pairCount && i < 8; i++)
  3186. {
  3187. string pair = g_pairNames[i];
  3188. if(pair == Symbol()) continue;
  3189.  
  3190. double c1 = iClose(pair, PERIOD_M1, 1);
  3191. double o1 = iOpen(pair, PERIOD_M1, 1);
  3192. double c2 = iClose(pair, PERIOD_M1, 2);
  3193. double o2 = iOpen(pair, PERIOD_M1, 2);
  3194. double c3 = iClose(pair, PERIOD_M1, 3);
  3195. double o3 = iOpen(pair, PERIOD_M1, 3);
  3196.  
  3197. if(c1 == 0 || o1 == 0) continue;
  3198.  
  3199. string pColors = "";
  3200. if(c1 > o1) pColors += "G"; else if(c1 < o1) pColors += "R"; else pColors += "D";
  3201. if(c2 > o2) pColors += "G"; else if(c2 < o2) pColors += "R"; else pColors += "D";
  3202. if(c3 > o3) pColors += "G"; else if(c3 < o3) pColors += "R"; else pColors += "D";
  3203.  
  3204. string sig = "WAIT";
  3205. double conf = 50;
  3206.  
  3207. if(pColors == "GGR") { sig = "PUT"; conf = 70; }
  3208. else if(pColors == "RRG") { sig = "CALL"; conf = 70; }
  3209. else if(pColors == "GGG") { sig = "PUT"; conf = 85; }
  3210. else if(pColors == "RRR") { sig = "CALL"; conf = 85; }
  3211.  
  3212. if(sig != "WAIT")
  3213. {
  3214. g_bgMHI_Signal[g_bgMHI_Count] = sig;
  3215. g_bgMHI_Conf[g_bgMHI_Count] = conf;
  3216. g_bgMHI_Pair[g_bgMHI_Count] = pair;
  3217. g_bgMHI_Count++;
  3218. }
  3219. }
  3220. }
  3221.  
  3222. // ============================================================
  3223. // MAIN QUANTUM v9.1 PRO ENGINE
  3224. // ============================================================
  3225. void CalculateQuantumEngine()
  3226. {
  3227. g_qSignal = "WAIT";
  3228. g_qConf = 50.0;
  3229. g_qPattern = "---";
  3230. g_qStatus = "SCANNING";
  3231. g_qColor = NEON_YELLOW;
  3232. g_qReason = "";
  3233. g_qIsTrap = false;
  3234. g_qCallScore = 50.0;
  3235. g_qPutScore = 50.0;
  3236. g_qConfluence = "";
  3237. g_qHistoryScore = 0;
  3238.  
  3239. if(Bars < 20) return;
  3240.  
  3241. static datetime lastHistBuild = 0;
  3242. if(Time[0] != lastHistBuild)
  3243. {
  3244. QuantumBuildHistory();
  3245. lastHistBuild = Time[0];
  3246. }
  3247.  
  3248. QuantumCalcTimeProb();
  3249. QuantumScanBackgroundMHI();
  3250.  
  3251. // MODULE 1: MHI PATTERN
  3252. double mhiCall = 0, mhiPut = 0;
  3253. string colors = "";
  3254. for(int i = 1; i <= 3; i++)
  3255. {
  3256. if(Close[i] > Open[i]) colors += "G";
  3257. else if(Close[i] < Open[i]) colors += "R";
  3258. else colors += "D";
  3259. }
  3260. g_qPattern = colors;
  3261.  
  3262. if(colors == "GGR") { mhiPut = 85; mhiCall = 15; }
  3263. else if(colors == "RRG") { mhiCall = 85; mhiPut = 15; }
  3264. else if(colors == "GGG") { mhiPut = 90; mhiCall = 10; }
  3265. else if(colors == "RRR") { mhiCall = 90; mhiPut = 10; }
  3266. else if(colors == "GRG") { mhiCall = 40; mhiPut = 40; }
  3267. else if(colors == "RGR") { mhiCall = 40; mhiPut = 40; }
  3268. else if(colors == "GRR") { mhiCall = 65; mhiPut = 35; }
  3269. else if(colors == "RGG") { mhiPut = 65; mhiCall = 35; }
  3270.  
  3271. if(StringFind(colors, "D") >= 0) { mhiCall -= 10; mhiPut -= 10; }
  3272.  
  3273. int currentPat = ((Close[3]>Open[3]?1:Close[3]<Open[3]?-1:0)+1)*100 +
  3274. ((Close[2]>Open[2]?1:Close[2]<Open[2]?-1:0)+1)*10 +
  3275. ((Close[1]>Open[1]?1:Close[1]<Open[1]?-1:0)+1);
  3276. int histBoost = QuantumCheckHistory(currentPat);
  3277.  
  3278. if(histBoost > 0) { mhiCall += histBoost * 0.3; mhiPut -= histBoost * 0.3; }
  3279. else if(histBoost < 0) { mhiPut += MathAbs(histBoost) * 0.3; mhiCall -= MathAbs(histBoost) * 0.3; }
  3280.  
  3281. int bgAgreeCall = 0, bgAgreePut = 0;
  3282. for(int b = 0; b < g_bgMHI_Count; b++)
  3283. {
  3284. if(g_bgMHI_Signal[b] == "CALL") bgAgreeCall++;
  3285. else if(g_bgMHI_Signal[b] == "PUT") bgAgreePut++;
  3286. }
  3287.  
  3288. if(bgAgreeCall >= 2) { mhiCall += 10; mhiPut -= 5; }
  3289. if(bgAgreePut >= 2) { mhiPut += 10; mhiCall -= 5; }
  3290.  
  3291. // MODULE 2: NCP PRO v8
  3292. double ncpCall = 50, ncpPut = 50;
  3293. if(ENABLE_MTG && g_mtgState != "INIT")
  3294. {
  3295. ncpCall = g_smoothCallScore;
  3296. ncpPut = g_smoothPutScore;
  3297. }
  3298.  
  3299. // MODULE 3: VISUAL TRAP
  3300. double trapCall = 50, trapPut = 50;
  3301. if(g_visualTrapPro.callBias > 0 || g_visualTrapPro.putBias > 0)
  3302. {
  3303. trapCall = g_visualTrapPro.callBias;
  3304. trapPut = g_visualTrapPro.putBias;
  3305. }
  3306. if(g_visualTrapPro.trapScore >= 70)
  3307. {
  3308. double temp = trapCall; trapCall = trapPut; trapPut = temp;
  3309. g_qIsTrap = true;
  3310. }
  3311.  
  3312. // MODULE 4: FIB REJECTION
  3313. double fibCall = 50, fibPut = 50;
  3314. if(g_otcHasSignal && g_otcFibStrength > 0)
  3315. {
  3316. if(g_otcFibDir == "CALL") { fibCall = g_otcFibStrength; fibPut = 100 - g_otcFibStrength; }
  3317. else if(g_otcFibDir == "PUT") { fibPut = g_otcFibStrength; fibCall = 100 - g_otcFibStrength; }
  3318. }
  3319.  
  3320. // MODULE 5: BB PULLBACK
  3321. double bbCall = 50, bbPut = 50;
  3322. if(ENABLE_BB_PULLBACK && g_bbSignal != "--")
  3323. {
  3324. if(g_bbSignal == "CALL") { bbCall = 80; bbPut = 20; }
  3325. else if(g_bbSignal == "PUT") { bbPut = 80; bbCall = 20; }
  3326. }
  3327.  
  3328. // MODULE 6: NEURAL AI
  3329. double neuralCall = 50, neuralPut = 50;
  3330. double nb = NeuralBiasFast();
  3331. if(nb > 50) { neuralCall = nb; neuralPut = 100 - nb; }
  3332. else { neuralPut = 100 - nb; neuralCall = nb; }
  3333.  
  3334. // MODULE 7: HA CANDLES
  3335. double haCall = 50, haPut = 50;
  3336. if(g_haM1 == "HA BULLISH 1") haCall += 20;
  3337. else if(g_haM1 == "HA BEARISH 1") haPut += 20;
  3338. if(g_haM5 == "HA BULLISH 5") haCall += 15;
  3339. else if(g_haM5 == "HA BEARISH 5") haPut += 15;
  3340. if(g_haBoth == "BOTH BULL") haCall += 25;
  3341. else if(g_haBoth == "BOTH BEAR") haPut += 25;
  3342.  
  3343. // QUANTUM CONFLUENCE (Dynamic Weights)
  3344. g_qCallScore = (
  3345. mhiCall * g_qWeightMHI +
  3346. ncpCall * g_qWeightNCP +
  3347. trapCall * g_qWeightTrap +
  3348. fibCall * g_qWeightFib +
  3349. bbCall * g_qWeightBB +
  3350. neuralCall * g_qWeightNeural +
  3351. haCall * g_qWeightHA
  3352. );
  3353.  
  3354. g_qPutScore = (
  3355. mhiPut * g_qWeightMHI +
  3356. ncpPut * g_qWeightNCP +
  3357. trapPut * g_qWeightTrap +
  3358. fibPut * g_qWeightFib +
  3359. bbPut * g_qWeightBB +
  3360. neuralPut * g_qWeightNeural +
  3361. haPut * g_qWeightHA
  3362. );
  3363.  
  3364. // Time & Spread Adjustments
  3365. double timeBoost = (g_qTimeProb - 50) * 0.3;
  3366. if(g_qCallScore > g_qPutScore) g_qCallScore += timeBoost;
  3367. else g_qPutScore += timeBoost;
  3368.  
  3369. if(g_qCallScore > g_qPutScore) g_qCallScore -= g_qSpreadPenalty;
  3370. else g_qPutScore -= g_qSpreadPenalty;
  3371.  
  3372. // Normalize
  3373. double total = g_qCallScore + g_qPutScore;
  3374. if(total > 0)
  3375. {
  3376. g_qCallScore = (g_qCallScore / total) * 100;
  3377. g_qPutScore = (g_qPutScore / total) * 100;
  3378. }
  3379.  
  3380. // CONFLUENCE CHECK
  3381. string agreeSources = "";
  3382. int agreeCount = 0;
  3383.  
  3384. if(mhiCall > 60 && g_qCallScore > 55) { agreeSources += "MHI+"; agreeCount++; }
  3385. if(mhiPut > 60 && g_qPutScore > 55) { agreeSources += "MHI+"; agreeCount++; }
  3386. if(ncpCall > 60 && g_qCallScore > 55) { agreeSources += "NCP+"; agreeCount++; }
  3387. if(ncpPut > 60 && g_qPutScore > 55) { agreeSources += "NCP+"; agreeCount++; }
  3388. if(trapCall > 60 && g_qCallScore > 55) { agreeSources += "TRP+"; agreeCount++; }
  3389. if(trapPut > 60 && g_qPutScore > 55) { agreeSources += "TRP+"; agreeCount++; }
  3390. if(fibCall > 60 && g_qCallScore > 55) { agreeSources += "FIB+"; agreeCount++; }
  3391. if(fibPut > 60 && g_qPutScore > 55) { agreeSources += "FIB+"; agreeCount++; }
  3392. if(bbCall > 60 && g_qCallScore > 55) { agreeSources += "BB+"; agreeCount++; }
  3393. if(bbPut > 60 && g_qPutScore > 55) { agreeSources += "BB+"; agreeCount++; }
  3394.  
  3395. g_qConfluence = agreeSources + " (" + IntegerToString(agreeCount) + "/7)";
  3396.  
  3397. // FINAL DECISION
  3398. double dominant = MathMax(g_qCallScore, g_qPutScore);
  3399. int minAgree = 3;
  3400.  
  3401. if(agreeCount < minAgree || dominant < 58)
  3402. {
  3403. g_qSignal = "WAIT";
  3404. g_qConf = dominant;
  3405. g_qStatus = "LOW CONF";
  3406. g_qColor = NEON_YELLOW;
  3407. g_qReason = "Need " + IntegerToString(minAgree) + "+ src | Got:" + IntegerToString(agreeCount);
  3408. return;
  3409. }
  3410.  
  3411. // TRAP OVERRIDE
  3412. if(g_qIsTrap && dominant >= 75)
  3413. {
  3414. if(g_qCallScore > g_qPutScore)
  3415. {
  3416. g_qSignal = "TRAP_PUT";
  3417. g_qColor = NEON_PURPLE;
  3418. g_qStatus = "QUANTUM TRAP";
  3419. }
  3420. else
  3421. {
  3422. g_qSignal = "TRAP_CALL";
  3423. g_qColor = NEON_PURPLE;
  3424. g_qStatus = "QUANTUM TRAP";
  3425. }
  3426. g_qConf = dominant;
  3427. g_qReason = "TRAP:" + IntegerToString((int)g_visualTrapPro.trapScore) + "% | " + g_qConfluence;
  3428. return;
  3429. }
  3430.  
  3431. // NORMAL SIGNAL
  3432. if(g_qCallScore > g_qPutScore)
  3433. {
  3434. g_qSignal = "CALL";
  3435. g_qConf = g_qCallScore;
  3436. if(g_qCallScore >= 85) { g_qStatus = "QUANTUM STRONG"; g_qColor = NEON_GREEN; }
  3437. else if(g_qCallScore >= 72) { g_qStatus = "QUANTUM GOOD"; g_qColor = NEON_LIME; }
  3438. else { g_qStatus = "QUANTUM WEAK"; g_qColor = NEON_CYAN; }
  3439. }
  3440. else
  3441. {
  3442. g_qSignal = "PUT";
  3443. g_qConf = g_qPutScore;
  3444. if(g_qPutScore >= 85) { g_qStatus = "QUANTUM STRONG"; g_qColor = NEON_RED; }
  3445. else if(g_qPutScore >= 72) { g_qStatus = "QUANTUM GOOD"; g_qColor = C'255,100,100'; }
  3446. else { g_qStatus = "QUANTUM WEAK"; g_qColor = NEON_ORANGE; }
  3447. }
  3448.  
  3449. g_qReason = g_qConfluence;
  3450.  
  3451. if(g_qHistoryScore > 0)
  3452. {
  3453. g_qReason += " | HIST:" + IntegerToString(g_qHistoryScore);
  3454. }
  3455. }
  3456.  
  3457. // ============================================================
  3458. // v9.1 PRO DASHBOARD DRAW (WIDE GAP & BIG FONTS)
  3459. // ============================================================
  3460. void DrawQuantumCube(int x, int y, int w, int h)
  3461. {
  3462. color borderC = g_qIsTrap ? NEON_PURPLE : g_qColor;
  3463. if(g_qSignal == "WAIT") borderC = C'80,80,90';
  3464.  
  3465. Bx("c_quantum", x, y, w, h, BG_DARK2, borderC);
  3466.  
  3467. // 1. Title (Bada)
  3468. Tx("c_quantum_l", x+10, y+5, "MHI-QUANTUM v9.1", NEON_CYAN, 10, true);
  3469.  
  3470. // 2. MAIN LINE: Signal (Left) + Conf/Phase (Right) - WIDE GAP
  3471. string sigDisplay = g_qSignal;
  3472. if(StringFind(g_qSignal, "TRAP_") == 0)
  3473. sigDisplay = "!" + StringSubstr(g_qSignal, 5);
  3474.  
  3475. // Signal left side (Font 16)
  3476. Tx("c_quantum_sig", x+10, y+22, sigDisplay, g_qColor, 16, true);
  3477.  
  3478. // Conf & Time right side mein (Font 12, Gap x+105)
  3479. string confText = DoubleToString(g_qConf, 0) + "% " + g_qTimePhase;
  3480. Tx("c_quantum_conf", x+105, y+24, confText, NEON_WHITE, 12, true);
  3481.  
  3482. // 3. Pattern & History (Font 11)
  3483. color patC = (g_qPattern == "GGG" || g_qPattern == "GGR") ? NEON_RED :
  3484. (g_qPattern == "RRR" || g_qPattern == "RRG") ? NEON_GREEN : CGR;
  3485. string patText = "PATTERN: " + g_qPattern;
  3486. if(g_qHistoryScore > 0) patText += " [H" + IntegerToString(g_qHistoryScore) + "]";
  3487. Tx("c_quantum_pat", x+10, y+46, patText, patC, 11, true);
  3488.  
  3489. // 4. Status (Font 12)
  3490. Tx("c_quantum_stat", x+10, y+68, g_qStatus, g_qColor, 12, true);
  3491.  
  3492. // 5. Confluence Sources (Font 10)
  3493. string confl = g_qConfluence;
  3494. if(StringLen(confl) > 28) confl = StringSubstr(confl, 0, 26) + "..";
  3495. Tx("c_quantum_src", x+10, y+90, confl, NEON_GOLD, 10, true);
  3496.  
  3497. // 6. Spread Warning
  3498. if(g_qSpreadPenalty > 0)
  3499. {
  3500. Tx("c_quantum_spd", x+10, y+110, "SPREAD -" + DoubleToString(g_qSpreadPenalty,0) + "%", NEON_ORANGE, 10, true);
  3501. }
  3502.  
  3503. // 7. Background Pairs Sync
  3504. if(g_bgMHI_Count > 0)
  3505. {
  3506. string bgText = "BG PAIRS: " + IntegerToString(g_bgMHI_Count);
  3507. Tx("c_quantum_bg", x+10, y+126, bgText, NEON_PURPLE, 10, true);
  3508. }
  3509.  
  3510. // 8. Dual Bar - Call vs Put (Bottom)
  3511. int barW = w - 20;
  3512. int barY = y + h - 10;
  3513.  
  3514. Bx("c_quantum_bar_bg", x+10, barY, barW, 6, BG_DARK4, BG_DARK4);
  3515.  
  3516. int callW = (int)(g_qCallScore / 100.0 * barW);
  3517. if(callW < 1) callW = 1; if(callW > barW) callW = barW;
  3518. Bx("c_quantum_call", x+10, barY, callW, 6, C'0,180,90', C'0,180,90');
  3519.  
  3520. int putW = (int)(g_qPutScore / 100.0 * barW);
  3521. if(putW < 1) putW = 1; if(putW > barW) putW = barW;
  3522. int putX = x + 10 + barW - putW;
  3523. Bx("c_quantum_put", putX, barY, putW, 6, C'200,50,50', C'200,50,50');
  3524.  
  3525. int centerX = x + 10 + barW/2 - 1;
  3526. Bx("c_quantum_ctr", centerX, barY-1, 2, 8, NEON_WHITE, NEON_WHITE);
  3527. }
  3528.  
  3529. // ============================================================
  3530. // ACCURACY TRACKING FOR AUTO-LEARNING
  3531. // ============================================================
  3532. void QuantumCheckResult()
  3533. {
  3534. static datetime lastCheckBar = 0;
  3535. if(Time[0] == lastCheckBar) return;
  3536. lastCheckBar = Time[0];
  3537.  
  3538. if(g_qSignal != "WAIT" && g_qLastSignalBar == Time[1])
  3539. {
  3540. bool wasCall = (g_qLastSignal == "CALL" || g_qLastSignal == "TRAP_PUT");
  3541. bool wasPut = (g_qLastSignal == "PUT" || g_qLastSignal == "TRAP_CALL");
  3542.  
  3543. bool greenBar = (Close[1] > Open[1]);
  3544. bool redBar = (Close[1] < Open[1]);
  3545.  
  3546. bool win = false;
  3547. if(wasCall && greenBar) win = true;
  3548. if(wasPut && redBar) win = true;
  3549.  
  3550. QuantumUpdateLearning(win);
  3551. }
  3552.  
  3553. if(g_qSignal != "WAIT")
  3554. {
  3555. g_qLastSignal = g_qSignal;
  3556. g_qLastSignalBar = Time[0];
  3557. }
  3558. }
  3559.  
  3560.  
  3561. //+------------------------------------------------------------------+
  3562. //| CORRECTED ORDER BLOCK DETECTION (Single Opposite + BOS) |
  3563. //+------------------------------------------------------------------+
  3564. int DetectM1_OrderBlock(double &OB_High, double &OB_Low, datetime &OB_Time)
  3565. {
  3566. if(Bars < 15) return 0;
  3567.  
  3568. for(int i = 2; i < 15; i++) // OB_LookBack = 15
  3569. {
  3570. if(i + 3 >= Bars) continue;
  3571.  
  3572. double move_body = MathAbs(iClose(NULL,PERIOD_M1,i)-iOpen(NULL,PERIOD_M1,i))/Point;
  3573. if(move_body < 1.5) continue; // MinBodyPoints = 1.5
  3574.  
  3575. if(iClose(NULL,PERIOD_M1,i) < iOpen(NULL,PERIOD_M1,i))
  3576. {
  3577. if(iClose(NULL,PERIOD_M1,i+1) > iOpen(NULL,PERIOD_M1,i+1))
  3578. {
  3579. bool multiBull = false;
  3580. if(i+2 < Bars && iClose(NULL,PERIOD_M1,i+2) > iOpen(NULL,PERIOD_M1,i+2)) multiBull = true;
  3581. if(i+3 < Bars && iClose(NULL,PERIOD_M1,i+3) > iOpen(NULL,PERIOD_M1,i+3)) multiBull = true;
  3582.  
  3583. if(!multiBull)
  3584. {
  3585. OB_High = iHigh(NULL, PERIOD_M1, i+1);
  3586. OB_Low = iLow(NULL, PERIOD_M1, i+1);
  3587. OB_Time = iTime(NULL, PERIOD_M1, i+1);
  3588. return -1;
  3589. }
  3590. }
  3591. }
  3592.  
  3593. if(iClose(NULL,PERIOD_M1,i) > iOpen(NULL,PERIOD_M1,i))
  3594. {
  3595. if(iClose(NULL,PERIOD_M1,i+1) < iOpen(NULL,PERIOD_M1,i+1))
  3596. {
  3597. bool multiBear = false;
  3598. if(i+2 < Bars && iClose(NULL,PERIOD_M1,i+2) < iOpen(NULL,PERIOD_M1,i+2)) multiBear = true;
  3599. if(i+3 < Bars && iClose(NULL,PERIOD_M1,i+3) < iOpen(NULL,PERIOD_M1,i+3)) multiBear = true;
  3600.  
  3601. if(!multiBear)
  3602. {
  3603. OB_High = iHigh(NULL, PERIOD_M1, i+1);
  3604. OB_Low = iLow(NULL, PERIOD_M1, i+1);
  3605. OB_Time = iTime(NULL, PERIOD_M1, i+1);
  3606. return 1;
  3607. }
  3608. }
  3609. }
  3610. }
  3611.  
  3612. return 0;
  3613. }
  3614.  
  3615. // ============================================================
  3616. // MAIN DASHBOARD DRAW (FIXED NEXT CANDLE CUBE)
  3617. // ============================================================
  3618. void Draw(){
  3619. HideSPM(); DelDashboard();
  3620. int W=DASH_W,G=8,CW=(W-2*G)/3,CH=110,cx=POS_X+G,yy=POS_Y;
  3621. int cp=0;double brain=BrainSc(cp);double nb=NeuralBiasFast();double mw=MicroWick();bool bf=IsBrokerForce();
  3622. g_marketMode=DetectMarketMode(brain,g_rsc,cp,nb);
  3623. if(StringLen(g_prevMode)>0&&g_prevMode!=g_marketMode){g_signalTime=0;g_finalSignal="WAIT";}g_prevMode=g_marketMode;
  3624. CalcOTCCrowd();UpdateAccuracy();UpdateWeights();
  3625. double mai=CalcAdvancedMicroAI();int mts=CalcAdvancedMicroTrap();
  3626. CalcFibRejectionHunter();
  3627. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  3628. UpdateRiskControl();CheckMTFConfirmation();
  3629. double tfaScore=CalcAdvancedTFA(g_tfa_detail);
  3630. color ha1C=(g_haM1=="HA BULLISH 1")?NEON_GREEN:(g_haM1=="HA BEARISH 1")?NEON_RED:NEON_YELLOW;
  3631. color ha5C=(g_haM5=="HA BULLISH 5")?NEON_GREEN:(g_haM5=="HA BEARISH 5")?NEON_RED:NEON_YELLOW;
  3632. color biasClr;string biasStr=CalcAdvancedMarketBias(biasClr,brain,g_rsc);
  3633. string stratLabel=(g_strategyType=="TRAP")?"TRAP":(g_strategyType=="TREND")?"TREND":"NO STRATEGY";
  3634. color stratC=(g_strategyType=="TRAP")?NEON_PURPLE:(g_strategyType=="TREND")?NEON_GREEN:CGR;
  3635.  
  3636. g_rsc = brain * 0.5;
  3637.  
  3638. g_frozen_gProb = g_calc_gProb;
  3639. g_frozen_rProb = g_calc_rProb;
  3640. g_frozen_pAction = g_calc_pAction;
  3641. g_frozen_pColor = g_calc_pColor;
  3642.  
  3643.  
  3644.  
  3645. // ROW 1
  3646. Cube("c1",cx,yy,CW,CH,NEON_CYAN,BG_DARK2,"PAIR",NEON_CYAN,Symbol(),NEON_BLUE,13);
  3647.  
  3648. DrawQuantumCube(cx+CW+G, yy, CW, 130);
  3649.  
  3650. // ----- TIMER & STRENGTH CUBE (NEW) -----
  3651. int tfSec=300; static datetime lastM5Bar=0; static datetime m5BarStartLocal=0; datetime curM5=iTime(NULL,PERIOD_M5,0); if(curM5!=lastM5Bar){lastM5Bar=curM5; m5BarStartLocal=TimeLocal();} int elapsedM5=(int)(TimeLocal()-m5BarStartLocal); int trem=tfSec-elapsedM5; if(trem<0)trem=0; if(trem>=tfSec)trem=tfSec-1; string timerStr=StringFormat("M5: %02d:%02d", trem/60, trem%60);
  3652.  
  3653. // --- M5 STRENGTH ---
  3654. double m5H=iHigh(NULL,PERIOD_M5,1), m5L=iLow(NULL,PERIOD_M5,1), m5O=iOpen(NULL,PERIOD_M5,1), m5C=iClose(NULL,PERIOD_M5,1);
  3655. double m5Rng=m5H-m5L; if(m5Rng<=Point) m5Rng=Point;
  3656. double m5Body=MathAbs(m5C-m5O);
  3657. double m5Uw=m5H-MathMax(m5O,m5C), m5Lw=MathMin(m5O,m5C)-m5L;
  3658. double m5Str=MathMax(0, MathMin(100, (m5Body/m5Rng)*100.0 - ((m5Uw+m5Lw)/m5Rng)*25.0));
  3659. bool m5Bull=m5C>m5O; string m5Txt="M5: "+DoubleToString(m5Str,0)+"% "+(m5Bull?"UP":"DN");
  3660. color m5Clr=(m5Str>=70)?((m5Bull)?NEON_GREEN:NEON_RED):((m5Str>=40)?NEON_YELLOW:CGR);
  3661.  
  3662. // --- M1 STRENGTH ---
  3663. double m1Rng=High[1]-Low[1]; if(m1Rng<=Point) m1Rng=Point;
  3664. double m1Body=MathAbs(Close[1]-Open[1]);
  3665. double m1Uw=High[1]-MathMax(Open[1],Close[1]), m1Lw=MathMin(Open[1],Close[1])-Low[1];
  3666. double m1Str=MathMax(0, MathMin(100, (m1Body/m1Rng)*100.0 - ((m1Uw+m1Lw)/m1Rng)*25.0));
  3667. bool m1Bull=Close[1]>Open[1]; string m1Txt="M1: "+DoubleToString(m1Str,0)+"% "+(m1Bull?"UP":"DN");
  3668. color m1Clr=(m1Str>=70)?((m1Bull)?NEON_GREEN:NEON_RED):((m1Str>=40)?NEON_YELLOW:CGR);
  3669.  
  3670. // --- M2 STRENGTH (Last 2 M1 Candles Combined) ---
  3671. double m2H=MathMax(High[1],High[2]), m2L=MathMin(Low[1],Low[2]);
  3672. double m2O=Open[2], m2C=Close[1];
  3673. double m2Rng=m2H-m2L; if(m2Rng<=Point) m2Rng=Point;
  3674. double m2Body=MathAbs(m2C-m2O);
  3675. double m2Uw=m2H-MathMax(m2O,m2C), m2Lw=MathMin(m2O,m2C)-m2L;
  3676. double m2Str=MathMax(0, MathMin(100, (m2Body/m2Rng)*100.0 - ((m2Uw+m2Lw)/m2Rng)*25.0));
  3677. bool m2Bull=m2C>m2O; string m2Txt="M2: "+DoubleToString(m2Str,0)+"% "+(m2Bull?"UP":"DN");
  3678. color m2Clr=(m2Str>=70)?((m2Bull)?NEON_GREEN:NEON_RED):((m2Str>=40)?NEON_YELLOW:CGR);
  3679.  
  3680. // --- DRAW CUBE ---
  3681. Bx("c_str",cx+2*(CW+G),yy,CW,CH,BG_DARK2,NEON_CYAN);
  3682. Bx("c_str_acc",cx+2*(CW+G),yy,3,CH,NEON_CYAN,NEON_CYAN);
  3683. Tx("c_str_t",cx+2*(CW+G)+10,yy+5,timerStr,NEON_GREEN,12,true);
  3684. Tx("c_str_m5",cx+2*(CW+G)+10,yy+24,m5Txt,m5Clr,11,true);
  3685. Tx("c_str_m2",cx+2*(CW+G)+10,yy+48,m2Txt,m2Clr,11,true);
  3686. Tx("c_str_m1",cx+2*(CW+G)+10,yy+72,m1Txt,m1Clr,11,true);
  3687.  
  3688. // --- AVERAGE STRENGTH BAR ---
  3689. int barY=yy+CH-8; int barW=CW-24;
  3690. double avgStr=(m5Str+m2Str+m1Str)/3.0;
  3691. Bx("c_str_bg",cx+2*(CW+G)+10,barY,barW,4,BG_DARK4,BG_DARK4);
  3692. int fillW=(int)(avgStr/100.0*barW); if(fillW<2)fillW=2; if(fillW>barW)fillW=barW;
  3693. color avgClr=(avgStr>=60)?NEON_GREEN:((avgStr>=40)?NEON_YELLOW:NEON_RED);
  3694. Bx("c_str_fg",cx+2*(CW+G)+10,barY,fillW,4,avgClr,avgClr);
  3695. yy += CH + G;
  3696.  
  3697.  
  3698.  
  3699.  
  3700. // ROW 2
  3701. 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);NeonBar("c7b",cx+12,yy+92,CW-24,7,nb,(nb>72)?NEON_ORANGE:(nb<35)?NEON_CYAN:NEON_GREEN);
  3702. Bx("c8",cx+CW+G,yy,CW,CH,BG_DARK2,NEON_CYAN);Tx("c8_l",cx+CW+G+10,yy+4,"CROWD",NEON_CYAN,10,true);Tx("c8_c",cx+CW+G+10,yy+24,"CALL "+IntegerToString(g_otcCallPct)+"%",NEON_RED,14,true);Tx("c8_p",cx+CW+G+10,yy+54,"PUT "+IntegerToString(g_otcPutPct)+"%",NEON_GREEN,14,true);Tx("c8_b",cx+CW+G+10,yy+86,"OTC Crowd",NEON_GOLD,9,false);
  3703. color sClr=CGR;string sName=GetCurrentSession(sClr);bool isAct=IsSessionActive();
  3704. Bx("c_ses",cx+2*(CW+G),yy,CW,CH,BG_DARK2,sClr);Tx("c_ses_l",cx+2*(CW+G)+10,yy+4,"SESSION",NEON_CYAN,10,true);Tx("c_ses_v",cx+2*(CW+G)+10,yy+22,sName,sClr,14,true);int gH=TimeHour(TimeGMT()),gMn=TimeMinute(TimeGMT());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);Tx("c_ses_s",cx+2*(CW+G)+10,yy+76,isAct?"ACTIVE":"LOW VOL",isAct?NEON_GREEN:NEON_ORANGE,11,true);yy+=CH+G;
  3705.  
  3706. // ROW 3
  3707. DrawOtherPairsCube(cx,yy,CW,CH);DrawConfluenceCube(cx+CW+G,yy,CW,140);
  3708. Bx("c_brain_r3",cx+2*(CW+G),yy,CW,CH,BG_DARK3,NEON_CYAN);Tx("c_brain_r3_l",cx+2*(CW+G)+10,yy+5,"BRAIN AI",NEON_PURPLE,10,true);color brainClr3=(brain>0)?NEON_GREEN:(brain<0)?NEON_RED:NEON_YELLOW;double brainBar=MathMax(5,MathMin(95,50+brain*5));Tx("c_brain_r3_v",cx+2*(CW+G)+10,yy+25,(brain>=0?"+":"")+DoubleToString(brain,1),brainClr3,24,true);Tx("c_brain_r3_s",cx+2*(CW+G)+10,yy+58,stratLabel,stratC,10,false);string brainDir=(brain>2)?"BULL PRESSURE":(brain<-2)?"BEAR PRESSURE":"NEUTRAL";Tx("c_brain_r3_d",cx+2*(CW+G)+10,yy+76,brainDir,(brain>2)?NEON_GREEN:(brain<-2)?NEON_RED:NEON_YELLOW,9,false);NeonBar("c_brain_r3_b",cx+2*(CW+G)+12,yy+92,CW-24,7,brainBar,brainClr3);yy+=CH+G;
  3709.  
  3710. // ROW 4
  3711. int CH4=120;color fibC;if(g_fibBestDir=="CALL")fibC=NEON_GREEN;else if(g_fibBestDir=="PUT")fibC=NEON_RED;else fibC=NEON_YELLOW;
  3712. Bx("c_fib",cx,yy,CW,CH4,BG_DARK2,fibC);Tx("c_fib_l",cx+10,yy+4,"FIB REJECTION",NEON_PURPLE,10,true);Tx("c_fib_dir",cx+10,yy+22,g_fibBestDir,fibC,16,true);
  3713. string confTxt="CONF: "+IntegerToString(g_fibConfidence)+"%";color confC=(g_fibConfidence>=70)?NEON_GREEN:(g_fibConfidence>=50)?NEON_YELLOW:CGR;Tx("c_fib_conf",cx+10,yy+46,confTxt,confC,11,true);
  3714. color patC=(g_fibPattern=="PIN"||g_fibPattern=="FAKE")?NEON_GOLD:(g_fibPattern=="ENGULF")?NEON_PURPLE:CGR;Tx("c_fib_pat",cx+10,yy+64,"PAT: "+g_fibPattern,patC,10,false);Tx("c_fib_lev",cx+10,yy+82,"FIB: "+g_fibFibLevel,NEON_CYAN,10,false);
  3715. double fibBarPct=MathMin(100,g_fibBestScore);color fibBarC=(g_fibBestDir=="CALL")?NEON_GREEN:(g_fibBestDir=="PUT")?NEON_RED:NEON_YELLOW;NeonBar("c_fib_b",cx+12,yy+104,CW-24,7,fibBarPct,fibBarC);
  3716. color mtsC=(mts>=90)?NEON_PURPLE:(mts>=75)?NEON_RED:(mts>=50)?NEON_ORANGE:NEON_GREEN;Cube("c_mts",cx+CW+G,yy,CW,CH4,mtsC,BG_DARK2,"MICRO TRAP",NEON_CYAN,"SCORE "+IntegerToString(mts),mtsC,14);NeonBar("c_mts_b",cx+CW+G+12,yy+104,CW-24,7,MathMin(100,mts),mtsC);
  3717. int tfaInt=(int)tfaScore;color tfaColor;string tfaLabel;if(tfaInt>=5){tfaLabel="STRONG";tfaColor=NEON_GREEN;}else if(tfaInt>=4){tfaLabel="GOOD";tfaColor=NEON_LIME;}else if(tfaInt>=3){tfaLabel="MEDIUM";tfaColor=NEON_YELLOW;}else{tfaLabel="WEAK";tfaColor=NEON_ORANGE;}
  3718. color tfaBC=tfaColor;string dirTxt="";if(g_haM1=="HA BEARISH 1"){tfaBC=NEON_RED;dirTxt="[DOWN]";}else if(g_haM1=="HA BULLISH 1"){tfaBC=NEON_GREEN;dirTxt="[UP]";}else dirTxt="[MIX]";
  3719. Bx("c_tfa",cx+2*(CW+G),yy,CW,CH4,BG_DARK3,tfaBC);Tx("c_tfa_l",cx+2*(CW+G)+10,yy+4,"TFA SCORE",NEON_CYAN,10,true);Tx("c_tfa_v",cx+2*(CW+G)+10,yy+22,IntegerToString(tfaInt)+"/6 "+tfaLabel,tfaBC,12,true);Tx("c_tfa_dir",cx+2*(CW+G)+10,yy+44,dirTxt,tfaBC,11,true);
  3720. int tfaFW=(int)((double)tfaInt/6.0*(CW-20));if(tfaFW<2)tfaFW=2;if(tfaFW>CW-20)tfaFW=CW-20;Bx("c_tfa_bb",cx+2*(CW+G)+10,yy+104,CW-20,7,BG_DARK4,BG_DARK4);Bx("c_tfa_bf",cx+2*(CW+G)+10,yy+104,tfaFW,7,tfaBC,tfaBC);yy+=CH4+G;
  3721.  
  3722. // ROW 5
  3723. int CH5=140;
  3724. if(ENABLE_MTG){
  3725. bool isTrap=(g_mtgState=="PUT TRAP"||g_mtgState=="CALL TRAP");color mtgC=g_mtgClr;if(g_mtgState=="WAIT"||g_mtgState=="...")mtgC=NEON_YELLOW;color borderC=isTrap?NEON_PURPLE:mtgC;
  3726. Bx("c_mtg",cx,yy,CW,CH5,BG_DARK2,borderC);string runStr="";if(g_mtgBullCount>=2)runStr=" +"+IntegerToString(g_mtgBullCount)+"B";else if(g_mtgBearCount>=2)runStr=" +"+IntegerToString(g_mtgBearCount)+"R";
  3727. Tx("c_mtg_l",cx+10,yy+4,"NCP PRO v8"+runStr,NEON_PURPLE,10,true);Tx("c_mtg_s",cx+10,yy+18,g_mtgState,mtgC,isTrap?11:13,true);
  3728. int cW=(int)(g_mtgHype/100.0*(CW-22));if(cW<2)cW=2;if(cW>CW-22)cW=CW-22;Bx("c_mtg_cbb",cx+10,yy+37,CW-22,6,BG_DARK4,BG_DARK4);Bx("c_mtg_cbf",cx+10,yy+37,cW,6,(g_mtgHype>=g_mtgBetrayal)?NEON_GREEN:C'35,70,35',NEON_GREEN);
  3729. Tx("c_mtg_ct",cx+10,yy+43,"CALL: "+DoubleToString(g_mtgHype,1)+"%",NEON_GREEN,10,true);
  3730. int pW=(int)(g_mtgBetrayal/100.0*(CW-22));if(pW<2)pW=2;if(pW>CW-22)pW=CW-22;Bx("c_mtg_pbb",cx+10,yy+59,CW-22,6,BG_DARK4,BG_DARK4);Bx("c_mtg_pbf",cx+10,yy+59,pW,6,(g_mtgBetrayal>g_mtgHype)?NEON_RED:C'70,25,25',NEON_RED);
  3731. Tx("c_mtg_pt",cx+10,yy+65,"PUT: "+DoubleToString(g_mtgBetrayal,1)+"%",NEON_RED,10,true);
  3732. string actD=g_mtgAction;if(StringLen(actD)>28)actD=StringSubstr(actD,0,27);color actC=isTrap?NEON_PURPLE:(g_mtgState=="CALL")?NEON_GREEN:(g_mtgState=="PUT")?NEON_RED:NEON_YELLOW;
  3733. Tx("c_mtg_a",cx+10,yy+81,actD,actC,10,true);Tx("c_mtg_r",cx+10,yy+97,"ATR: "+DoubleToString(g_mtgRecovery,1)+"p",CGR,9,false);
  3734. } else {
  3735. double frac=CalcAdvancedFractal();color frC=(frac>=75)?NEON_RED:(frac>=50)?NEON_ORANGE:(frac>=25)?NEON_YELLOW:NEON_GREEN;string frT=(frac>=75)?"TRAP ZONE!":(frac>=50)?"WARNING":(frac>=25)?"CAUTION":"CLEAR";Cube("c_frc",cx,yy,CW,CH5,frC,BG_DARK2,"FRACTAL",NEON_CYAN,frT,frC,13);NeonBar("c_frc_b",cx+12,yy+104,CW-24,7,frac,frC);
  3736. }
  3737. Bx("c_bias",cx+CW+G,yy,CW,CH5,BG_DARK2,biasClr);Tx("c_bias_l",cx+CW+G+10,yy+4,"MARKET BIAS",NEON_CYAN,10,true);string bStr=biasStr;if(StringLen(bStr)>13)bStr=StringSubstr(bStr,0,12);Tx("c_bias_v",cx+CW+G+10,yy+22,bStr,biasClr,13,true);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);color accC=(g_accuracy>=70)?NEON_GREEN:(g_accuracy>=60)?NEON_YELLOW:NEON_RED;Tx("c_bias_a",cx+CW+G+10,yy+72,"ACC: "+DoubleToString(g_accuracy,1)+"%",accC,10,false);NeonBar("c_bias_b",cx+CW+G+10,yy+88,CW-20,7,g_accuracy,accC);
  3738. // ===== SMC OB v3.0 DASHBOARD CUBE =====
  3739. {
  3740. double obBullLow=0, obBearHigh=0;
  3741. int obBestBullScore=0, obBestBearScore=0;
  3742. string obBullPrem="", obBearPrem="";
  3743. int obBullRetest=0, obBearRetest=0;
  3744. int activeOBs=0, premiumOBs=0;
  3745.  
  3746. for(int oi=0; oi<g_O3c; oi++){
  3747. if(!g_O3[oi].active || g_O3[oi].mitigated) continue;
  3748. activeOBs++;
  3749. if(g_O3[oi].str >= OB_PremiumScore) premiumOBs++;
  3750. double cur=Close[0];
  3751. if(g_O3[oi].dir==OB3_BULL && g_O3[oi].lo<cur){
  3752. double d=cur-g_O3[oi].lo;
  3753. if(obBullLow==0 || d<cur-obBullLow){
  3754. obBullLow=g_O3[oi].lo;
  3755. obBestBullScore=g_O3[oi].str;
  3756. obBullPrem=(g_O3[oi].str>=OB_PremiumScore)?"[P]":"";
  3757. obBullRetest=g_O3[oi].tests;
  3758. }
  3759. }
  3760. if(g_O3[oi].dir==OB3_BEAR && g_O3[oi].hi>cur){
  3761. double d=g_O3[oi].hi-cur;
  3762. if(obBearHigh==0 || d<obBearHigh-cur){
  3763. obBearHigh=g_O3[oi].hi;
  3764. obBestBearScore=g_O3[oi].str;
  3765. obBearPrem=(g_O3[oi].str>=OB_PremiumScore)?"[P]":"";
  3766. obBearRetest=g_O3[oi].tests;
  3767. }
  3768. }
  3769. }
  3770.  
  3771. color obBorderC = (premiumOBs>0) ? NEON_PURPLE : NEON_CYAN;
  3772. Bx("c18",cx+2*(CW+G),yy,CW,CH5,BG_DARK2,obBorderC);
  3773. Tx("c18_l",cx+2*(CW+G)+10,yy+4,"SMC OB v3.0",NEON_PURPLE,10,true);
  3774.  
  3775. string rTxt="R: "+(obBearHigh>0?ShortPrice(obBearHigh):"--");
  3776. if(obBearHigh>0) rTxt+=" "+obBearPrem+IntegerToString(obBestBearScore)+"% R"+IntegerToString(obBearRetest);
  3777. color rClr=(obBestBearScore>=OB_PremiumScore)?NEON_PURPLE:NEON_RED;
  3778. Tx("c18_r",cx+2*(CW+G)+10,yy+24,rTxt,rClr,11,true);
  3779.  
  3780. string sTxt="S: "+(obBullLow>0?ShortPrice(obBullLow):"--");
  3781. if(obBullLow>0) sTxt+=" "+obBullPrem+IntegerToString(obBestBullScore)+"% R"+IntegerToString(obBullRetest);
  3782. color obSClr=(obBestBullScore>=OB_PremiumScore)?NEON_PURPLE:NEON_GREEN;
  3783. Tx("c18_s",cx+2*(CW+G)+10,yy+50,sTxt,obSClr,11,true);
  3784.  
  3785. Tx("c18_b",cx+2*(CW+G)+10,yy+76,"Z:"+IntegerToString(activeOBs)+" P:"+IntegerToString(premiumOBs),NEON_GOLD,10,true);
  3786.  
  3787. int bestOB=MathMax(obBestBullScore,obBestBearScore);
  3788. NeonBar("c18_bar",cx+2*(CW+G)+12,yy+94,CW-24,6,bestOB,(bestOB>=80)?NEON_PURPLE:(bestOB>=60)?NEON_GREEN:CGR);
  3789. }
  3790. yy+=CH5+G;
  3791.  
  3792. // ROW 6
  3793. int CH6 = 120;
  3794. Bx("c15", cx, yy, CW, CH6, BG_DARK2, g_visualTrapPro.verdictColor);
  3795. Tx("c15_l", cx+10, yy+4, "VISUAL TRAP", NEON_PURPLE, 10, true);
  3796. Tx("c15_box", cx+10, yy+22, g_visualTrapPro.boxStatus, CGR, 10, true);
  3797. Tx("c15_gann", cx+10, yy+38, g_visualTrapPro.gannStatus, g_visualTrapPro.gannColor, 10, true);
  3798. Tx("c15_m30", cx+10, yy+54, g_visualTrapPro.m30Dir, (StringFind(g_visualTrapPro.m30Dir,"UP")>=0)?NEON_GREEN:NEON_RED, 10, true);
  3799. Tx("c15_m1", cx+10, yy+70, g_visualTrapPro.m1Seq, CGR, 9, false);
  3800. Tx("c15_v", cx+10, yy+88, g_visualTrapPro.verdict, g_visualTrapPro.verdictColor, 13, true);
  3801.  
  3802. Bx("c_ha", cx+CW+G, yy, CW, CH6, BG_DARK2, ha1C);
  3803. Tx("c_ha_l", cx+CW+G+10, yy+4, "HA CANDLES", NEON_CYAN, 10, true);
  3804. string haM1txt = g_haM1, haM5txt = g_haM5;
  3805. if(StringLen(haM1txt) > 16) haM1txt = StringSubstr(haM1txt, 3);
  3806. if(StringLen(haM5txt) > 16) haM5txt = StringSubstr(haM5txt, 3);
  3807. Tx("c_ha_m1", cx+CW+G+10, yy+24, "M1: "+haM1txt, ha1C, 11, true);
  3808. Tx("c_ha_m5", cx+CW+G+10, yy+50, "M5: "+haM5txt, ha5C, 11, true);
  3809. string haComb = "";
  3810. color haCombC = NEON_YELLOW;
  3811. if(g_haM1 == "HA BULLISH 1" && g_haM5 == "HA BULLISH 5") {
  3812. haComb = "BOTH BULL"; haCombC = NEON_GREEN;
  3813. } else if(g_haM1 == "HA BEARISH 1" && g_haM5 == "HA BEARISH 5") {
  3814. haComb = "BOTH BEAR"; haCombC = NEON_RED;
  3815. } else haComb = g_haBoth;
  3816. Tx("c_ha_c", cx+CW+G+10, yy+76, haComb, haCombC, 11, true);
  3817.  
  3818. // ----- NCP ANALYSIS CUBE (PROFESSIONAL v2 - NEUTRAL = NEON FLOWER) -----
  3819. int ncpW = CW;
  3820. int ncpH = CH6;
  3821. int ncpX = cx + 2*(CW+G);
  3822. int ncpY = yy;
  3823.  
  3824. string patternName = (g_ncpMainPattern != "") ? g_ncpMainPattern : "NO PATTERN";
  3825. int bullScore = g_ncpPatternBullScore;
  3826. int bearScore = g_ncpPatternBearScore;
  3827. int totalStrength = MathMax(bullScore, bearScore);
  3828. int strengthPct = MathMin(100, (int)(totalStrength * 1.25));
  3829.  
  3830. bool isBullish = (bullScore > bearScore + 5);
  3831. bool isBearish = (bearScore > bullScore + 5);
  3832. bool isNeutral = (!isBullish && !isBearish);
  3833.  
  3834. color borderColor, textColor, barColor;
  3835. string dirSymbol;
  3836.  
  3837. if(isBullish) {
  3838. borderColor = NEON_GREEN;
  3839. textColor = NEON_GREEN;
  3840. barColor = NEON_GREEN;
  3841. dirSymbol = "▲";
  3842. } else if(isBearish) {
  3843. borderColor = NEON_RED;
  3844. textColor = NEON_RED;
  3845. barColor = NEON_RED;
  3846. dirSymbol = "▼";
  3847. } else {
  3848. borderColor = NEON_PINK;
  3849. textColor = NEON_PINK;
  3850. barColor = NEON_PINK;
  3851. dirSymbol = "●";
  3852. }
  3853.  
  3854. Bx("c_reason", ncpX, ncpY, ncpW, ncpH, BG_DARK2, borderColor);
  3855. Tx("c_reason_l", ncpX+10, ncpY+4, "NCP ANALYSIS", NEON_CYAN, 10, true);
  3856.  
  3857. string patternLine = dirSymbol + " " + patternName + " [" + IntegerToString(strengthPct) + "%]";
  3858. Tx("c_reason_p", ncpX+10, ncpY+22, patternLine, textColor, 12, true);
  3859.  
  3860. int barMaxWidth = ncpW - 24;
  3861. int barWidth = (int)(strengthPct / 100.0 * barMaxWidth);
  3862. if(barWidth < 2) barWidth = 2;
  3863. if(barWidth > barMaxWidth) barWidth = barMaxWidth;
  3864.  
  3865. Bx("c_reason_bar_bg", ncpX+10, ncpY+44, barMaxWidth, 8, BG_DARK4, BG_DARK4);
  3866. Bx("c_reason_bar_fill", ncpX+10, ncpY+44, barWidth, 8, barColor, barColor);
  3867.  
  3868. string strengthText = "";
  3869. if(strengthPct >= 70) strengthText = "STRONG";
  3870. else if(strengthPct >= 45) strengthText = "MEDIUM";
  3871. else if(strengthPct >= 20) strengthText = "WEAK";
  3872. else strengthText = "NONE";
  3873. Tx("c_reason_strength", ncpX+10, ncpY+62, "Str: " + strengthText, textColor, 9, false);
  3874.  
  3875. double adx = g_ncpADX;
  3876. double pDI = g_ncpPlusDI;
  3877. double mDI = g_ncpMinusDI;
  3878. string dirStr = (pDI > mDI) ? "DI+:BULL" : "DI-:BEAR";
  3879. color adxColor = GetADXColor(adx, pDI, mDI);
  3880. Tx("c_reason_r", ncpX+10, ncpY+82, "ADX:" + DoubleToString(adx,0) + " " + dirStr, adxColor, 11, false);
  3881.  
  3882. string revReason = DetectSuddenReversal();
  3883. color revColor = CGR;
  3884. if(StringFind(revReason, "BULL") >= 0) revColor = NEON_GREEN;
  3885. if(StringFind(revReason, "BEAR") >= 0) revColor = NEON_RED;
  3886. Tx("c_reason_v", ncpX+10, ncpY+102, (StringLen(revReason) > 0 ? "REV: "+revReason : "NO REVERSAL"), revColor, 10, false);
  3887.  
  3888. yy += ncpH + G;
  3889.  
  3890. // BB Row
  3891. if(ENABLE_BB_PULLBACK) {
  3892. int bbRowH = 52;
  3893. color bbRowBorder = (g_bbSignal == "CALL") ? NEON_GREEN : (g_bbSignal == "PUT") ? NEON_RED : BB_GREY;
  3894. Bx("c_bb_row", cx, yy, W-2*G, bbRowH, BG_DARK3, bbRowBorder);
  3895.  
  3896. string ha30Str = IsM30HABull() ? "BULL" : (IsM30HABear() ? "BEAR" : "DOJI");
  3897. color ha30C2 = IsM30HABull() ? NEON_GREEN : (IsM30HABear() ? NEON_RED : NEON_YELLOW);
  3898. double adxD = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  3899. double rsiD = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  3900.  
  3901. Tx("c_bb_row_l", cx+10, yy+4, "BB("+IntegerToString(BB_PERIOD)+") PULLBACK", BB_GREY, 9, true);
  3902. Tx("c_bb_ha2", cx+190, yy+4, "HA30:"+ha30Str, ha30C2, 9, true);
  3903. Tx("c_bb_adx", cx+310, yy+4, "ADX:"+DoubleToString(adxD,0), (adxD>=20) ? NEON_LIME : CGR, 9, false);
  3904. Tx("c_bb_rsi", cx+390, yy+4, "RSI:"+DoubleToString(rsiD,0), (rsiD>=70 || rsiD<=30) ? NEON_RED : CGR, 9, false);
  3905.  
  3906. if(g_bbSignal != "--") {
  3907. string sigTxt = "BB "+g_bbSignal+" | "+g_bbDetail;
  3908. if(StringLen(sigTxt) > 60) sigTxt = StringSubstr(sigTxt, 0, 58) + "..";
  3909. Tx("c_bb_sig2", cx+10, yy+22, sigTxt, g_bbColor, 10, true);
  3910. } else {
  3911. Tx("c_bb_sig2", cx+10, yy+22, "Waiting for BB touch + 2+ confirmations...", CGR, 9, false);
  3912. }
  3913. yy += bbRowH + G;
  3914. }
  3915.  
  3916.  
  3917.  
  3918.  
  3919.  
  3920. // BOTTOM FILTER STATUS
  3921. int fsW=W-2*G;color fsC=(g_filterStatus=="ALL CLEAR")?NEON_GREEN:(StringFind(g_filterStatus,"ACTIVE")>=0)?NEON_LIME:(StringFind(g_filterStatus,"LOCK")>=0)?NEON_RED:NEON_ORANGE;
  3922. Bx("c_filter",cx,yy,fsW,22,BG_DARK3,fsC);Tx("c_filter_v",cx+10,yy+4,"FILTER: "+g_filterStatus,fsC,11,true);
  3923. ChartRedraw();
  3924. }
  3925.  
  3926. double CalcAdvancedFractal(){if(Bars<15)return 0;double score=0;for(int i=2;i<=8&&i+2<Bars;i++){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;}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;}}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++;}if(dj>=3)score+=25;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;}return MathMin(100,score);}
  3927.  
  3928. void UpdateRiskControl(){static datetime llt=0;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";}}}
  3929.  
  3930. void FinalSignalEngine(double brain,int cp,int mts){
  3931. g_filterStatus="ALL CLEAR";
  3932. 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;}
  3933. 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;}
  3934. 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;}
  3935. 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;}
  3936. 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;}}
  3937. double conf=MathMax(g_calc_gProb,g_calc_rProb);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;}
  3938. bool reverseSignal=(g_accuracy<45.0&&g_acc_total>20);if(reverseSignal)g_filterStatus="REVERSE MODE";
  3939. string ms=DetectMyStrategy();
  3940. if(ms!="WAIT"){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;}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;}if(!EntryPrecisionOK()){g_finalSignal="WAIT";g_finalColor=NEON_ORANGE;g_filterStatus="PRECISION FAIL";if(TimeCurrent()-g_signalTime>HOLD_SEC)g_signalTime=TimeCurrent();return;}if(reverseSignal){if(ms=="CALL")ms="PUT";else if(ms=="PUT")ms="CALL";}g_finalSignal=ms;g_finalColor=(ms=="CALL")?NEON_GREEN:NEON_RED;g_filterStatus=(g_strategyType=="TRAP")?"TRAP ACTIVE":"TREND ACTIVE";if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT"){g_finalSignal=ms;g_signalTime=TimeCurrent();}SendSignalNotification(ms,conf);return;}
  3941. double th=FAST_MODE?70:68;string rawSignal="WAIT";if(g_calc_gProb>th)rawSignal="CALL";else if(g_calc_rProb>th)rawSignal="PUT";else if(g_calc_gProb>60)rawSignal="WEAK CALL";else if(g_calc_rProb>60)rawSignal="WEAK PUT";
  3942. 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";}
  3943. g_finalSignal=rawSignal;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;}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;}
  3944. if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT")g_signalTime=TimeCurrent();
  3945. if(g_finalSignal=="CALL"){g_finalColor=NEON_GREEN;g_filterStatus="GREEN SIGNAL";SendSignalNotification("CALL",conf);}else if(g_finalSignal=="PUT"){g_finalColor=NEON_RED;g_filterStatus="RED SIGNAL";SendSignalNotification("PUT",conf);}else if(g_finalSignal=="WEAK CALL"){g_finalColor=NEON_CYAN;g_filterStatus="WEAK GREEN";}else if(g_finalSignal=="WEAK PUT"){g_finalColor=NEON_ORANGE;g_filterStatus="WEAK RED";}else g_finalColor=NEON_YELLOW;
  3946. }
  3947.  
  3948. void SendSignalNotification(string sig,double conf){if(!ENABLE_NOTIFY)return;string msg=Symbol()+" | "+sig+" | "+DoubleToString(conf,0)+"% | "+g_filterStatus;if(msg==g_lastNotified)return;g_lastNotified=msg;if(ENABLE_TELEGRAM)SendTelegramAlert("AIBRAIN\n"+Symbol()+"\n"+sig+"\nConf: "+DoubleToString(conf,0)+"%\n"+g_filterStatus);Print(msg);}
  3949.  
  3950. void BroadcastSignal(){
  3951. string fn="OTC_Signals_Master.txt",pair=Symbol();
  3952. int exp=(int)(TimeCurrent()+65);int now=(int)TimeCurrent();
  3953. string content="";
  3954.  
  3955. int h=FileOpen(fn,FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);
  3956. if(h==INVALID_HANDLE){h=FileOpen(fn,FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);if(h==INVALID_HANDLE)return;}
  3957. while(!FileIsEnding(h)){string ex=FileReadString(h);if(StringLen(ex)>10&&StringFind(ex,pair+"|")!=0)content+=ex+"\n";}
  3958.  
  3959. // 1. Normal Pattern Signal
  3960. double conf1=MathMax(g_frozen_gProb,g_frozen_rProb);
  3961. if(conf1>=55){
  3962. string sig1=(g_frozen_gProb>g_frozen_rProb)?"CALL":"PUT";
  3963. content=pair+"|"+sig1+"|"+DoubleToString(conf1,0)+"|"+IntegerToString(now)+"|"+IntegerToString(exp)+"\n"+content;
  3964. }
  3965.  
  3966. // 2. PREMIUM OB SIGNAL (NEW OB3 Engine)
  3967. for(int obi=0; obi<g_O3c; obi++){
  3968. if(!g_O3[obi].active || g_O3[obi].mitigated) continue;
  3969. if(g_O3[obi].str < OB_PremiumScore) continue;
  3970. if(g_O3[obi].tests > 0) continue;
  3971. string obSig = (g_O3[obi].dir==OB3_BULL) ? "PREM_OB_BULL" : "PREM_OB_BEAR";
  3972. double obConf = g_O3[obi].str;
  3973. content=pair+"|"+obSig+"|"+DoubleToString(obConf,0)+"|"+IntegerToString(now)+"|"+IntegerToString(exp)+"\n"+content;
  3974. break;
  3975. }
  3976.  
  3977. // 3. MICRO OB SIGNAL (NEW OB3 Engine)
  3978. for(int mbi=0; mbi<g_M3c; mbi++){
  3979. if(!g_M3[mbi].alive) continue;
  3980. string mSig = (g_M3[mbi].dir==OB3_BULL) ? "MICRO_CALL" : "MICRO_PUT";
  3981. mSig += "_" + IntegerToString(g_M3[mbi].tf) + "s";
  3982. double mConf = 97.0;
  3983. content=pair+"|"+mSig+"|"+DoubleToString(mConf,0)+"|"+IntegerToString(now)+"|"+IntegerToString(exp)+"\n"+content;
  3984. break;
  3985. }
  3986.  
  3987. // 4. NEXT CANDLE PREDICTOR SIGNAL
  3988. if(g_otpSignal != "WAIT" && g_otpSignal != "" && g_otpConf >= 60){
  3989. string sig2 = (StringFind(g_otpSignal, "PUT") >= 0) ? "OTP_PUT" : "OTP_CALL";
  3990. content=pair+"|"+sig2+"|"+DoubleToString(g_otpConf,0)+"|"+IntegerToString(now)+"|"+IntegerToString(exp)+"\n"+content;
  3991. }
  3992.  
  3993. FileSeek(h,0,SEEK_SET);FileWriteString(h,content);FileFlush(h);FileClose(h);
  3994. }
  3995.  
  3996. int CalcPairQuality(string pair,string &outSig,string &outDetail){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);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);double o3=iOpen(pair,PERIOD_M1,3),c3=iClose(pair,PERIOD_M1,3);if(o1==0||c1==0||o2==0||c2==0||o3==0||c3==0){outSig="WAIT";return 0;}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;bool haM1Bull=(haC1>haO1);double haBody=MathAbs(haC1-haO1),haRange=(h1-l1);double haStrength=(haRange>0)?haBody/haRange:0;double m5_o1=iOpen(pair,PERIOD_M5,1),m5_h1=iHigh(pair,PERIOD_M5,1),m5_l1=iLow(pair,PERIOD_M5,1),m5_c1=iClose(pair,PERIOD_M5,1);double m5_o2=iOpen(pair,PERIOD_M5,2),m5_h2=iHigh(pair,PERIOD_M5,2),m5_l2=iLow(pair,PERIOD_M5,2),m5_c2=iClose(pair,PERIOD_M5,2);double m5_o3=iOpen(pair,PERIOD_M5,3),m5_c3=iClose(pair,PERIOD_M5,3);bool haM5Bull=false;if(m5_o1>0&&m5_c1>0&&m5_o2>0&&m5_c2>0&&m5_o3>0&&m5_c3>0){double m5haC1=(m5_o1+m5_h1+m5_l1+m5_c1)/4.0,m5haC2=(m5_o2+m5_h2+m5_l2+m5_c2)/4.0,m5haO2=(m5_o3+m5_c3)/2.0,m5haO1=(m5haO2+m5haC2)/2.0;haM5Bull=(m5haC1>m5haO1);}double rsi=iRSI(pair,PERIOD_M1,14,PRICE_CLOSE,1);double vol1=(double)iVolume(pair,PERIOD_M1,1),vol2=(double)iVolume(pair,PERIOD_M1,2);double volRatio=(vol2>0)?vol1/vol2:1.0;double spread=(double)MarketInfo(pair,MODE_SPREAD);int bullStreak=0,bearStreak=0;for(int i=1;i<=4;i++){double ci=iClose(pair,PERIOD_M1,i),oi=iOpen(pair,PERIOD_M1,i);if(ci==0||oi==0)break;if(ci>oi){if(bearStreak>0)break;bullStreak++;}else if(ci<oi){if(bullStreak>0)break;bearStreak++;}else break;}bool bullDir=false,bearDir=false;if(haM1Bull&&haM5Bull)bullDir=true;else if(!haM1Bull&&!haM5Bull)bearDir=true;else{outSig="WAIT";return 0;}int score=0;string det="";if(bullDir&&haM1Bull){int s=(haStrength>0.6)?25:(haStrength>0.4)?18:12;score+=s;det+="HA1+";}if(bearDir&&!haM1Bull){int s=(haStrength>0.6)?25:(haStrength>0.4)?18:12;score+=s;det+="HA1+";}if(bullDir&&haM5Bull){score+=15;det+="HA5+";}else if(bearDir&&!haM5Bull){score+=15;det+="HA5+";}if(bullDir){if(rsi<30){score+=20;det+="RSI_OS ";}else if(rsi<40){score+=14;det+="RSI_low ";}else{score+=2;det+="RSI_hi ";}}else{if(rsi>70){score+=20;det+="RSI_OB ";}else if(rsi>60){score+=14;det+="RSI_hi ";}else{score+=2;det+="RSI_lo ";}}if(volRatio>=2.0){score+=15;det+="VOL_HI ";}else if(volRatio>=1.5){score+=10;det+="VOL_MD ";}int streak=(bullDir)?bullStreak:bearStreak;if(streak>=3){score+=15;det+="STK"+IntegerToString(streak);}else if(streak>=2){score+=10;det+="STK"+IntegerToString(streak);}if(spread<=3)score+=10;else if(spread<=6)score+=6;outSig=bullDir?"CALL":"PUT";outDetail=det;return MathMin(100,score);}
  3997.  
  3998. // FIX: Corrected undeclared 'cur' variable
  3999. void ScanSignals(){
  4000. g_pairCount=0;
  4001. string fn="OTC_Signals_Master.txt";
  4002. string cur=Symbol(); // FIX: Added string type
  4003. int h=FileOpen(fn,FILE_READ|FILE_TXT|FILE_SHARE_READ);
  4004. if(h==INVALID_HANDLE)return;
  4005. datetime now=TimeCurrent();
  4006. string tmpNames[8];string tmpSigs[8];double tmpConfs[8];int tmpScores[8];string tmpDetails[8];int tmpCount=0;
  4007. while(!FileIsEnding(h)&&tmpCount<8){
  4008. string ln=FileReadString(h);if(StringLen(ln)<15)continue;
  4009. int p1=StringFind(ln,"|"),p2=StringFind(ln,"|",p1+1),p3=StringFind(ln,"|",p2+1),p4=StringFind(ln,"|",p3+1);
  4010. if(p1<0||p2<0||p3<0||p4<0)continue;
  4011. string pair=StringSubstr(ln,0,p1);if(pair==cur)continue;
  4012. string sig=StringSubstr(ln,p1+1,p2-p1-1);
  4013. double conf=StringToDouble(StringSubstr(ln,p2+1,p3-p2-1));
  4014. int expTime=(int)StringToInteger(StringSubstr(ln,p4+1));
  4015. if(expTime<(int)(now-20)) continue;
  4016.  
  4017.  
  4018.  
  4019. // --- SIGNAL TYPE DETECTION (OTP + PREM OB + MICRO OB) ---
  4020. bool isOtp = (StringFind(sig, "OTP_") == 0);
  4021. bool isPremOB = (StringFind(sig, "PREM_OB_") == 0);
  4022. bool isMicroOB = (StringFind(sig, "MICRO_") == 0);
  4023. string pureSig = sig;
  4024.  
  4025. if(isOtp) {
  4026. pureSig = StringSubstr(sig, 4);
  4027. if(conf < 60) continue;
  4028. } else if(isPremOB) {
  4029. pureSig = (StringFind(sig, "BULL") >= 0) ? "CALL" : "PUT";
  4030. if(conf < 70) continue;
  4031. } else if(isMicroOB) {
  4032. pureSig = (StringFind(sig, "CALL") >= 0) ? "CALL" : "PUT";
  4033. if(conf < 70) continue;
  4034. } else {
  4035. if(conf < 60) continue;
  4036. }
  4037.  
  4038. string qSig="WAIT",qDetail="";int qScore=CalcPairQuality(pair,qSig,qDetail);
  4039.  
  4040. if(isOtp) {
  4041. qScore = (int)conf;
  4042. sig = pureSig;
  4043. } else if(isPremOB) {
  4044. qScore = (int)conf;
  4045. sig = pureSig;
  4046. } else if(isMicroOB) {
  4047. qScore = 97; // Force 97 so DrawOtherPairsCube shows NEON GOLD
  4048. sig = pureSig;
  4049. } else {
  4050. if(qSig!=pureSig) continue;
  4051. if(qScore<45) continue;
  4052. }
  4053.  
  4054. tmpNames[tmpCount]=pair;tmpSigs[tmpCount]=sig;tmpConfs[tmpCount]=conf;tmpScores[tmpCount]=qScore;tmpDetails[tmpCount]=qDetail;tmpCount++;
  4055. }
  4056. FileClose(h);
  4057. for(int i=0;i<tmpCount-1;i++){for(int j=0;j<tmpCount-i-1;j++){if(tmpScores[j]<tmpScores[j+1]){string ts=tmpNames[j];tmpNames[j]=tmpNames[j+1];tmpNames[j+1]=ts;ts=tmpSigs[j];tmpSigs[j]=tmpSigs[j+1];tmpSigs[j+1]=ts;ts=tmpDetails[j];tmpDetails[j]=tmpDetails[j+1];tmpDetails[j+1]=ts;double td=tmpConfs[j];tmpConfs[j]=tmpConfs[j+1];tmpConfs[j+1]=td;int ti=tmpScores[j];tmpScores[j]=tmpScores[j+1];tmpScores[j+1]=ti;}}}
  4058. g_pairCount=MathMin(5,tmpCount);for(int i=0;i<g_pairCount;i++){g_pairNames[i]=tmpNames[i];g_pairSigs[i]=tmpSigs[i];g_pairConfs[i]=tmpScores[i];}
  4059. string curPred=(g_frozen_gProb>g_frozen_rProb)?"CALL":"PUT";int sameDir=0;double boostTotal=0;
  4060. for(int i=0;i<g_pairCount;i++){if(g_pairSigs[i]==curPred&&tmpScores[i]>=60){sameDir++;boostTotal+=tmpScores[i];}}
  4061. if(sameDir>=2&&!g_tradingStopped){double boost=MathMin(12.0,boostTotal/50.0);if(curPred=="CALL"){g_calc_gProb+=boost;g_calc_rProb-=boost;}else{g_calc_rProb+=boost;g_calc_gProb-=boost;}g_calc_gProb=MathMax(8.0,MathMin(92.0,g_calc_gProb));g_calc_rProb=100.0-g_calc_gProb;if(StringFind(g_filterStatus,"BOOST")<0)g_filterStatus="PAIR BOOST+"+DoubleToString(boost,0)+"%";}
  4062. }
  4063.  
  4064. void UpdateTimer()
  4065. {
  4066. if(!SHOW_TIMER) return;
  4067. string nm = PFX + "candle_timer";
  4068. int ps = 60;
  4069.  
  4070. static datetime lastBarTime = 0;
  4071. static datetime barStartLocal = 0;
  4072.  
  4073. if(Time[0] != lastBarTime)
  4074. {
  4075. lastBarTime = Time[0];
  4076. barStartLocal = TimeLocal(); // PC time se sync karo
  4077. }
  4078.  
  4079. int elapsed = (int)(TimeLocal() - barStartLocal);
  4080. int rem = ps - elapsed;
  4081. if(rem < 0) rem = 0;
  4082. if(rem > ps) rem = ps;
  4083.  
  4084. int mm = rem / 60;
  4085. int ss = rem % 60;
  4086. string t = (mm > 0 ? IntegerToString(mm) + ":" : "") + (ss < 10 ? "0" : "") + IntegerToString(ss) + "s";
  4087.  
  4088. double atrVal = iATR(NULL, PERIOD_M1, 14, 0);
  4089. double offset = (atrVal > 0) ? atrVal * 0.25 : Point * 30;
  4090. datetime tPos = Time[0] + Period() * 60 * 2;
  4091. double pPos = High[0] + offset;
  4092.  
  4093. if(ObjectFind(0, nm) < 0)
  4094. {
  4095. ObjectCreate(0, nm, OBJ_TEXT, 0, tPos, pPos);
  4096. ObjectSetInteger(0, nm, OBJPROP_BACK, false);
  4097. }
  4098. else
  4099. {
  4100. ObjectMove(0, nm, 0, tPos, pPos);
  4101. }
  4102.  
  4103. ObjectSetText(nm, t, 11, "Arial Bold", NEON_YELLOW);
  4104. }
  4105.  
  4106. // ============================================================
  4107. // EVENT HANDLERS
  4108. // ============================================================
  4109. int OnInit(){
  4110. InitNW();NuclearDeleteAll();
  4111. g_hrn_price=0;g_hrn_brk_bars=0;g_hrn_scan_bar=0;g_hrn_score=0;g_hrn_confirmed_break=false;
  4112. g_nearest_res=0;g_nearest_sup=0;g_rj_cnt=0;g_pairCount=0;
  4113. g_marketMode="RANGE";g_prevMode="";g_last_freeze_bar=0;g_lastNotified="";
  4114. g_accuracy=ValidateHistoricalAccuracy();g_acc_correct=0;g_acc_total=0;g_acc_lastBar=0;
  4115. g_lastPredGreen=50.0;g_lastPredBar=0;g_lossStreak=0;g_tradingStopped=false;
  4116. g_strategyType="NONE";g_frozen_gProb=50;g_frozen_rProb=50;g_calc_gProb=50;g_calc_rProb=50;
  4117. g_commonCount=0;g_wickLineCount=0;g_tfa_detail="";g_htfLevelCount=0;g_mtfConfirmed=true;
  4118. g_filterStatus="ALL CLEAR";g_symbolKey="";g_brokerBias=0.0;
  4119. g_bbSignal="--";g_bbColor=NEON_YELLOW;g_bbTouchPrice=0.0;g_bbSignalTime=0;g_bbSignalBars=0;g_bbDetail="";g_bbLineCount=0;
  4120. g_mtgHype=50;g_mtgBetrayal=50;g_mtgRecovery=0;g_mtgState="INIT";g_mtgReason="";g_mtgAction="";g_mtgClr=CGR;g_mtg_lastBar=0;g_mtgPattern="";g_mtgBullCount=0;g_mtgBearCount=0;g_trapScore=0;g_ncpADX=0;g_ncpPlusDI=0;g_ncpMinusDI=0;
  4121. g_smoothCallScore=50.0;g_smoothPutScore=50.0;g_callWeight=1.0;g_putWeight=1.0;
  4122. g_totalTrades=0;g_callTrades=0;g_putTrades=0;g_callWins=0;g_putWins=0;
  4123. g_ncpLastSignalTime=0;g_ncpLastSignalType="";g_ncpLastEntryPrice=0.0;g_ncpSignalProcessed=true;
  4124. g_ncpTrend.m15Direction="FLAT";g_ncpTrend.m15Strength=0;g_ncpTrend.aligned=false;
  4125. g_ncpSRCount=0;g_ncpDetailLine1="";g_ncpDetailLine2="";g_ncpDetailLine3="";
  4126. g_ncpPatternBullScore=0;g_ncpPatternBearScore=0;g_ncpMainPattern="";
  4127. g_stableResCount=0;g_stableSupCount=0;g_lastSRScan=0;
  4128. g_ha30_open=0;g_ha30_close=0;g_ha30_high=0;g_ha30_low=0;g_ha30_bar=0;
  4129. g_rsc=0; g_psycheScore=0; g_psycheDir="NONE"; g_psycheSignal="SCANNING"; g_psycheClr=CGR;
  4130. OB3_Init();
  4131. g_otcFibPrice = 0; g_otcFibDir = "WAIT"; g_otcFibStrength = 0;
  4132. g_otcFibPattern = "--"; g_otcFibLevel = "--"; g_otcFibSignalTime = 0;
  4133. g_otcHasSignal = false; DeleteOTCFibLine();
  4134. QuantumLoadLearning();
  4135. if(ENABLE_LEARNING)LoadBrainMemory();LoadMLWeights();
  4136.  
  4137. // ✅ FIX 1: Test Label (Agar ye dikha toh indicator load ho raha hai)
  4138. ObjectCreate(0, PFX+"TEST_LABEL", OBJ_LABEL, 0, 0, 0);
  4139. ObjectSetInteger(0, PFX+"TEST_LABEL", OBJPROP_XDISTANCE, 50);
  4140. ObjectSetInteger(0, PFX+"TEST_LABEL", OBJPROP_YDISTANCE, 50);
  4141. ObjectSetText(PFX+"TEST_LABEL", "AIBRAIN v46.3 LOADED!", 20, "Arial Bold", clrLime);
  4142. ObjectSetInteger(0, PFX+"TEST_LABEL", OBJPROP_CORNER, 0);
  4143.  
  4144. // ✅ FIX 2: Reliable Timer
  4145. EventSetMillisecondTimer(1000);
  4146.  
  4147. Print("AIBRAIN v46.3 INIT COMPLETE");
  4148. return INIT_SUCCEEDED;
  4149. }
  4150.  
  4151.  
  4152.  
  4153.  
  4154. string CheckPairOB(string pair) {
  4155. int bars = iBars(pair, PERIOD_M1); if(bars < 5) return "";
  4156. for(int i = 1; i <= 3; i++) {
  4157. double o1=iOpen(pair,PERIOD_M1,i+1), c1=iClose(pair,PERIOD_M1,i+1);
  4158. double o2=iOpen(pair,PERIOD_M1,i), c2=iClose(pair,PERIOD_M1,i);
  4159. if(o1==0||c1==0||o2==0||c2==0) continue;
  4160. if(c1<o1 && c2>o2 && MathAbs(c2-o2)>MathAbs(c1-o1)) return "BUY OB";
  4161. if(c1>o1 && c2<o2 && MathAbs(c2-o2)>MathAbs(c1-o1)) return "SELL OB";
  4162. }
  4163. return "";
  4164. }
  4165.  
  4166. void ScanBackgroundOBs() {
  4167. g_bgOB_Count = 0;
  4168. for(int i = 0; i < g_pairCount && i < 8; i++) {
  4169. string pair = g_pairNames[i];
  4170. if(pair == Symbol()) continue;
  4171. string obSig = CheckPairOB(pair);
  4172. if(obSig != "") {
  4173. g_bgOB_Pair[g_bgOB_Count] = pair;
  4174. g_bgOB_Signal[g_bgOB_Count] = obSig;
  4175. g_bgOB_Count++;
  4176. }
  4177. }
  4178. }
  4179.  
  4180. // ============================================================
  4181. // BACKGROUND PAIRS NEXT CANDLE CHECKER
  4182. // ============================================================
  4183. void CheckBackgroundNextCandleSignals(){
  4184. g_bgOtpCount = 0;
  4185. if(g_pairCount == 0) return;
  4186.  
  4187. for(int i=0; i<g_pairCount && i<8; i++){
  4188. string pair = g_pairNames[i];
  4189. if(pair == Symbol()) continue;
  4190.  
  4191. // ✅ FIX: PERIOD_M1 parameter add kiya sabhi functions mein
  4192. double o1 = iOpen(pair, PERIOD_M1, 1);
  4193. double c1 = iClose(pair, PERIOD_M1, 1);
  4194. double h1 = iHigh(pair, PERIOD_M1, 1);
  4195. double l1 = iLow(pair, PERIOD_M1, 1);
  4196.  
  4197. double o2 = iOpen(pair, PERIOD_M1, 2);
  4198. double c2 = iClose(pair, PERIOD_M1, 2);
  4199. double o3 = iOpen(pair, PERIOD_M1, 3);
  4200. double c3 = iClose(pair, PERIOD_M1, 3);
  4201.  
  4202. if(o1==0 || c1==0 || o2==0 || c3==0) continue;
  4203.  
  4204. // ✅ FIX: iRSI mein bhi PERIOD_M1 confirm
  4205. double rsi = iRSI(pair, PERIOD_M1, 14, PRICE_CLOSE, 1);
  4206. int score = 0;
  4207.  
  4208. double rng = h1 - l1;
  4209. if(rng > 0){
  4210. double uw = (h1 - MathMax(o1, c1)) / rng;
  4211. double lw = (MathMin(o1, c1) - l1) / rng;
  4212. if(uw > 0.65) score -= 15;
  4213. if(lw > 0.65) score += 15;
  4214. }
  4215.  
  4216. double body = MathAbs(c1 - o1), pBody = MathAbs(c2 - o2);
  4217. bool bull = (c1 > o1), bear = (c1 < o1);
  4218.  
  4219. if(bull && pBody < body && c1 >= o2 && o1 <= c2) score += 25;
  4220. if(bear && pBody < body && c1 <= o2 && o1 >= c2) score -= 25;
  4221.  
  4222. if(rng > 0 && (MathMin(o1,c1)-l1)/rng > 0.60 && body < rng*0.30 && bear) score += 20;
  4223. if(rng > 0 && (h1-MathMax(o1,c1))/rng > 0.60 && body < rng*0.30 && bull) score -= 20;
  4224.  
  4225. if(rsi < 30) score += 20; else if(rsi < 40) score += 10;
  4226. else if(rsi > 70) score -= 20; else if(rsi > 60) score -= 10;
  4227.  
  4228. if(c1>o1 && c2>o2 && c3>o3) score -= 10;
  4229. if(c1<o1 && c2<o2 && c3<o3) score += 10;
  4230.  
  4231. // ✅ FIXED LOGIC (MathAbs use kiya)
  4232. double absScore = MathAbs(score);
  4233. double conf = MathMax(5.0, MathMin(95.0, 50.0 + absScore));
  4234. string sig = "WAIT";
  4235.  
  4236. if(conf >= 72) sig = (score > 0) ? "CALL" : "PUT";
  4237. else if(conf >= 60) sig = (score > 0) ? "WEAK CALL" : "WEAK PUT";
  4238.  
  4239. if(sig != "WAIT"){
  4240. string sp = pair;
  4241. if(StringLen(sp) > 9) sp = StringSubstr(sp, 0, 9);
  4242. g_bgOtpSignal[g_bgOtpCount] = "* " + sp + " " + sig + " " + DoubleToString((int)conf, 0) + "%";
  4243. g_bgOtpCount++;
  4244. }
  4245. }
  4246. }
  4247.  
  4248.  
  4249.  
  4250. // ============================================================
  4251. // CHART CLEANER & DYNAMIC S/R SHIFT ENGINE
  4252. // ============================================================
  4253. void CleanChartAndShiftSR(){
  4254. // 1. REMOVE BLUE ZONE LINES (CALL ZONE / PUT ZONE)
  4255. for(int i = ObjectsTotal() - 1; i >= 0; i--){
  4256. string nm = ObjectName(i);
  4257. if(StringFind(nm, "CALL ZONE") >= 0 || StringFind(nm, "PUT ZONE") >= 0){
  4258. ObjectDelete(0, nm);
  4259. }
  4260. }
  4261.  
  4262. // 2. HIDE OLD S/R LINES (Keep only current top 2, but don't delete for background math)
  4263. int hiddenCount = 0;
  4264. for(int i = ObjectsTotal() - 1; i >= 0; i--){
  4265. string nm = ObjectName(i);
  4266. // Jo lines PFX (AIB9_) se start nahi hoti, unhe hide karo
  4267. if(StringFind(nm, PFX) < 0){
  4268. int objType = (int)ObjectGetInteger(0, nm, OBJPROP_TYPE);
  4269. if(objType == OBJ_HLINE){
  4270. if(hiddenCount > 4){ // Purani lines hide kar do
  4271. ObjectSetInteger(0, nm, OBJPROP_COLOR, clrNONE);
  4272. }
  4273. hiddenCount++;
  4274. }
  4275. }
  4276. }
  4277.  
  4278. // 3. DYNAMIC 1-MINUTE S/R SHIFT LOGIC (Ek Line Upar Neeche)
  4279. if(Bars < 10) return;
  4280.  
  4281. // Check all non-indicator HLines for closest match
  4282. double closestSR = 0;
  4283. bool isRes = false;
  4284. double minDist = 999999;
  4285.  
  4286. for(int i = ObjectsTotal() - 1; i >= 0; i--){
  4287. string nm = ObjectName(i);
  4288. if(StringFind(nm, PFX) >= 0) continue; // Apne indicator ke lines chhod do
  4289.  
  4290. int objType = (int)ObjectGetInteger(0, nm, OBJPROP_TYPE);
  4291. if(objType == OBJ_HLINE){
  4292. double linePrice = ObjectGetDouble(0, nm, OBJPROP_PRICE);
  4293. if(linePrice <= 0) continue;
  4294.  
  4295. bool jpy = (StringFind(Symbol(), "JPY") >= 0);
  4296. double pip = jpy ? 0.01 : 0.0001;
  4297. double dist = MathAbs(Close[0] - linePrice) / pip;
  4298.  
  4299. // Sabse paas wali line dhundho (15 pips ke andar)
  4300. if(dist < minDist && dist < 15){
  4301. minDist = dist;
  4302. closestSR = linePrice;
  4303. isRes = (linePrice > Close[0]); // Agar upar hai toh Resistance
  4304. }
  4305. }
  4306. }
  4307.  
  4308. // Agar koi line mile toh usko shift karo (Sirf ek line ko target banao)
  4309. if(closestSR > 0){
  4310. string shiftLineName = "DYN_SHIFT_SR";
  4311. SafeDel(shiftLineName);
  4312.  
  4313. // Agar candle Green hai toh line ko Resistance maan lo
  4314. // Agar candle Red hai toh line ko Support maan lo
  4315. bool treatAsRes = (Close[0] > Open[0]) ? true : isRes;
  4316.  
  4317. color shiftColor = treatAsRes ? C'255,80,80' : C'80,255,80'; // Faint Red/Green
  4318. ObjectCreate(0, shiftLineName, OBJ_HLINE, 0, 0, closestSR);
  4319. ObjectSetInteger(0, shiftLineName, OBJPROP_COLOR, shiftColor);
  4320. ObjectSetInteger(0, shiftLineName, OBJPROP_WIDTH, 2);
  4321. ObjectSetInteger(0, shiftLineName, OBJPROP_STYLE, STYLE_SOLID);
  4322. ObjectSetInteger(0, shiftLineName, OBJPROP_BACK, false);
  4323.  
  4324. // Label lagao (Premium Style - Bada aur Chamakdaar)
  4325. string lblName = "DYN_SHIFT_SR_LBL";
  4326. SafeDel(lblName);
  4327. string lblText = treatAsRes ? "RES" : "SUP";
  4328. if(Bars > 3){
  4329. ObjectCreate(0, lblName, OBJ_TEXT, 0, Time[3], closestSR);
  4330. color dynLabelColor = C'255,50,255'; // Neon Magenta (Aankhon mein padne wala chamak!)
  4331. ObjectSetText(lblName, lblText, 11, "Arial Bold", dynLabelColor);
  4332. ObjectSetInteger(0, lblName, OBJPROP_BACK, false);
  4333. }
  4334. }
  4335. }
  4336.  
  4337.  
  4338.  
  4339.  
  4340. void OnDeinit(const int r){EventKillTimer();Comment("");for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX)==0)ObjectDelete(nm);}}
  4341.  
  4342.  
  4343.  
  4344. void OnTimer(){
  4345. if(Bars<30)return; // ✅ FIX: 100 se 30 kiya, kam candles pe bhi chalega
  4346. ChartSetInteger(0, CHART_FOREGROUND, false); // FIX: Dashboard upar rahega
  4347. OB3_OnTick(); // Sub-candle builder
  4348. OB3_MitCheckLive(); // REAL-TIME: Candle break = turant delete
  4349. OB3_RunEngine(); // M1 OB Scanner v3.0
  4350. OB3_MicroScan(); // Micro OB Scanner
  4351. OB3_MicroDraw(); // Micro OB Draw + Alerts
  4352. CalcVisualTrapPro();CalcHA();UpdateHRN();UpdateStableM5SR();DetectCommonPoints();
  4353. DrawCommonPoints();
  4354. if(ENABLE_BB_PULLBACK)DetectBBPullback();
  4355. if(ENABLE_MTG)CalculateMTG();
  4356. CalculateQuantumEngine();
  4357. QuantumCheckResult();
  4358. CleanChartAndShiftSR();
  4359. DrawSwingTrendLines();
  4360. int cp=0;double brain=BrainSc(cp);double nb=NeuralBiasFast();double mw=MicroWick();bool bf=IsBrokerForce();
  4361. double mai=CalcAdvancedMicroAI();int mts=CalcAdvancedMicroTrap();
  4362.  
  4363. CalcFibRejectionHunter();
  4364.  
  4365. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  4366. ScanSignals();CheckBackgroundNextCandleSignals();ScanBackgroundOBs();FinalSignalEngine(brain,cp,mts);BroadcastSignal();UpdateTimer();Draw();
  4367.  
  4368. DrawFibRejectionLine();
  4369. }
  4370.  
  4371. //+------------------------------------------------------------------+
  4372. //| SWING TREND LINES - White (Resistance + Support) |
  4373. //+------------------------------------------------------------------+
  4374. void DrawSwingTrendLines(){
  4375. if(Bars < 10) return;
  4376.  
  4377. string pfx = PFX + "SWTL_";
  4378. ObjectDelete(0, pfx + "RES");
  4379. ObjectDelete(0, pfx + "SUP");
  4380.  
  4381. int sh1 = -1, sh2 = -1;
  4382. int sl1 = -1, sl2 = -1;
  4383.  
  4384. for(int i = 2; i < Bars - 2; i++){
  4385. bool isSH = (High[i] > High[i-1] && High[i] > High[i-2] && High[i] > High[i+1] && High[i] > High[i+2]);
  4386. if(isSH){
  4387. if(sh1 == -1) sh1 = i;
  4388. else if(sh2 == -1) sh2 = i;
  4389. }
  4390. bool isSL = (Low[i] < Low[i-1] && Low[i] < Low[i-2] && Low[i] < Low[i+1] && Low[i] < Low[i+2]);
  4391. if(isSL){
  4392. if(sl1 == -1) sl1 = i;
  4393. else if(sl2 == -1) sl2 = i;
  4394. }
  4395. if(sh1 != -1 && sh2 != -1 && sl1 != -1 && sl2 != -1) break;
  4396. }
  4397.  
  4398. if(sh1 != -1 && sh2 != -1){
  4399. ObjectCreate(0, pfx+"RES", OBJ_TREND, 0, Time[sh2], High[sh2], Time[sh1], High[sh1]);
  4400. ObjectSetInteger(0, pfx+"RES", OBJPROP_COLOR, clrWhite);
  4401. ObjectSetInteger(0, pfx+"RES", OBJPROP_WIDTH, 1);
  4402. ObjectSetInteger(0, pfx+"RES", OBJPROP_STYLE, STYLE_SOLID);
  4403. ObjectSetInteger(0, pfx+"RES", OBJPROP_RAY_RIGHT, true);
  4404. ObjectSetInteger(0, pfx+"RES", OBJPROP_BACK, false);
  4405. ObjectSetInteger(0, pfx+"RES", OBJPROP_SELECTABLE, false);
  4406. }
  4407. if(sl1 != -1 && sl2 != -1){
  4408. ObjectCreate(0, pfx+"SUP", OBJ_TREND, 0, Time[sl2], Low[sl2], Time[sl1], Low[sl1]);
  4409. ObjectSetInteger(0, pfx+"SUP", OBJPROP_COLOR, clrWhite);
  4410. ObjectSetInteger(0, pfx+"SUP", OBJPROP_WIDTH, 1);
  4411. ObjectSetInteger(0, pfx+"SUP", OBJPROP_STYLE, STYLE_SOLID);
  4412. ObjectSetInteger(0, pfx+"SUP", OBJPROP_RAY_RIGHT, true);
  4413. ObjectSetInteger(0, pfx+"SUP", OBJPROP_BACK, false);
  4414. ObjectSetInteger(0, pfx+"SUP", OBJPROP_SELECTABLE, false);
  4415. }
  4416. }
  4417.  
  4418. //+------------------------------------------------------------------+
  4419. //| ISOLATED OTC v3.0 OB ENGINE - ONLY 3 DOTTED LINES |
  4420. //| NO RECTANGLE, NO FILL - Clean Chart |
  4421. //+------------------------------------------------------------------+
  4422. #define MAX_OB3 8
  4423. #define MAX_MICRO3 20
  4424. #define SUB_BUF3 80
  4425. #define MAX_OB3_BROKEN 100
  4426.  
  4427. enum OB3_TYPE { OB3_VAN=0, OB3_ADV=1, OB3_FVG=2, OB3_REJ=3 };
  4428. enum OB3_DIR { OB3_BEAR=0, OB3_BULL=1 };
  4429.  
  4430. struct OB3_Record {
  4431. OB3_TYPE type; OB3_DIR dir; double hi,lo,mid,body_hi,body_lo,vol_rat;
  4432. int cdl_cnt, str, tests; datetime ob_t, mit_t; bool active, mitigated;
  4433. string nm_r, nm_m, nm_l, nm_x;
  4434. };
  4435. struct OB3_Sub { datetime t; double o,h,l,c; bool bull; int tf; bool ok; };
  4436. struct OB3_Micro { double hi,lo,mid; datetime t; OB3_DIR dir; int pIdx,tf,mType; bool alive,sigDone; string nm_r,nm_l; };
  4437.  
  4438. OB3_Record g_O3[MAX_OB3]; int g_O3c=0;
  4439. OB3_Sub g_s5_3[SUB_BUF3],g_s10_3[SUB_BUF3],g_s15_3[SUB_BUF3],g_s30_3[SUB_BUF3]; int g_n5_3=0,g_n10_3=0,g_n15_3=0,g_n30_3=0;
  4440. double g_o5_3=0,g_h5_3=0,g_l5_3=0,g_o10_3=0,g_h10_3=0,g_l10_3=0,g_o15_3=0,g_h15_3=0,g_l15_3=0,g_o30_3=0,g_h30_3=0,g_l30_3=0;
  4441. datetime g_t5_3=0,g_t10_3=0,g_t15_3=0,g_t30_3=0;
  4442. OB3_Micro g_M3[MAX_MICRO3]; int g_M3c=0;
  4443. datetime g_O3_lastBar=0;
  4444.  
  4445. // ===== BROKEN OB TRACKING =====
  4446. static datetime g_O3_brokenTimes[MAX_OB3_BROKEN];
  4447. static int g_O3_brokenCount = 0;
  4448.  
  4449. bool OB3_IsBroken(datetime t){
  4450. for(int i=0; i<g_O3_brokenCount; i++){
  4451. if(g_O3_brokenTimes[i] == t) return true;
  4452. }
  4453. return false;
  4454. }
  4455.  
  4456. void OB3_AddBroken(datetime t){
  4457. if(OB3_IsBroken(t)) return;
  4458. if(g_O3_brokenCount < MAX_OB3_BROKEN){
  4459. g_O3_brokenTimes[g_O3_brokenCount] = t;
  4460. g_O3_brokenCount++;
  4461. }
  4462. }
  4463.  
  4464. bool OB3_Exist(datetime t){for(int i=0;i<g_O3c;i++)if(g_O3[i].ob_t==t&&g_O3[i].active)return true;return false;}
  4465. void OB3_Del(string n){if(n!="")ObjectDelete(0,n);}
  4466.  
  4467. void OB3_DelAll(){
  4468. for(int i=0;i<g_O3c;i++){
  4469. OB3_Del(g_O3[i].nm_r);
  4470. OB3_Del(g_O3[i].nm_r+"_TOP");
  4471. OB3_Del(g_O3[i].nm_r+"_BOT");
  4472. OB3_Del(g_O3[i].nm_r+"_LFT");
  4473. OB3_Del(g_O3[i].nm_r+"_RGT");
  4474. OB3_Del(g_O3[i].nm_r+"_FILL"); // ✅ FILL cleanup
  4475. OB3_Del(g_O3[i].nm_m);
  4476. OB3_Del(g_O3[i].nm_l);
  4477. OB3_Del(g_O3[i].nm_x);
  4478. }
  4479. for(int i=0;i<g_M3c;i++){
  4480. OB3_Del(g_M3[i].nm_r);
  4481. OB3_Del(g_M3[i].nm_l);
  4482. }
  4483. }
  4484.  
  4485. void OB3_Init(){
  4486. g_n5_3=0;g_n10_3=0;g_n15_3=0;g_n30_3=0;g_t5_3=0;g_t10_3=0;g_t15_3=0;g_t30_3=0;
  4487. g_o5_3=0;g_h5_3=0;g_l5_3=0;g_o10_3=0;g_h10_3=0;g_l10_3=0;g_o15_3=0;g_h15_3=0;g_l15_3=0;g_o30_3=0;g_h30_3=0;g_l30_3=0;
  4488. g_M3c=0;g_O3c=0;g_O3_lastBar=0;
  4489. g_O3_brokenCount=0;
  4490. for(int i=0;i<SUB_BUF3;i++){g_s5_3[i].ok=false;g_s10_3[i].ok=false;g_s15_3[i].ok=false;g_s30_3[i].ok=false;}
  4491. for(int i=0;i<MAX_MICRO3;i++)g_M3[i].alive=false;
  4492. for(int i=0;i<MAX_OB3;i++)g_O3[i].active=false;
  4493. for(int i=0;i<MAX_OB3_BROKEN;i++)g_O3_brokenTimes[i]=0;
  4494. }
  4495.  
  4496. void OB3_BuildSub(double p, datetime n, int sec, double &lO,double &lH,double &lL,datetime &lT,OB3_Sub &buf[],int &cnt){
  4497. if(lT==0){lT=n-(n%sec);lO=lH=lL=p;return;}
  4498. if((int)(n-lT)>=sec){
  4499. int lm=MathMin(cnt,SUB_BUF3-2);for(int k=lm;k>=0;k--)buf[k+1]=buf[k];
  4500. buf[0].t=lT;buf[0].o=lO;buf[0].h=lH;buf[0].l=lL;buf[0].c=p;buf[0].bull=(p>lO);buf[0].tf=sec;buf[0].ok=((lH-lL)>Point*0.5); // ✅ Chhoti candles bhi accept karo
  4501. if(cnt<SUB_BUF3)cnt++;lT=n-(n%sec);lO=lH=lL=p;
  4502. }else{lH=MathMax(lH,p);lL=MathMin(lL,p);}
  4503. }
  4504.  
  4505. void OB3_OnTick(){
  4506. if(Bid<=0||Ask<=0)return;double p=(Bid+Ask)*0.5;datetime n=TimeCurrent();
  4507. OB3_BuildSub(p,n,5,g_o5_3,g_h5_3,g_l5_3,g_t5_3,g_s5_3,g_n5_3);
  4508. OB3_BuildSub(p,n,10,g_o10_3,g_h10_3,g_l10_3,g_t10_3,g_s10_3,g_n10_3);
  4509. OB3_BuildSub(p,n,15,g_o15_3,g_h15_3,g_l15_3,g_t15_3,g_s15_3,g_n15_3);
  4510. OB3_BuildSub(p,n,30,g_o30_3,g_h30_3,g_l30_3,g_t30_3,g_s30_3,g_n30_3);
  4511. }
  4512.  
  4513. bool OB3_IsValid(int s,int z,OB3_DIR d){
  4514. if(s+z>=Bars)return false;int bad=0;
  4515. for(int c=0;c<z;c++){int idx=s+c;double b=MathAbs(Close[idx]-Open[idx]);double r=High[idx]-Low[idx];
  4516. bool dj=(r>0&&b<r*0.35);bool br=(Close[idx]<Open[idx]);bool bl=(Close[idx]>Open[idx]);
  4517. bool ok=dj||(d==OB3_BEAR?br:bl);if((c==0||c==z-1)&&!ok)bad++;}return(bad==0);
  4518. }
  4519.  
  4520. double OB3_CalcScore(int s,int z,OB3_DIR d){
  4521. if(s<3||s+z+3>=Bars)return 0;double sc=0;double zH=-DBL_MAX,zL=DBL_MAX,tB=0,tR=0;int al=0;double pip=GetUniversalPip();
  4522. for(int c=0;c<z;c++){int idx=s+c;if(idx>=Bars)break;zH=MathMax(zH,High[idx]);zL=MathMin(zL,Low[idx]);
  4523. double bd=MathAbs(Close[idx]-Open[idx]);double rn=High[idx]-Low[idx];tB+=bd;tR+=rn;
  4524. if(d==OB3_BEAR&&Close[idx]<Open[idx])al++;if(d==OB3_BULL&&Close[idx]>Open[idx])al++;
  4525. if(rn>0)sc+=(bd/rn)*16.0;double uw=High[idx]-MathMax(Open[idx],Close[idx]);double dw=MathMin(Open[idx],Close[idx])-Low[idx];double bw=MathMax(uw,dw);if(bd>0&&bw>bd*2.0)sc+=7.0;}
  4526. double zR=zH-zL;double aB=(z>0)?tB/z:0;double aR=(z>0)?tR/z:0;
  4527. if(aR>0&&zR<aR*0.65)sc+=12.0;double ar=(z>0)?(double)al/z:0;sc+=ar*14.0;
  4528. if(z>=3)sc+=7.0;if(z>=4)sc+=4.0;
  4529. if(s-1>=0&&pip>0){double dp=0;if(d==OB3_BEAR)dp=High[s-1]-Low[s+z-1];else dp=High[s+z-1]-Low[s-1];double pp=dp/pip;if(pp>=5)sc+=7;if(pp>=10)sc+=6;if(pp>=20)sc+=5;}
  4530. double zv=0,pv=0;for(int c=0;c<z;c++)if(s+c<Bars)zv+=(double)Volume[s+c];for(int c=1;c<=5;c++)if(s+c<Bars)pv+=(double)Volume[s+c];double ap=(pv>0)?pv/5.0:1.0;double vr=zv/(ap*z);if(vr>=1.8)sc+=10;else if(vr>=1.3)sc+=5;
  4531. if(s-1>=0){double pR=High[s-1]-Low[s-1];if(pR>0&&zR>pR*1.5)sc+=10;}
  4532. if(s-1>=0){double ib=MathAbs(Close[s-1]-Open[s-1]);if(aB>0&&ib>aB*1.8)sc+=6;}
  4533. for(int c=0;c<=MathMin(s-1,4);c++){if(c>=Bars)break;if(Low[c]<=zH&&High[c]>=zL){sc+=12;break;}}
  4534. return MathMin(sc,100.0);
  4535. }
  4536.  
  4537. void OB3_Save(int s,int z,OB3_DIR d,int sc,OB3_TYPE tp,datetime obT){
  4538. if(g_O3c>=MAX_OB3) return;
  4539. if(OB3_IsBroken(obT)) return;
  4540.  
  4541. double zH=High[s],zL=Low[s];
  4542. for(int c=1;c<z;c++){zH=MathMax(zH,High[s+c]);zL=MathMin(zL,Low[s+c]);}
  4543.  
  4544. double zv=0,pv=0;
  4545. for(int c=0;c<z;c++) if(s+c<Bars) zv+=(double)Volume[s+c];
  4546. for(int c=1;c<=5;c++) if(s+c<Bars) pv+=(double)Volume[s+c];
  4547. double ap=(pv>0)?pv/5.0:1.0;
  4548.  
  4549. string uid=(d==OB3_BEAR?"S":"B")+IntegerToString((int)obT)+"_"+IntegerToString(z);
  4550. int i=g_O3c;
  4551. g_O3[i].type=tp; g_O3[i].dir=d; g_O3[i].hi=zH; g_O3[i].lo=zL; g_O3[i].mid=(zH+zL)/2.0;
  4552. g_O3[i].body_hi=MathMax(Open[s],Close[s]); g_O3[i].body_lo=MathMin(Open[s],Close[s]);
  4553. g_O3[i].cdl_cnt=z; g_O3[i].str=sc; g_O3[i].ob_t=obT; g_O3[i].mit_t=0;
  4554. g_O3[i].active=true; g_O3[i].mitigated=false; g_O3[i].tests=0; g_O3[i].vol_rat=zv/(ap*z);
  4555. g_O3[i].nm_r=uid+"_R"; g_O3[i].nm_m=uid+"_M"; g_O3[i].nm_l=uid+"_L"; g_O3[i].nm_x=uid+"_X";
  4556. g_O3c++;
  4557. }
  4558.  
  4559. void OB3_MitCheck(){
  4560. for(int i=0;i<g_O3c;i++){
  4561. if(!g_O3[i].active||g_O3[i].mitigated) continue;
  4562. bool broken=false;
  4563. if(g_O3[i].dir==OB3_BULL && Close[0]<g_O3[i].lo) broken=true;
  4564. if(g_O3[i].dir==OB3_BEAR && Close[0]>g_O3[i].hi) broken=true;
  4565. if(broken){
  4566. // ✅ DON'T DELETE — Mark as mitigated, draw in grey with arrow
  4567. g_O3[i].mitigated=true;
  4568. g_O3[i].mit_t=Time[0];
  4569. OB3_AddBroken(g_O3[i].ob_t);
  4570. }
  4571. }
  4572. }
  4573.  
  4574. void OB3_MitCheckLive(){
  4575. if(g_O3c==0) return;
  4576. for(int i=0;i<g_O3c;i++){
  4577. if(!g_O3[i].active||g_O3[i].mitigated) continue;
  4578. bool broken=false;
  4579. if(g_O3[i].dir==OB3_BULL && Close[0]<g_O3[i].lo) broken=true;
  4580. if(g_O3[i].dir==OB3_BEAR && Close[0]>g_O3[i].hi) broken=true;
  4581. if(broken){
  4582. // ✅ DON'T DELETE — Mark as mitigated, next draw will show grey + arrow
  4583. g_O3[i].mitigated=true;
  4584. g_O3[i].mit_t=Time[0];
  4585. OB3_AddBroken(g_O3[i].ob_t);
  4586. }
  4587. }
  4588. }
  4589.  
  4590. //+------------------------------------------------------------------+
  4591. //| OB3_Draw - DOTTED RECTANGLE with SUBTLE FILL (Professional) |
  4592. //+------------------------------------------------------------------+
  4593. void OB3_Draw(int idx){
  4594. OB3_Record ob=g_O3[idx];
  4595. double pip=GetUniversalPip();
  4596. datetime tFr=ob.ob_t;
  4597. datetime tTo=Time[0]+PeriodSeconds(PERIOD_M1)*15;
  4598.  
  4599. // ===== CLEANUP =====
  4600. OB3_Del(ob.nm_r);
  4601. OB3_Del(ob.nm_r+"_TOP");
  4602. OB3_Del(ob.nm_r+"_BOT");
  4603. OB3_Del(ob.nm_r+"_LFT");
  4604. OB3_Del(ob.nm_r+"_RGT");
  4605. OB3_Del(ob.nm_r+"_FILL");
  4606. OB3_Del(ob.nm_m);
  4607. OB3_Del(ob.nm_l);
  4608. OB3_Del(ob.nm_x);
  4609.  
  4610.  
  4611. // ===== COLOR LOGIC (Volume Based Premium + Live Check) =====
  4612. color obColor, fillColor, midColor=C'255,200,0';
  4613.  
  4614. // Check 1: OB bante waqt heavy volume tha
  4615. bool isPremium = (ob.vol_rat >= 2.0);
  4616.  
  4617. // Check 2: CURRENT candle mein achanak heavy volume aaya OB ke paas
  4618. double curVol = (double)iVolume(NULL, PERIOD_M1, 0);
  4619. double avgVol = 0;
  4620. for(int v=1; v<=5 && v<Bars; v++) avgVol += (double)iVolume(NULL, PERIOD_M1, v);
  4621. if(avgVol > 0) avgVol /= 5.0;
  4622. double curVolRatio = (avgVol > 0) ? curVol / avgVol : 1.0;
  4623. double pipCheck = GetUniversalPip(); if(pipCheck <= 0) pipCheck = Point * 10;
  4624. bool isNearOB = (Close[0] >= ob.lo - pipCheck*5 && Close[0] <= ob.hi + pipCheck*5);
  4625.  
  4626. // Agar current candle OB ke paas hai AUR uska volume 2x hai = PREMIUM!
  4627. if(isNearOB && curVolRatio >= 2.0) isPremium = true;
  4628.  
  4629.  
  4630. if(ob.mitigated){
  4631. obColor=C'90,90,90'; fillColor=C'20,20,20'; midColor=C'70,70,70';
  4632. }
  4633. else if(ob.dir==OB3_BULL){
  4634. if(isPremium) {obColor=C'30,144,255'; fillColor=C'5,20,50';} // BLUE = Heavy Volume Buy
  4635. else {obColor=C'0,200,80'; fillColor=C'0,35,14';} // GREEN = Normal Buy
  4636. }
  4637. else{ // OB3_BEAR
  4638. if(isPremium) {obColor=C'180,0,255'; fillColor=C'30,0,40';} // PURPLE = Heavy Volume Sell
  4639. else {obColor=C'220,40,40'; fillColor=C'35,0,0';} // RED = Normal Sell
  4640. }
  4641.  
  4642. // ===== SUBTLE FILL =====
  4643. ObjectCreate(0, ob.nm_r+"_FILL", OBJ_RECTANGLE, 0, tFr, ob.hi, tTo, ob.lo);
  4644. ObjectSetInteger(0, ob.nm_r+"_FILL", OBJPROP_COLOR, fillColor);
  4645. ObjectSetInteger(0, ob.nm_r+"_FILL", OBJPROP_FILL, true);
  4646. ObjectSetInteger(0, ob.nm_r+"_FILL", OBJPROP_WIDTH, 0);
  4647. ObjectSetInteger(0, ob.nm_r+"_FILL", OBJPROP_BACK, true);
  4648. ObjectSetInteger(0, ob.nm_r+"_FILL", OBJPROP_SELECTABLE, false);
  4649.  
  4650.  
  4651. // ===== DOTTED BORDER LINES (Green se Green, Red se Red) =====
  4652.  
  4653.  
  4654. // TOP Dotted Line
  4655. ObjectCreate(0, ob.nm_r+"_TOP", OBJ_TREND, 0, tFr, ob.hi, tTo, ob.hi);
  4656. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_COLOR, obColor);
  4657. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_STYLE, STYLE_DOT);
  4658. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_WIDTH, 1);
  4659. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_RAY_RIGHT, false);
  4660. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_BACK, false);
  4661.  
  4662. // BOT Dotted Line
  4663. ObjectCreate(0, ob.nm_r+"_BOT", OBJ_TREND, 0, tFr, ob.lo, tTo, ob.lo);
  4664. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_COLOR, obColor);
  4665. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_STYLE, STYLE_DOT);
  4666. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_WIDTH, 1);
  4667. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_RAY_RIGHT, false);
  4668. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_BACK, false);
  4669.  
  4670. // LFT Dotted Line
  4671. ObjectCreate(0, ob.nm_r+"_LFT", OBJ_TREND, 0, tFr, ob.hi, tFr, ob.lo);
  4672. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_COLOR, obColor);
  4673. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_STYLE, STYLE_DOT);
  4674. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_WIDTH, 1);
  4675. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_RAY_RIGHT, false);
  4676. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_BACK, false);
  4677.  
  4678. // RGT Dotted Line
  4679. ObjectCreate(0, ob.nm_r+"_RGT", OBJ_TREND, 0, tTo, ob.hi, tTo, ob.lo);
  4680. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_COLOR, obColor);
  4681. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_STYLE, STYLE_DOT);
  4682. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_WIDTH, 1);
  4683. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_RAY_RIGHT, false);
  4684. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_BACK, false);
  4685.  
  4686.  
  4687. // ===== DOTTED BORDER LINES =====
  4688. ObjectCreate(0, ob.nm_r+"_TOP", OBJ_TREND, 0, tFr, ob.hi, tTo, ob.hi);
  4689. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_COLOR, obColor);
  4690. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_STYLE, STYLE_DOT);
  4691. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_WIDTH, 1);
  4692. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_RAY_RIGHT, false);
  4693. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_BACK, false);
  4694. ObjectSetInteger(0, ob.nm_r+"_TOP", OBJPROP_SELECTABLE, false);
  4695.  
  4696. ObjectCreate(0, ob.nm_r+"_BOT", OBJ_TREND, 0, tFr, ob.lo, tTo, ob.lo);
  4697. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_COLOR, obColor);
  4698. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_STYLE, STYLE_DOT);
  4699. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_WIDTH, 1);
  4700. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_RAY_RIGHT, false);
  4701. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_BACK, false);
  4702. ObjectSetInteger(0, ob.nm_r+"_BOT", OBJPROP_SELECTABLE, false);
  4703.  
  4704. ObjectCreate(0, ob.nm_r+"_LFT", OBJ_TREND, 0, tFr, ob.hi, tFr, ob.lo);
  4705. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_COLOR, obColor);
  4706. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_STYLE, STYLE_DOT);
  4707. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_WIDTH, 1);
  4708. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_RAY_RIGHT, false);
  4709. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_BACK, false);
  4710. ObjectSetInteger(0, ob.nm_r+"_LFT", OBJPROP_SELECTABLE, false);
  4711.  
  4712. ObjectCreate(0, ob.nm_r+"_RGT", OBJ_TREND, 0, tTo, ob.hi, tTo, ob.lo);
  4713. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_COLOR, obColor);
  4714. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_STYLE, STYLE_DOT);
  4715. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_WIDTH, 1);
  4716. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_RAY_RIGHT, false);
  4717. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_BACK, false);
  4718. ObjectSetInteger(0, ob.nm_r+"_RGT", OBJPROP_SELECTABLE, false);
  4719.  
  4720. // ===== MIDDLE DOTTED LINE (Gold) =====
  4721. ObjectCreate(0, ob.nm_m, OBJ_TREND, 0, tFr, ob.mid, tTo, ob.mid);
  4722. ObjectSetInteger(0, ob.nm_m, OBJPROP_COLOR, midColor);
  4723. ObjectSetInteger(0, ob.nm_m, OBJPROP_STYLE, STYLE_DOT);
  4724. ObjectSetInteger(0, ob.nm_m, OBJPROP_WIDTH, 1);
  4725. ObjectSetInteger(0, ob.nm_m, OBJPROP_RAY_RIGHT, false);
  4726. ObjectSetInteger(0, ob.nm_m, OBJPROP_BACK, false);
  4727. ObjectSetInteger(0, ob.nm_m, OBJPROP_SELECTABLE, false);
  4728.  
  4729. // ===== LABEL =====
  4730. string ds=(ob.dir==OB3_BEAR)?"v":"^";
  4731. string ts="OB";
  4732. string pr=(isPremium)?"[P]":""; // Premium tag only on heavy volume
  4733. string mit=(ob.mitigated)?"X":"";
  4734. string lb=pr+ds+ts+" S:"+IntegerToString(ob.str)+"%"+mit;
  4735. double lY=(ob.dir==OB3_BEAR)?ob.hi+pip*2:ob.lo-pip*2;
  4736.  
  4737. ObjectCreate(0, ob.nm_l, OBJ_TEXT, 0, tFr, lY);
  4738. ObjectSetString(0, ob.nm_l, OBJPROP_TEXT, lb);
  4739. ObjectSetInteger(0, ob.nm_l, OBJPROP_COLOR, obColor);
  4740. ObjectSetInteger(0, ob.nm_l, OBJPROP_FONTSIZE, 8);
  4741. ObjectSetString(0, ob.nm_l, OBJPROP_FONT, "Arial Bold");
  4742. ObjectSetInteger(0, ob.nm_l, OBJPROP_SELECTABLE, false);
  4743.  
  4744. // ===== MITIGATION ARROW =====
  4745. if(ob.mitigated && ob.mit_t > 0){
  4746. double arrY=(ob.dir==OB3_BEAR)?ob.lo-pip*5:ob.hi+pip*5;
  4747. int code=(ob.dir==OB3_BEAR)?242:241;
  4748. ObjectCreate(0, ob.nm_x, OBJ_ARROW, 0, ob.mit_t, arrY);
  4749. ObjectSetInteger(0, ob.nm_x, OBJPROP_ARROWCODE, code);
  4750. ObjectSetInteger(0, ob.nm_x, OBJPROP_COLOR, clrLime);
  4751. ObjectSetInteger(0, ob.nm_x, OBJPROP_WIDTH, 2);
  4752. ObjectSetInteger(0, ob.nm_x, OBJPROP_BACK, false);
  4753. ObjectSetInteger(0, ob.nm_x, OBJPROP_SELECTABLE, false);
  4754. }
  4755. }
  4756.  
  4757. //+------------------------------------------------------------------+
  4758. //| DetectRejection - Wick Rejection OB (NO LAMBDA - MQL4 SAFE) |
  4759. //+------------------------------------------------------------------+
  4760. void DetectRejection(int idx){
  4761. if(g_O3c >= MAX_OB3 - 1) return;
  4762. if(idx < 1 || idx + 1 >= Bars) return;
  4763.  
  4764. double body = MathAbs(Close[idx] - Open[idx]);
  4765. double rng = High[idx] - Low[idx];
  4766. if(rng <= 0) return;
  4767.  
  4768. double upW = High[idx] - MathMax(Open[idx], Close[idx]);
  4769. double dwW = MathMin(Open[idx], Close[idx]) - Low[idx];
  4770.  
  4771. // ===== UPPER WICK = BEARISH REJECTION OB =====
  4772. if(upW >= rng * 0.62 && body < rng * 0.28){
  4773. datetime obT = iTime(NULL, PERIOD_M1, idx);
  4774. if(!OB3_Exist(obT) && !OB3_IsBroken(obT)){
  4775. string uid = "RJS" + IntegerToString((int)obT);
  4776. int i = g_O3c;
  4777. g_O3[i].type = OB3_REJ;
  4778. g_O3[i].dir = OB3_BEAR;
  4779. g_O3[i].hi = High[idx];
  4780. g_O3[i].lo = MathMax(Open[idx], Close[idx]);
  4781. g_O3[i].mid = (g_O3[i].hi + g_O3[i].lo) / 2.0;
  4782. g_O3[i].body_hi = MathMax(Open[idx], Close[idx]);
  4783. g_O3[i].body_lo = MathMin(Open[idx], Close[idx]);
  4784. g_O3[i].cdl_cnt = 1;
  4785. g_O3[i].str = (int)(78.0 + (upW/rng)*22.0); // ✅ Min 78, Max 100
  4786. g_O3[i].ob_t = obT;
  4787. g_O3[i].mit_t = 0;
  4788. g_O3[i].active = true;
  4789. g_O3[i].mitigated = false;
  4790. g_O3[i].tests = 0;
  4791. g_O3[i].vol_rat = 1.0;
  4792. g_O3[i].nm_r = uid+"_R";
  4793. g_O3[i].nm_m = uid+"_M";
  4794. g_O3[i].nm_l = uid+"_L";
  4795. g_O3[i].nm_x = uid+"_X";
  4796. g_O3c++;
  4797. }
  4798. }
  4799.  
  4800. // ===== LOWER WICK = BULLISH REJECTION OB =====
  4801. if(dwW >= rng * 0.62 && body < rng * 0.28){
  4802. datetime obT = iTime(NULL, PERIOD_M1, idx);
  4803. if(!OB3_Exist(obT) && !OB3_IsBroken(obT)){
  4804. string uid = "RJB" + IntegerToString((int)obT);
  4805. int i = g_O3c;
  4806. g_O3[i].type = OB3_REJ;
  4807. g_O3[i].dir = OB3_BULL;
  4808. g_O3[i].hi = MathMin(Open[idx], Close[idx]);
  4809. g_O3[i].lo = Low[idx];
  4810. g_O3[i].mid = (g_O3[i].hi + g_O3[i].lo) / 2.0;
  4811. g_O3[i].body_hi = MathMax(Open[idx], Close[idx]);
  4812. g_O3[i].body_lo = MathMin(Open[idx], Close[idx]);
  4813. g_O3[i].cdl_cnt = 1;
  4814. g_O3[i].str = (int)(78.0 + (dwW/rng)*22.0); // ✅ Min 78, Max 100
  4815. g_O3[i].ob_t = obT;
  4816. g_O3[i].mit_t = 0;
  4817. g_O3[i].active = true;
  4818. g_O3[i].mitigated = false;
  4819. g_O3[i].tests = 0;
  4820. g_O3[i].vol_rat = 1.0;
  4821. g_O3[i].nm_r = uid+"_R";
  4822. g_O3[i].nm_m = uid+"_M";
  4823. g_O3[i].nm_l = uid+"_L";
  4824. g_O3[i].nm_x = uid+"_X";
  4825. g_O3c++;
  4826. }
  4827. }
  4828. }
  4829.  
  4830.  
  4831. //+------------------------------------------------------------------+
  4832. //| DetectFVG - Fair Value Gap Detection (Dotted Box) |
  4833. //+------------------------------------------------------------------+
  4834. void DetectFVG(int i){
  4835. if(g_O3c >= MAX_OB3 - 2) return;
  4836. if(i < 1 || i + 1 >= Bars) return;
  4837.  
  4838. double pip = GetUniversalPip();
  4839. double minGap = pip * 6; // ✅ 2 se 6 pips = bigger gaps only
  4840.  
  4841. // ===== BULLISH FVG: Gap Up (candle[i-1].low > candle[i+1].high) =====
  4842. if(Low[i-1] > High[i+1] && (Low[i-1] - High[i+1]) >= minGap){
  4843. datetime obT = iTime(NULL, PERIOD_M1, i);
  4844. if(!OB3_Exist(obT + 1) && !OB3_IsBroken(obT + 1)){
  4845. string uid = "FVGB" + IntegerToString((int)obT);
  4846. int idx = g_O3c;
  4847. g_O3[idx].type = OB3_FVG;
  4848. g_O3[idx].dir = OB3_BULL;
  4849. g_O3[idx].hi = Low[i-1];
  4850. g_O3[idx].lo = High[i+1];
  4851. g_O3[idx].mid = (g_O3[idx].hi + g_O3[idx].lo) / 2.0;
  4852. g_O3[idx].body_hi = g_O3[idx].hi;
  4853. g_O3[idx].body_lo = g_O3[idx].lo;
  4854. g_O3[idx].cdl_cnt = 1;
  4855. g_O3[idx].str = 82; // ✅ FVG bhi premium quality only
  4856. g_O3[idx].ob_t = obT + 1;
  4857. g_O3[idx].mit_t = 0;
  4858. g_O3[idx].active = true;
  4859. g_O3[idx].mitigated = false;
  4860. g_O3[idx].tests = 0;
  4861. g_O3[idx].vol_rat = 1.0;
  4862. g_O3[idx].nm_r = uid+"_R";
  4863. g_O3[idx].nm_m = uid+"_M";
  4864. g_O3[idx].nm_l = uid+"_L";
  4865. g_O3[idx].nm_x = uid+"_X";
  4866. g_O3c++;
  4867. }
  4868. }
  4869.  
  4870. // ===== BEARISH FVG: Gap Down (candle[i+1].low > candle[i-1].high) =====
  4871. if(Low[i+1] > High[i-1] && (Low[i+1] - High[i-1]) >= minGap){
  4872. datetime obT = iTime(NULL, PERIOD_M1, i);
  4873. if(!OB3_Exist(obT + 2) && !OB3_IsBroken(obT + 2)){
  4874. string uid = "FVGS" + IntegerToString((int)obT);
  4875. int idx = g_O3c;
  4876. g_O3[idx].type = OB3_FVG;
  4877. g_O3[idx].dir = OB3_BEAR;
  4878. g_O3[idx].hi = Low[i+1];
  4879. g_O3[idx].lo = High[i-1];
  4880. g_O3[idx].mid = (g_O3[idx].hi + g_O3[idx].lo) / 2.0;
  4881. g_O3[idx].body_hi = g_O3[idx].hi;
  4882. g_O3[idx].body_lo = g_O3[idx].lo;
  4883. g_O3[idx].cdl_cnt = 1;
  4884. g_O3[idx].str = 82; // ✅ FVG bhi premium quality only
  4885. g_O3[idx].ob_t = obT + 2;
  4886. g_O3[idx].mit_t = 0;
  4887. g_O3[idx].active = true;
  4888. g_O3[idx].mitigated = false;
  4889. g_O3[idx].tests = 0;
  4890. g_O3[idx].vol_rat = 1.0;
  4891. g_O3[idx].nm_r = uid+"_R";
  4892. g_O3[idx].nm_m = uid+"_M";
  4893. g_O3[idx].nm_l = uid+"_L";
  4894. g_O3[idx].nm_x = uid+"_X";
  4895. g_O3c++;
  4896. }
  4897. }
  4898. }
  4899.  
  4900. //+------------------------------------------------------------------+
  4901. //| PROXIMITY FILTER - Paas mein already OB hai toh naya mat banao |
  4902. //+------------------------------------------------------------------+
  4903. void OB3_RemoveOverlap(double newHi, double newLo, int dir){
  4904. double pip=GetUniversalPip();if(pip<=0)pip=Point*10;
  4905.  
  4906. for(int j=0;j<g_O3c;j++){
  4907. if(!g_O3[j].active) continue;
  4908. if(g_O3[j].mitigated) continue;
  4909.  
  4910. double oldHi=g_O3[j].hi;
  4911. double oldLo=g_O3[j].lo;
  4912.  
  4913. // OVERLAP CHECK: Kya naya OB purane OB ke area mein hai? (Koi bhi direction ho)
  4914. bool overlap = (newHi >= oldLo && newLo <= oldHi);
  4915.  
  4916. // NEARBY CHECK: Kya 10 pips ke andar koi bhi OB hai?
  4917. double oldCenter=(oldHi+oldLo)/2.0;
  4918. double newCenter=(newHi+newLo)/2.0;
  4919. bool nearBy=(MathAbs(oldCenter-newCenter) < pip*10);
  4920.  
  4921. if(overlap || nearBy){
  4922. // Purana OB delete karo (Chart objects bhi hata do)
  4923. OB3_Del(g_O3[j].nm_r);
  4924. OB3_Del(g_O3[j].nm_r+"_TOP");
  4925. OB3_Del(g_O3[j].nm_r+"_BOT");
  4926. OB3_Del(g_O3[j].nm_r+"_LFT");
  4927. OB3_Del(g_O3[j].nm_r+"_RGT");
  4928. OB3_Del(g_O3[j].nm_r+"_FILL");
  4929. OB3_Del(g_O3[j].nm_m);
  4930. OB3_Del(g_O3[j].nm_l);
  4931. OB3_Del(g_O3[j].nm_x);
  4932. g_O3[j].active=false;
  4933. }
  4934. }
  4935. }
  4936.  
  4937. void DetectHistoricalMicroOB(int s, int pIdx){
  4938. if(g_M3c>=MAX_MICRO3) return;
  4939. if(pIdx<0 || pIdx>=g_O3c) return;
  4940. double pip=GetUniversalPip(); if(pip<=0) pip=Point*10;
  4941.  
  4942. double body=MathAbs(Close[s]-Open[s]);
  4943. double rng=High[s]-Low[s];
  4944. if(rng<=0) return;
  4945.  
  4946. double upWick=High[s]-MathMax(Open[s],Close[s]);
  4947. double dnWick=MathMin(Open[s],Close[s])-Low[s];
  4948.  
  4949. if(g_O3[pIdx].dir==OB3_BULL && dnWick>rng*0.10){
  4950. bool dup=false;
  4951. for(int d=0;d<g_M3c;d++){
  4952. if(g_M3[d].alive && g_M3[d].pIdx==pIdx){dup=true;break;}
  4953. }
  4954. if(!dup){
  4955. int m=g_M3c;
  4956. string uid="HMOB_"+IntegerToString((int)iTime(NULL,PERIOD_M1,s))+"_P"+IntegerToString(pIdx);
  4957.  
  4958. // ✅ FIX: BODY only + CLIP inside parent OB
  4959. g_M3[m].hi=MathMin(Open[s], g_O3[pIdx].hi);
  4960. g_M3[m].lo=MathMax(Close[s], g_O3[pIdx].lo);
  4961. g_M3[m].mid=(g_M3[m].hi+g_M3[m].lo)/2.0;
  4962.  
  4963. // ✅ FIX: Agar Micro OB Parent OB ka 60% se zyada bada hai, toh mat banao
  4964. double parentH1 = g_O3[pIdx].hi - g_O3[pIdx].lo;
  4965. double microH1 = g_M3[m].hi - g_M3[m].lo;
  4966. if(parentH1 > 0 && microH1 > parentH1 * 0.6) return;
  4967.  
  4968. g_M3[m].t=iTime(NULL,PERIOD_M1,s);
  4969. g_M3[m].dir=OB3_BULL;
  4970. g_M3[m].pIdx=pIdx; g_M3[m].tf=60; g_M3[m].mType=1; g_M3[m].alive=true; g_M3[m].sigDone=false;
  4971. g_M3[m].nm_r="MOB3_R_"+uid; g_M3[m].nm_l="MOB3_L_"+uid;
  4972. g_M3c++;
  4973. }
  4974. }
  4975. else if(g_O3[pIdx].dir==OB3_BEAR && upWick>rng*0.10){
  4976. bool dup=false;
  4977. for(int d=0;d<g_M3c;d++){
  4978. if(g_M3[d].alive && g_M3[d].pIdx==pIdx){dup=true;break;}
  4979. }
  4980. if(!dup){
  4981. int m=g_M3c;
  4982. string uid="HMOB_"+IntegerToString((int)iTime(NULL,PERIOD_M1,s))+"_P"+IntegerToString(pIdx);
  4983.  
  4984. // ✅ FIX: BODY only + CLIP inside parent OB
  4985. g_M3[m].hi=MathMin(Close[s], g_O3[pIdx].hi);
  4986. g_M3[m].lo=MathMax(Open[s], g_O3[pIdx].lo);
  4987. g_M3[m].mid=(g_M3[m].hi+g_M3[m].lo)/2.0;
  4988.  
  4989. // ✅ FIX: Agar Micro OB Parent OB ka 60% se zyada bada hai, toh mat banao
  4990. double parentH2 = g_O3[pIdx].hi - g_O3[pIdx].lo;
  4991. double microH2 = g_M3[m].hi - g_M3[m].lo;
  4992. if(parentH2 > 0 && microH2 > parentH2 * 0.6) return;
  4993.  
  4994. g_M3[m].t=iTime(NULL,PERIOD_M1,s);
  4995. g_M3[m].dir=OB3_BEAR;
  4996. g_M3[m].pIdx=pIdx; g_M3[m].tf=60; g_M3[m].mType=1; g_M3[m].alive=true; g_M3[m].sigDone=false;
  4997. g_M3[m].nm_r="MOB3_R_"+uid; g_M3[m].nm_l="MOB3_L_"+uid;
  4998. g_M3c++;
  4999. }
  5000. }
  5001. }
  5002.  
  5003.  
  5004. //+------------------------------------------------------------------+
  5005. //| OB3_RunEngine - SMC STANDARD v4.0 (FIXED DIRECTIONS) |
  5006. //| Bullish OB = Last bearish candle before strong UP move |
  5007. //| Bearish OB = Last bullish candle before strong DOWN move |
  5008. //+------------------------------------------------------------------+
  5009. void OB3_RunEngine(){
  5010. if(Time[0]==g_O3_lastBar) return;
  5011. g_O3_lastBar=Time[0];
  5012. OB3_DelAll();
  5013. g_O3c=0;
  5014.  
  5015. double pip=GetUniversalPip();
  5016. if(pip<=0) pip=Point*10;
  5017. int maxLB=MathMin(50,Bars-10);
  5018.  
  5019. for(int i=2; i<maxLB && g_O3c<MAX_OB3-2; i++){
  5020.  
  5021. // Skip if already inside an active OB zone
  5022. bool insideExisting=false;
  5023. for(int e=0;e<g_O3c;e++){
  5024. if(g_O3[e].active && High[i]<=g_O3[e].hi && Low[i]>=g_O3[e].lo){
  5025. insideExisting=true; break;
  5026. }
  5027. }
  5028. if(insideExisting) continue;
  5029.  
  5030.  
  5031. // ✅ FIX: Rejection aur FVG Order Blocks Activate kiye
  5032. DetectRejection(i);
  5033. DetectFVG(i);
  5034.  
  5035.  
  5036. // ============================================================
  5037. // BULLISH OB: Last BEARISH candle before strong UP move + BOS
  5038. // ============================================================
  5039. if(Close[i] < Open[i]){
  5040. double baseHi = High[i];
  5041. double bullMove=0;
  5042. int bullCount=0;
  5043. bool hasBOS = false;
  5044.  
  5045. for(int j=i-1; j>=MathMax(0,i-5); j--){
  5046. if(Close[j]>Open[j]){
  5047. bullMove+=Close[j]-Open[j];
  5048. bullCount++;
  5049. if(Close[j] > baseHi) hasBOS = true;
  5050. } else break;
  5051. }
  5052.  
  5053. double movePips=bullMove/pip;
  5054.  
  5055. if(hasBOS && bullCount>=1 && movePips>=4){
  5056. datetime obT=iTime(NULL,PERIOD_M1,i);
  5057. if(!OB3_Exist(obT) && !OB3_IsBroken(obT)){
  5058. double zHi=High[i], zLo=Low[i];
  5059.  
  5060. OB3_RemoveOverlap(zHi,zLo,OB3_BULL);
  5061. {
  5062. // Score Calculation (Strict)
  5063. double sc=50.0;
  5064. double body=MathAbs(Close[i]-Open[i]);
  5065. double rng=zHi-zLo;
  5066. if(rng>0) sc+=((body/rng)*15.0); // Reduced weight
  5067. sc+=MathMin(20.0, movePips*0.8); // Reduced weight
  5068. if(bullCount>=2) sc+=5.0;
  5069.  
  5070. // Volume Check for Premium
  5071. double vol0=(double)iVolume(NULL,PERIOD_M1,i);
  5072. double volAvg=0;
  5073. for(int v=i+1;v<=i+5&&v<Bars;v++) volAvg+=(double)iVolume(NULL,PERIOD_M1,v);
  5074. volAvg/=5.0;
  5075. double volRatio=(volAvg>0)?vol0/volAvg:1.0;
  5076.  
  5077. if(volRatio>=2.0) sc+=15.0; // Heavy volume = big bonus
  5078. if(sc>100) sc=100;
  5079. if(sc<50) continue;
  5080.  
  5081. int idx=g_O3c;
  5082. string uid="B"+IntegerToString((int)obT);
  5083. g_O3[idx].type=OB3_VAN; g_O3[idx].dir=OB3_BULL; g_O3[idx].hi=zHi; g_O3[idx].lo=zLo; g_O3[idx].mid=(zHi+zLo)/2.0;
  5084. g_O3[idx].body_hi=MathMax(Open[i],Close[i]); g_O3[idx].body_lo=MathMin(Open[i],Close[i]);
  5085. g_O3[idx].cdl_cnt=1; g_O3[idx].str=(int)sc; g_O3[idx].ob_t=obT; g_O3[idx].mit_t=0;
  5086. g_O3[idx].active=true; g_O3[idx].mitigated=false; g_O3[idx].tests=0; g_O3[idx].vol_rat=volRatio;
  5087. g_O3[idx].nm_r=uid+"_R"; g_O3[idx].nm_m=uid+"_M"; g_O3[idx].nm_l=uid+"_L"; g_O3[idx].nm_x=uid+"_X";
  5088. g_O3c++;
  5089.  
  5090. i += bullCount; // SKIP move candles (1 Move = 1 OB Rule)
  5091. }
  5092. }
  5093. }
  5094. }
  5095.  
  5096. // ============================================================
  5097. // BEARISH OB: Last BULLISH candle before strong DOWN move + BOS
  5098. // ============================================================
  5099. if(Close[i] > Open[i]){
  5100. double baseLo = Low[i];
  5101. double bearMove=0;
  5102. int bearCount=0;
  5103. bool hasBOS = false;
  5104.  
  5105. for(int j=i-1; j>=MathMax(0,i-5); j--){
  5106. if(Close[j]<Open[j]){
  5107. bearMove+=Open[j]-Close[j];
  5108. bearCount++;
  5109. if(Close[j] < baseLo) hasBOS = true;
  5110. } else break;
  5111. }
  5112.  
  5113. double movePips=bearMove/pip;
  5114.  
  5115. if(hasBOS && bearCount>=1 && movePips>=4){
  5116. datetime obT=iTime(NULL,PERIOD_M1,i);
  5117. if(!OB3_Exist(obT) && !OB3_IsBroken(obT)){
  5118. double zHi=High[i], zLo=Low[i];
  5119.  
  5120. OB3_RemoveOverlap(zHi,zLo,OB3_BEAR);
  5121. {
  5122. double sc=50.0;
  5123. double body=MathAbs(Close[i]-Open[i]);
  5124. double rng=zHi-zLo;
  5125. if(rng>0) sc+=((body/rng)*15.0);
  5126. sc+=MathMin(20.0, movePips*0.8);
  5127. if(bearCount>=2) sc+=5.0;
  5128.  
  5129. double vol0=(double)iVolume(NULL,PERIOD_M1,i);
  5130. double volAvg=0;
  5131. for(int v=i+1;v<=i+5&&v<Bars;v++) volAvg+=(double)iVolume(NULL,PERIOD_M1,v);
  5132. volAvg/=5.0;
  5133. double volRatio=(volAvg>0)?vol0/volAvg:1.0;
  5134.  
  5135. if(volRatio>=2.0) sc+=15.0;
  5136. if(sc>100) sc=100;
  5137. if(sc<50) continue;
  5138.  
  5139. int idx=g_O3c;
  5140. string uid="S"+IntegerToString((int)obT);
  5141. g_O3[idx].type=OB3_VAN; g_O3[idx].dir=OB3_BEAR; g_O3[idx].hi=zHi; g_O3[idx].lo=zLo; g_O3[idx].mid=(zHi+zLo)/2.0;
  5142. g_O3[idx].body_hi=MathMax(Open[i],Close[i]); g_O3[idx].body_lo=MathMin(Open[i],Close[i]);
  5143. g_O3[idx].cdl_cnt=1; g_O3[idx].str=(int)sc; g_O3[idx].ob_t=obT; g_O3[idx].mit_t=0;
  5144. g_O3[idx].active=true; g_O3[idx].mitigated=false; g_O3[idx].tests=0; g_O3[idx].vol_rat=volRatio;
  5145. g_O3[idx].nm_r=uid+"_R"; g_O3[idx].nm_m=uid+"_M"; g_O3[idx].nm_l=uid+"_L"; g_O3[idx].nm_x=uid+"_X";
  5146. g_O3c++;
  5147.  
  5148. i += bearCount; // SKIP move candles (1 Move = 1 OB Rule)
  5149. }
  5150. }
  5151. }
  5152. }
  5153. }
  5154.  
  5155. OB3_MitCheck();
  5156. for(int i=0;i<g_O3c;i++){
  5157. if(g_O3[i].active) OB3_Draw(i);
  5158. }
  5159. }
  5160.  
  5161. //+------------------------------------------------------------------+
  5162. //| OB3_ScanAll v3.0 - Track smallest opposite + rejection |
  5163. //+------------------------------------------------------------------+
  5164. void OB3_ScanAll(OB3_Sub &buf[], int cnt, double obHi, double obLo,
  5165. OB3_DIR pDir, int tf, double tol, double pip,
  5166. double &oppBody, OB3_Sub &bestOpp, int &oppTF,
  5167. double &rejBody, OB3_Sub &bestRej, int &rejTF)
  5168. {
  5169. int scan = MathMin(25, cnt);
  5170. double minBody = pip * 0.3;
  5171.  
  5172. for(int k = 0; k < scan; k++)
  5173. {
  5174. OB3_Sub sc = buf[k];
  5175. if(!sc.ok && sc.h - sc.l <= Point * 0.3) continue;
  5176.  
  5177. if(sc.h < obLo - tol || sc.l > obHi + tol) continue;
  5178.  
  5179. double rng = sc.h - sc.l;
  5180. if(rng <= 0) continue;
  5181. double body = MathAbs(sc.c - sc.o);
  5182. if(body < minBody) continue;
  5183.  
  5184. double upWick = sc.h - MathMax(sc.o, sc.c);
  5185. double dnWick = MathMin(sc.o, sc.c) - sc.l;
  5186. bool wantBull = (pDir == OB3_BEAR);
  5187. bool isBull = (sc.c > sc.o);
  5188.  
  5189. if(isBull == wantBull && body < oppBody)
  5190. {
  5191. oppBody = body;
  5192. bestOpp = sc;
  5193. oppTF = tf;
  5194. }
  5195.  
  5196. if((upWick > body * 1.2 || dnWick > body * 1.2) && body < rejBody)
  5197. {
  5198. rejBody = body;
  5199. bestRej = sc;
  5200. rejTF = tf;
  5201. }
  5202. }
  5203. }
  5204.  
  5205.  
  5206.  
  5207.  
  5208.  
  5209. void OB3_MicroScan()
  5210. {
  5211. if(g_O3c == 0) return;
  5212. double pip = GetUniversalPip();
  5213. if(pip <= 0) pip = Point * 10;
  5214.  
  5215. for(int i = 0; i < g_O3c; i++)
  5216. {
  5217. if(!g_O3[i].active) continue;
  5218.  
  5219. bool hasMicro = false;
  5220. for(int j = 0; j < g_M3c; j++)
  5221. {
  5222. if(g_M3[j].alive && g_M3[j].pIdx == i) { hasMicro = true; break; }
  5223. }
  5224. if(hasMicro) continue;
  5225.  
  5226. double tol = pip * 8;
  5227.  
  5228. double oppBody = 9999999;
  5229. double rejBody = 9999999;
  5230. OB3_Sub bestOppSub; bestOppSub.ok = false;
  5231. OB3_Sub bestRejSub; bestRejSub.ok = false;
  5232. int oppTF = 0, rejTF = 0;
  5233.  
  5234. OB3_ScanAll(g_s30_3, g_n30_3, g_O3[i].hi, g_O3[i].lo, g_O3[i].dir, 30, tol, pip, oppBody, bestOppSub, oppTF, rejBody, bestRejSub, rejTF);
  5235. OB3_ScanAll(g_s15_3, g_n15_3, g_O3[i].hi, g_O3[i].lo, g_O3[i].dir, 15, tol, pip, oppBody, bestOppSub, oppTF, rejBody, bestRejSub, rejTF);
  5236. OB3_ScanAll(g_s10_3, g_n10_3, g_O3[i].hi, g_O3[i].lo, g_O3[i].dir, 10, tol, pip, oppBody, bestOppSub, oppTF, rejBody, bestRejSub, rejTF);
  5237. OB3_ScanAll(g_s5_3, g_n5_3, g_O3[i].hi, g_O3[i].lo, g_O3[i].dir, 5, tol, pip, oppBody, bestOppSub, oppTF, rejBody, bestRejSub, rejTF);
  5238.  
  5239. double bestHi = 0, bestLo = 0, bestMid = 0;
  5240. datetime bestT = 0;
  5241. int bestTF_final = 0;
  5242. int bestType = -1;
  5243. bool found = false;
  5244.  
  5245. if(bestOppSub.ok)
  5246. {
  5247. // ✅ Opposite mili → White (chhoti ya badi, dono White)
  5248. bestHi = MathMin(bestOppSub.h, g_O3[i].hi); // ✅ CLIP to OB top
  5249. bestLo = MathMax(bestOppSub.l, g_O3[i].lo); // ✅ CLIP to OB bottom
  5250. bestMid = (bestHi + bestLo) * 0.5;
  5251. bestT = bestOppSub.t;
  5252. bestTF_final = oppTF;
  5253. bestType = 0; // White
  5254. found = true;
  5255. }
  5256. else if(bestRejSub.ok)
  5257. {
  5258. // ✅ Sirf rejection → Purple
  5259. bestHi = MathMin(bestRejSub.h, g_O3[i].hi); // ✅ CLIP to OB top
  5260. bestLo = MathMax(bestRejSub.l, g_O3[i].lo); // ✅ CLIP to OB bottom
  5261. bestMid = (bestHi + bestLo) * 0.5;
  5262. bestT = bestRejSub.t;
  5263. bestTF_final = rejTF;
  5264. bestType = 1; // Purple
  5265. found = true;
  5266. }
  5267.  
  5268. if(found && bestType >= 0 && g_M3c < MAX_MICRO3)
  5269. {
  5270. bool dup = false;
  5271. for(int d = 0; d < g_M3c; d++)
  5272. {
  5273. if(!g_M3[d].alive || g_M3[d].pIdx != i) continue;
  5274. if(MathAbs(g_M3[d].hi - bestHi) < pip*3 && MathAbs(g_M3[d].lo - bestLo) < pip*3)
  5275. { dup = true; break; }
  5276. }
  5277. if(!dup)
  5278. {
  5279. int m = g_M3c;
  5280. string uid = "SUB_" + IntegerToString((int)bestT) + "_P" + IntegerToString(i);
  5281. g_M3[m].hi = bestHi;
  5282. g_M3[m].lo = bestLo;
  5283. g_M3[m].mid = bestMid;
  5284. g_M3[m].t = bestT;
  5285. g_M3[m].dir = g_O3[i].dir;
  5286. g_M3[m].pIdx = i;
  5287. g_M3[m].tf = bestTF_final;
  5288. g_M3[m].mType = bestType;
  5289. g_M3[m].alive = true;
  5290. g_M3[m].sigDone = false;
  5291. g_M3[m].nm_r = "MOB3_R_" + uid;
  5292. g_M3[m].nm_l = "MOB3_L_" + uid;
  5293. g_M3c++;
  5294. }
  5295. continue;
  5296. }
  5297.  
  5298. // M1 FALLBACK
  5299. double m1OppBody = 9999999; int m1OppC = -1;
  5300. double m1RejBody = 9999999; int m1RejC = -1;
  5301.  
  5302. for(int c = 1; c <= 10; c++)
  5303. {
  5304. if(c >= Bars) break;
  5305. if(High[c] < g_O3[i].lo - tol || Low[c] > g_O3[i].hi + tol) continue;
  5306.  
  5307. double body = MathAbs(Close[c] - Open[c]);
  5308. double rng = High[c] - Low[c];
  5309. if(rng <= 0 || body < pip * 1.0) continue;
  5310.  
  5311. double upW = High[c] - MathMax(Open[c], Close[c]);
  5312. double dnW = MathMin(Open[c], Close[c]) - Low[c];
  5313. bool isOppBull = (Close[c] > Open[c]);
  5314. bool wantBull = (g_O3[i].dir == OB3_BEAR);
  5315.  
  5316. if(isOppBull == wantBull && body < m1OppBody)
  5317. { m1OppBody = body; m1OppC = c; }
  5318.  
  5319. if((upW > body * 1.2 || dnW > body * 1.2) && body < m1RejBody)
  5320. { m1RejBody = body; m1RejC = c; }
  5321. }
  5322.  
  5323. int pickC = -1; int pickType = -1;
  5324. if(m1OppC >= 0)
  5325. {
  5326. pickC = m1OppC;
  5327. pickType = 0; // ✅ White (opposite = White)
  5328. }
  5329. else if(m1RejC >= 0)
  5330. {
  5331. pickC = m1RejC;
  5332. pickType = 1; // Purple
  5333. }
  5334.  
  5335. if(pickC >= 0 && g_M3c < MAX_MICRO3)
  5336. {
  5337. double clipHi = MathMin(High[pickC], g_O3[i].hi); // ✅ CLIP to OB top
  5338. double clipLo = MathMax(Low[pickC], g_O3[i].lo); // ✅ CLIP to OB bottom
  5339.  
  5340. bool dup = false;
  5341. for(int d = 0; d < g_M3c; d++)
  5342. {
  5343. if(!g_M3[d].alive || g_M3[d].pIdx != i) continue;
  5344. if(MathAbs(g_M3[d].hi - clipHi) < pip*3 &&
  5345. MathAbs(g_M3[d].lo - clipLo) < pip*3)
  5346. { dup = true; break; }
  5347. }
  5348. if(!dup)
  5349. {
  5350. int m = g_M3c;
  5351. string uid = "M1_" + IntegerToString(pickC) + "_P" + IntegerToString(i);
  5352. g_M3[m].hi = clipHi;
  5353. g_M3[m].lo = clipLo;
  5354. g_M3[m].mid = (clipHi + clipLo) * 0.5;
  5355. g_M3[m].t = Time[pickC];
  5356. g_M3[m].dir = g_O3[i].dir;
  5357. g_M3[m].pIdx = i;
  5358. g_M3[m].tf = 60;
  5359. g_M3[m].mType = pickType;
  5360. g_M3[m].alive = true;
  5361. g_M3[m].sigDone = false;
  5362. g_M3[m].nm_r = "MOB3_R_" + uid;
  5363. g_M3[m].nm_l = "MOB3_L_" + uid;
  5364. g_M3c++;
  5365. }
  5366. }
  5367. }
  5368. }
  5369.  
  5370. void OB3_MicroDraw(){
  5371. double pip=GetUniversalPip(); if(pip<=0) pip=Point*10;
  5372. double cur=(Bid+Ask)*0.5;
  5373. datetime now=TimeCurrent();
  5374. int age=(int)(now-Time[0]);
  5375. bool inW=(age>=2&&age<=55);
  5376.  
  5377. for(int i=0;i<g_M3c;i++){
  5378. if(!g_M3[i].alive) continue;
  5379. int pi=g_M3[i].pIdx;
  5380.  
  5381. // Parent OB dead = micro bhi delete
  5382. if(pi>=0 && pi<g_O3c && !g_O3[pi].active){
  5383. g_M3[i].alive=false;
  5384. OB3_Del(g_M3[i].nm_r); OB3_Del(g_M3[i].nm_l);
  5385. continue;
  5386. }
  5387. // Price door chala gaya = delete
  5388. if(g_M3[i].dir==OB3_BULL && cur<g_M3[i].lo-pip*3){
  5389. g_M3[i].alive=false; OB3_Del(g_M3[i].nm_r); OB3_Del(g_M3[i].nm_l); continue;
  5390. }
  5391. if(g_M3[i].dir==OB3_BEAR && cur>g_M3[i].hi+pip*3){
  5392. g_M3[i].alive=false; OB3_Del(g_M3[i].nm_r); OB3_Del(g_M3[i].nm_l); continue;
  5393. }
  5394. // ✅ White=Chhoti opposite, Yellow=Badi opposite, Purple=Sirf rejection
  5395. color mc = clrWhite;
  5396. string typeLabel = "SMALL";
  5397. if(g_M3[i].mType==1){ mc = C'200,0,255'; typeLabel = "REJ"; }
  5398. if(g_M3[i].mType==2){ mc = C'255,80,180'; typeLabel = "BIG"; }
  5399.  
  5400.  
  5401. datetime tTo = Time[0] + PeriodSeconds(PERIOD_M1)*4;
  5402.  
  5403. OB3_Del(g_M3[i].nm_r);
  5404. ObjectCreate(0, g_M3[i].nm_r, OBJ_RECTANGLE, 0, g_M3[i].t, g_M3[i].hi, tTo, g_M3[i].lo);
  5405. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_COLOR, mc);
  5406. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_FILL, false);
  5407. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_WIDTH, 1);
  5408. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_STYLE, STYLE_DOT);
  5409. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_BACK, false);
  5410. ObjectSetInteger(0, g_M3[i].nm_r, OBJPROP_SELECTABLE, false);
  5411.  
  5412. // Label
  5413. string sig = "TRADE";
  5414. if(pi>=0 && pi<g_O3c) sig = (g_O3[pi].dir==OB3_BULL) ? "CALL" : "PUT";
  5415. string lb = sig + " [" + IntegerToString(g_M3[i].tf) + "s] " + typeLabel;
  5416. double lY = (g_M3[i].dir==OB3_BULL) ? g_M3[i].lo-pip*6 : g_M3[i].hi+pip*5;
  5417.  
  5418. OB3_Del(g_M3[i].nm_l);
  5419. ObjectCreate(0, g_M3[i].nm_l, OBJ_TEXT, 0, g_M3[i].t, lY);
  5420. ObjectSetString(0, g_M3[i].nm_l, OBJPROP_TEXT, lb);
  5421. ObjectSetInteger(0, g_M3[i].nm_l, OBJPROP_COLOR, mc);
  5422. ObjectSetInteger(0, g_M3[i].nm_l, OBJPROP_FONTSIZE, 8);
  5423. ObjectSetString(0, g_M3[i].nm_l, OBJPROP_FONT, "Arial Bold");
  5424. ObjectSetInteger(0, g_M3[i].nm_l, OBJPROP_SELECTABLE, false);
  5425.  
  5426. // Price ke andar hai + window mein hai = Signal
  5427. bool inZ = (cur>=g_M3[i].lo-pip && cur<=g_M3[i].hi+pip);
  5428. if(inZ && inW && !g_M3[i].sigDone){
  5429. g_M3[i].sigDone = true;
  5430. Comment("MICRO OB: " + sig + " NOW! " + IntegerToString(55-age) + "s LEFT");
  5431. }
  5432. }
  5433.  
  5434. // Dead micros compact karo
  5435. int w=0;
  5436. for(int i=0;i<g_M3c;i++){
  5437. if(g_M3[i].alive){
  5438. if(w!=i) g_M3[w]=g_M3[i];
  5439. w++;
  5440. }
  5441. }
  5442. g_M3c=w;
  5443. }
  5444.  
  5445.  
  5446. int OnCalculate(const int rates_total,const int prev_calculated,
  5447. const datetime &time[],const double &open[],
  5448. const double &high[],const double &low[],
  5449. const double &close[],const long &tick_volume[],
  5450. const long &volume[],const int &spread[]){
  5451.  
  5452. // ✅ FIX: firstRun flag add kiya - pehli baar load hone pe bhi chalega
  5453. static datetime lastCalcBar = 0;
  5454. static bool firstRun = true;
  5455.  
  5456. if(firstRun || Time[0] != lastCalcBar){
  5457. if(Bars < 30) return(rates_total); // ✅ FIX: 100 se 30 kiya
  5458.  
  5459. firstRun = false;
  5460. lastCalcBar = Time[0];
  5461.  
  5462. OB3_RunEngine();
  5463. CalcVisualTrapPro();
  5464. CalcHA();
  5465. UpdateHRN();
  5466. UpdateStableM5SR();
  5467. DetectCommonPoints();
  5468. DrawCommonPoints();
  5469. if(ENABLE_BB_PULLBACK) DetectBBPullback();
  5470. if(ENABLE_MTG) CalculateMTG();
  5471. CalculateQuantumEngine();
  5472. QuantumCheckResult();
  5473. CleanChartAndShiftSR();
  5474. DrawSwingTrendLines();
  5475.  
  5476. int cp=0;
  5477. double brain=BrainSc(cp);
  5478. double nb=NeuralBiasFast();
  5479. double mw=MicroWick();
  5480. bool bf=IsBrokerForce();
  5481. double mai=CalcAdvancedMicroAI();
  5482. int mts=CalcAdvancedMicroTrap();
  5483.  
  5484. CalcFibRejectionHunter();
  5485. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  5486. ScanSignals();
  5487. CheckBackgroundNextCandleSignals();
  5488. ScanBackgroundOBs();
  5489. FinalSignalEngine(brain,cp,mts);
  5490. BroadcastSignal();
  5491. UpdateTimer();
  5492. Draw();
  5493. DrawFibRejectionLine();
  5494. }
  5495.  
  5496.  
  5497.  
  5498.  
  5499.  
  5500.  
  5501. return(rates_total);
  5502. }
  5503.  
  5504.  
  5505. //+------------------------------------------------------------------+