EA doesn't open BUYSTOP orders

 

Hi


I have o problem with my EA. It doesn't open BUYSTOP orders. I copied the code to SELLSTOP which works perfectly. Can someone check what's wrong?


#property copyright "xxx"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double   OpenPrice;
double   StopLoss;
double   TakeProfit;
bool     Ticket;
double   Spread;

input int TimeFrame_Long = 1440;
input int TimeFrame_Short = 60; 
input int      MagicNumber = 55555;
input double   Change = 0.03;
input int      StosunekZysku = 2;
input double   LotSize = 0.2;


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
if(TotalOpenOrders() == 0)
   {
   if(TrendWzrostowy_D1() == true)
      {
      if(TrendWzrostowy_H1() == true)
         {
         Spread = Ask - Bid;
         OpenPrice = High[iHighest(NULL, TimeFrame_Short, MODE_HIGH, 5, 1)] + Change + Spread;
         StopLoss = iLow(NULL, TimeFrame_Short, 1) - Change;
         TakeProfit = OpenPrice + StosunekZysku*(OpenPrice - StopLoss);
         Ticket = OrderSend(NULL, OP_BUYSTOP, LotSize, OpenPrice, 0, StopLoss, TakeProfit, NULL, MagicNumber, 0, clrGreen);
         if(Ticket == true)
            {
            Alert("Otwarta pozycja BUY STOP");
            Sleep(TimeFrame_Short*60000);
            }
         else GetLastError();
         }   
      }         

   if(TrendSpadkowy_D1() == true)
      {
      if(TrendSpadkowy_H1() == true)
         {
         Spread = Ask - Bid;
         OpenPrice = Low[iLowest(NULL, TimeFrame_Short, MODE_LOW, 5, 1)] - Change;
         StopLoss = iHigh(NULL, TimeFrame_Short, 1) + Change + Spread;
         TakeProfit = OpenPrice - StosunekZysku*(StopLoss - OpenPrice);
         Ticket = OrderSend(NULL, OP_SELLSTOP, LotSize, OpenPrice, 0, StopLoss, TakeProfit, NULL, MagicNumber, 0, clrRed);
         if(Ticket == true)
            {
            Alert("Otwarta pozycja Sell STOP");
            Sleep(TimeFrame_Short*60000);
            }
         else GetLastError();
         }          
      }
   }

if(TotalOpenOrders() == 1)
   {
   if(OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == true)
      {
      if(OrderType() == OP_BUYSTOP)
         {
         if(iClose(NULL, TimeFrame_Short, 1) < iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 1))
            {
            OrderDelete(OrderTicket()); 
            }
         Alert(GetLastError());   
         }
      if(OrderType() == OP_SELLSTOP)     
         {
         if(iClose(NULL, TimeFrame_Short, 1) > iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 1))
            {
            OrderDelete(OrderTicket()); 
            }
         Alert(GetLastError());  
         }
      }
   }   
               
}

bool TrendWzrostowy_D1() // EMA8 > EMA21 i aktualna cena > EMA8
   {
   if(iMA(NULL, TimeFrame_Long, 8, 0, MODE_EMA, PRICE_CLOSE, 0) > iMA(NULL, TimeFrame_Long, 21, 0, MODE_EMA, PRICE_CLOSE, 0)
      && Bid > iMA(NULL, TimeFrame_Long, 8, 0, MODE_EMA, PRICE_CLOSE, 0))
         {
         return(true);
         }
   return(false);      
   }
   
bool TrendSpadkowy_D1() // EMA8 < EMA21 i aktualna cena < EMA8
   {
   if(iMA(NULL, TimeFrame_Long, 8, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, TimeFrame_Long, 21, 0, MODE_EMA, PRICE_CLOSE, 0)
      && Bid < iMA(NULL, TimeFrame_Long, 8, 0, MODE_EMA, PRICE_CLOSE, 0))
         {
         return(true);
         }
   return(false);
   }
   
bool TrendWzrostowy_H1() // EMA8 > EMA13 > EMA21, LOW 2 świeczki wstecz > EMA8, LOW 1 świeczka wstecz < EMA 8 i Cena zamknięcia tej świeczki > EMA21
   {
   if(iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 0) > iMA(NULL, TimeFrame_Short, 13, 0, MODE_EMA, PRICE_CLOSE, 0) > iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 0)
      && iLow(NULL, TimeFrame_Short, 2) > iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 2)
      && iLow(NULL, TimeFrame_Short, 1) < iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 1)
      && iClose(NULL, TimeFrame_Short, 1) > iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 1))
         {
         return(true);
         }
   return(false);
   }
   
bool TrendSpadkowy_H1() // EMA8 < EMA13 < EMA21, HIGH 2 świeczki wstecz < EMA8, HIGH 1 świeczka wstecz > EMA 8 i Cena zamknięcia tej świeczki < EMA21
   {
   if(iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, TimeFrame_Short, 13, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 0)
      && iHigh(NULL, TimeFrame_Short, 2) < iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 2)
      && iHigh(NULL, TimeFrame_Short, 1) > iMA(NULL, TimeFrame_Short, 8, 0, MODE_EMA, PRICE_CLOSE, 1)
      && iClose(NULL, TimeFrame_Short, 1) < iMA(NULL, TimeFrame_Short, 21, 0, MODE_EMA, PRICE_CLOSE, 1))
         {
         return(true);
         }
   return(false) ;      
   }
   
int TotalOpenOrders()
   {
      int total_orders = 0;
      for(int order = 0; order < OrdersTotal(); order++)
         {
            if(OrderSelect(order, SELECT_BY_POS, MODE_TRADES) == false) break;    
            if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
               {
                  total_orders++;
               }
         }
    return total_orders;
    }          
 
         Ticket = OrderSend(NULL, OP_BUYSTOP, LotSize, OpenPrice, 0, StopLoss, TakeProfit, NULL, MagicNumber, 0, clrGreen);
         if(Ticket == true)
  1. There is no need for Stop orders in EAs. Humans use them because they can't watch the market 24/day. EAs can; just wait until market reaches the trigger and open.

  2. OrderSend does not return true or false, it returns a ticket number or -1. Bogus test.

  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:
  1. There is no need for Stop orders in EAs. Humans use them because they can't watch the market 24/day. EAs can; just wait until market reaches the trigger and open.

  2. OrderSend does not return true or false, it returns a ticket number or -1. Bogus test.

  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

Thank you for your respond.


I changed the code to:

 Ticket = OrderSend(_Symbol, OP_BUYSTOP, LotSize, OpenPrice, 0, StopLoss, TakeProfit, NULL, MagicNumber, 0, clrGreen);
         if(Ticket > 0)


There is no need for Stop orders in EAs. Humans use them because they can't watch the market 24/day. EAs can; just wait until market reaches the trigger and open.


I wrote the BUYSTOP and SELLSTOP because it's much easier for me. I am not sure how to change it to SELL or BUY. Because when the trigger point happens, the BUYSTOP takes the last parameters and waits. If the price closes below EMA8 the pending order is deleted.


If I understand it right, I would have to write a loop which would check if the price goes to the entry point. If not then the loop ends?

 
forexG: If I understand it right, I would have to write a loop which would check if the price goes to the entry point. If not then the loop ends?

No loop required. Tick arrives, you check price. Return.