EA can not take position

 

i wrote an EA but this simple EA cannot take the position Please help me.


THANKS



#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double Lot = 0.01;
extern int Stoploss  = 10;
extern int Takeprofit = 10;

int NumBuy, NumSell,NumPenBuyStop, NumBuyLimit, NumSellStop, NumSellLimit,NumPenBuyLimit;
double totalProfit, totalSellProfit, totalBuyProfit;

datetime lastBuyTime, lastSellTime;
double pt;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void init()
  {
   pt= Point;
   if(Digits==3 || Digits==5)
     {
      pt=10*pt;
     }
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void start()
  {
   count();
   double rsi;
   string print = "\n\n\n\n\n "+
                  "\nnum buy: " + NumBuy +
                  "\nnum Sell: " + NumSell +
                  "\n total order profit: " + totalProfit +
                  "\n total buy profit :"  +totalBuyProfit +
                  "\n total buy profit :"  +totalSellProfit +
                  "\n RSI Price: " + rsi;
   Comment(print);
   rsi = iRSI(Symbol(), 0,14, PRICE_CLOSE,0);
   tpsl();
   count();

   if(NumBuy==0&&NumSell==0)
     {
      if(rsi >70)
        {
         OrderSend(Symbol(),OP_SELL, Lot,Bid,10,0,0,0,0,0,Red);
        }
      if(rsi < 30)
        {
         OrderSend(Symbol(),OP_BUY, Lot,Ask,10, 0, 0, 0, 0, 0,Green);
        }

     }

  }
/* int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }  */
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void count()
  {
   NumBuy= 0;
   NumSell= 0;
   NumPenBuyLimit= 0;
   NumPenBuyStop= 0;
   NumSellLimit= 0;
   NumSellStop= 0;
   totalBuyProfit= 0;
   totalProfit= 0;
   totalSellProfit= 0;
   for(int i= OrdersTotal()-1; i>=0; i--)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol())
        {

         if(OrderType()== OP_BUY)
           {
            NumBuy++;
            totalBuyProfit +=  OrderProfit()+ OrderSwap()+ OrderCommission();
           }
         else
            if(OrderType()==OP_SELL)
              {
               NumSell++;
               totalSellProfit +=  OrderProfit()+ OrderSwap()+ OrderCommission();
              }
            else
               if(OrderType()==OP_BUYSTOP)
                 {
                  NumPenBuyStop++;
                 }
               else
                  if(OrderType()==OP_BUYLIMIT)
                    {
                     NumPenBuyLimit++;
                    }
                  else
                     if(OrderType()==OP_SELLSTOP)
                       {
                        NumSellStop++;
                       }
                     else
                        if(OrderType()==OP_SELLLIMIT)
                          {
                           NumSellLimit++;
                          }

        }
     }

   totalProfit = totalBuyProfit+totalSellProfit;

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void tpsl()
  {
   double tp=0;
   double sl=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType() == OP_BUY)
           {
            tp=OrderOpenPrice()+Takeprofit*pt;
            sl=OrderOpenPrice()-Stoploss*pt;
            OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
           }

         if(OrderType() == OP_SELL)
           {
            tp=OrderOpenPrice()-Takeprofit*pt;
            sl=OrderOpenPrice()+Stoploss*pt;
            OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
           }


        }

     }

  }
//+------------------------------------------------------------------+
 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.          OrderSend(Symbol(),OP_SELL, Lot,Bid,10,0,0,0,0,0,Red);
    
             OrderSend(Symbol(),OP_BUY, Lot,Ask,10, 0, 0, 0, 0, 0,Green);
    
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    
                OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
    
                OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
    
    Check your return codes for errors, and report them including GLE/LE and your variable values. Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  3. Use the debugger or print out your variables, including _LastError and find out why. Do you really expect us to debug your code for you?
 
farzin:

i wrote an EA but this simple EA cannot take the position Please help me.


THANKS



Improved code....


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double Lot = 0.01;
extern int Stoploss  = 10;
extern int Takeprofit = 10;

int NumBuy, NumSell,NumPenBuyStop, NumBuyLimit, NumSellStop, NumSellLimit,NumPenBuyLimit;
double totalProfit, totalSellProfit, totalBuyProfit;

datetime lastBuyTime, lastSellTime;
double pt;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   pt= Point;
   if(Digits==3 || Digits==5)
     {
      pt=10*pt;
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   count();
   bool OrdrSend=false;
   double rsi=0;
   tpsl();
   rsi = iRSI(Symbol(), 0,14, PRICE_CLOSE,0);
//---
   if(NumBuy==0&&NumSell==0)
     {
      if(rsi >70)
        {
         OrdrSend=OrderSend(Symbol(),OP_SELL, Lot,Bid,10,0,0,"",0,0,Red);
        }
      if(rsi < 30)
        {
         OrdrSend=OrderSend(Symbol(),OP_BUY, Lot,Ask,10, 0, 0, "", 0, 0,Green);
        }

     }
//---
   string print = "\n\n\n\n\n "+
                  "\nnum buy: " + IntegerToString(NumBuy) +
                  "\nnum Sell: " + IntegerToString(NumSell) +
                  "\n total order profit: " + DoubleToStr(totalProfit,2) +
                  "\n total buy profit :"  +DoubleToStr(totalBuyProfit,2) +
                  "\n total buy profit :"  +DoubleToStr(totalSellProfit,2) +
                  "\n RSI Price: " + DoubleToStr(rsi,2);
   Comment(print);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void count()
  {
   NumBuy= 0;
   NumSell= 0;
   NumPenBuyLimit= 0;
   NumPenBuyStop= 0;
   NumSellLimit= 0;
   NumSellStop= 0;
   totalBuyProfit= 0;
   totalProfit= 0;
   totalSellProfit= 0;
   for(int i= OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol())
           {

            if(OrderType()== OP_BUY)
              {
               NumBuy++;
               totalBuyProfit +=  OrderProfit()+ OrderSwap()+ OrderCommission();
              }
            else
               if(OrderType()==OP_SELL)
                 {
                  NumSell++;
                  totalSellProfit +=  OrderProfit()+ OrderSwap()+ OrderCommission();
                 }
               else
                  if(OrderType()==OP_BUYSTOP)
                    {
                     NumPenBuyStop++;
                    }
                  else
                     if(OrderType()==OP_BUYLIMIT)
                       {
                        NumPenBuyLimit++;
                       }
                     else
                        if(OrderType()==OP_SELLSTOP)
                          {
                           NumSellStop++;
                          }
                        else
                           if(OrderType()==OP_SELLLIMIT)
                             {
                              NumSellLimit++;
                             }

           }
        }
     }

   totalProfit = totalBuyProfit+totalSellProfit;

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void tpsl()
  {
   double tp=0;
   double sl=0;
   bool OrdrModify=false;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType() == OP_BUY)
           {
            tp=Ask+Takeprofit*pt;
            sl=Bid-Stoploss*pt;
            if(OrderStopLoss()==0 || OrderTakeProfit()==0) OrdrModify=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
           }

         if(OrderType() == OP_SELL)
           {
            tp=Bid-Takeprofit*pt;
            sl=Ask+Stoploss*pt;
            if(OrderStopLoss()==0 || OrderTakeProfit()==0) OrdrModify=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,0);
           }


        }

     }

  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+