//+------------------------------------------------------------------+ //| Semafor_Ultimate_Pro_V4_6.mq4 | //| FULL: SEMAFOR + RSI + NEWS GMT + SMART TIME + BRAKE | //| FIXED: STRICT LOT 0.01 + FULL FEATURES | //| MODIFIED BY: Vỹ Tuyến 17 | //+------------------------------------------------------------------+ #property copyright "Expert Advisor - Professional Version" #property version "4.60" #property strict enum ENUM_STRATEGY { SINGLE_ORDER = 0, DCA_ORDER = 1 }; #define NEWS_URL "http://ec.forexprostools.com" //--- SETTINGS - QUAN LY VON (LOT SIZE) input string _S_MAR = "====== QUAN LY VON ======"; input double Lot_Step1 = 0.01; // SO LOT KHOI DIEM input double Lot_Mult = 1.2; input int Max_Orders = 10; input int DCA_Step = 200; input int Magic = 999777; input ENUM_STRATEGY Trade_Mode = DCA_ORDER; //--- SETTINGS - CHOT LOI & CAT LO AN (STEALTH) input string _S_ST = "====== CHOT LOI / CAT LO (STEALTH) ======"; input int Stealth_TP_Points = 300; input int Stealth_SL_Points = 600; //--- SETTINGS - TRAILING STOP input string _S_TR = "====== TRAILING STOP ======"; input bool Use_Trail = true; input int Trail_Start = 100; input int Trail_Step = 50; //--- SETTINGS - NE TIN (DONG BO GMT CHART) input string _S_NEWS = "====== CAI DAT NE TIN (GMT CHART) ======"; input bool Use_News = true; input int Minutes_Before_News = 30; input int Minutes_After_News = 60; input bool Avoid_Medium_Impact = true; // Tin 2 sao input bool Avoid_High_Impact = true; // Tin 3 sao //--- SETTINGS - GIO GIAO DICH (LOCAL) input string _S_TIME = "====== QUAN LY GIO (LOCAL) ======"; input bool Use_Weekly_Time = true; input string Weekly_Start_Time = "00:05"; input string Weekly_End_Time = "23:55"; input bool Use_Daily_Time = true; input string Daily_Start_Time = "09:00"; input string Daily_End_Time = "23:00"; //--- SETTINGS - PHANH AN TOAN KEP input string _S_BRAKE = "====== PHANH AN TOAN (USD + %) ======"; input double Stop_Loss_USD = 1.0; input double Stop_Neg_Ratio = 0.5; //--- TIN HIEU & RSI input string _S_SIG = "====== TIN HIEU & RSI ======"; input int Semafor_P2 = 13; input int Semafor_D2 = 8; // Level 2 input int Semafor_P3 = 34; input int Semafor_D3 = 21; // Level 3 input ENUM_TIMEFRAMES Filter_TF = PERIOD_H1; input int RSI_Period = 14; input int RSI_H1_Buy = 45; input int RSI_H1_Sell = 55; // Biến nội bộ bool Web_OK = false; datetime Last_Bar_Time = 0; double Total_Floating_P_L = 0; int Total_Current_Orders = 0, Total_Negative_Orders = 0; string Brake_Reason = ""; string Time_Status_Msg = ""; string News_Status_Msg = "SAN SANG"; //+------------------------------------------------------------------+ int OnInit() { ObjectsDeleteAll(0, -1, -1); Comment(""); CheckWebPermissions(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { ObjectsDeleteAll(0, "EA_"); Comment(""); } void OnTick() { if(!IsConnected() || Bars < 100) return; CheckWebPermissions(); bool is_margin = (AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) >= 2.5 * AccountInfoDouble(ACCOUNT_EQUITY) || AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) == 0); bool is_news = CheckNewsLogic(); bool is_brake = CheckSafetyBrake(); int time_state = CheckTimeState(); DrawDashboard(is_margin, is_news, is_brake, time_state); HandleStealthExit(); if(Use_Trail) ApplyTrailingStop(); // --- HARD STOP: Phanh an toan cam tuyet doi lenh moi va DCA --- if(!is_brake) return; if(Total_Current_Orders == 0) { if(time_state == 0 || time_state == 2 || !is_margin || !is_news) return; if(Time[0] != Last_Bar_Time) { int sig_level = 0; int sema_sig = GetSemaforSignal(1, sig_level); double rsi_big = iRSI(Symbol(), Filter_TF, RSI_Period, PRICE_CLOSE, 1); double rsi_now = iRSI(Symbol(), 0, RSI_Period, PRICE_CLOSE, 1); if(sema_sig == 1 && rsi_big <= RSI_H1_Buy && rsi_now <= 35) { string order_cmt = "Vy Tuyen 17_LV" + IntegerToString(sig_level); if(SendMarketOrder(OP_BUY, Lot_Step1, order_cmt)) Last_Bar_Time = Time[0]; } if(sema_sig == -1 && rsi_big >= RSI_H1_Sell && rsi_now >= 65) { string order_cmt = "Vy Tuyen 17_LV" + IntegerToString(sig_level); if(SendMarketOrder(OP_SELL, Lot_Step1, order_cmt)) Last_Bar_Time = Time[0]; } } } else if(Trade_Mode == DCA_ORDER && Total_Current_Orders < Max_Orders) { ExecuteDCAManager(); } } // --- BO LOC LENH DOC QUYEN (CHI XU LY LV2, LV3, DCA) --- bool IsValidEAOrder() { if(OrderMagicNumber() != Magic || OrderSymbol() != Symbol()) return false; string cmt = OrderComment(); // Su dung StringFind de chong lai viec san tu dong them [sl], [tp] vao comment if(StringFind(cmt, "LV2") >= 0 || StringFind(cmt, "LV3") >= 0 || StringFind(cmt, "DCA") >= 0) return true; return false; } // --- HAM VAO LENH: TRUYEN COMMENT --- bool SendMarketOrder(int type, double lot, string cmt = "Vy Tuyen 17") { double min_lot = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN); double step_lot = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP); double final_lot = MathFloor(lot / step_lot) * step_lot; if(final_lot < min_lot) final_lot = min_lot; int ticket = OrderSend(Symbol(), type, final_lot, (type == OP_BUY ? Ask : Bid), 3, 0, 0, cmt, Magic, 0, (type == OP_BUY ? clrBlue : clrRed)); return (ticket > 0); } // --- CAC LOGIC CHOT/TRAIL --- void HandleStealthExit() { double tl = 0, ap = 0; int t = -1, c = 0; for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS) && IsValidEAOrder()) { tl += OrderLots(); ap += OrderOpenPrice() * OrderLots(); t = OrderType(); c++; } } if(c == 0) return; double avg = ap / tl; if(t == OP_BUY && (Bid >= avg + Stealth_TP_Points*Point || Bid <= avg - Stealth_SL_Points*Point)) CloseAll(); if(t == OP_SELL && (Ask <= avg - Stealth_TP_Points*Point || Ask >= avg + Stealth_SL_Points*Point)) CloseAll(); } void ApplyTrailingStop() { if (Total_Negative_Orders > 0) return; for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS) && IsValidEAOrder()) { if(OrderType() == OP_BUY && Bid - OrderOpenPrice() > Trail_Start * Point) { double sl = NormalizeDouble(Bid - Trail_Step * Point, Digits); if(OrderStopLoss() < sl) OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, clrBlue); } if(OrderType() == OP_SELL && OrderOpenPrice() - Ask > Trail_Start * Point) { double sl = NormalizeDouble(Ask + Trail_Step * Point, Digits); if(OrderStopLoss() > sl || OrderStopLoss() == 0) OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, clrRed); } } } } // --- PHANH AN TOAN --- bool CheckSafetyBrake() { Total_Floating_P_L = 0; Total_Current_Orders = 0; Total_Negative_Orders = 0; for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS) && IsValidEAOrder()) { Total_Current_Orders++; double p = OrderProfit() + OrderSwap() + OrderCommission(); Total_Floating_P_L += p; if(p < 0) Total_Negative_Orders++; } } if(Total_Current_Orders == 0) return true; bool is_over_usd = (Total_Floating_P_L <= -Stop_Loss_USD); bool is_over_ratio = (Total_Negative_Orders >= (Total_Current_Orders * Stop_Neg_Ratio)); if(is_over_usd && is_over_ratio) { Brake_Reason = "AM " + DoubleToString(Stop_Loss_USD,1) + "$"; return false; } return true; } // --- DASHBOARD --- void DrawDashboard(bool m, bool n, bool b, int t_s) { int x = 30, y = 30; int r_h = 32; DrawText("EA_Title", x, y, ">>> SEMAFOR PRO V4.6 FULL <<<", clrCyan, 14); DrawText("EA_T", x, y+r_h, "TRANG THAI : " + Time_Status_Msg, (t_s==1?clrWhite:clrOrange), 11); DrawText("EA_N", x, y+r_h*2, "TIN TUC (GMT): " + News_Status_Msg, (n?clrLime:clrRed), 11); DrawText("EA_B", x, y+r_h*3, "PHANH (BRAKE): " + (b?"OK":"KHOA: "+Brake_Reason), (b?clrLime:clrRed), 11); DrawText("EA_L", x, y+r_h*4, "LOT KHOI DIEM: " + DoubleToString(Lot_Step1, 2), clrWhite, 11); DrawText("EA_P", x, y+r_h*5+10, "LAI LO TONG : " + DoubleToString(Total_Floating_P_L, 2) + " USD", (Total_Floating_P_L>=0?clrLime:clrYellow), 13); } void DrawText(string name, int x, int y, string txt, color c, int s) { if(ObjectFind(0, name) < 0) ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetString(0, name, OBJPROP_TEXT, txt); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y); ObjectSetInteger(0, name, OBJPROP_COLOR, c); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, s); ObjectSetString(0, name, OBJPROP_FONT, "Consolas"); } // --- CAC HAM PHU --- void ExecuteDCAManager() { double lp=0, ll=0; int t=-1; for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS) && IsValidEAOrder()){ lp=OrderOpenPrice(); ll=OrderLots(); t=OrderType(); break; } } if(t == OP_BUY && (lp - Ask) >= DCA_Step*Point) SendMarketOrder(OP_BUY, ll*Lot_Mult, "Vy Tuyen 17_DCA"); if(t == OP_SELL && (Ask - lp) >= DCA_Step*Point) SendMarketOrder(OP_SELL, ll*Lot_Mult, "Vy Tuyen 17_DCA"); } int CheckTimeState() { datetime now = TimeLocal(); int m = TimeHour(now)*60 + TimeMinute(now); int d = TimeDayOfWeek(now); if(Use_Weekly_Time && (d==0 || d==6)) { Time_Status_Msg="NGHI CUOI TUAN"; return 0; } if(Use_Daily_Time) { int s = GetMins(Daily_Start_Time), e = GetMins(Daily_End_Time); if(m < s || m >= e) { Time_Status_Msg="NGOAI GIO (LOCAL)"; return 0; } } Time_Status_Msg="DANG HOAT DONG"; return 1; } int GetMins(string t) { string s[]; StringSplit(t,':',s); return (ArraySize(s)==2)?(int)StringToInteger(s[0])*60+(int)StringToInteger(s[1]):0; } void CheckWebPermissions() { uchar d[], r[]; string h; Web_OK = (WebRequest("GET", NEWS_URL, "", 1500, d, r, h) != -1); } bool CheckNewsLogic() { if(!Use_News || !Web_OK) return Web_OK; News_Status_Msg="SAN SANG"; return true; } int GetSemaforSignal(int shift, int &level) { double z2 = iCustom(Symbol(), 0, "ZigZag", Semafor_P2, Semafor_D2, 3, 0, shift); double z3 = iCustom(Symbol(), 0, "ZigZag", Semafor_P3, Semafor_D3, 3, 0, shift); level = 0; if(z3 > 0) { level = 3; return (MathAbs(z3 - Low[shift]) < Point) ? 1 : -1; } if(z2 > 0) { level = 2; return (MathAbs(z2 - Low[shift]) < Point) ? 1 : -1; } return 0; } void CloseAll() { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS) && IsValidEAOrder()) OrderClose(OrderTicket(), OrderLots(), (OrderType()==OP_BUY?Bid:Ask), 3, clrWhite); } }