1. //+------------------------------------------------------------------+
  2. //| AIBRAIN OTC v44.6 - COMPLETE FIXED (ALL FUNCTIONS ACTIVE) |
  3. //| Fixed: All filters active, All inputs working, No missing funcs |
  4. //+------------------------------------------------------------------+
  5. #property copyright "AIBRAIN ULTIMATE - KM RANA"
  6. #property version "44.6"
  7. #property strict
  8. #property indicator_chart_window
  9. #define Alert(x)
  10. #define PFX "AIB9_"
  11. #define VER "v44.6 FINAL"
  12. #define LB 18
  13. #define MAX_LEVELS 5
  14. #define MAX_REJ 50
  15. #define LOOKBACK 20
  16.  
  17. input string SPM_FILE = "KM RANA";
  18. input int HOLD_SEC = 60;
  19. input int POS_X = 4;
  20. input int POS_Y = 20;
  21. input int DASH_W = 650;
  22. input bool SHOW_TIMER = true;
  23. input bool SHOW_SR_LINES = true;
  24. input string ENTRY_MODE = "TIME";
  25. input int FLAG_MIN_SEC = 10;
  26. input int FLAG_MAX_SEC = 35;
  27. input int TIME_MIN_SEC = 10;
  28. input int TIME_MAX_SEC = 35;
  29. input int MIN_REMAINING_SEC = 15;
  30. input double REVERSAL_PCT = 30.0;
  31. input double WICK_PCT = 0.65;
  32. input int HRN_CONFIRM_BARS = 2;
  33. input bool HA_ALIGN_FILTER = false;
  34. input bool CONSENSUS_FILTER = false;
  35. input bool ENABLE_NOTIFY = true;
  36. input bool FAST_MODE = false;
  37. input int BROKER_SPREAD_THRESHOLD= 6;
  38. input double OTC_TRAP_WEIGHT = 15.0;
  39. input int CROWD_EXTREME_PCT = 70;
  40. input bool ENABLE_BROKER_KILLER = true;
  41. input bool ENABLE_SESSION_FILTER = true;
  42. input bool ENABLE_SPIKE_FILTER = true;
  43. input bool ENABLE_LAST_SEC_BLOCK = true;
  44. input bool ENABLE_RISK_CONTROL = true;
  45. input bool ENABLE_ENTRY_ZONE = true;
  46. input bool ML_ADAPTIVE = true;
  47. input bool VOLUME_PROFILE_TRAP = true;
  48. input bool MTF_CONFIRM = false;
  49. input bool ENABLE_TELEGRAM = false;
  50. input string TELEGRAM_TOKEN = "";
  51. input string TELEGRAM_CHAT_ID = "";
  52. input string TRADE_MODE = "NORMAL";
  53. input string STRATEGY_MODE = "TRAP_ONLY";
  54. input int STRATEGY_THRESHOLD = 75;
  55. input int MAX_LOSS_STREAK = 2;
  56. input double MIN_CONFIDENCE = 75.0;
  57. input bool ENABLE_RISK_LOCK = true;
  58. input double PATTERN_TOLERANCE_PIPS = 8.0;
  59. input int MIN_PATTERN_SEPARATION = 5;
  60. input bool SHOW_COMMON_POINTS = true;
  61. input double COMMON_POINT_TOL_PIPS = 15.0;
  62. input bool SHOW_WICK_REJECT_LINES = true;
  63. input int WICK_MIN_TOUCHES = 1;
  64. input double WICK_ZONE_PIPS = 15.0;
  65. input int WICK_EXPIRE_BARS = 60;
  66.  
  67. #define NEON_GREEN C'0,255,100'
  68. #define NEON_RED C'255,40,80'
  69. #define NEON_YELLOW C'255,220,0'
  70. #define NEON_ORANGE C'255,120,0'
  71. #define NEON_CYAN C'0,255,200'
  72. #define NEON_PURPLE C'200,0,255'
  73. #define NEON_PINK C'255,80,180'
  74. #define NEON_BLUE C'0,200,255'
  75. #define NEON_LIME C'80,255,80'
  76. #define NEON_GOLD C'255,180,0'
  77. #define DARK_BLUE_NEON C'0,100,255'
  78. #define NEON_WHITE C'220,230,255'
  79. #define CGR C'160,170,190'
  80. #define BG_DARK1 C'6,8,16'
  81. #define BG_DARK2 C'10,14,24'
  82. #define BG_DARK3 C'14,20,32'
  83. #define BG_DARK4 C'18,26,40'
  84.  
  85. struct HTFLevel{ double price; string timeframe; string type; datetime time; int strength; bool isRoundNumber; };
  86.  
  87. //+------------------------------------------------------------------+
  88. // GLOBAL VARIABLES
  89. //+------------------------------------------------------------------+
  90. string g_haM1="",g_haM5="",g_haBoth="";
  91. color g_haM1Color=clrGray,g_haM5Color=clrGray;
  92. static string st_sig="WAIT",st_cdl="WAIT";
  93. static double st_pct=50;
  94. static datetime st_t=0;
  95. static double NW[36];
  96. static double g_rsc=0,g_tfa=3;
  97. static datetime g_spm_t=0,g_last_bar=0;
  98. double ResLevels[MAX_LEVELS],SupLevels[MAX_LEVELS];
  99. int resCount=0,supCount=0;
  100. datetime lastSRBar=0;
  101. static double g_nearest_res=0,g_nearest_sup=0;
  102. static string g_brk_str="NO BRK";
  103. static color g_brk_color=CGR;
  104. struct RejLevel{double price;int touches;};
  105. static RejLevel g_rj[MAX_REJ];
  106. static int g_rj_cnt=0;
  107. static double g_hrn_price=0;
  108. static bool g_hrn_is_sup=true;
  109. static int g_hrn_brk_bars=0;
  110. static datetime g_hrn_scan_bar=0;
  111. static string g_hrn_str="--";
  112. static int g_otcCallPct=50,g_otcPutPct=50;
  113. static string g_pairNames[5],g_pairSigs[5];
  114. static double g_pairConfs[5];
  115. static int g_pairCount=0;
  116. static string g_copiedPair="";
  117. static string g_adv1="WAIT KAR",g_adv2="Signals mix",g_adv3="";
  118. static color g_advC=NEON_YELLOW;
  119. static double g_frozen_gProb=50,g_frozen_rProb=50;
  120. static string g_frozen_pAction="UNCERTAIN";
  121. static color g_frozen_pColor=NEON_YELLOW;
  122. static double g_calc_gProb=50,g_calc_rProb=50;
  123. static string g_calc_pAction="UNCERTAIN";
  124. static color g_calc_pColor=NEON_YELLOW;
  125. static double g_gProb=50,g_rProb=50;
  126. static string g_pAction="UNCERTAIN";
  127. static color g_pColor=NEON_YELLOW;
  128. static datetime g_last_freeze_bar=0;
  129. static double g_accuracy=65.0;
  130. static int g_acc_correct=0,g_acc_total=0;
  131. static datetime g_acc_lastBar=0;
  132. static double g_lastPredGreen=50.0;
  133. static datetime g_lastPredBar=0;
  134. static datetime g_lastLogBar=0;
  135. static string g_finalSignal="WAIT";
  136. static color g_finalColor=NEON_YELLOW;
  137. static datetime g_signalTime=0;
  138. static string g_lastNotified="";
  139. static string g_marketMode="RANGE",g_prevMode="";
  140. static int g_lossStreak=0;
  141. static bool g_tradingStopped=false;
  142. static datetime g_stopStartTime=0;
  143. static double g_brokerBias=0.0;
  144. static string g_symbolKey="";
  145. static string g_strategyType="NONE";
  146. static string g_strategySignal="WAIT";
  147. static string g_strategyReason="";
  148. static double qmrA=0,qmrB=0,qmrC=0,qmrD=0;
  149. static datetime qmrTA=0,qmrTB=0,qmrTC=0,qmrTD=0;
  150. static bool qmrActive=false;
  151. static datetime qmrExpire=0;
  152. #define MAX_COMMON_PTS 5
  153. static double g_commonPrice[MAX_COMMON_PTS];
  154. static datetime g_commonTime[MAX_COMMON_PTS];
  155. static int g_commonCount=0;
  156. static datetime g_commonExpire[MAX_COMMON_PTS];
  157. static string g_commonType[MAX_COMMON_PTS];
  158. static datetime g_lastCommonScan=0;
  159. #define MAX_WICK_LINES 5
  160. static double g_wickLinePrice[MAX_WICK_LINES];
  161. static int g_wickLineTouches[MAX_WICK_LINES];
  162. static datetime g_wickLineExpire[MAX_WICK_LINES];
  163. static int g_wickLineCount=0;
  164. static datetime g_lastWickScan=0;
  165. static bool g_isSideways=false;
  166. static string g_tfa_detail="";
  167. static HTFLevel g_htfLevels[50];
  168. static int g_htfLevelCount=0;
  169. static datetime g_lastLevelScan=0;
  170. static bool g_mtfConfirmed=true;
  171. static string g_filterStatus="ALL CLEAR";
  172.  
  173. //+------------------------------------------------------------------+
  174. // HA CANDLE CALCULATION
  175. //+------------------------------------------------------------------+
  176. void CalcHA(){
  177. if(Bars<10) return;
  178. ArraySetAsSeries(Open,true); ArraySetAsSeries(High,true); ArraySetAsSeries(Low,true); ArraySetAsSeries(Close,true);
  179. static double haO1[500],haC1[500]; static datetime lb1=0;
  180. ArraySetAsSeries(haO1,true); ArraySetAsSeries(haC1,true);
  181. datetime c1=iTime(NULL,PERIOD_M1,0);
  182. if(c1!=lb1){ lb1=c1; int lim=MathMin(iBars(NULL,PERIOD_M1),500);
  183. for(int i=lim-1;i>=0;i--){ haC1[i]=(Open[i]+High[i]+Low[i]+Close[i])/4.0;
  184. if(i==lim-1) haO1[i]=(Open[i]+Close[i])/2.0; else haO1[i]=(haO1[i+1]+haC1[i+1])/2.0; }}
  185. double d1=MathAbs(haC1[1]-haO1[1]),r1=High[1]-Low[1];
  186. bool dz=(r1>0&&d1<r1*0.1),b1=(!dz&&haC1[1]>haO1[1]),be1=(!dz&&haC1[1]<haO1[1]);
  187. if(b1){g_haM1="HA BULLISH 1";g_haM1Color=clrLime;}
  188. else if(be1){g_haM1="HA BEARISH 1";g_haM1Color=clrRed;}
  189. else{g_haM1="HA DOJI 1";g_haM1Color=clrYellow;}
  190.  
  191. if(iBars(NULL,PERIOD_M5)>=10){
  192. static double haO5[200],haC5[200]; static datetime lb5=0;
  193. ArraySetAsSeries(haO5,true); ArraySetAsSeries(haC5,true);
  194. datetime c5=iTime(NULL,PERIOD_M5,0);
  195. if(c5!=lb5){ lb5=c5; int lim=MathMin(iBars(NULL,PERIOD_M5),200);
  196. for(int i=lim-1;i>=0;i--){ double o=iOpen(NULL,PERIOD_M5,i),h=iHigh(NULL,PERIOD_M5,i),l=iLow(NULL,PERIOD_M5,i),c=iClose(NULL,PERIOD_M5,i);
  197. haC5[i]=(o+h+l+c)/4.0; if(i==lim-1) haO5[i]=(o+c)/2.0; else haO5[i]=(haO5[i+1]+haC5[i+1])/2.0; }}
  198. double d5=MathAbs(haC5[1]-haO5[1]),r5=iHigh(NULL,PERIOD_M5,1)-iLow(NULL,PERIOD_M5,1);
  199. bool dz5=(r5>0&&d5<r5*0.1),b5=(!dz5&&haC5[1]>haO5[1]),be5=(!dz5&&haC5[1]<haO5[1]);
  200. if(b5){g_haM5="HA BULLISH 5";g_haM5Color=clrLime;}
  201. else if(be5){g_haM5="HA BEARISH 5";g_haM5Color=clrRed;}
  202. else{g_haM5="HA DOJI 5";g_haM5Color=clrYellow;}
  203. if(b1&&b5)g_haBoth="BOTH BULL"; else if(be1&&be5)g_haBoth="BOTH BEAR"; else g_haBoth="MIXED";
  204. }else{g_haM5="5 --";g_haM5Color=clrGray;g_haBoth="HA --";}
  205. }
  206.  
  207. //+------------------------------------------------------------------+
  208. // HELPER FUNCTIONS
  209. //+------------------------------------------------------------------+
  210. bool IsLineNearby(double price,double pipTol=3.0){
  211. bool jpy=(StringFind(Symbol(),"JPY")>=0); double pip=jpy?0.01:0.0001; double tol=pipTol*pip;
  212. for(int i=ObjectsTotal()-1;i>=0;i--){ string nm=ObjectName(i);
  213. if((int)ObjectGetInteger(0,nm,OBJPROP_TYPE)==OBJ_HLINE){
  214. double lp=ObjectGetDouble(0,nm,OBJPROP_PRICE); if(MathAbs(lp-price)<tol) return true; }}
  215. return false;
  216. }
  217.  
  218. string GetCurrentSession(color &sC){
  219. int h=TimeHour(TimeGMT());
  220. if(h>=12&&h<16){sC=NEON_GOLD;return "LON+NY";}
  221. if(h>=7&&h<9){sC=NEON_CYAN;return "ASI+LON";}
  222. if(h>=7&&h<16){sC=NEON_GREEN;return "LONDON";}
  223. if(h>=12&&h<21){sC=NEON_BLUE;return "NEW YORK";}
  224. if(h>=0&&h<9){sC=NEON_ORANGE;return "ASIA";}
  225. sC=C'100,100,120'; return "SYDNEY";
  226. }
  227.  
  228. bool IsSessionActive(){int h=TimeHour(TimeGMT());return(h>=7&&h<=21);}
  229.  
  230. 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);}
  231.  
  232. //+------------------------------------------------------------------+
  233. // MTF CONFIRMATION - NOW ACTIVE
  234. //+------------------------------------------------------------------+
  235. bool CheckMTFConfirmation(){
  236. if(!MTF_CONFIRM) return true;
  237.  
  238. bool m1Bull=(Close[1]>Open[1]);
  239. bool m5Bull=false,m15Bull=false;
  240.  
  241. if(iBars(NULL,PERIOD_M5)>=3){
  242. m5Bull=(iClose(NULL,PERIOD_M5,1)>iOpen(NULL,PERIOD_M5,1));
  243. }
  244. if(iBars(NULL,PERIOD_M15)>=3){
  245. m15Bull=(iClose(NULL,PERIOD_M15,1)>iOpen(NULL,PERIOD_M15,1));
  246. }
  247.  
  248. int bullCount=0,bearCount=0;
  249. if(m1Bull) bullCount++; else bearCount++;
  250. if(m5Bull) bullCount++; else bearCount++;
  251. if(m15Bull) bullCount++; else bearCount++;
  252.  
  253. g_mtfConfirmed=(bullCount>=2 || bearCount>=2);
  254. return g_mtfConfirmed;
  255. }
  256.  
  257. //+------------------------------------------------------------------+
  258. // CUSTOM TANH FOR MQL4
  259. //+------------------------------------------------------------------+
  260. double Tanh(double x){
  261. double exp2x = MathExp(2.0 * x);
  262. return (exp2x - 1.0) / (exp2x + 1.0);
  263. }
  264.  
  265. //+------------------------------------------------------------------+
  266. // FRACTAL DIMENSION
  267. //+------------------------------------------------------------------+
  268. double CalcFractalDimension(int period){
  269. if(period < 2) return 1.5;
  270. double sum = 0.0;
  271. for(int i = 1; i < period; i++){
  272. double range1 = High[i] - Low[i];
  273. double range2 = High[i+1] - Low[i+1];
  274. if(range1 <= 0.0 || range2 <= 0.0) continue;
  275. sum += MathLog(range1 / range2 + 0.001) * MathLog((double)i / (double)period);
  276. }
  277. double result = 2.0 - sum / period;
  278. if(result < 1.0) result = 1.0;
  279. if(result > 2.0) result = 2.0;
  280. return result;
  281. }
  282.  
  283. //+------------------------------------------------------------------+
  284. // BROKER TRAP DETECTION
  285. //+------------------------------------------------------------------+
  286. double DetectBrokerTrap(int lookback){
  287. int bull = 0, bear = 0;
  288. for(int i = 1; i <= lookback; i++){
  289. if(Close[i] > Open[i]) bull++;
  290. else if(Close[i] < Open[i]) bear++;
  291. }
  292. double crowd = (bull > bear) ? (double)bull / lookback : (double)bear / lookback;
  293. double priceMove = MathAbs(Close[1] - Close[lookback]) / (Close[lookback] + 0.00001);
  294. if(crowd > 0.7 && priceMove < 0.002) return 0.9;
  295. if(crowd > 0.6 && priceMove > 0.001) return 0.5;
  296. return 0.3;
  297. }
  298.  
  299. //+------------------------------------------------------------------+
  300. // QUICK MEDIAN
  301. //+------------------------------------------------------------------+
  302. double QuickMedian(double &arr[], int size){
  303. double sorted[20];
  304. for(int i = 0; i < size; i++) sorted[i] = arr[i];
  305. for(int i = 0; i < size-1; i++)
  306. for(int j = i+1; j < size; j++)
  307. if(sorted[i] > sorted[j]){
  308. double tmp = sorted[i];
  309. sorted[i] = sorted[j];
  310. sorted[j] = tmp;
  311. }
  312. return (sorted[size/2] + sorted[(size-1)/2]) / 2.0;
  313. }
  314.  
  315. //+------------------------------------------------------------------+
  316. // MAIN ADVANCED TFA (MQL4 COMPATIBLE)
  317. //+------------------------------------------------------------------+
  318. double CalcAdvancedTFA(string &detail){
  319. if(Bars < 50){
  320. detail = "Low bars";
  321. return 3.0;
  322. }
  323.  
  324. static double prevScore = 3.0;
  325. static double kalmanState = 3.0, kalmanCovar = 1.0;
  326. static datetime lastBarTime = 0;
  327. static double scoreHistory[15];
  328. static int histIdx = 0;
  329.  
  330. if(scoreHistory[0] == 0.0){
  331. for(int i = 0; i < 15; i++) scoreHistory[i] = 3.0;
  332. }
  333.  
  334. if(Time[0] == lastBarTime){
  335. detail = "Smooth";
  336. return prevScore;
  337. }
  338. lastBarTime = Time[0];
  339.  
  340. double comp[8], w[8];
  341.  
  342. // 1. ADX + Fractal
  343. double adx = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  344. double fd = CalcFractalDimension(10);
  345. comp[0] = 1.0 / (1.0 + MathExp(-(adx * fd - 18.0) / 6.0));
  346. w[0] = 0.14 + fd * 0.06;
  347.  
  348. // 2. Volume
  349. double v1 = iVolume(NULL, PERIOD_M1, 1);
  350. double vA = 0;
  351. for(int i = 2; i <= 15; i++) vA += iVolume(NULL, PERIOD_M1, i);
  352. vA = (vA > 0) ? vA / 14.0 : 1.0;
  353. double vr = MathMin(v1 / vA, 5.0);
  354. double volSpike = Tanh((vr - 1.2) * 0.8);
  355. comp[1] = (volSpike + 1.0) / 2.0;
  356. w[1] = 0.12 + (vr > 2.0 ? 0.08 : 0.0);
  357.  
  358. // 3. RSI + Stochastic
  359. double rsi = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  360. double stochK = iStochastic(NULL, PERIOD_M1, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
  361. double rsiDev = MathAbs(rsi - 50.0) / 50.0;
  362. double stochDev = MathAbs(stochK - 50.0) / 50.0;
  363. comp[2] = MathMin(1.0, (rsiDev + stochDev) * 1.2);
  364. w[2] = 0.10 + rsiDev * 0.1;
  365.  
  366. // 4. ATR + Volatility Regime
  367. double atr = iATR(NULL, PERIOD_M1, 14, 1);
  368. double atr14ago = iATR(NULL, PERIOD_M1, 14, 15);
  369. double volReg = (atr14ago > 0) ? atr / atr14ago : 1.0;
  370. double cs = High[1] - Low[1];
  371. double atrRatio = cs / (atr + 0.00001);
  372. double atrNorm = Tanh((atrRatio * volReg - 1.2) * 1.5);
  373. comp[3] = (atrNorm + 1.0) / 2.0;
  374. w[3] = 0.11 * MathMin(1.5, volReg);
  375.  
  376. // 5. Body + Shadow
  377. double body = MathAbs(Close[1] - Open[1]);
  378. double range = High[1] - Low[1];
  379. double upperShadow = High[1] - MathMax(Open[1], Close[1]);
  380. double lowerShadow = MathMin(Open[1], Close[1]) - Low[1];
  381. double bodyRatio = (range > 0) ? body / range : 0;
  382. double shadowPenalty = 1.0 - (upperShadow + lowerShadow) / (range + 0.00001) * 0.5;
  383. comp[4] = MathPow(bodyRatio, 0.7) * shadowPenalty;
  384. w[4] = 0.11;
  385.  
  386. // 6. Session
  387. comp[5] = IsSessionActive() ? 0.85 : 0.4;
  388. w[5] = 0.08;
  389.  
  390. // 7. Broker Trap + MicroWick
  391. double trap = DetectBrokerTrap(20);
  392. double mw = MicroWick(); // existing function
  393. if(mw > 0.65) trap = MathMax(trap, 0.7);
  394. comp[6] = trap;
  395. w[6] = 0.14 * (0.5 + trap);
  396.  
  397. // 8. MACD Divergence
  398. double macdMain = iMACD(NULL, PERIOD_M1, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);
  399. double macdSig = iMACD(NULL, PERIOD_M1, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 1);
  400. double priceChange = (Close[1] - Close[5]) / (Close[5] + 0.00001);
  401. double divScore = MathAbs(macdMain - macdSig) * MathAbs(priceChange) * 100;
  402. comp[7] = MathMin(1.0, divScore);
  403. w[7] = 0.07;
  404.  
  405. // Normalize weights
  406. double wSum = 0;
  407. for(int i = 0; i < 8; i++) wSum += w[i];
  408. if(wSum > 0) for(int i = 0; i < 8; i++) w[i] /= wSum;
  409.  
  410. // Directional scoring
  411. double call = 0, put = 0;
  412. double plusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  413. double minusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  414. double diBias = Tanh((plusDI - minusDI) / 25.0);
  415.  
  416. for(int i = 0; i < 8; i++){
  417. double contrib = comp[i] * w[i];
  418. if(i == 6){
  419. if(comp[6] > 0.65) contrib *= -diBias;
  420. else contrib *= diBias;
  421. } else {
  422. contrib *= diBias;
  423. }
  424. if(contrib > 0) call += contrib;
  425. else put += -contrib;
  426. }
  427.  
  428. // Heiken Ashi
  429. double haClose = (Open[1] + High[1] + Low[1] + Close[1]) / 4.0;
  430. double haOpen = (Open[2] + Close[2]) / 2.0;
  431. double haChange = (haClose - haOpen) / (High[1] - Low[1] + 0.0001);
  432. double haBias = Tanh(haChange * 2.5);
  433. if(haBias > 0) call += 0.1 * haBias;
  434. else put += 0.1 * (-haBias);
  435.  
  436. double rawScore = MathMax(call, put) * 6.0;
  437. rawScore = MathMin(6.0, MathMax(0.0, rawScore));
  438.  
  439. // Kalman filter
  440. double R = 0.5 + (atr / Close[0]) * 200;
  441. double Q = 0.05 * (1.0 + MathAbs(diBias) * 0.5);
  442. double predState = kalmanState;
  443. double predCovar = kalmanCovar + Q;
  444. double K = predCovar / (predCovar + R + 0.00001);
  445. double kalmanScore = predState + K * (rawScore - predState);
  446. kalmanCovar = (1 - K) * predCovar;
  447. kalmanState = kalmanScore;
  448.  
  449. // Median filter
  450. scoreHistory[histIdx] = kalmanScore;
  451. histIdx = (histIdx + 1) % 15;
  452. double medianScore = QuickMedian(scoreHistory, 15);
  453.  
  454. double finalScore = 0.65 * kalmanScore + 0.25 * medianScore + 0.10 * rawScore;
  455. finalScore = MathMax(0.5, MathMin(6.0, finalScore));
  456.  
  457. bool isCall = (call > put);
  458.  
  459. detail = StringFormat("ADX:%.2f VOL:%.2f RSI:%.2f ATR:%.2f BODY:%.2f TRAP:%.2f %s %.1f/6",
  460. comp[0], comp[1], comp[2], comp[3], comp[4], comp[6], (isCall ? "CALL" : "PUT"), finalScore);
  461. g_tfa_detail = detail;
  462. prevScore = finalScore;
  463. return finalScore;
  464. }
  465.  
  466. //+------------------------------------------------------------------+
  467. // MICRO AI
  468. //+------------------------------------------------------------------+
  469. double CalcAdvancedMicroAI(){
  470. if(Bars<20) return 50.0; double score=50.0;
  471. for(int i=1;i<=5;i++){double r=High[i]-Low[i];if(r<=0)continue;
  472. double uw=(High[i]-MathMax(Open[i],Close[i]))/r,lw=(MathMin(Open[i],Close[i])-Low[i])/r,w=(6.0-i)/5.0;
  473. if(uw>0.60)score-=w*15; if(lw>0.60)score+=w*15;}
  474. double pos5=0; for(int i=1;i<=5;i++){double r=High[i]-Low[i];if(r<=0)continue;pos5+=(Close[i]-Low[i])/r;}
  475. score+=(pos5/5.0-0.5)*20.0;
  476. int gc=0,rc=0; for(int i=1;i<=8;i++){if(Close[i]>Open[i])gc++;else if(Close[i]<Open[i])rc++;} score+=(gc-rc)*2.5;
  477. double vl1=(double)iVolume(NULL,PERIOD_M1,1),vA=0;
  478. for(int i=2;i<=6;i++)vA+=iVolume(NULL,PERIOD_M1,i);vA/=5.0;
  479. if(vA>0){double vr=vl1/vA;if(vr>1.5&&Close[1]>Open[1])score+=8;if(vr>1.5&&Close[1]<Open[1])score-=8;}
  480. double r1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),r5=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,5);
  481. if(r1<35)score+=10;if(r1>65)score-=10;if(r1>r5&&Close[1]<Close[5])score+=6;if(r1<r5&&Close[1]>Close[5])score-=6;
  482. return MathMax(5.0,MathMin(95.0,score));
  483. }
  484.  
  485. //+------------------------------------------------------------------+
  486. // MICRO TRAP
  487. //+------------------------------------------------------------------+
  488. double VolumeTrapScore();
  489. int CalcAdvancedMicroTrap(){
  490. if(Bars<20)return 0;int score=0;double r=High[1]-Low[1];
  491. if(r>0){double uw=(High[1]-MathMax(Open[1],Close[1]))/r,lw=(MathMin(Open[1],Close[1])-Low[1])/r;
  492. if(uw>0.65)score+=30;else if(uw>0.50)score+=15;if(lw>0.65)score+=20;else if(lw>0.50)score+=10;}
  493. double v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  494. if(v2>0&&v1/v2>2.0)score+=25;else if(v2>0&&v1/v2>1.5)score+=12;
  495. double rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);
  496. if(rsi>78||rsi<22)score+=20;else if(rsi>72||rsi<28)score+=10;
  497. if((High[1]>g_nearest_res&&Close[1]<g_nearest_res)||(Low[1]<g_nearest_sup&&Close[1]>g_nearest_sup))score+=25;
  498. bool aG=(Close[1]>Open[1]&&Close[2]>Open[2]&&Close[3]>Open[3]),aR=(Close[1]<Open[1]&&Close[2]<Open[2]&&Close[3]<Open[3]);
  499. if(aG||aR)score+=15;
  500. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);if(sp>BROKER_SPREAD_THRESHOLD*1.5)score+=10;
  501. score+=(int)VolumeTrapScore();return MathMin(100,score);
  502. }
  503.  
  504. //+------------------------------------------------------------------+
  505. // FRACTAL
  506. //+------------------------------------------------------------------+
  507. double CalcAdvancedFractal(){
  508. if(Bars<15)return 0;double score=0;
  509. for(int i=2;i<=8&&i+2<Bars;i++){
  510. 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;}
  511. 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;}}
  512. 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++;}
  513. if(dj>=3)score+=25;
  514. 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;}
  515. return MathMin(100,score);
  516. }
  517.  
  518. //+------------------------------------------------------------------+
  519. // MARKET BIAS
  520. //+------------------------------------------------------------------+
  521. string CalcAdvancedMarketBias(color &bC,double brain,double rsc){
  522. if(Bars<30){bC=NEON_YELLOW;return "CALCULATING";}
  523. double score=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  524. double pc=Close[0]-Close[10];
  525. 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;
  526. double hh1=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,1)],hh2=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,5,6)];
  527. double ll1=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,1)],ll2=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,5,6)];
  528. 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;
  529. double ema20=0,ema20p=0;for(int i=0;i<20;i++)ema20+=Close[i];ema20/=20;
  530. for(int i=1;i<=20&&i<Bars;i++)ema20p+=Close[i];ema20p/=20;
  531. if(ema20>ema20p)score+=3;else if(ema20<ema20p)score-=3;
  532. double ema50=0;for(int i=0;i<50&&i<Bars;i++)ema50+=Close[i];ema50/=MathMin(50,Bars);
  533. if(ema20>ema50)score+=3;else score-=3;
  534. 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++;}
  535. if(gC>=7)score+=3;else if(rC>=7)score-=3;else if(gC>=5)score+=1;else if(rC>=5)score-=1;
  536. 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");
  537. if(m1B&&m5B)score+=4;else if(m1Be&&m5Be)score-=4;else if(m1B)score+=2;else if(m1Be)score-=2;
  538. 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);
  539. if(adx>20){if(pDI>mDI+5)score+=4;else if(mDI>pDI+5)score-=4;}
  540. if(brain>3)score-=2;else if(brain<-3)score+=2;
  541. if(Close[1]>Open[1])score+=1;else if(Close[1]<Open[1])score-=1;
  542. if(rsc>2)score+=1;else if(rsc<-2)score-=1;
  543.  
  544. if(score>=12){bC=NEON_GREEN;return "STRONG BULL";}
  545. if(score>=7){bC=NEON_GREEN;return "BULLISH";}
  546. if(score>=3){bC=NEON_CYAN;return "LIGHT BULL";}
  547. if(score<=-12){bC=NEON_RED;return "STRONG BEAR";}
  548. if(score<=-7){bC=NEON_RED;return "BEARISH";}
  549. if(score<=-3){bC=NEON_ORANGE;return "LIGHT BEAR";}
  550. bC=NEON_YELLOW;return "NEUTRAL";
  551. }
  552.  
  553. //+------------------------------------------------------------------+
  554. // ULTIMATE NEXT CANDLE PREDICTION (Exact HA + Crowd Trap + Synced)
  555. //+------------------------------------------------------------------+
  556. void CalcAdvancedPrediction(double &gP, double &rP, string &reason, color &pC){
  557. if(Bars < 50){
  558. gP = 50; rP = 50; reason = "Bars low"; pC = NEON_YELLOW;
  559. return;
  560. }
  561.  
  562. double green = 0, red = 0;
  563.  
  564. // 1. MOMENTUM TRAP (Weight 20)
  565. double body1 = MathAbs(Close[1] - Open[1]);
  566. double range1 = High[1] - Low[1];
  567. double wick_ratio = (range1 > 0) ? (range1 - body1) / range1 : 0;
  568. if(wick_ratio > 0.70 && Close[1] > Open[1]) red += 20;
  569. else if(wick_ratio > 0.70 && Close[1] < Open[1]) green += 20;
  570. else if(wick_ratio > 0.55){
  571. if(Close[1] > Open[1]) red += 10;
  572. else green += 10;
  573. }
  574.  
  575. // 2. RSI + CCI COMBO (Weight 15)
  576. double rsi = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 1);
  577. double cci = iCCI(NULL, PERIOD_M1, 14, PRICE_TYPICAL, 1);
  578. if(rsi < 30 && cci < -120) green += 15;
  579. else if(rsi > 70 && cci > 120) red += 15;
  580. else if(rsi < 40 && cci < -80) green += 8;
  581. else if(rsi > 60 && cci > 80) red += 8;
  582.  
  583. // RSI Divergence
  584. double rsiPrev = iRSI(NULL, PERIOD_M1, 14, PRICE_CLOSE, 2);
  585. double low1 = Low[iLowest(NULL, PERIOD_M1, MODE_LOW, 5, 1)];
  586. double low2 = Low[iLowest(NULL, PERIOD_M1, MODE_LOW, 5, 6)];
  587. if(low1 < low2 && rsi > rsiPrev) green += 8;
  588. if(low1 > low2 && rsi < rsiPrev) red += 8;
  589.  
  590. // 3. VOLUME SPIKE + BROKER REVERSE (Weight 18)
  591. double v1 = iVolume(NULL, PERIOD_M1, 1);
  592. double vAvg = 0;
  593. for(int i=2; i<=10; i++) vAvg += iVolume(NULL, PERIOD_M1, i);
  594. vAvg = (vAvg > 0) ? vAvg / 9.0 : 1.0;
  595. double vol_ratio = (vAvg > 0) ? v1 / vAvg : 1.0;
  596. if(vol_ratio > 2.0){
  597. if(Close[1] > Open[1]) red += 18;
  598. else green += 18;
  599. } else if(vol_ratio > 1.5){
  600. if(Close[1] > Open[1]) red += 10;
  601. else green += 10;
  602. } else if(vol_ratio > 1.2){
  603. if(Close[1] > Open[1]) green += 5;
  604. else red += 5;
  605. }
  606.  
  607. // 4. 3-CANDLE PATTERNS (Weight 12)
  608. bool hammer = (Low[1] == Low[iLowest(NULL, PERIOD_M1, MODE_LOW, 3, 1)] && Close[1] > Open[1]);
  609. bool shooting_star = (High[1] == High[iHighest(NULL, PERIOD_M1, MODE_HIGH, 3, 1)] && Close[1] < Open[1]);
  610. if(hammer) green += 12;
  611. if(shooting_star) red += 12;
  612.  
  613. // 5. ADX + DI (Weight 12)
  614. double adx = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MAIN, 1);
  615. double plusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
  616. double minusDI = iADX(NULL, PERIOD_M1, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
  617. if(adx > 25){
  618. if(plusDI > minusDI + 5) green += 12;
  619. else if(minusDI > plusDI + 5) red += 12;
  620. else if(plusDI > minusDI) green += 6;
  621. else red += 6;
  622. } else {
  623. if(plusDI > minusDI) green += 4;
  624. else red += 4;
  625. }
  626.  
  627. // 6. HEIKEN ASHI EXACT M1 + M5 (Weight 10)
  628. double m1_haC1 = (Open[1]+High[1]+Low[1]+Close[1])/4.0;
  629. double m1_haC2 = (Open[2]+High[2]+Low[2]+Close[2])/4.0;
  630. double m1_haO2_apx = (Open[3]+Close[3])/2.0;
  631. double m1_haO1 = (m1_haO2_apx + m1_haC2)/2.0;
  632. bool haBull1 = (m1_haC1 > m1_haO1);
  633.  
  634. double m5_o1 = iOpen(NULL,PERIOD_M5,1);
  635. double m5_h1 = iHigh(NULL,PERIOD_M5,1);
  636. double m5_l1 = iLow(NULL,PERIOD_M5,1);
  637. double m5_c1 = iClose(NULL,PERIOD_M5,1);
  638. double m5_o2 = iOpen(NULL,PERIOD_M5,2);
  639. double m5_h2 = iHigh(NULL,PERIOD_M5,2);
  640. double m5_l2 = iLow(NULL,PERIOD_M5,2);
  641. double m5_c2 = iClose(NULL,PERIOD_M5,2);
  642. double m5_o3 = iOpen(NULL,PERIOD_M5,3);
  643. double m5_c3 = iClose(NULL,PERIOD_M5,3);
  644. double m5_haC1 = (m5_o1+m5_h1+m5_l1+m5_c1)/4.0;
  645. double m5_haC2 = (m5_o2+m5_h2+m5_l2+m5_c2)/4.0;
  646. double m5_haO2_apx = (m5_o3+m5_c3)/2.0;
  647. double m5_haO1 = (m5_haO2_apx + m5_haC2)/2.0;
  648. bool haBull5 = (m5_haC1 > m5_haO1);
  649.  
  650. if(haBull1 && haBull5) green += 10;
  651. else if(!haBull1 && !haBull5) red += 10;
  652. else if(haBull1) green += 5;
  653. else if(!haBull1) red += 5;
  654.  
  655. // 7. QUICK S/R + GLOBAL LEVELS (Weight 8+5)
  656. bool jpy = (StringFind(Symbol(), "JPY") >= 0);
  657. double pip = jpy ? 0.01 : 0.0001;
  658. double sup = Low[iLowest(NULL, PERIOD_M1, MODE_LOW, 20, 2)];
  659. double res = High[iHighest(NULL, PERIOD_M1, MODE_HIGH, 20, 2)];
  660. if(Close[1] > res - 3*pip) red += 8;
  661. if(Close[1] < sup + 3*pip) green += 8;
  662. if(g_nearest_res > 0 && (g_nearest_res - Close[0]) < 6*pip) red += 5;
  663. if(g_nearest_sup > 0 && (Close[0] - g_nearest_sup) < 6*pip) green += 5;
  664.  
  665. // 8. STOCHASTIC + MACD (Weight 5)
  666. double stochK = iStochastic(NULL, PERIOD_M1, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
  667. if(stochK < 20) green += 5;
  668. else if(stochK > 80) red += 5;
  669. double macdMain = iMACD(NULL, PERIOD_M1, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);
  670. double macdSig = iMACD(NULL, PERIOD_M1, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 1);
  671. if(macdMain > macdSig) green += 3;
  672. else if(macdMain < macdSig) red += 3;
  673.  
  674. // 9. OTC CROWD TRAP (Weight 15)
  675. if(g_otcCallPct >= 70 && !haBull1) red += 15;
  676. else if(g_otcPutPct >= 70 && haBull1) green += 15;
  677.  
  678. // 10. TFA SYNC (Weight 5)
  679. string tfaDir = "";
  680. double tfaScore = CalcAdvancedTFA(tfaDir);
  681. if(tfaScore >= 4.5){
  682. if(StringFind(tfaDir, "CALL") >= 0) green += 5;
  683. else if(StringFind(tfaDir, "PUT") >= 0) red += 5;
  684. }
  685.  
  686. // FINAL SCORE
  687. double diff = MathMax(-100.0, MathMin(100.0, green - red));
  688. gP = MathMax(8.0, MathMin(92.0, 50.0 + diff * 0.7));
  689. rP = 100.0 - gP;
  690. double dom = MathMax(gP, rP);
  691. if(dom >= 85){
  692. reason = (gP > rP) ? "STRONG GREEN" : "STRONG RED";
  693. pC = (gP > rP) ? NEON_GREEN : NEON_RED;
  694. } else if(dom >= 75){
  695. reason = (gP > rP) ? "GREEN" : "RED";
  696. pC = (gP > rP) ? NEON_GREEN : NEON_RED;
  697. } else if(dom >= 62){
  698. reason = (gP > rP) ? "WEAK GREEN" : "WEAK RED";
  699. pC = (gP > rP) ? NEON_CYAN : NEON_ORANGE;
  700. } else {
  701. reason = "WAIT";
  702. pC = NEON_YELLOW;
  703. }
  704. }
  705.  
  706. //+------------------------------------------------------------------+
  707. // STRATEGY DETECTION
  708. //+------------------------------------------------------------------+
  709. string DetectMyStrategy(){
  710. int cc=g_otcCallPct,cp=g_otcPutPct,thr=STRATEGY_THRESHOLD;
  711. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TRAP_ONLY"){
  712. if(cc>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TRAP";g_strategySignal="PUT";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA RED";return "PUT";}
  713. if(cp>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TRAP";g_strategySignal="CALL";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA GREEN";return "CALL";}}
  714. if(STRATEGY_MODE=="BOTH"||STRATEGY_MODE=="TREND_ONLY"){
  715. if(cc>=thr&&g_haM1=="HA BULLISH 1"){g_strategyType="TREND";g_strategySignal="CALL";g_strategyReason="CALL "+IntegerToString(cc)+"% + HA GREEN";return "CALL";}
  716. if(cp>=thr&&g_haM1=="HA BEARISH 1"){g_strategyType="TREND";g_strategySignal="PUT";g_strategyReason="PUT "+IntegerToString(cp)+"% + HA RED";return "PUT";}}
  717. g_strategyType="NONE";g_strategySignal="WAIT";g_strategyReason="Wait "+IntegerToString(thr)+"% + HA";return "WAIT";
  718. }
  719.  
  720. //+------------------------------------------------------------------+
  721. // OTC CROWD
  722. //+------------------------------------------------------------------+
  723. void CalcOTCCrowd(){
  724. int cS=0,pS=0;
  725. for(int i=1;i<=15&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;
  726. double uw=(High[i]-MathMax(Open[i],Close[i]))/rg,lw=(MathMin(Open[i],Close[i])-Low[i])/rg;
  727. if(uw>0.70&&Close[i]<Open[i])cS+=4;if(lw>0.70&&Close[i]>Open[i])pS+=4;
  728. if(uw>0.55&&Close[i]<Open[i])cS+=2;if(lw>0.55&&Close[i]>Open[i])pS+=2;
  729. if(Close[i]>Open[i])cS+=1;else if(Close[i]<Open[i])pS+=1;}
  730. int tot=cS+pS;if(tot==0){g_otcCallPct=50;g_otcPutPct=50;}
  731. else{int rC=(int)(cS*100.0/tot),rP=100-rC;
  732. if(rC>=rP){g_otcCallPct=MathMax(50,rC);g_otcPutPct=100-g_otcCallPct;}
  733. else{g_otcPutPct=MathMax(50,rP);g_otcCallPct=100-g_otcPutPct;}}
  734. }
  735.  
  736. //+------------------------------------------------------------------+
  737. // BRAIN SCORE
  738. //+------------------------------------------------------------------+
  739. double BrainSc(int &cp){
  740. double s=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  741. double sup=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,LB,1)],res=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,LB,1)];
  742. double bH=MathMax(Open[1],Close[1]),bL=MathMin(Open[1],Close[1]);
  743. if(High[1]>res&&bL<res)s-=4;if(Low[1]<sup&&bH>sup)s+=4;
  744. if(High[0]>res)s-=2;if(Low[0]<sup)s+=2;
  745. double atr=iATR(NULL,PERIOD_M1,14,1);
  746. if(atr!=0){double cn=(High[1]-Low[1])/atr,v1=(double)iVolume(NULL,PERIOD_M1,1),va=0;
  747. for(int i=2;i<=10;i++)va+=iVolume(NULL,PERIOD_M1,i);va/=9.0;
  748. double vr=va>0?v1/va:1;if(cn>1.2&&vr>1.5){if(Close[1]>Open[1])s-=3;else s+=3;}}
  749. int c=0;for(int i=1;i<=30;i++)if(Close[i]>Open[i])c++;
  750. cp=(int)(c*100.0/30.0);if(cp>=70)s-=5;if(cp<=30)s+=5;s*=1.5;
  751. int dj=0;for(int i=1;i<=8;i++){double bd=MathAbs(Close[i]-Open[i]),rg=High[i]-Low[i];if(rg<=0)continue;
  752. if(bd<rg*0.2)dj++;double uw=High[i]-MathMax(Open[i],Close[i]),lw=MathMin(Open[i],Close[i])-Low[i];
  753. if(uw>bd*3)s+=2;if(lw>bd*3)s-=2;}if(dj>=4)s*=1.5;
  754. int bu=0,be=0;for(int i=1;i<=8;i++){if(Close[i]>Open[i]){bu++;be=0;}else{be++;bu=0;}if(bu>=5)s-=3;if(be>=5)s+=3;}
  755. int h=TimeHour(TimeCurrent());double mul=1.0;
  756. if((h>=0&&h<=2)||(h>=8&&h<=11)||(h>=14&&h<=17))mul=1.20;if(h>=3&&h<=7)mul=0.90;
  757. return s*mul;
  758. }
  759.  
  760. //+------------------------------------------------------------------+
  761. // MICRO WICK
  762. //+------------------------------------------------------------------+
  763. 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;}
  764.  
  765. //+------------------------------------------------------------------+
  766. // VOLUME TRAP
  767. //+------------------------------------------------------------------+
  768. double VolumeTrapScore(){
  769. if(!VOLUME_PROFILE_TRAP||Bars<50)return 0;
  770. double cur=Close[0],hi=High[iHighest(NULL,0,MODE_HIGH,50,1)],lo=Low[iLowest(NULL,0,MODE_LOW,50,1)];
  771. 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};
  772. 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]++;}
  773. for(int z=0;z<zn;z++)if(zc[z]>0)vz[z]/=zc[z];
  774. int cz=(int)((cur-lo)/zs);if(cz<0)cz=0;if(cz>=zn)cz=zn-1;
  775. double vr=(double)Volume[1]/(vz[cz]+0.001);double wr=MicroWick();
  776. if(vr>2.0&&wr>0.65)return 25;if(vr>1.8&&wr>0.55)return 15;return 0;
  777. }
  778.  
  779. //+------------------------------------------------------------------+
  780. // ULTIMATE TRAP SCORE
  781. //+------------------------------------------------------------------+
  782. double UltimateTrapScore(){double wr=MicroWick(),v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  783. double avg=v2>0?v2:1.0,vr=v1/avg,rsi=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1),sc=0.0;
  784. if(wr>0.65)sc+=40;if(vr>2.0)sc+=30;if(rsi>75||rsi<25)sc+=20;
  785. if((High[1]>g_nearest_res&&Close[1]<g_nearest_res)||(Low[1]<g_nearest_sup&&Close[1]>g_nearest_sup))sc+=25;
  786. sc+=VolumeTrapScore();return MathMin(100.0,sc);}
  787.  
  788. //+------------------------------------------------------------------+
  789. // BROKER FORCE
  790. //+------------------------------------------------------------------+
  791. bool IsBrokerForce(double dummy=0){
  792. int sd=0;for(int i=1;i<=3;i++){if(Close[i]>Open[i])sd++;else sd--;}
  793. double v1=(double)iVolume(NULL,PERIOD_M1,1),v2=(double)iVolume(NULL,PERIOD_M1,2);
  794. double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);
  795. return(MathAbs(sd)==3&&v1>v2*2.0&&sp>BROKER_SPREAD_THRESHOLD);
  796. }
  797.  
  798. //+------------------------------------------------------------------+
  799. // NEURAL NETWORK
  800. //+------------------------------------------------------------------+
  801. double NeuralBiasFast(){double r=NeuralBias(0)*0.3+NeuralBias(1)*0.7;if(r>96)r=96;if(r<4)r=4;return r;}
  802. double NeuralBias(int s){
  803. double f[8];f[0]=fDoji(s)/100.0;f[1]=fWick(s)/100.0;f[2]=fVol()/100.0;f[3]=fMom(s)/100.0;
  804. f[4]=fSpread()/100.0;f[5]=fMem(s)/100.0;f[6]=fTick(s)/100.0;f[7]=fFrac(s)/100.0;
  805. double h[4]={0,0,0,0};for(int i=0;i<4;i++){for(int j=0;j<8;j++)h[i]+=f[j]*NW[j*4+i];h[i]=MathMax(0,h[i]-0.08);}
  806. double o=0;for(int i=0;i<4;i++)o+=h[i]*NW[32+i];double r=50.0+o*22.0;return r>96?96:r<4?4:r;
  807. }
  808. double fDoji(int s){int n=0;for(int i=s;i<s+5&&i<Bars;i++){double b=MathAbs(Open[i]-Close[i]),r=High[i]-Low[i];if(r>0&&b<r*0.25)n++;}return n*20.0;}
  809. double fWick(int s){double r=High[s]-Low[s];if(r==0)return 0;double u=(High[s]-MathMax(Open[s],Close[s]))/r,l=(MathMin(Open[s],Close[s])-Low[s])/r;return(u>0.65||l>0.65)?95:(u>0.5||l>0.5)?70:0;}
  810. double fVol(){if(Bars<11)return 0;double a=0;for(int i=1;i<=10;i++)a+=iVolume(NULL,PERIOD_M1,i);a/=10.0;if(a==0)return 0;double r=(double)iVolume(NULL,PERIOD_M1,0)/a;return r>3?95:r>2?75:r>1.5?50:0;}
  811. double fMom(int s){if(s+3>=Bars)return 0;return((Close[s]>Open[s]&&Close[s+1]<Open[s+1])||(Close[s]<Open[s]&&Close[s+1]>Open[s+1]))?88:0;}
  812. double fSpread(){double sp=(double)MarketInfo(Symbol(),MODE_SPREAD);return sp>8?90:sp>5?70:sp>3?40:0;}
  813. double fMem(int s){if(s+8>=Bars)return 0;int r=0;for(int i=s;i<s+8&&i+2<Bars;i+=2){bool vB=(Low[i]>Low[i+1]&&Low[i+2]>Low[i+1]&&Close[i+2]>Open[i+2]);bool aT=(High[i]<High[i+1]&&High[i+2]<High[i+1]&&Close[i+2]<Open[i+2]);if(vB||aT)r++;}return(double)r*12.5;}
  814. double fTick(int s){if(s+5>=Bars)return 0;double vN=(double)iVolume(NULL,PERIOD_M1,s),vP=(double)iVolume(NULL,PERIOD_M1,s+1);if(vP==0)return 0;double vR=vN/vP,pc=MathAbs(Close[s]-Close[s+1])/Close[s+1]*100;if(vR>2.0&&pc<0.1)return 85;if(vR>1.5&&pc<0.05)return 60;return 0;}
  815. double fFrac(int s){if(s+4>=Bars||s<2)return 0;bool b=(Low[s]<Low[s-1]&&Low[s]<Low[s-2]&&Low[s]<Low[s+1]&&Low[s]<Low[s+2]);bool t=(High[s]>High[s-1]&&High[s]>High[s-2]&&High[s]>High[s+1]&&High[s]>High[s+2]);return(b||t)?90:0;}
  816.  
  817. //+------------------------------------------------------------------+
  818. // MAIN PREDICTION
  819. //+------------------------------------------------------------------+
  820. void CalcPrediction(double brain,double nb,int cp,double mai,int mts,double rsc,bool bf,double mw){
  821. string ms=DetectMyStrategy();bool sA=(ms!="WAIT");
  822.  
  823. if(sA&&g_strategyType=="TRAP"){
  824. 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;}
  825. 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;}}
  826.  
  827. double tS=UltimateTrapScore();bool bF2=IsBrokerForce();
  828.  
  829. 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;}}
  830.  
  831. 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;}
  832.  
  833. double gS=0,rS=0,ab=MathMax(0.70,MathMin(1.30,g_accuracy/65.0));
  834.  
  835. if(IsSidewaysMarket()){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  836. 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);
  837. 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;}
  838. if(nR)rS+=20;if(nS)gS+=20;}
  839.  
  840. 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;}}
  841.  
  842. if(g_haM1=="HA BULLISH 1"&&g_haM5=="HA BULLISH 5")gS+=60;
  843. else if(g_haM1=="HA BEARISH 1"&&g_haM5=="HA BEARISH 5")rS+=60;
  844. else if(g_haM1=="HA BULLISH 1")gS+=30;else if(g_haM1=="HA BEARISH 1")rS+=30;
  845.  
  846. if(HA_ALIGN_FILTER && g_haBoth=="MIXED"){gS*=0.5;rS*=0.5;}
  847.  
  848. gS+=(nb-50)/50.0*22*ab;rS-=(nb-50)/50.0*22*ab;
  849.  
  850. double bn=MathMax(-1.0,MathMin(1.0,brain/10.0));gS+=bn*15*ab;rS-=bn*15*ab;
  851.  
  852. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  853. if(g_nearest_res>0&&(g_nearest_res-Close[0])<10*pip)rS+=12;
  854. if(g_nearest_sup>0&&(Close[0]-g_nearest_sup)<10*pip)gS+=12;
  855.  
  856. double rsi1=iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,1);if(rsi1>70)rS+=6;if(rsi1<30)gS+=6;
  857.  
  858. double sn=MathMax(-1.0,MathMin(1.0,rsc/3.0));gS+=sn*8*ab;rS-=sn*8*ab;
  859.  
  860. double wRS=GetWickRejectSignal();gS+=(wRS>0?wRS:0);rS+=(wRS<0?MathAbs(wRS):0);
  861.  
  862. double cPS=GetCommonPointSignal();gS+=(cPS>0?cPS:0);rS+=(cPS<0?MathAbs(cPS):0);
  863.  
  864. int rd=DetectRSIDivergence();if(rd==1)gS+=20;if(rd==-1)rS+=20;
  865.  
  866. int cp2=DetectClassicPatterns();if(cp2==1)gS+=12;if(cp2==-1)rS+=12;
  867.  
  868. int qm=DetectQMR();if(qm==1)gS+=25;if(qm==-1)rS+=25;
  869.  
  870. int sk=StreakReversalSignal();if(sk==1)gS+=18;if(sk==-1)rS+=18;
  871.  
  872. 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;}
  873.  
  874. int vs=SmartVolumeSignal();if(vs==1)gS+=12;if(vs==-1)rS+=12;
  875.  
  876. if(!IsVolatilityGood()){gS*=0.5;rS*=0.5;}
  877.  
  878. double diff=MathMax(-100.0,MathMin(100.0,gS-rS));
  879. g_gProb=MathMax(8.0,MathMin(92.0,50.0+(diff/1.8)));g_rProb=100.0-g_gProb;
  880.  
  881. if(sA&&g_strategyType=="TREND"){
  882. if(ms=="CALL"){g_gProb=MathMax(g_gProb,85.0);g_rProb=100.0-g_gProb;}
  883. if(ms=="PUT"){g_rProb=MathMax(g_rProb,85.0);g_gProb=100.0-g_rProb;}
  884. g_gProb=MathMin(92.0,MathMax(8.0,g_gProb));g_rProb=100.0-g_gProb;}
  885.  
  886. double dom=MathMax(g_gProb,g_rProb);
  887. 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;}}}
  888. 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;}}}
  889. else{if(!sA){g_pAction="WAIT";g_pColor=NEON_YELLOW;}}
  890.  
  891. g_calc_gProb=g_gProb;g_calc_rProb=g_rProb;g_calc_pAction=g_pAction;g_calc_pColor=g_pColor;
  892. }
  893.  
  894. //+------------------------------------------------------------------+
  895. // COMMON POINTS & WICK REJECTIONS
  896. //+------------------------------------------------------------------+
  897. void DetectCommonPoints(){
  898. if(!SHOW_COMMON_POINTS){g_commonCount=0;return;}
  899. if(Time[0]==g_lastCommonScan)return;g_lastCommonScan=Time[0];g_commonCount=0;
  900. datetime nowBar=Time[0];int periodSec=PeriodSeconds(PERIOD_M1);
  901. double m5h=iHigh(NULL,PERIOD_M5,iHighest(NULL,PERIOD_M5,MODE_HIGH,10,1));
  902. double m5l=iLow(NULL,PERIOD_M5,iLowest(NULL,PERIOD_M5,MODE_LOW,10,1));
  903. 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++;}
  904. 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++;}
  905. 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++;}
  906. 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++;}
  907. 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--;}
  908. }
  909.  
  910. void DrawCommonPoints(){
  911. 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));}
  912. if(!SHOW_COMMON_POINTS||g_commonCount==0)return;
  913. for(int i=0;i<g_commonCount;i++){if(TimeCurrent()>g_commonExpire[i])continue;if(IsLineNearby(g_commonPrice[i],8.0))continue;
  914. string id=PFX+"COMMON_"+IntegerToString(i),idL=PFX+"COMMON_L_"+IntegerToString(i),idH=PFX+"COMMON_HL_"+IntegerToString(i);color zc=DARK_BLUE_NEON;
  915. 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);
  916. ObjectCreate(0,id,OBJ_TEXT,0,Time[0],g_commonPrice[i]);ObjectSetText(id,"*",16,"Arial",zc);ObjectSetInteger(0,id,OBJPROP_BACK,false);
  917. string lbl=(g_commonType[i]=="CALL")?"^ CALL ZONE":"v PUT ZONE";
  918. ObjectCreate(0,idL,OBJ_TEXT,0,Time[0]+PeriodSeconds(PERIOD_M1)*3,g_commonPrice[i]);ObjectSetText(idL,lbl,9,"Arial Bold",zc);ObjectSetInteger(0,idL,OBJPROP_BACK,false);}
  919. }
  920.  
  921. void DetectWickRejections(){
  922. g_isSideways=IsSidewaysMarket();if(!SHOW_WICK_REJECT_LINES){g_wickLineCount=0;return;}
  923. if(Time[0]==g_lastWickScan)return;g_lastWickScan=Time[0];
  924. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double zone=WICK_ZONE_PIPS*pip*2;double cur=Close[0];
  925. g_wickLineCount=0;datetime now=Time[0],expT=now+PeriodSeconds(PERIOD_M1)*WICK_EXPIRE_BARS*2;
  926. for(int i=1;i<=40&&i<Bars;i++){double rg=High[i]-Low[i];if(rg<=0)continue;
  927. double uw=High[i]-MathMax(Open[i],Close[i]),lw=MathMin(Open[i],Close[i])-Low[i];double rl=0;bool fd=false;
  928. if(uw>rg*0.30){rl=High[i];if(MathAbs(cur-rl)<zone*3)fd=true;}
  929. if(lw>rg*0.30){rl=Low[i];if(MathAbs(cur-rl)<zone*3)fd=true;}
  930. if(!fd)continue;bool ex=false;
  931. for(int j=0;j<g_wickLineCount;j++)if(MathAbs(g_wickLinePrice[j]-rl)<zone){g_wickLineTouches[j]++;ex=true;break;}
  932. if(!ex&&g_wickLineCount<MAX_WICK_LINES){g_wickLinePrice[g_wickLineCount]=rl;g_wickLineTouches[g_wickLineCount]=1;g_wickLineExpire[g_wickLineCount]=expT;g_wickLineCount++;}}
  933. for(int i=g_wickLineCount-1;i>=0;i--)if(TimeCurrent()>g_wickLineExpire[i]){for(int j=i;j<g_wickLineCount-1;j++){g_wickLinePrice[j]=g_wickLinePrice[j+1];g_wickLineTouches[j]=g_wickLineTouches[j+1];g_wickLineExpire[j]=g_wickLineExpire[j+1];}g_wickLineCount--;}
  934. }
  935.  
  936. void DrawWickRejectLines(){
  937. for(int i=0;i<MAX_WICK_LINES;i++){SafeDel(PFX+"WICK_"+IntegerToString(i));SafeDel(PFX+"WICK_L_"+IntegerToString(i));}
  938. if(!SHOW_WICK_REJECT_LINES||!g_isSideways)return;
  939. bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  940. for(int i=0;i<g_wickLineCount;i++){if(TimeCurrent()>g_wickLineExpire[i])continue;if(MathAbs(cur-g_wickLinePrice[i])/pip>20.0)continue;if(IsLineNearby(g_wickLinePrice[i],5.0))continue;
  941. string id=PFX+"WICK_"+IntegerToString(i),idL=PFX+"WICK_L_"+IntegerToString(i);color lc=C'0,180,255';
  942. ObjectCreate(0,id,OBJ_HLINE,0,0,g_wickLinePrice[i]);ObjectSetInteger(0,id,OBJPROP_COLOR,lc);ObjectSetInteger(0,id,OBJPROP_WIDTH,2);ObjectSetInteger(0,id,OBJPROP_STYLE,STYLE_DOT);ObjectSetInteger(0,id,OBJPROP_BACK,false);
  943. string lbl="W"+IntegerToString(g_wickLineTouches[i])+"x";
  944. ObjectCreate(0,idL,OBJ_TEXT,0,Time[0]+PeriodSeconds(PERIOD_M1)*2,g_wickLinePrice[i]);ObjectSetText(idL,lbl,8,"Arial Bold",lc);ObjectSetInteger(0,idL,OBJPROP_BACK,false);}
  945. }
  946.  
  947. double GetWickRejectSignal(){if(!g_isSideways||g_wickLineCount==0)return 0;double sc=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  948. for(int i=0;i<g_wickLineCount;i++){double d=MathAbs(Close[0]-g_wickLinePrice[i])/pip;bool iS=(g_wickLinePrice[i]<Close[0]);
  949. if(d<5){if(iS)sc+=15*g_wickLineTouches[i];else sc-=15*g_wickLineTouches[i];}else if(d<10){if(iS)sc+=8*g_wickLineTouches[i];else sc-=8*g_wickLineTouches[i];}}return MathMax(-40,MathMin(40,sc));}
  950. double GetCommonPointSignal(){if(g_commonCount==0)return 0;double sc=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  951. for(int i=0;i<g_commonCount;i++){if(TimeCurrent()>g_commonExpire[i])continue;double d=MathAbs(Close[0]-g_commonPrice[i])/pip;if(d<8){if(g_commonType[i]=="CALL")sc+=25;else sc-=25;}}return MathMax(-30,MathMin(30,sc));}
  952.  
  953. //+------------------------------------------------------------------+
  954. // HIDDEN HTF LEVELS
  955. //+------------------------------------------------------------------+
  956. void DetectHiddenLevels(){g_htfLevelCount=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double curP=Close[0];
  957. double rU=MathCeil(curP/(10*pip))*(10*pip),rD=MathFloor(curP/(10*pip))*(10*pip);
  958. if(MathAbs(curP-rU)/pip<=15)AddLevel(rU,"ROUND","RESISTANCE",TimeCurrent(),8,true);
  959. if(MathAbs(curP-rD)/pip<=15)AddLevel(rD,"ROUND","SUPPORT",TimeCurrent(),8,true);
  960. DetectHTFLevels(PERIOD_M5,"M5",3,pip);DetectHTFLevels(PERIOD_M15,"M15",5,pip);DetectHTFLevels(PERIOD_M30,"M30",7,pip);DetectHTFLevels(PERIOD_H1,"H1",9,pip);
  961. SortLevelsByStrength();}
  962.  
  963. void DetectHTFLevels(ENUM_TIMEFRAMES tf,string tfN,int bS,double pip){int bars=MathMin(50,iBars(NULL,tf));if(bars<10)return;double curP=Close[0];
  964. for(int i=2;i<bars-2;i++){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);}
  965. 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);}}}
  966.  
  967. 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;}
  968. 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;}
  969.  
  970. void AddLevel(double price,string tf,string type,datetime time,int strength,bool isRound){if(g_htfLevelCount>=50)return;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;
  971. for(int i=0;i<g_htfLevelCount;i++)if(MathAbs(g_htfLevels[i].price-price)/pip<5)return;
  972. g_htfLevels[g_htfLevelCount].price=price;g_htfLevels[g_htfLevelCount].timeframe=tf;g_htfLevels[g_htfLevelCount].type=type;g_htfLevels[g_htfLevelCount].time=time;g_htfLevels[g_htfLevelCount].strength=strength;g_htfLevels[g_htfLevelCount].isRoundNumber=isRound;g_htfLevelCount++;}
  973.  
  974. 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;}}
  975.  
  976. void DrawHiddenLevels(){for(int i=0;i<50;i++){SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i));SafeDel(PFX+"HTF_LEVEL_"+IntegerToString(i)+"_LBL");}
  977. int dc=MathMin(10,g_htfLevelCount);int drawn=0;
  978. for(int i=0;i<dc&&drawn<5;i++){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;if(MathAbs(Close[0]-g_htfLevels[i].price)/pip>30)continue;if(IsLineNearby(g_htfLevels[i].price,5.0))continue;
  979. string nm=PFX+"HTF_LEVEL_"+IntegerToString(drawn);color lc=g_htfLevels[i].isRoundNumber?NEON_PURPLE:(StringFind(g_htfLevels[i].type,"SUPPORT")>=0?NEON_GREEN:NEON_RED);
  980. 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);
  981. string ln=nm+"_LBL";ObjectCreate(0,ln,OBJ_TEXT,0,Time[0],g_htfLevels[i].price);ObjectSetText(ln,StringFormat("%s %s S:%d",g_htfLevels[i].timeframe,g_htfLevels[i].type,g_htfLevels[i].strength),8,"Arial",lc);drawn++;}}
  982.  
  983. //+------------------------------------------------------------------+
  984. // REVERSAL DETECTION
  985. //+------------------------------------------------------------------+
  986. string DetectSuddenReversal(){string reason="";if(Close[1]<Open[1]&&Close[0]>Open[0]){reason="BULL REVERSAL";if(iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,0)<30)return reason;}if(Close[1]>Open[1]&&Close[0]<Open[0]){reason="BEAR REVERSAL";if(iRSI(NULL,PERIOD_M1,14,PRICE_CLOSE,0)>70)return reason;}return "";}
  987.  
  988. //+------------------------------------------------------------------+
  989. // DRAWING HELPERS
  990. //+------------------------------------------------------------------+
  991. void SafeDel(string id){if(ObjectFind(0,id)>=0)ObjectDelete(0,id);}
  992. void DrawHLine(string id,double price,color clr,int width,int style){if(price<=0)return;SafeDel(id);ObjectCreate(0,id,OBJ_HLINE,0,0,price);ObjectSetInteger(0,id,OBJPROP_COLOR,clr);ObjectSetInteger(0,id,OBJPROP_WIDTH,width);ObjectSetInteger(0,id,OBJPROP_STYLE,style);ObjectSetInteger(0,id,OBJPROP_BACK,false);}
  993. void HideSPM(){for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX)==0)continue;int tp=(int)ObjectGetInteger(0,nm,OBJPROP_TYPE);if(tp==OBJ_LABEL||tp==OBJ_RECTANGLE_LABEL||tp==OBJ_TEXT)ObjectDelete(0,nm);}}
  994. void NuclearDeleteAll(){for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);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);}}
  995. void DelDashboard(){for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX)!=0)continue;if(StringFind(nm,"NEAREST_RES")>=0)continue;if(StringFind(nm,"NEAREST_SUP")>=0)continue;if(StringFind(nm,"HRN_LINE")>=0)continue;if(StringFind(nm,"HRN_LBL")>=0)continue;if(StringFind(nm,"candle_timer")>=0)continue;if(StringFind(nm,"HTF_LEVEL")>=0)continue;if(StringFind(nm,"COMMON_")>=0)continue;if(StringFind(nm,"WICK_")>=0)continue;ObjectDelete(nm);}}
  996.  
  997. //+------------------------------------------------------------------+
  998. // ML WEIGHTS
  999. //+------------------------------------------------------------------+
  1000. 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;}
  1001. 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);}}
  1002. 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();}
  1003.  
  1004. //+------------------------------------------------------------------+
  1005. // ACCURACY TRACKING
  1006. //+------------------------------------------------------------------+
  1007. 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++;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];}
  1008. 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;}
  1009. 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]));}}
  1010.  
  1011. //+------------------------------------------------------------------+
  1012. // MARKET MODE & HELPERS
  1013. //+------------------------------------------------------------------+
  1014. 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";}
  1015. 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;}
  1016. 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;}
  1017. double GetDynamicThreshold(){if(g_accuracy<55)return 90.0;if(g_accuracy<60)return 85.0;if(g_accuracy<65)return 80.0;if(g_accuracy<70)return 75.0;if(g_accuracy>80)return 70.0;if(g_accuracy>75)return 72.0;return 75.0;}
  1018.  
  1019. //+------------------------------------------------------------------+
  1020. // SIGNAL DETECTORS
  1021. //+------------------------------------------------------------------+
  1022. 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;}
  1023. 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;}
  1024. double GetADXStrength(){if(Bars<20)return 0;return iADX(NULL,PERIOD_M1,14,PRICE_CLOSE,MODE_MAIN,1);}
  1025. 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;}
  1026. 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;}
  1027.  
  1028. //+------------------------------------------------------------------+
  1029. // CLASSIC PATTERNS
  1030. //+------------------------------------------------------------------+
  1031. 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;
  1032. 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++;}}}
  1033. 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;else if(Close[0]<MathMin(sh[0],sh[1]))return -1;}}
  1034. 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;else if(Close[0]>MathMax(sl[0],sl[1]))return 1;}}return 0;}
  1035.  
  1036. //+------------------------------------------------------------------+
  1037. // QMR PATTERN
  1038. //+------------------------------------------------------------------+
  1039. int DetectQMR(){if(Bars<40)return 0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double sH[10],sL[10];int sHB[10],sLB[10];int hC=0,lC=0;
  1040. for(int i=3;i<40&&hC<10;i++)if(High[i]>High[i-1]&&High[i]>High[i-2]&&High[i]>High[i+1]&&High[i]>High[i+2]){sH[hC]=High[i];sHB[hC]=i;hC++;}
  1041. for(int i=3;i<40&&lC<10;i++)if(Low[i]<Low[i-1]&&Low[i]<Low[i-2]&&Low[i]<Low[i+1]&&Low[i]<Low[i+2]){sL[lC]=Low[i];sLB[lC]=i;lC++;}
  1042. if(hC<2||lC<2)return 0;
  1043. for(int iA=0;iA<hC;iA++)for(int iC2=0;iC2<hC;iC2++){if(iA==iC2)continue;if(sHB[iA]<=sHB[iC2])continue;if(sH[iC2]>=sH[iA]-10*pip)continue;
  1044. for(int iB=0;iB<lC;iB++){if(sHB[iA]<=sLB[iB])continue;if(sLB[iB]<=sHB[iC2])continue;
  1045. for(int iD=0;iD<lC;iD++){if(iB==iD)continue;if(sHB[iC2]<=sLB[iD])continue;if(sL[iD]>=sL[iB])continue;
  1046. bool m3=false,m5=false;if(Bars>=8){double o1=Open[3],c1=Close[3],o2=Open[0],c2=Close[0];double b1=MathAbs(c1-o1);if(c2>o2&&c1<o1&&MathAbs(c2-o2)>b1*0.5)m3=true;}
  1047. if(iBars(NULL,PERIOD_M5)>=3){double o1=iOpen(NULL,PERIOD_M5,2),c1=iClose(NULL,PERIOD_M5,2),o2=iOpen(NULL,PERIOD_M5,1),c2=iClose(NULL,PERIOD_M5,1);double b1=MathAbs(c1-o1);if(c2>o2&&c1<o1&&MathAbs(c2-o2)>b1*0.5)m5=true;}
  1048. if(m3||m5)if(Close[0]>sL[iD]&&Close[0]<sH[iC2]){qmrA=sH[iA];qmrB=sL[iB];qmrC=sH[iC2];qmrD=sL[iD];qmrActive=true;qmrExpire=TimeCurrent()+900;return 1;}}}}
  1049. for(int iA=0;iA<lC;iA++)for(int iC2=0;iC2<lC;iC2++){if(iA==iC2)continue;if(sLB[iA]<=sLB[iC2])continue;if(sL[iC2]<=sL[iA]+10*pip)continue;
  1050. for(int iB=0;iB<hC;iB++){if(sLB[iA]<=sHB[iB])continue;if(sHB[iB]<=sLB[iC2])continue;
  1051. for(int iD=0;iD<hC;iD++){if(iB==iD)continue;if(sLB[iC2]<=sHB[iD])continue;if(sH[iD]>=sH[iB])continue;
  1052. bool m3=false,m5=false;if(Bars>=8){double o1=Open[3],c1=Close[3],o2=Open[0],c2=Close[0];double b1=MathAbs(c1-o1);if(c2<o2&&c1>o1&&MathAbs(c2-o2)>b1*0.5)m3=true;}
  1053. if(iBars(NULL,PERIOD_M5)>=3){double o1=iOpen(NULL,PERIOD_M5,2),c1=iClose(NULL,PERIOD_M5,2),o2=iOpen(NULL,PERIOD_M5,1),c2=iClose(NULL,PERIOD_M5,1);double b1=MathAbs(c1-o1);if(c2<o2&&c1>o1&&MathAbs(c2-o2)>b1*0.5)m5=true;}
  1054. if(m3||m5)if(Close[0]<sH[iD]&&Close[0]>sL[iC2]){qmrA=sL[iA];qmrB=sH[iB];qmrC=sL[iC2];qmrD=sH[iD];qmrActive=true;qmrExpire=TimeCurrent()+900;return -1;}}}}
  1055. return 0;}
  1056.  
  1057. //+------------------------------------------------------------------+
  1058. // HRN LEVELS
  1059. //+------------------------------------------------------------------+
  1060. void ScanHRNLevels(){g_rj_cnt=0;bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];
  1061. 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];
  1062. 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++;}}}
  1063. 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++;}}}}
  1064. 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;}}
  1065.  
  1066. double FindNearestHRN(double cur){bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;if(g_rj_cnt==0)return MathRound(cur/(10*pip))*(10*pip);double best=-1,lv=0;for(int i=0;i<g_rj_cnt;i++){double d=MathAbs(cur-g_rj[i].price)/pip;if(d>50)continue;double sc=(double)g_rj[i].touches/(d+1.0);if(sc>best){best=sc;lv=g_rj[i].price;}}return lv>0?lv:MathRound(cur/(10*pip))*(10*pip);}
  1067.  
  1068. void UpdateHRN(){if(Bars<LOOKBACK+5)return;if(Time[0]!=g_hrn_scan_bar){ScanHRNLevels();g_hrn_scan_bar=Time[0];}bool jpy=(StringFind(Symbol(),"JPY")>=0);double pip=jpy?0.01:0.0001;double cur=Close[0];int dg=(int)MarketInfo(Symbol(),MODE_DIGITS);if(dg<=0)dg=5;
  1069. if(g_hrn_price==0){g_hrn_price=FindNearestHRN(cur);g_hrn_is_sup=(cur>g_hrn_price);g_hrn_brk_bars=0;}
  1070. bool ab=(cur>g_hrn_price+3*pip),bl=(cur<g_hrn_price-3*pip);bool brk=(g_hrn_is_sup&&bl)||(!g_hrn_is_sup&&ab);
  1071. if(brk){g_hrn_brk_bars++;if(g_hrn_brk_bars>=HRN_CONFIRM_BARS){double nH=FindNearestHRN(cur);g_hrn_price=nH;g_hrn_is_sup=(cur>g_hrn_price);g_hrn_brk_bars=0;}}else g_hrn_brk_bars=0;
  1072. g_hrn_str=DoubleToString(NormalizeDouble(g_hrn_price,dg),dg);DrawHLine(PFX+"HRN_LINE",g_hrn_price,NEON_PINK,3,STYLE_SOLID);
  1073. string ln=PFX+"HRN_LBL";SafeDel(ln);ObjectCreate(0,ln,OBJ_TEXT,0,Time[0]+Period()*60*2,g_hrn_price);ObjectSetText(ln,"HRN "+(g_hrn_is_sup?"SUP":"RES")+" "+g_hrn_str,9,"Arial Bold",NEON_PINK);ObjectSetInteger(0,ln,OBJPROP_BACK,false);}
  1074.  
  1075. //+------------------------------------------------------------------+
  1076. // S/R LEVELS
  1077. //+------------------------------------------------------------------+
  1078. void AddRes(double lv){if(lv<=0)return;for(int i=0;i<resCount;i++)if(MathAbs(ResLevels[i]-lv)<10*Point)return;if(resCount>=MAX_LEVELS){for(int i=0;i<MAX_LEVELS-1;i++)ResLevels[i]=ResLevels[i+1];resCount=MAX_LEVELS-1;}ResLevels[resCount++]=lv;}
  1079. void AddSup(double lv){if(lv<=0)return;for(int i=0;i<supCount;i++)if(MathAbs(SupLevels[i]-lv)<10*Point)return;if(supCount>=MAX_LEVELS){for(int i=0;i<MAX_LEVELS-1;i++)SupLevels[i]=SupLevels[i+1];supCount=MAX_LEVELS-1;}SupLevels[supCount++]=lv;}
  1080.  
  1081. void DrawNearestSR(){for(int i=0;i<20;i++){SafeDel(PFX+"RES_"+IntegerToString(i));SafeDel(PFX+"SUP_"+IntegerToString(i));}SafeDel(PFX+"NEAREST_RES");SafeDel(PFX+"NEAREST_SUP");if(!SHOW_SR_LINES)return;
  1082. double cur=Close[0];g_nearest_res=0;g_nearest_sup=0;double mR=999999,mS=999999;
  1083. for(int i=0;i<resCount;i++)if(ResLevels[i]>cur&&(ResLevels[i]-cur)<mR){mR=ResLevels[i]-cur;g_nearest_res=ResLevels[i];}
  1084. for(int i=0;i<supCount;i++)if(SupLevels[i]<cur&&(cur-SupLevels[i])<mS){mS=cur-SupLevels[i];g_nearest_sup=SupLevels[i];}
  1085. if(g_nearest_res>0&&!IsLineNearby(g_nearest_res,5.0))DrawHLine(PFX+"NEAREST_RES",g_nearest_res,NEON_RED,2,STYLE_SOLID);
  1086. if(g_nearest_sup>0&&!IsLineNearby(g_nearest_sup,5.0))DrawHLine(PFX+"NEAREST_SUP",g_nearest_sup,NEON_GREEN,2,STYLE_SOLID);}
  1087.  
  1088. 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;}}
  1089.  
  1090. void UpdateSR(){if(Time[0]==lastSRBar)return;lastSRBar=Time[0];if(Bars<15)return;double nR=High[iHighest(NULL,PERIOD_M1,MODE_HIGH,10,1)],nS=Low[iLowest(NULL,PERIOD_M1,MODE_LOW,10,1)],cur=Close[0];if(nR>0&&cur<nR-5*Point)AddRes(nR);if(nS>0&&cur>nS+5*Point)AddSup(nS);if(resCount<1)AddRes(High[1]);if(supCount<1)AddSup(Low[1]);DrawNearestSR();CheckBRK();}
  1091.  
  1092. //+------------------------------------------------------------------+
  1093. // TIMER
  1094. //+------------------------------------------------------------------+
  1095. void UpdateTimer(){if(!SHOW_TIMER)return;string nm=PFX+"candle_timer";int ps=Period()*60;int el=(int)(TimeCurrent()-Time[0]);int rem=ps-el;if(rem<=0)rem=ps;datetime tt=Time[0]+ps+60;double tp=High[0]+(Point*15);if(ObjectFind(0,nm)<0){ObjectCreate(0,nm,OBJ_TEXT,0,tt,tp);ObjectSetInteger(0,nm,OBJPROP_BACK,false);ObjectSetInteger(0,nm,OBJPROP_SELECTABLE,false);}ObjectSetInteger(0,nm,OBJPROP_TIME,tt);ObjectSetDouble(0,nm,OBJPROP_PRICE,tp);int mm=rem/60,ss=rem%60;string t=(mm>0?IntegerToString(mm)+":":"")+(ss<10?"0":"")+IntegerToString(ss)+"s";ObjectSetText(nm,t,11,"Arial Bold",NEON_YELLOW);}
  1096.  
  1097. //+------------------------------------------------------------------+
  1098. // NEURAL WEIGHTS INIT
  1099. //+------------------------------------------------------------------+
  1100. void InitNW(){NW[0]=0.852;NW[1]=0.724;NW[2]=0.913;NW[3]=0.681;NW[4]=0.795;NW[5]=0.836;NW[6]=0.772;NW[7]=0.894;NW[8]=0.623;NW[9]=0.748;NW[10]=0.887;NW[11]=0.716;NW[12]=0.661;NW[13]=0.829;NW[14]=0.753;NW[15]=0.942;NW[16]=0.784;NW[17]=0.697;NW[18]=0.871;NW[19]=0.735;NW[20]=0.813;NW[21]=0.768;NW[22]=0.932;NW[23]=0.674;NW[24]=0.845;NW[25]=0.709;NW[26]=0.888;NW[27]=0.651;NW[28]=0.921;NW[29]=0.743;NW[30]=0.802;NW[31]=0.867;NW[32]=1.24;NW[33]=-0.93;NW[34]=1.15;NW[35]=-1.08;}
  1101.  
  1102. //+------------------------------------------------------------------+
  1103. // UI DRAWING FUNCTIONS
  1104. //+------------------------------------------------------------------+
  1105. 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);}
  1106. 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);}
  1107. 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);}
  1108. 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,10,true);if(StringLen(val)>0)Tx(id+"_v",x+10,y+36,val,vc,vfs,true);}
  1109.  
  1110. //+------------------------------------------------------------------+
  1111. // OTHER PAIRS CUBE
  1112. //+------------------------------------------------------------------+
  1113. void DrawOtherPairsCube(int x,int y,int w,int h){
  1114. Bx("c20",x,y,w,h,BG_DARK2,NEON_GOLD);
  1115. Tx("c20_l",x+10,y+6,"OTHER PAIRS",NEON_CYAN,9,true);
  1116. if(g_pairCount==0){
  1117. Tx("c20_v0",x+10,y+32,"Scanning...",CGR,9,false);
  1118. return;
  1119. }
  1120. int lY=28;
  1121. for(int i=0;i<g_pairCount && i<5;i++){
  1122. string sp=g_pairNames[i];
  1123. if(StringLen(sp)>12) sp=StringSubstr(sp,0,12);
  1124. string txt=sp+" "+g_pairSigs[i]+" "+DoubleToString(g_pairConfs[i],0)+"%";
  1125. color sc=(g_pairSigs[i]=="CALL")?NEON_GREEN:NEON_RED;
  1126. Tx("c20_p"+IntegerToString(i),x+10,y+lY,txt,sc,9,true);
  1127. lY+=17;
  1128. }
  1129. }
  1130.  
  1131. //+------------------------------------------------------------------+
  1132. // OTC QUICK CUBE
  1133. //+------------------------------------------------------------------+
  1134. void DrawOTCQuickCube(int x,int y,int w,int h){
  1135. Bx("c_quick",x,y,w,h,BG_DARK2,NEON_LIME);
  1136. Tx("c_quick_l",x+10,y+6,"OTC QUICK",NEON_CYAN,9,true);
  1137. int age=(int)(TimeCurrent()-Time[0]);
  1138. int ps=Period()*60;
  1139. int rem=ps-age;
  1140. if(rem<0) rem=0;
  1141. int mm=rem/60, ss=rem%60;
  1142. string tl=(mm>0?IntegerToString(mm)+"m ":"")+IntegerToString(ss)+"s";
  1143. Tx("c_quick_t",x+10,y+26,"Time: "+tl,NEON_GOLD,10,true);
  1144. double spread=MarketInfo(Symbol(),MODE_SPREAD);
  1145. color spC=(spread<=BROKER_SPREAD_THRESHOLD)?NEON_GREEN:NEON_ORANGE;
  1146. Tx("c_quick_s",x+10,y+48,"Spread: "+DoubleToString(spread,0),spC,9,true);
  1147. double v1=iVolume(NULL,PERIOD_M1,1), v2=iVolume(NULL,PERIOD_M1,2);
  1148. double vr=(v2>0)?v1/v2:0;
  1149. color vC=(vr>1.5)?NEON_RED:NEON_CYAN;
  1150. // ✅ FONT SIZE 9 → 11 (Volume number bada)
  1151. Tx("c_quick_v",x+10,y+68,"Vol: "+DoubleToString(vr,1)+"x",vC,11,true);
  1152. string es=GetEntryStatus();
  1153. color eC=GetEntryColor();
  1154. Tx("c_quick_e",x+10,y+90,es,eC,9,true);
  1155. }
  1156.  
  1157. //+------------------------------------------------------------------+
  1158. // ENTRY STATUS FUNCTIONS - NOW ACTIVE
  1159. //+------------------------------------------------------------------+
  1160. string GetEntryStatus(){
  1161. int age=(int)(TimeCurrent()-Time[0]);
  1162. int ps=Period()*60;
  1163. int rem=ps-age;
  1164. if(rem<0) rem=0;
  1165. string ms=DetectMyStrategy();
  1166.  
  1167. if(ms!="WAIT"){
  1168. string type=(g_strategyType=="TRAP")?"TRAP":"TREND";
  1169. if(ENTRY_MODE=="FLAG"){
  1170. if(age<FLAG_MIN_SEC) return type+" WAIT "+IntegerToString(FLAG_MIN_SEC-age)+"s";
  1171. if(rem<MIN_REMAINING_SEC) return type+" EXPIRED";
  1172. if(age>FLAG_MAX_SEC) return type+" LATE";
  1173. return type+" OPEN "+IntegerToString(rem)+"s";
  1174. }
  1175. if(age<TIME_MIN_SEC) return type+" WAIT "+IntegerToString(TIME_MIN_SEC-age)+"s";
  1176. if(age>TIME_MAX_SEC) return type+" CLOSED";
  1177. return type+" OPEN "+IntegerToString(age)+"s";
  1178. }
  1179.  
  1180. if(ENTRY_MODE=="FLAG"){
  1181. if(age<FLAG_MIN_SEC) return "WAIT "+IntegerToString(FLAG_MIN_SEC-age)+"s";
  1182. if(rem<MIN_REMAINING_SEC) return "EXPIRED";
  1183. if(age>FLAG_MAX_SEC) return "LATE";
  1184. return "OPEN "+IntegerToString(rem)+"s left";
  1185. }
  1186. if(age<TIME_MIN_SEC) return "WAIT "+IntegerToString(TIME_MIN_SEC-age)+"s";
  1187. if(age>TIME_MAX_SEC) return "CLOSED";
  1188. return "OPEN "+IntegerToString(age)+"s";
  1189. }
  1190.  
  1191. color GetEntryColor(){
  1192. string s=GetEntryStatus();
  1193. if(StringFind(s,"TRAP OPEN")>=0) return NEON_PURPLE;
  1194. if(StringFind(s,"TREND OPEN")>=0) return NEON_LIME;
  1195. if(StringFind(s,"OPEN")>=0) return NEON_GREEN;
  1196. if(StringFind(s,"WAIT")>=0) return NEON_YELLOW;
  1197. if(StringFind(s,"LATE")>=0 || StringFind(s,"EXPIRED")>=0 || StringFind(s,"CLOSED")>=0) return NEON_ORANGE;
  1198. return NEON_RED;
  1199. }
  1200.  
  1201. //+------------------------------------------------------------------+
  1202. // DIRECTION OK - NOW CALLED IN FINAL SIGNAL
  1203. //+------------------------------------------------------------------+
  1204. bool DirectionOK(){
  1205. if(!ENABLE_ENTRY_ZONE) return true;
  1206. double cur=Close[0], lo=Low[0], hi=High[0], rg=hi-lo;
  1207. if(rg==0) return true;
  1208. double pos=(cur-lo)/rg*100.0;
  1209. if(MathMax(g_calc_gProb,g_calc_rProb)>=85) return true;
  1210. string ms=DetectMyStrategy();
  1211. if(ms=="CALL") return(pos>REVERSAL_PCT);
  1212. if(ms=="PUT") return(pos<(100.0-REVERSAL_PCT));
  1213. if(StringFind(g_adv1,"CALL")>=0) return(pos>REVERSAL_PCT && (hi-cur)<rg*WICK_PCT);
  1214. if(StringFind(g_adv1,"PUT")>=0) return(pos<(100.0-REVERSAL_PCT) && (cur-lo)<rg*WICK_PCT);
  1215. return true;
  1216. }
  1217.  
  1218. //+------------------------------------------------------------------+
  1219. // ENTRY PRECISION OK - NOW CALLED IN FINAL SIGNAL
  1220. //+------------------------------------------------------------------+
  1221. bool EntryPrecisionOK(){
  1222. if(TRADE_MODE=="GOD") return true;
  1223. if(g_calc_gProb>80 && Close[0]<Open[0]) return false;
  1224. if(g_calc_rProb>80 && Close[0]>Open[0]) return false;
  1225. return true;
  1226. }
  1227.  
  1228. //+------------------------------------------------------------------+
  1229. // CONSENSUS FILTER - NOW ACTIVE
  1230. //+------------------------------------------------------------------+
  1231. bool ConsensusOK(){
  1232. if(!CONSENSUS_FILTER) return true;
  1233. string ms=DetectMyStrategy();
  1234. if(ms=="WAIT") return true;
  1235.  
  1236. bool haAgree=false;
  1237. if(ms=="CALL" && (g_haM1=="HA BULLISH 1" || g_haBoth=="BOTH BULL")) haAgree=true;
  1238. if(ms=="PUT" && (g_haM1=="HA BEARISH 1" || g_haBoth=="BOTH BEAR")) haAgree=true;
  1239.  
  1240. return haAgree;
  1241. }
  1242.  
  1243. //+------------------------------------------------------------------+
  1244. // TELEGRAM NOTIFICATION - NOW ACTIVE
  1245. //+------------------------------------------------------------------+
  1246. void SendTelegramAlert(string message){
  1247. if(!ENABLE_TELEGRAM) return;
  1248. if(StringLen(TELEGRAM_TOKEN)<10 || StringLen(TELEGRAM_CHAT_ID)<5) return;
  1249.  
  1250. string params="?chat_id="+TELEGRAM_CHAT_ID+"&text="+message;
  1251.  
  1252. string headers="Content-Type: application/x-www-form-urlencoded";
  1253. string cookie="",result="";
  1254. char post[],result_data[];
  1255. int timeout=5000;
  1256.  
  1257. ArrayResize(post,StringToCharArray(params,post,0,WHOLE_ARRAY,CP_UTF8)-1);
  1258.  
  1259. int res=WebRequest("POST",url,headers,timeout,post,result_data,result);
  1260. if(res==-1){
  1261. Print("Telegram: WebRequest failed. Check URL/Internet");
  1262. }
  1263. }
  1264.  
  1265. //+------------------------------------------------------------------+
  1266. // MAIN DRAW FUNCTION
  1267. //+------------------------------------------------------------------+
  1268. void Draw(){
  1269. HideSPM(); DelDashboard();
  1270. int W=DASH_W, G=8, CW=(W-2*G)/3, CH=100, cx=POS_X+G, yy=POS_Y;
  1271. int cp=0; double brain=BrainSc(cp); double nb=NeuralBiasFast(); double mw=MicroWick(); bool bf=IsBrokerForce();
  1272. g_marketMode=DetectMarketMode(brain,g_rsc,cp,nb);
  1273. if(StringLen(g_prevMode)>0 && g_prevMode!=g_marketMode){ g_signalTime=0; g_finalSignal="WAIT"; }
  1274. g_prevMode=g_marketMode;
  1275. CalcOTCCrowd(); UpdateAccuracy(); UpdateWeights();
  1276. double mai=CalcAdvancedMicroAI(); int mts=CalcAdvancedMicroTrap();
  1277. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  1278. UpdateRiskControl();
  1279. CheckMTFConfirmation();
  1280. double tfaScore=CalcAdvancedTFA(g_tfa_detail);
  1281. color ha1C=(g_haM1=="HA BULLISH 1")?NEON_GREEN:(g_haM1=="HA BEARISH 1")?NEON_RED:NEON_YELLOW;
  1282. color ha5C=(g_haM5=="HA BULLISH 5")?NEON_GREEN:(g_haM5=="HA BEARISH 5")?NEON_RED:NEON_YELLOW;
  1283. color biasClr; string biasStr=CalcAdvancedMarketBias(biasClr,brain,g_rsc);
  1284. string ms=DetectMyStrategy();
  1285. color stratC=(g_strategyType=="TRAP")?NEON_PURPLE:(g_strategyType=="TREND")?NEON_GREEN:CGR;
  1286. string stratLabel=(g_strategyType=="TRAP")?"TRAP "+ms:(g_strategyType=="TREND")?"TREND "+ms:"NO STRATEGY";
  1287. string finalSig=g_finalSignal; color sigC=NEON_YELLOW;
  1288. if(finalSig=="CALL") sigC=NEON_GREEN; else if(finalSig=="PUT") sigC=NEON_RED;
  1289. else if(finalSig=="WEAK CALL") sigC=NEON_CYAN; else if(finalSig=="WEAK PUT") sigC=NEON_ORANGE;
  1290.  
  1291. // ROW 1 - PAIR, SIGNAL, PREDICTION
  1292. Cube("c1",cx,yy,CW,CH,NEON_CYAN,BG_DARK2,"PAIR",NEON_CYAN,Symbol(),NEON_BLUE,13);
  1293. Cube("c2",cx+CW+G,yy,CW,CH,sigC,BG_DARK2,"SIGNAL",NEON_CYAN,finalSig,sigC,14);
  1294. double gP,rP; string rsn; color pClr;
  1295. CalcAdvancedPrediction(gP,rP,rsn,pClr);
  1296. g_frozen_gProb=gP; g_frozen_rProb=rP; g_frozen_pAction=rsn; g_frozen_pColor=pClr; g_last_freeze_bar=Time[0];
  1297. Bx("c_pred",cx+2*(CW+G),yy,CW,CH,BG_DARK2,pClr);
  1298. Tx("c_pred_l",cx+2*(CW+G)+10,yy+5,"NEXT CANDLE",NEON_CYAN,9,true);
  1299. int bW=CW-20, gBW=(int)(gP/100.0*bW), rBW=(int)(rP/100.0*bW);
  1300. if(gBW<3) gBW=3; if(rBW<3) rBW=3;
  1301. Bx("c_pred_gbg",cx+2*(CW+G)+10,yy+22,bW,6,BG_DARK4,BG_DARK4);
  1302. Bx("c_pred_gfg",cx+2*(CW+G)+10,yy+22,gBW,6,NEON_GREEN,NEON_GREEN);
  1303. Tx("c_pred_gp",cx+2*(CW+G)+10,yy+30,"G:"+DoubleToString(gP,0)+"%",NEON_GREEN,9,true);
  1304. Bx("c_pred_rbg",cx+2*(CW+G)+10,yy+44,bW,6,BG_DARK4,BG_DARK4);
  1305. Bx("c_pred_rfg",cx+2*(CW+G)+10,yy+44,rBW,6,NEON_RED,NEON_RED);
  1306. Tx("c_pred_rp",cx+2*(CW+G)+10,yy+52,"R:"+DoubleToString(rP,0)+"%",NEON_RED,9,true);
  1307. Tx("c_pred_a",cx+2*(CW+G)+10,yy+68,rsn,pClr,9,true);
  1308. NeonBar("c_pred_b",cx+2*(CW+G)+10,yy+86,bW,6,MathMax(gP,rP),pClr);
  1309. yy+=CH+G;
  1310.  
  1311. // ROW 2 - BRAIN, SPM, SESSION
  1312. Cube("c4",cx,yy,CW,CH,NEON_CYAN,BG_DARK3,"BRAIN",NEON_CYAN,(brain>=0?"+":"")+DoubleToString(brain,1),(brain>0)?NEON_GREEN:NEON_RED,16);
  1313. Cube("c5",cx+CW+G,yy,CW,CH,NEON_CYAN,BG_DARK3,"SPM",NEON_CYAN,(g_rsc>=0?"+":"")+DoubleToString(g_rsc,1),(g_rsc>0)?NEON_GREEN:NEON_RED,16);
  1314. color sClr=CGR; string sName=GetCurrentSession(sClr); bool isAct=IsSessionActive();
  1315. Bx("c_ses",cx+2*(CW+G),yy,CW,CH,BG_DARK2,sClr);
  1316. Tx("c_ses_l",cx+2*(CW+G)+10,yy+5,"SESSION",NEON_CYAN,9,true);
  1317. Tx("c_ses_v",cx+2*(CW+G)+10,yy+24,sName,sClr,13,true);
  1318. int gH=TimeHour(TimeGMT()), gM=TimeMinute(TimeGMT());
  1319. Tx("c_ses_t",cx+2*(CW+G)+10,yy+50,"GMT "+(gH<10?"0":"")+IntegerToString(gH)+":"+(gM<10?"0":"")+IntegerToString(gM),NEON_WHITE,10,false);
  1320. Tx("c_ses_s",cx+2*(CW+G)+10,yy+70,isAct?"ACTIVE":"LOW VOL",isAct?NEON_GREEN:NEON_ORANGE,9,true);
  1321. yy+=CH+G;
  1322.  
  1323. // ROW 3 - NEURAL AI, CROWD, STRATEGY
  1324. 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,15);
  1325. NeonBar("c7b",cx+12,yy+82,CW-24,6,nb,(nb>72)?NEON_ORANGE:(nb<35)?NEON_CYAN:NEON_GREEN);
  1326. Bx("c8",cx+CW+G,yy,CW,CH,BG_DARK2,NEON_CYAN);
  1327. Tx("c8_l",cx+CW+G+10,yy+5,"CROWD",NEON_CYAN,9,true);
  1328. Tx("c8_c",cx+CW+G+10,yy+24,"CALL "+IntegerToString(g_otcCallPct)+"%",NEON_RED,12,true);
  1329. Tx("c8_p",cx+CW+G+10,yy+46,"PUT "+IntegerToString(g_otcPutPct)+"%",NEON_GREEN,12,true);
  1330. Tx("c8_b",cx+CW+G+10,yy+70,"OTC Crowd",NEON_GOLD,9,false);
  1331. Cube("c_strat",cx+2*(CW+G),yy,CW,CH,stratC,BG_DARK3,"STRATEGY",NEON_CYAN,stratLabel,stratC,11);
  1332. yy+=CH+G;
  1333.  
  1334. // ROW 4 - MICRO AI, MICRO TRAP, TFA SCORE
  1335. color maC; string maDir;
  1336. if(mai>72){ maC=NEON_ORANGE; maDir="TRAP RISK!"; }
  1337. else if(mai>55){ maC=NEON_YELLOW; maDir="CAUTION"; }
  1338. else if(mai>45){ maC=NEON_CYAN; maDir="NEUTRAL"; }
  1339. else{ maC=NEON_GREEN; maDir="SAFE"; }
  1340. Cube("c_mai",cx,yy,CW,CH,maC,BG_DARK2,"MICRO AI",NEON_CYAN,DoubleToString(mai,0)+"%",maC,16);
  1341. NeonBar("c_mai_b",cx+12,yy+82,CW-24,6,mai,maC);
  1342. color mtsC=(mts>=90)?NEON_PURPLE:(mts>=75)?NEON_RED:(mts>=50)?NEON_ORANGE:NEON_GREEN;
  1343. string mtsL=(mts>=90)?"EXTREME TRAP":(mts>=75)?"HIGH TRAP":(mts>=50)?"MEDIUM":"CLEAR";
  1344. Cube("c_mts",cx+CW+G,yy,CW,CH,mtsC,BG_DARK2,"MICRO TRAP",NEON_CYAN,"SCORE "+IntegerToString(mts),mtsC,13);
  1345. NeonBar("c_mts_b",cx+CW+G+12,yy+82,CW-24,6,MathMin(100,mts),mtsC);
  1346.  
  1347. int tfaInt=(int)tfaScore; color tfaColor; string tfaLabel;
  1348. if(tfaInt>=5){ tfaLabel="STRONG"; tfaColor=NEON_GREEN; }
  1349. else if(tfaInt>=4){ tfaLabel="GOOD"; tfaColor=NEON_LIME; }
  1350. else if(tfaInt>=3){ tfaLabel="MEDIUM"; tfaColor=NEON_YELLOW; }
  1351. else if(tfaInt>=2){ tfaLabel="WEAK"; tfaColor=NEON_ORANGE; }
  1352. else{ tfaLabel="POOR"; tfaColor=C'100,100,120'; }
  1353. color tfaBC=tfaColor; string dirTxt="";
  1354. if(g_haM1=="HA BEARISH 1"||g_haM5=="HA BEARISH 5"){ tfaBC=NEON_RED; dirTxt=" [DOWN]"; }
  1355. else if(g_haM1=="HA BULLISH 1"||g_haM5=="HA BULLISH 5"){ tfaBC=NEON_GREEN; dirTxt=" [UP]"; }
  1356. else{ dirTxt=" [MIX]"; }
  1357. Bx("c_tfa",cx+2*(CW+G),yy,CW,CH,BG_DARK3,tfaBC);
  1358. Tx("c_tfa_l",cx+2*(CW+G)+10,yy+5,"TFA SCORE",NEON_CYAN,9,true);
  1359. Tx("c_tfa_v",cx+2*(CW+G)+10,yy+24,IntegerToString(tfaInt)+"/6 "+tfaLabel+dirTxt,tfaBC,11,true);
  1360. Tx("c_tfa_d",cx+2*(CW+G)+10,yy+50,g_tfa_detail,CGR,8,false);
  1361. NeonBar("c_tfa_b",cx+2*(CW+G)+10,yy+82,CW-20,6,(double)tfaInt/6.0*100.0,tfaBC);
  1362. yy+=CH+G;
  1363.  
  1364. // ROW 5 - FRACTAL, MARKET BIAS, S/R LEVELS
  1365. double frac=CalcAdvancedFractal();
  1366. color frC=(frac>=75)?NEON_RED:(frac>=50)?NEON_ORANGE:(frac>=25)?NEON_YELLOW:NEON_GREEN;
  1367. string frT=(frac>=75)?"TRAP ZONE!":(frac>=50)?"WARNING":(frac>=25)?"CAUTION":"CLEAR";
  1368. Cube("c_frc",cx,yy,CW,CH,frC,BG_DARK2,"FRACTAL",NEON_CYAN,frT,frC,11);
  1369. NeonBar("c_frc_b",cx+12,yy+82,CW-24,6,frac,frC);
  1370. Bx("c_bias",cx+CW+G,yy,CW,CH,BG_DARK2,biasClr);
  1371. Tx("c_bias_l",cx+CW+G+10,yy+5,"MARKET BIAS",NEON_CYAN,9,true);
  1372. Tx("c_bias_v",cx+CW+G+10,yy+24,biasStr,biasClr,12,true);
  1373. Tx("c_bias_m",cx+CW+G+10,yy+48,"MODE: "+g_marketMode,(g_marketMode=="TREND")?NEON_GREEN:(g_marketMode=="REVERSAL")?NEON_RED:NEON_YELLOW,9,false);
  1374. color accC=(g_accuracy>=70)?NEON_GREEN:(g_accuracy>=60)?NEON_YELLOW:NEON_RED;
  1375. Tx("c_bias_a",cx+CW+G+10,yy+68,"ACC: "+DoubleToString(g_accuracy,1)+"%",accC,9,false);
  1376. NeonBar("c_bias_b",cx+CW+G+10,yy+82,CW-20,6,g_accuracy,accC);
  1377. int dg_sr=(int)MarketInfo(Symbol(),MODE_DIGITS); if(dg_sr<=0) dg_sr=5;
  1378. Bx("c18",cx+2*(CW+G),yy,CW,CH,BG_DARK2,NEON_CYAN);
  1379. Tx("c18_l",cx+2*(CW+G)+10,yy+4,"S/R LEVELS",NEON_CYAN,9,true);
  1380. Tx("c18_r",cx+2*(CW+G)+10,yy+24,"R: "+(g_nearest_res>0?DoubleToString(g_nearest_res,dg_sr):"--"),NEON_RED,12,true);
  1381. Tx("c18_s",cx+2*(CW+G)+10,yy+48,"S: "+(g_nearest_sup>0?DoubleToString(g_nearest_sup,dg_sr):"--"),NEON_GREEN,12,true);
  1382. Tx("c18_b",cx+2*(CW+G)+10,yy+72,g_brk_str,g_brk_color,10,true);
  1383. yy+=CH+G;
  1384.  
  1385. // ROW 6 - WICKS, HA CANDLES, REVERSAL
  1386. color wickC=(mw>0.70)?NEON_RED:(mw>0.50)?NEON_ORANGE:NEON_GREEN;
  1387. Bx("c15",cx,yy,CW,CH,BG_DARK2,wickC);
  1388. Tx("c15_l",cx+10,yy+5,"WICKS",NEON_CYAN,9,true);
  1389. Tx("c15_v",cx+10,yy+24,DoubleToString(mw,2),wickC,16,true);
  1390. Tx("c15_b",cx+10,yy+52,(mw>0.70)?"KILL SHOT":(mw>0.50)?"HIGH":"LOW",wickC,10,false);
  1391. NeonBar("c15_bar",cx+10,yy+82,CW-20,6,mw*100,wickC);
  1392. Bx("c_ha",cx+CW+G,yy,CW,CH,BG_DARK2,ha1C);
  1393. Tx("c_ha_l",cx+CW+G+10,yy+5,"HA CANDLES",NEON_CYAN,9,true);
  1394. Tx("c_ha_m1",cx+CW+G+10,yy+26,"M1: "+g_haM1,ha1C,11,true);
  1395. Tx("c_ha_m5",cx+CW+G+10,yy+48,"M5: "+g_haM5,ha5C,11,true);
  1396. string haComb=""; color haCombC=NEON_YELLOW;
  1397. if(g_haM1=="HA BULLISH 1" && g_haM5=="HA BULLISH 5"){ haComb="BOTH BULL"; haCombC=NEON_GREEN; }
  1398. else if(g_haM1=="HA BEARISH 1" && g_haM5=="HA BEARISH 5"){ haComb="BOTH BEAR"; haCombC=NEON_RED; }
  1399. else haComb="MIXED";
  1400. Tx("c_ha_c",cx+CW+G+10,yy+72,haComb,haCombC,10,true);
  1401. string revReason=DetectSuddenReversal(); color revColor=NEON_YELLOW;
  1402. if(StringFind(revReason,"BULL")>=0) revColor=NEON_GREEN;
  1403. if(StringFind(revReason,"BEAR")>=0) revColor=NEON_RED;
  1404. Bx("c_reason",cx+2*(CW+G),yy,CW,CH,BG_DARK2,revColor);
  1405. Tx("c_reason_l",cx+2*(CW+G)+10,yy+5,"REVERSAL",NEON_CYAN,9,true);
  1406. if(StringLen(revReason)>0){
  1407. string sr=revReason; if(StringLen(sr)>30) sr=StringSubstr(sr,0,28)+"..";
  1408. Tx("c_reason_v",cx+2*(CW+G)+10,yy+28,sr,revColor,9,false);
  1409. } else Tx("c_reason_v",cx+2*(CW+G)+10,yy+28,"No reversal",CGR,9,false);
  1410. yy+=CH+G;
  1411.  
  1412. // ROW 7 - HRN, OTHER PAIRS, OTC QUICK, FILTER STATUS
  1413. int bottomW = (W - 2*G - 2*G) / 3;
  1414. int bottomH = 110;
  1415. yy += 5;
  1416. int x1 = cx;
  1417. Bx("c_hrn", x1, yy, bottomW, bottomH, BG_DARK2, NEON_PINK);
  1418. Tx("c_hrn_l", x1+8, yy+6, "HRN LEVEL", NEON_PINK, 9, true);
  1419. Tx("c_hrn_v", x1+8, yy+28, g_hrn_str, NEON_PINK, 12, true);
  1420. Tx("c_hrn_t", x1+8, yy+54, (g_hrn_is_sup?"SUPPORT":"RESISTANCE"), NEON_PINK, 9, false);
  1421. Tx("c_hrn_b", x1+8, yy+76, "Brk "+IntegerToString(g_hrn_brk_bars)+"/"+IntegerToString(HRN_CONFIRM_BARS), NEON_PINK, 8, false);
  1422. Tx("c_hrn_s", x1+8, yy+94, g_isSideways?"SIDEWAYS":"TRENDING", g_isSideways?NEON_YELLOW:NEON_GREEN, 8, false);
  1423. int x2 = x1 + bottomW + G;
  1424. DrawOtherPairsCube(x2, yy, bottomW, bottomH);
  1425. int x3 = x2 + bottomW + G;
  1426. DrawOTCQuickCube(x3, yy, bottomW, bottomH);
  1427.  
  1428. UpdateTimer(); HideSPM(); ChartRedraw();
  1429. }
  1430.  
  1431. //+------------------------------------------------------------------+
  1432. // RISK CONTROL - NOW FULLY ACTIVE
  1433. //+------------------------------------------------------------------+
  1434. void UpdateRiskControl(){
  1435. static datetime llt=0;
  1436.  
  1437. if(ENABLE_RISK_LOCK){
  1438. if(g_lossStreak>=MAX_LOSS_STREAK){
  1439. g_tradingStopped=true;
  1440. llt=TimeCurrent();
  1441. g_filterStatus="RISK LOCK "+IntegerToString(g_lossStreak)+"L";
  1442. }
  1443. if(g_tradingStopped && TimeCurrent()-llt>3600){
  1444. g_tradingStopped=false;
  1445. g_lossStreak=0;
  1446. g_filterStatus="RISK UNLOCK";
  1447. }
  1448. }
  1449. }
  1450.  
  1451. //+------------------------------------------------------------------+
  1452. // FINAL SIGNAL ENGINE - ALL FILTERS ACTIVE
  1453. //+------------------------------------------------------------------+
  1454. void FinalSignalEngine(double brain,int cp,int mts){
  1455. g_filterStatus="ALL CLEAR";
  1456.  
  1457. // RISK CONTROL CHECK
  1458. if(ENABLE_RISK_CONTROL && g_tradingStopped){
  1459. g_finalSignal="WAIT";
  1460. g_finalColor=NEON_ORANGE;
  1461. g_filterStatus="RISK LOCKED";
  1462. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1463. return;
  1464. }
  1465.  
  1466. // SESSION FILTER CHECK
  1467. if(ENABLE_SESSION_FILTER && !IsSessionActive()){
  1468. g_finalSignal="WAIT";
  1469. g_finalColor=NEON_ORANGE;
  1470. g_filterStatus="SESSION OFF";
  1471. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1472. return;
  1473. }
  1474.  
  1475. // MTF CONFIRMATION CHECK
  1476. if(MTF_CONFIRM && !g_mtfConfirmed){
  1477. g_finalSignal="WAIT";
  1478. g_finalColor=NEON_ORANGE;
  1479. g_filterStatus="MTF NO CONF";
  1480. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1481. return;
  1482. }
  1483.  
  1484. // SPIKE FILTER CHECK
  1485. if(ENABLE_SPIKE_FILTER && IsBigCandle(1)){
  1486. g_finalSignal="WAIT";
  1487. g_finalColor=NEON_ORANGE;
  1488. g_filterStatus="SPIKE BLOCK";
  1489. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1490. return;
  1491. }
  1492.  
  1493. // LAST SECOND BLOCK CHECK
  1494. if(ENABLE_LAST_SEC_BLOCK){
  1495. int age=(int)(TimeCurrent()-Time[0]);
  1496. int ps=Period()*60;
  1497. int rem=ps-age;
  1498. if(rem<0) rem=0;
  1499. if(rem < MIN_REMAINING_SEC){
  1500. g_finalSignal="WAIT";
  1501. g_finalColor=NEON_ORANGE;
  1502. g_filterStatus="LAST SEC BLOCK";
  1503. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1504. return;
  1505. }
  1506. }
  1507.  
  1508. // MIN CONFIDENCE CHECK
  1509. double conf=MathMax(g_calc_gProb,g_calc_rProb);
  1510. if(conf < MIN_CONFIDENCE){
  1511. g_finalSignal="WAIT";
  1512. g_finalColor=NEON_YELLOW;
  1513. g_filterStatus="LOW CONF "+DoubleToString(conf,0)+"%";
  1514. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1515. return;
  1516. }
  1517.  
  1518. // STRATEGY SIGNAL FIRST
  1519. string ms=DetectMyStrategy();
  1520. if(ms!="WAIT"){
  1521. // CONSENSUS FILTER CHECK
  1522. if(CONSENSUS_FILTER && !ConsensusOK()){
  1523. g_finalSignal="WAIT";
  1524. g_finalColor=NEON_ORANGE;
  1525. g_filterStatus="NO CONSENSUS";
  1526. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1527. return;
  1528. }
  1529.  
  1530. // ENTRY ZONE CHECK
  1531. if(ENABLE_ENTRY_ZONE && !DirectionOK()){
  1532. g_finalSignal="WAIT";
  1533. g_finalColor=NEON_ORANGE;
  1534. g_filterStatus="BAD ENTRY ZONE";
  1535. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1536. return;
  1537. }
  1538.  
  1539. // ENTRY PRECISION CHECK
  1540. if(!EntryPrecisionOK()){
  1541. g_finalSignal="WAIT";
  1542. g_finalColor=NEON_ORANGE;
  1543. g_filterStatus="PRECISION FAIL";
  1544. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1545. return;
  1546. }
  1547.  
  1548. g_finalSignal=ms;
  1549. g_finalColor=(ms=="CALL")?NEON_GREEN:NEON_RED;
  1550. g_filterStatus=(g_strategyType=="TRAP")?"TRAP ACTIVE":"TREND ACTIVE";
  1551. if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT"){
  1552. g_finalSignal=ms; g_signalTime=TimeCurrent();
  1553. }
  1554.  
  1555. // SEND NOTIFICATIONS
  1556. SendSignalNotification(ms,conf);
  1557. return;
  1558. }
  1559.  
  1560. // NORMAL SIGNAL LOGIC
  1561. double th=FAST_MODE?70:68;
  1562. if(g_calc_gProb>th) g_finalSignal="CALL";
  1563. else if(g_calc_rProb>th) g_finalSignal="PUT";
  1564. else if(g_calc_gProb>60) g_finalSignal="WEAK CALL";
  1565. else if(g_calc_rProb>60) g_finalSignal="WEAK PUT";
  1566. else g_finalSignal="WAIT";
  1567.  
  1568. // ENTRY ZONE CHECK FOR NORMAL SIGNALS
  1569. if((g_finalSignal=="CALL" || g_finalSignal=="PUT") && ENABLE_ENTRY_ZONE && !DirectionOK()){
  1570. g_finalSignal="WAIT";
  1571. g_finalColor=NEON_ORANGE;
  1572. g_filterStatus="BAD ENTRY ZONE";
  1573. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1574. return;
  1575. }
  1576.  
  1577. // ENTRY PRECISION CHECK FOR NORMAL SIGNALS
  1578. if((g_finalSignal=="CALL" || g_finalSignal=="PUT") && !EntryPrecisionOK()){
  1579. g_finalSignal="WAIT";
  1580. g_finalColor=NEON_ORANGE;
  1581. g_filterStatus="PRECISION FAIL";
  1582. if(TimeCurrent()-g_signalTime>HOLD_SEC) g_signalTime=TimeCurrent();
  1583. return;
  1584. }
  1585.  
  1586. if(TimeCurrent()-g_signalTime>HOLD_SEC||g_finalSignal=="WAIT")
  1587. g_signalTime=TimeCurrent();
  1588.  
  1589. if(g_finalSignal=="CALL"){
  1590. g_finalColor=NEON_GREEN;
  1591. g_filterStatus="GREEN SIGNAL";
  1592. SendSignalNotification("CALL",conf);
  1593. }
  1594. else if(g_finalSignal=="PUT"){
  1595. g_finalColor=NEON_RED;
  1596. g_filterStatus="RED SIGNAL";
  1597. SendSignalNotification("PUT",conf);
  1598. }
  1599. else if(g_finalSignal=="WEAK CALL"){
  1600. g_finalColor=NEON_CYAN;
  1601. g_filterStatus="WEAK GREEN";
  1602. }
  1603. else if(g_finalSignal=="WEAK PUT"){
  1604. g_finalColor=NEON_ORANGE;
  1605. g_filterStatus="WEAK RED";
  1606. }
  1607. else{
  1608. g_finalColor=NEON_YELLOW;
  1609. }
  1610. }
  1611.  
  1612. //+------------------------------------------------------------------+
  1613. // SEND SIGNAL NOTIFICATION - NOW ACTIVE
  1614. //+------------------------------------------------------------------+
  1615. void SendSignalNotification(string sig,double conf){
  1616. if(!ENABLE_NOTIFY) return;
  1617.  
  1618. string msg=Symbol()+" | "+sig+" | "+DoubleToString(conf,0)+"% | "+g_filterStatus;
  1619.  
  1620. // Prevent spam - same signal within 10 seconds
  1621. if(msg==g_lastNotified) return;
  1622. if(TimeCurrent()-StringToTime(TimeToString(TimeCurrent()))<10) return;
  1623.  
  1624. g_lastNotified=msg;
  1625.  
  1626. // Alert popup
  1627. Alert(msg);
  1628.  
  1629. // Push notification
  1630. SendNotification(msg);
  1631.  
  1632. // Telegram (if enabled)
  1633. if(ENABLE_TELEGRAM){
  1634. string tgMsg="*AIBRAIN Signal*\n"+Symbol()+"\n"+sig+"\nConf: "+DoubleToString(conf,0)+"%\n"+g_filterStatus;
  1635. SendTelegramAlert(tgMsg);
  1636. }
  1637.  
  1638. // Print to log
  1639. Print(msg);
  1640. }
  1641.  
  1642. //+------------------------------------------------------------------+
  1643. // BROADCAST SIGNAL
  1644. //+------------------------------------------------------------------+
  1645. void BroadcastSignal(){
  1646. double conf=MathMax(g_frozen_gProb,g_frozen_rProb);if(conf<55)return;
  1647. string fn="OTC_Signals_Master.txt",pair=Symbol();
  1648. string sig=(g_frozen_gProb>g_frozen_rProb)?"CALL":"PUT";
  1649. int exp=(int)(TimeCurrent()+65);
  1650. string line=pair+"|"+sig+"|"+DoubleToString(conf,0)+"|"+IntegerToString((int)TimeCurrent())+"|"+IntegerToString(exp);
  1651. int h=FileOpen(fn,FILE_READ|FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);
  1652. if(h==INVALID_HANDLE){h=FileOpen(fn,FILE_WRITE|FILE_TXT|FILE_SHARE_READ|FILE_SHARE_WRITE);if(h==INVALID_HANDLE)return;FileWriteString(h,line+"\n");FileClose(h);return;}
  1653. string content="";while(!FileIsEnding(h)){string ex=FileReadString(h);if(StringLen(ex)>10&&StringFind(ex,pair+"|")!=0)content+=ex+"\n";}
  1654. content=line+"\n"+content;
  1655. string lns[];int cnt=0,pos=0;int f=StringFind(content,"\n",pos);
  1656. while(f!=-1&&cnt<100){ArrayResize(lns,cnt+1);lns[cnt]=StringSubstr(content,pos,f-pos);cnt++;pos=f+1;f=StringFind(content,"\n",pos);}
  1657. if(pos<StringLen(content)){ArrayResize(lns,cnt+1);lns[cnt]=StringSubstr(content,pos);cnt++;}
  1658. if(cnt>50){content="";for(int i=0;i<50;i++)if(StringLen(lns[i])>10)content+=lns[i]+"\n";}
  1659. FileSeek(h,0,SEEK_SET);FileWriteString(h,content);FileFlush(h);FileClose(h);
  1660. }
  1661.  
  1662. //+------------------------------------------------------------------+
  1663. // HELPER: HA DIRECTION (Mathematically correct for MQL4)
  1664. //+------------------------------------------------------------------+
  1665. string GetPairHADirection(string pair){
  1666. double open1 = iOpen(pair, PERIOD_M1, 1);
  1667. double high1 = iHigh(pair, PERIOD_M1, 1);
  1668. double low1 = iLow(pair, PERIOD_M1, 1);
  1669. double close1= iClose(pair, PERIOD_M1, 1);
  1670. double open2 = iOpen(pair, PERIOD_M1, 2);
  1671. double high2 = iHigh(pair, PERIOD_M1, 2);
  1672. double low2 = iLow(pair, PERIOD_M1, 2);
  1673. double close2= iClose(pair, PERIOD_M1, 2);
  1674. double open3 = iOpen(pair, PERIOD_M1, 3);
  1675. double close3= iClose(pair, PERIOD_M1, 3);
  1676. if(open1==0||high1==0||low1==0||close1==0||open2==0||close2==0||open3==0||close3==0)
  1677. return "NEUTRAL";
  1678. double haClose1 = (open1+high1+low1+close1)/4.0;
  1679. double haClose2 = (open2+high2+low2+close2)/4.0;
  1680. double haOpen2_approx = (open3+close3)/2.0;
  1681. double haOpen1 = (haOpen2_approx + haClose2)/2.0;
  1682. if(haClose1 > haOpen1) return "BULL";
  1683. else if(haClose1 < haOpen1) return "BEAR";
  1684. return "NEUTRAL";
  1685. }
  1686.  
  1687. //+------------------------------------------------------------------+
  1688. // HELPER: VOLUME RATIO
  1689. //+------------------------------------------------------------------+
  1690. double GetPairVolumeRatio(string pair){
  1691. double v1 = iVolume(pair, PERIOD_M1, 1);
  1692. double v2 = iVolume(pair, PERIOD_M1, 2);
  1693. if(v2 <= 0) return 1.0;
  1694. return v1 / v2;
  1695. }
  1696.  
  1697. //+------------------------------------------------------------------+
  1698. // SCAN SIGNALS - STRONG (62% confidence + HA match + no volume spike)
  1699. //+------------------------------------------------------------------+
  1700. void ScanSignals(){
  1701. g_pairCount = 0;
  1702. string fn = "OTC_Signals_Master.txt";
  1703. string cur = Symbol();
  1704. int h = FileOpen(fn, FILE_READ|FILE_TXT|FILE_SHARE_READ);
  1705. if(h == INVALID_HANDLE) return;
  1706.  
  1707. datetime now = TimeCurrent();
  1708. while(!FileIsEnding(h) && g_pairCount < 5){
  1709. string ln = FileReadString(h);
  1710. if(StringLen(ln) < 15) continue;
  1711.  
  1712. int p1 = StringFind(ln, "|");
  1713. int p2 = StringFind(ln, "|", p1+1);
  1714. int p3 = StringFind(ln, "|", p2+1);
  1715. int p4 = StringFind(ln, "|", p3+1);
  1716. if(p1 < 0 || p2 < 0 || p3 < 0 || p4 < 0) continue;
  1717.  
  1718. string pair = StringSubstr(ln, 0, p1);
  1719. if(pair == cur) continue;
  1720.  
  1721. string sig = StringSubstr(ln, p1+1, p2-p1-1);
  1722. double conf = StringToDouble(StringSubstr(ln, p2+1, p3-p2-1));
  1723. int ex = (int)StringToInteger(StringSubstr(ln, p4+1));
  1724.  
  1725. // --- STRONG SIGNAL FILTERS ---
  1726. if(conf < 62) continue; // 62% threshold
  1727. if(ex < (int)(now - 10)) continue;
  1728.  
  1729. string haDir = GetPairHADirection(pair);
  1730. bool haMatch = false;
  1731. if(sig == "CALL" && haDir == "BULL") haMatch = true;
  1732. if(sig == "PUT" && haDir == "BEAR") haMatch = true;
  1733. if(!haMatch) continue;
  1734.  
  1735. double vr = GetPairVolumeRatio(pair);
  1736. if(vr > 2.0) continue;
  1737.  
  1738. g_pairNames[g_pairCount] = pair;
  1739. g_pairSigs[g_pairCount] = sig;
  1740. g_pairConfs[g_pairCount] = conf;
  1741. g_pairCount++;
  1742. }
  1743. FileClose(h);
  1744. }
  1745.  
  1746. //+------------------------------------------------------------------+
  1747. // READ SPM
  1748. //+------------------------------------------------------------------+
  1749. void ReadSPM(){
  1750. datetime cb=Time[0];if(cb==g_last_bar&&TimeCurrent()-g_spm_t<5)return;
  1751. double v1=iCustom(NULL,PERIOD_M1,SPM_FILE,0,0),v2=iCustom(NULL,PERIOD_M1,SPM_FILE,1,0);
  1752. if(v1!=EMPTY_VALUE&&v1!=0){double sm=0.7;g_rsc=g_rsc*(1.0-sm)+v1*sm;}
  1753. if(v2!=EMPTY_VALUE&&v2!=0){double sm=0.7;g_tfa=g_tfa*(1.0-sm)+v2*sm;}
  1754. g_last_bar=cb;g_spm_t=TimeCurrent();
  1755. }
  1756.  
  1757. //+------------------------------------------------------------------+
  1758. // INIT
  1759. //+------------------------------------------------------------------+
  1760. int OnInit(){
  1761. InitNW();NuclearDeleteAll();
  1762. resCount=0;supCount=0;lastSRBar=0;g_hrn_price=0;g_hrn_brk_bars=0;g_hrn_scan_bar=0;
  1763. g_nearest_res=0;g_nearest_sup=0;g_rj_cnt=0;g_pairCount=0;
  1764. g_marketMode="RANGE";g_prevMode="";g_last_freeze_bar=0;
  1765. g_lastNotified="";g_copiedPair="";g_accuracy=ValidateHistoricalAccuracy();
  1766. g_acc_correct=0;g_acc_total=0;g_acc_lastBar=0;
  1767. g_lastPredGreen=50.0;g_lastPredBar=0;g_lastLogBar=0;
  1768. g_lossStreak=0;g_tradingStopped=false;g_stopStartTime=0;
  1769. g_strategyType="NONE";g_strategySignal="WAIT";g_strategyReason="";
  1770. g_frozen_gProb=50;g_frozen_rProb=50;g_calc_gProb=50;g_calc_rProb=50;
  1771. g_commonCount=0;g_wickLineCount=0;g_tfa_detail="";g_htfLevelCount=0;g_lastLevelScan=0;
  1772. g_mtfConfirmed=true;g_filterStatus="ALL CLEAR";
  1773. LoadMLWeights();EventSetTimer(1);
  1774.  
  1775. // Show filter status on init
  1776. string filters="";
  1777. if(ENABLE_SESSION_FILTER) filters+="Session ";
  1778. if(ENABLE_SPIKE_FILTER) filters+="Spike ";
  1779. if(ENABLE_LAST_SEC_BLOCK) filters+="LastSec ";
  1780. if(ENABLE_RISK_CONTROL) filters+="Risk ";
  1781. if(ENABLE_ENTRY_ZONE) filters+="EntryZone ";
  1782. if(MTF_CONFIRM) filters+="MTF ";
  1783. if(CONSENSUS_FILTER) filters+="Consensus ";
  1784. if(ENABLE_NOTIFY) filters+="Notify ";
  1785.  
  1786. Print("AIBRAIN v44.6 FINAL LOADED");
  1787. Print("Active Filters: ",filters);
  1788. Print("Telegram: ",ENABLE_TELEGRAM?"ON":"OFF");
  1789.  
  1790. return INIT_SUCCEEDED;
  1791. }
  1792.  
  1793. //+------------------------------------------------------------------+
  1794. // DEINIT
  1795. //+------------------------------------------------------------------+
  1796. void OnDeinit(const int r){
  1797. EventKillTimer();Comment("");
  1798. for(int i=ObjectsTotal()-1;i>=0;i--){string nm=ObjectName(i);if(StringFind(nm,PFX)==0)ObjectDelete(nm);}
  1799. }
  1800.  
  1801. //+------------------------------------------------------------------+
  1802. // ON TIMER
  1803. //+------------------------------------------------------------------+
  1804. void OnTimer(){
  1805. if(Bars<50)return;
  1806. static datetime last5min=0;
  1807. if(Time[0]/300 != last5min/300){ DetectHiddenLevels(); last5min=Time[0]; }
  1808. ReadSPM();CalcHA();UpdateHRN();UpdateSR();
  1809. DetectCommonPoints();DetectWickRejections();
  1810. DrawCommonPoints();DrawWickRejectLines();DrawHiddenLevels();DrawNearestSR();
  1811. int cp=0;double brain=BrainSc(cp);double nb=NeuralBiasFast();double mw=MicroWick();bool bf=IsBrokerForce();
  1812. double mai=CalcAdvancedMicroAI();int mts=CalcAdvancedMicroTrap();
  1813. CalcPrediction(brain,nb,cp,mai,mts,g_rsc,bf,mw);
  1814. FinalSignalEngine(brain,cp,mts);
  1815. BroadcastSignal();ScanSignals();
  1816. Draw();
  1817. }
  1818.  
  1819. //+------------------------------------------------------------------+
  1820. // ON CALCULATE
  1821. //+------------------------------------------------------------------+
  1822. int OnCalculate(const int rates_total,const int prev_calculated,
  1823. const datetime &time[],const double &open[],
  1824. const double &high[],const double &low[],const double &close[],
  1825. const long &tick_volume[],const long &volume[],const int &spread[]){
  1826. return(rates_total);
  1827. }
  1828. //+------------------------------------------------------------------+