hello everybody. I tried to create my first, simple, expert advisor to be use for XAUUSD.
but when i put it into MT5 grafic, nothing happen.. it is not working.
is there here someone more expert than me that can check the code and let me understand what is wrong ?
thanks a lot in advance
regards
marco
Traders and coders are working for free:
- if it is interesting for them personally, or
- if it is interesting for many members on this forum.
Freelance section of the forum should be used in most of the cases.
- 2025.07.03
- www.mql5.com
Hi
i supposed that is correct. i post it here.
//+------------------------------------------------------------------+ //| GoldCrosserEA.mq5 | //| Expert Advisor con EMA crossover, hedge e trailing stop | //+------------------------------------------------------------------+ #property description "EA basato su incrocio EMA 10/20, con hedging e trailing stop" // Input input double LotSizeMain = 0.03; input double LotSizeHedge = 0.02; input double TakeProfitPips = 200; input double StepPips = 100; input bool UseTrailingStop = true; input double TrailingStopPips = 50; input ENUM_TIMEFRAMES Timeframe = PERIOD_M5; // Variabili globali double lastBuyPrice = 0; double lastSellPrice = 0; //+------------------------------------------------------------------+ //| Funzione di inizializzazione | //+------------------------------------------------------------------+ int OnInit() { Print("GoldCrosserEA inizializzato"); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Funzione principale | //+------------------------------------------------------------------+ void OnTick() { if(_Period != Timeframe) return; double ema10[], ema20[]; int h_ema10 = iMA(_Symbol, Timeframe, 10, 0, MODE_EMA, PRICE_CLOSE); int h_ema20 = iMA(_Symbol, Timeframe, 20, 0, MODE_EMA, PRICE_CLOSE); if(CopyBuffer(h_ema10, 0, 0, 2, ema10) <= 0 || CopyBuffer(h_ema20, 0, 0, 2, ema20) <= 0) return; // Fallisce il recupero buffer, esci double ema10_0 = ema10[0]; // Valore corrente double ema10_1 = ema10[1]; // Valore precedente double ema20_0 = ema20[0]; double ema20_1 = ema20[1]; double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); double tp_price = TakeProfitPips * point; double step = StepPips * point; double trail = TrailingStopPips * point; double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); //--- Incrocio RIBASSISTA if(ema10_1 > ema20_1 && ema10_0 < ema20_0) { // ... } //--- Incrocio RIALZISTA if(ema10_1 < ema20_1 && ema10_0 > ema20_0) { // ... } // Trailing stop if(UseTrailingStop) ApplyTrailingStop(trail); } //+------------------------------------------------------------------+ //| Funzione per apertura ordini | //+------------------------------------------------------------------+ bool OpenOrder(ENUM_ORDER_TYPE type, double lots, double price, double tp) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = lots; request.type = type; request.price = price; request.tp = tp; request.sl = 0; request.deviation = 20; request.magic = 123456; request.type_filling = ORDER_FILLING_IOC; if(!OrderSend(request, result) || result.retcode != TRADE_RETCODE_DONE) { Print("Errore invio ordine: ", result.retcode); return false; } return true; } //+------------------------------------------------------------------+ //| Funzione per trailing stop | //+------------------------------------------------------------------+ void ApplyTrailingStop(double trail) { for(int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(!PositionSelectByTicket(ticket)) continue; string sym = PositionGetString(POSITION_SYMBOL); if(sym != _Symbol) continue; double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); long type = PositionGetInteger(POSITION_TYPE); double new_sl = sl; if(type == POSITION_TYPE_BUY) { double newStop = currentPrice - trail; if(newStop > openPrice && (sl == 0 || newStop > sl)) new_sl = newStop; } else if(type == POSITION_TYPE_SELL) { double newStop = currentPrice + trail; if(newStop < openPrice && (sl == 0 || newStop < sl)) new_sl = newStop; } if(new_sl != sl) { MqlTradeRequest req; MqlTradeResult res; ZeroMemory(req); ZeroMemory(res); req.action = TRADE_ACTION_SLTP; req.symbol = _Symbol; req.sl = new_sl; req.tp = tp; req.position = ticket; req.magic = 123456; if(!OrderSend(req, res) || res.retcode != TRADE_RETCODE_DONE) { Print("Errore nel trailing stop: ", res.retcode); } } } }
- You are accessing indicators wrongly (handles should be assigned into OnInit, and acessed with CopyBuffer into OnTick)
- The EA do nothing if the chart timeframe is different from the "TImeframe" declared in input (no reason)
- Your code is incomplete, there are nothing into the "entry conditions" block.
- Pay attention to acess shift 0 of any indicator/candle price. This values changes continously until the end of the bar.
In few words, there are nothing correct in that code.
You should ask the AI that generated that to fix it, or to hire a professional developer to write a working EA...
- You are accessing indicators wrongly (handles should be assigned into OnInit, and acessed with CopyBuffer into OnTick)
- The EA do nothing if the chart timeframe is different from the "TImeframe" declared in input (no reason)
- Your code is incomplete, there are nothing into the "entry conditions" block.
- Pay attention to acess shift 0 of any indicator/candle price. This values changes continously until the end of the bar.
In few words, there are nothing correct in that code.
You should ask the AI that generated that to fix it, or to hire a professional developer to write a working EA...
ciao
already asked AI to fix mistakes. and this is the results..
i will move so to freelance topics..
thanks
ciao
already asked AI to fix mistakes. and this is the results..
i will move so to freelance topics..
thanks
Hmm, well you are interested in writing code, if I'm not mistaken.
So then, why not go through an article instead of going to AI to learn https://www.mql5.com/en/articles/15299
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
hello everybody. I tried to create my first, simple, expert advisor to be use for XAUUSD.
but when i put it into MT5 grafic, nothing happen.. it is not working.
is there here someone more expert than me that can check the code and let me understand what is wrong ?
thanks a lot in advance
regards
marco