Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+-------------------------------------------------------------------------------------+
- //| MultiTF_CandleOverlayEA.mq5 |
- //| Expert pentru multiple timeframe overlay și trade management |
- //+-------------------------------------------------------------------------------------+
- #property copyright "ChatGPT"
- #property version "1.00"
- #property strict
- #define ColorAqua clrAqua
- #define ColorRed clrRed
- input double Lots = 0.1;
- input double SL_Points = 100; // stop loss in points
- input double TP_Points = 200; // take profit in points
- input double TrailStart = 50; // trailing start in points
- input double TrailStep = 20; // trailing step in points
- //--- simbolul curent
- string symbol;
- //--- handle-uri pentru timeframe-uri
- ENUM_TIMEFRAMES tfH1 = PERIOD_H1;
- ENUM_TIMEFRAMES tfH4 = PERIOD_H4;
- ENUM_TIMEFRAMES tfD1 = PERIOD_D1;
- ENUM_TIMEFRAMES tfW1 = PERIOD_W1;
- //--- nume obiecte grafice
- string objNameH1 = "Candle_H1";
- string objNameH4 = "Candle_H4";
- string objNameD1 = "Candle_D1";
- string objNameW1 = "Candle_W1";
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- symbol = _Symbol;
- //ChartSetInteger(0, CHART_SHOW_PERIOD_SEPARATORS, false); // ascunde liniile perioadelor pentru claritate
- // curata obiecte vechi
- ObjectDelete(0,objNameH1);
- ObjectDelete(0,objNameH4);
- ObjectDelete(0,objNameD1);
- ObjectDelete(0,objNameW1);
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Functie pentru obtinerea valorilor OHLC pentru o anumita TF |
- //+------------------------------------------------------------------+
- bool GetOHLC(ENUM_TIMEFRAMES tf, datetime &time_open, double &open, double &high, double &low, double &close)
- {
- MqlRates rates[];
- if(CopyRates(symbol, tf, 0, 1, rates) != 1)
- return(false);
- time_open = rates[0].time;
- open = rates[0].open;
- high = rates[0].high;
- low = rates[0].low;
- close = rates[0].close;
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Functie desenare candela suprapusa pe M1 |
- //+------------------------------------------------------------------+
- void DrawCandleOverlay(string name, datetime time_open, double open, double high, double low, double close, color col)
- {
- // Șterge obiectul vechi daca exista
- ObjectDelete(0,name);
- // Creaza un obiect rectangle pe grafic cu următoarele coordonate:
- // pe orizontala, de la time_open pana la timpul curent (acum)
- // pe verticala, intre low si high
- datetime time_now = TimeCurrent();
- // creeaza rectangle
- if(!ObjectCreate(0, name, OBJ_RECTANGLE, 0, time_open, high, time_now, low))
- {
- Print("Eroare la crearea obiectului ", name);
- return;
- }
- // setari vizuale
- ObjectSetInteger(0, name, OBJPROP_COLOR, col);
- ObjectSetInteger(0, name, OBJPROP_BACK, true); // sa fie in spate
- ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
- ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
- // Opacitate in functie de close > open
- if(close > open)
- ObjectSetInteger(0, name, OBJPROP_BGCOLOR, ColorToARGB(ColorAqua, 60));
- else
- ObjectSetInteger(0, name, OBJPROP_BGCOLOR, ColorToARGB(ColorRed, 60));
- }
- //+------------------------------------------------------------------+
- //| Functie pentru conversia culorii cu opacitate |
- //+------------------------------------------------------------------+
- ulong ColorToARGBAlpha(color c, uchar alpha)
- {
- return(((ulong)alpha << 24) | ((ulong)c & 0xFFFFFF));
- }
- //+------------------------------------------------------------------+
- //| Verifica regula simpla de intrare |
- //+------------------------------------------------------------------+
- int CheckTradeSignal()
- {
- datetime t_open;
- double open, high, low, close;
- // Preluam OHLC pentru fiecare timeframe
- if(!GetOHLC(tfH1, t_open, open, high, low, close)) return 0;
- bool h1Buy = (close > open);
- bool h1Sell = (close < open);
- if(!GetOHLC(tfH4, t_open, open, high, low, close)) return 0;
- bool h4Buy = (close > open);
- bool h4Sell = (close < open);
- if(!GetOHLC(tfD1, t_open, open, high, low, close)) return 0;
- bool d1Buy = (close > open);
- bool d1Sell = (close < open);
- if(!GetOHLC(tfW1, t_open, open, high, low, close)) return 0;
- bool w1Buy = (close > open);
- bool w1Sell = (close < open);
- // Conditii pentru BUY: toate in sus
- if(h1Buy && h4Buy && d1Buy && w1Buy) return 1;
- // Conditii pentru SELL: toate in jos
- if(h1Sell && h4Sell && d1Sell && w1Sell) return -1;
- return 0; // fara semnal clar
- }
- //+------------------------------------------------------------------+
- //| Functie executie ordine BUY |
- //+------------------------------------------------------------------+
- bool OpenBuy()
- {
- double price = SymbolInfoDouble(symbol, SYMBOL_ASK);
- double sl = price - SL_Points * _Point;
- double tp = price + TP_Points * _Point;
- MqlTradeRequest request;
- MqlTradeResult result;
- ZeroMemory(request);
- ZeroMemory(result);
- request.action = TRADE_ACTION_DEAL;
- request.symbol = symbol;
- request.volume = Lots;
- request.type = ORDER_TYPE_BUY;
- request.price = price;
- request.sl = sl;
- request.tp = tp;
- request.deviation= 10;
- request.magic = 123456;
- request.comment = "MultiTF_Buy";
- if(!OrderSend(request, result))
- {
- Print("Eroare la deschiderea BUY: ", GetLastError());
- return false;
- }
- Print("Buy deschis la ", price);
- return true;
- }
- //+------------------------------------------------------------------+
- //| Functie executie ordine SELL |
- //+------------------------------------------------------------------+
- bool OpenSell()
- {
- double price = SymbolInfoDouble(symbol, SYMBOL_BID);
- double sl = price + SL_Points * _Point;
- double tp = price - TP_Points * _Point;
- MqlTradeRequest request;
- MqlTradeResult result;
- ZeroMemory(request);
- ZeroMemory(result);
- request.action = TRADE_ACTION_DEAL;
- request.symbol = symbol;
- request.volume = Lots;
- request.type = ORDER_TYPE_SELL;
- request.price = price;
- request.sl = sl;
- request.tp = tp;
- request.deviation= 10;
- request.magic = 123456;
- request.comment = "MultiTF_Sell";
- if(!OrderSend(request, result))
- {
- Print("Eroare la deschiderea SELL: ", GetLastError());
- return false;
- }
- Print("Sell deschis la ", price);
- return true;
- }
- //+------------------------------------------------------------------+
- //| Functie de gestionare a trailing stop |
- //+------------------------------------------------------------------+
- void ManageTrailingStop()
- {
- ulong magic = 123456;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- ulong ticket = PositionGetTicket(i);
- if(PositionGetInteger(POSITION_MAGIC) != magic) continue;
- if(PositionGetString(POSITION_SYMBOL) != symbol) continue;
- double open_price = PositionGetDouble(POSITION_PRICE_OPEN);
- double current_price = SymbolInfoDouble(symbol, (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ? SYMBOL_BID : SYMBOL_ASK);
- double sl = PositionGetDouble(POSITION_SL);
- ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
- // Calculate profit in points
- double profit_points = (pos_type==POSITION_TYPE_BUY) ? (current_price - open_price)/_Point : (open_price - current_price)/_Point;
- if(profit_points > TrailStart)
- {
- double new_sl;
- if(pos_type==POSITION_TYPE_BUY)
- {
- new_sl = current_price - TrailStep * _Point;
- if(new_sl > sl)
- {
- // modifica SL in trailing
- ModifyPositionSL(ticket, new_sl);
- }
- }
- else
- {
- new_sl = current_price + TrailStep * _Point;
- if(new_sl < sl || sl==0)
- {
- ModifyPositionSL(ticket, new_sl);
- }
- }
- }
- }
- }
- //+------------------------------------------------------------------+
- //| Modificare SL pozitie |
- //+------------------------------------------------------------------+
- void ModifyPositionSL(ulong ticket, double new_sl)
- {
- MqlTradeRequest request;
- MqlTradeResult result;
- ZeroMemory(request);
- ZeroMemory(result);
- request.action = TRADE_ACTION_SLTP;
- request.position = ticket;
- request.sl = new_sl;
- request.tp = PositionGetDouble(POSITION_TP);
- if(!OrderSend(request, result))
- {
- Print("Eroare la modificarea SL: ", GetLastError());
- }
- else
- Print("SL modificat la ", DoubleToString(new_sl, _Digits));
- }
- //+------------------------------------------------------------------+
- //| Functia principala OnTick |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- // 1. Afisare candela suprapusa pe fiecare timeframe
- datetime t_open; double open, high, low, close;
- if(GetOHLC(tfH1, t_open, open, high, low, close))
- DrawCandleOverlay(objNameH1, t_open, open, high, low, close, (close>open) ? clrAqua : clrRed);
- if(GetOHLC(tfH4, t_open, open, high, low, close))
- DrawCandleOverlay(objNameH4, t_open, open, high, low, close, (close>open) ? clrAqua : clrRed);
- if(GetOHLC(tfD1, t_open, open, high, low, close))
- DrawCandleOverlay(objNameD1, t_open, open, high, low, close, (close>open) ? clrAqua : clrRed);
- if(GetOHLC(tfW1, t_open, open, high, low, close))
- DrawCandleOverlay(objNameW1, t_open, open, high, low, close, (close>open) ? clrAqua : clrRed);
- // 2. Verificam semnalul
- int signal = CheckTradeSignal();
- // 3. Managementul pozitiei
- ulong magic = 123456;
- bool hasPosition = false;
- ENUM_POSITION_TYPE pos_type = POSITION_TYPE_BUY;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetInteger(POSITION_MAGIC) != magic) continue;
- if(PositionGetString(POSITION_SYMBOL) != symbol) continue;
- hasPosition = true;
- pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
- break;
- }
- // 4. Executie ordine conform semnalului si pozitiei existente
- if(signal == 1) // BUY
- {
- if(!hasPosition || pos_type == POSITION_TYPE_SELL)
- {
- // inchidem sell daca exista
- if(hasPosition && pos_type == POSITION_TYPE_SELL)
- ClosePosition(symbol, POSITION_TYPE_SELL);
- // deschidem BUY
- OpenBuy();
- }
- }
- else if(signal == -1) // SELL
- {
- if(!hasPosition || pos_type == POSITION_TYPE_BUY)
- {
- // inchidem buy daca exista
- if(hasPosition && pos_type == POSITION_TYPE_BUY)
- ClosePosition(symbol, POSITION_TYPE_BUY);
- // deschidem SELL
- OpenSell();
- }
- }
- else
- {
- // fara semnal inchidem toate pozitiile noastre
- if(hasPosition)
- CloseAllPositions(symbol, magic);
- }
- // 5. Management trailing stop
- ManageTrailingStop();
- }
- //+------------------------------------------------------------------+
- //| Inchide pozitie dupa tip |
- //+------------------------------------------------------------------+
- void ClosePosition(string symb, ENUM_POSITION_TYPE type)
- {
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetInteger(POSITION_MAGIC) != 123456) continue;
- if(PositionGetString(POSITION_SYMBOL) != symb) continue;
- if(PositionGetInteger(POSITION_TYPE) != type) continue;
- ulong ticket = PositionGetTicket(i);
- ClosePositionByTicket(ticket);
- }
- }
- //+------------------------------------------------------------------+
- //| Inchide toate pozitiile pentru simbol si magic |
- //+------------------------------------------------------------------+
- void CloseAllPositions(string symb, ulong magic)
- {
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetInteger(POSITION_MAGIC) != magic) continue;
- if(PositionGetString(POSITION_SYMBOL) != symb) continue;
- ulong ticket = PositionGetTicket(i);
- ClosePositionByTicket(ticket);
- }
- }
- //+------------------------------------------------------------------+
- //| Inchide pozitia dupa ticket |
- //+------------------------------------------------------------------+
- void ClosePositionByTicket(ulong ticket)
- {
- MqlTradeRequest request;
- MqlTradeResult result;
- ZeroMemory(request);
- ZeroMemory(result);
- request.action = TRADE_ACTION_CLOSE_BY;
- request.position = ticket;
- if(!OrderSend(request,result))
- Print("Eroare inchidere pozitie: ", GetLastError());
- else
- Print("Pozitie inchisa, ticket: ", ticket);
- }
- //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement