EA don't open order
- www.mql5.com
Hi All,
I am a newbie in programming.
I am trying to build an expert advisor based on Engulfing Pattern.
That is, I want the EA to open a long position after a Bullish Engulfing is formed in an uptrend (50 EMA is above 200 EMA) and to open a short position after a Bearish Engulfing is formed in a downtrend (50 EMA is below 200EMA).
When I am running the EA in strategy tester, no positions are being opened.
Can someone please advise what is wrong with the attached code.
//+------------------------------------------------------------------+ //| Engulfing Pattern.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict double Open1; double Open2; double Close1; double Close2; double High1; double High2; double Low1; double Low2; double FastMA; double SlowMA; extern int StopLoss=100; extern int TakeProfit=100; extern double Lot_Size = 0.01; //double TakeProfit= NormalizeDouble(Take_Profit*10*Point,5); //double StopLoss= NormalizeDouble(Stop_Loss*10*Point,5); //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { Open1 = NormalizeDouble(iOpen(Symbol(), Period(), 1), Digits); Open2 = NormalizeDouble(iOpen(Symbol(), Period(), 2), Digits); Close1 = NormalizeDouble(iClose(Symbol(), Period(), 1), Digits); Close2 = NormalizeDouble(iClose(Symbol(), Period(), 2), Digits); High1 = NormalizeDouble(iHigh(Symbol(), Period(), 1), Digits); High2 = NormalizeDouble(iHigh(Symbol(), Period(), 2), Digits); Low1 = NormalizeDouble(iLow(Symbol(), Period(), 1), Digits); Low2 = NormalizeDouble(iLow(Symbol(), Period(), 2), Digits); FastMA = iMA(Symbol(), PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, 1); SlowMA = iMA(Symbol(), PERIOD_CURRENT, 200, 0, MODE_EMA, PRICE_CLOSE, 1); if ((FastMA < SlowMA) && CheckBullishEngulfing()) Long(); if ((FastMA > SlowMA) && CheckBearishEngulfing()) Short(); } //+------------------------------------------------------------------+ bool CheckBullishEngulfing() { bool Sonuc; if (Open1 < Close1 && Open2 > Close2 && MathAbs(Close1-Open1) > MathAbs(Open2-Close2) ) Sonuc=true; return(Sonuc); } bool CheckBearishEngulfing() { bool Sonuc; if (Open1 > Close1 && Open2 < Close2 && MathAbs(Open1-Close1) > MathAbs(Close2-Open2) ) Sonuc=true; return(Sonuc); } void Long() { if (OrdersTotal() == 0) { int LongTicket = OrderSend (Symbol(), OP_BUYSTOP, Lot_Size, High1, 3, NormalizeDouble((High1-StopLoss*Point),Digits), NormalizeDouble((High1+TakeProfit*Point),Digits),"BUY", 123456, clrNONE); if(GetLastError()!=0) Print(" BUY Error ",GetLastError()); } } void Short() { if (OrdersTotal() == 0) { // int ShortTicket = OrderSend (Symbol(), OP_SELLSTOP, Lot_Size, Low1, 3, NormalizeDouble((Low1+StopLoss*Point),Digits), NormalizeDouble((Low1-TakeProfit*Point),Digits), "SELL", 123456, clrNONE); if(GetLastError()!=0) Print(" SELL Error ",GetLastError()); } }
Hi All,
I am a newbie in programming.
I am trying to build an expert advisor based on Engulfing Pattern.
That is, I want the EA to open a long position after a Bullish Engulfing is formed in an uptrend (50 EMA is above 200 EMA) and to open a short position after a Bearish Engulfing is formed in a downtrend (50 EMA is below 200EMA).
When I am running the EA in strategy tester, no positions are being opened.
Can someone please advise what is wrong with the attached code.