Download MetaTrader 5
To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
Theekop
12
Theekop 2016.07.11 18:30 

 I recently started programming my very first EA. It may look a little confusing at first but i hope it is readable. The reason for this post is that i have encountered a problem. It comes down to this. I want to execute a buy/sell order once 3 requirements are met. 

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);
   point=MarketInfo(Symbol(),MODE_POINT);
   Alert("Stoplevel =",StopLevel,"point=",point);
   StopMin=StopLevel*point;
   Alert("Stplim=",StpLim*point);
   return(0);
  }
//--------------------------------------------------------------------------// 
int start() // Special function start()
  {
   int total=OrdersTotal();
   if((total==0)&&((Hour()>=Openhr) && (Hour()<Closehr)))
     {
      //--------------------------------------------------------------------------// Tech. ind. function call
      MA1=iMA(NULL,0,Period_MA1,0,MODE_EMA,PRICE_CLOSE,1);
      MA2=iMA(NULL,0,Period_MA2,0,MODE_EMA,PRICE_CLOSE,1);
      RSI=iRSI(NULL,0,Period_RSI,PRICE_CLOSE,1);
      iHigh2=iHigh(Symbol(),PERIOD_CURRENT,2);
      iHigh1=iHigh(Symbol(),PERIOD_CURRENT,1);
      iHigh0=iHigh(Symbol(),PERIOD_CURRENT,0);
      iLow2=iLow(Symbol(),PERIOD_CURRENT,2);
      iLow1=iLow(Symbol(),PERIOD_CURRENT,1);
      iLow0=iLow(Symbol(),PERIOD_CURRENT,0);
      //--------------------------------------------------------------------------// Condition 1: Moving Average Trend
      if(MA1>MA2)
        {
         //--------------------------------------------------------------------------// Condition 2: RSI
         if((RSILL<RSI) && (RSIHL>RSI))
           {
            //--------------------------------------------------------------------------// Condition 3: Candlesticks
            if((iLow2>iLow1) && (iLow1<iLow0) && (iHigh1<iHigh0))
              {
               Alert("Trigger for Long");
               //--------------------------------------------------------------------------// Calculate StopLoss & TakeProfit value 
               SL=iLow1-(SLV*point);
               if((StopMin)>(Ask-SL))
                 {
                  SL=Ask-StopMin;
                  Alert("Stoploss too small");
                 }
               RefreshRates();  
               TP_1=((Ask-SL)+Ask);
               TP_2=(2*(Ask-SL)+Ask);
               TP_3=(3*(Ask-SL)+Ask);
               //--------------------------------------------------------------------------// Place Order           
               Alert("Ask-SL=",Ask-SL);
               Alert("point*StpLim=",point*StpLim);
               if((Ask-SL)<=(point*StpLim))
                 {
                  int ticket_1=OrderSend(Symbol(),OP_BUY,vol,Ask,slip,SL,TP_1,0,Magicid1);
                  int ticket_2=OrderSend(Symbol(),OP_BUY,vol,Ask,slip,SL,TP_2,0,Magicid2);
                  int ticket_3=OrderSend(Symbol(),OP_BUY,vol,Ask,slip,SL,TP_3,0,Magicid3);
                  Alert("Long Orders Placed!");
                 }
              }
           } 
        }
        //--------------------------------------------------------------------------// End of Sell Order
      else
        {
         //--------------------------------------------------------------------------// Condition 2: RSI
         if((RSILS<RSI) && (RSIHS>RSI))
           {
            //--------------------------------------------------------------------------// Condition 3: Candlesticks
            if((iHigh1>iHigh2) && (iLow1>iLow0) && (iHigh1>iHigh0))
              {
               Alert("Trigger for Short");
               //--------------------------------------------------------------------------// Calculate StopLoss & TakeProfit value 
               SL=iHigh1+(SLV*point);
               if(StopMin>(SL-Bid))
                 {
                  SL=Bid+StopMin;
                  Alert("Stoploss too small");
                 }
               RefreshRates();  
               TP_1=(Bid-(SL-Bid));
               TP_2=(Bid-2*(SL-Bid));
               TP_3=(Bid-3*(SL-Bid));
               //--------------------------------------------------------------------------// Place Order           
               Alert("SL-Bid=",SL-Bid);
               Alert("StpLim*point=",StpLim*point);
               if((SL-Bid)<(StpLim*point))
                 {
                  int ticket_4=OrderSend(Symbol(),OP_SELL,vol,Bid,slip,SL,TP_1,0,Magicid1);
                  int ticket_5=OrderSend(Symbol(),OP_SELL,vol,Bid,slip,SL,TP_2,0,Magicid2);
                  int ticket_6=OrderSend(Symbol(),OP_SELL,vol,Bid,slip,SL,TP_3,0,Magicid3);
                  Alert("Short Orders Placed!");
                 }
              }
           }
        }
      //--------------------------------------------------------------------------// End of Sell Order
     }
   return(0);
//--------------------------------------------------------------------------// Exit start()
  }
//+------------------------------------------------------------------+

 Now, the EA handles assessing the MA and RSI conditions just fine. It also correctly handles the third condition. Where it fails sometimes is at the price where it sends an order. To show the situation where i want to buy/sell i have added a few pictures.

The picture below depicts a (in theory) correct short order: The high of candle 0 does not top the high of candle 1, and the low of candle 0 goes below the low of candle 1. The stoploss is set just above the high of candle 1, and the order is put in just below the low of candle 1. The method for long orders is exactly the opposite of this one. 

 Short Correct 

Now, sometimes the program send in orders that do not follow the algorithm as i desire.

In the picture below the point where an order is sent is not below the low of candle 1. The stoploss is set correctly though. 

 Short Incorrect

In the picture below we have a incorrect long order. Again, the StopLoss is set correctly (just below the low of candle 0). However, the order is sent in somewhere in the middle of candle 0, instead of just above the high of candle 0. 

Long incorrect 

My question is: what is going wrong here?

And while we're here: i want my EA to check if there is anyway for the line of code below to check for only orders with the magic numbers defined at the start of the program instead of any order:

 int total=OrdersTotal();
 if((total==0)&&((Hour()>=Openhr) && (Hour()<Closehr)))

 I want to be able to send orders manually too while the EA runs. The way it is set up now does not allow that since it considers my manual orders too for OrdersTotal.

 For those who got through this long read: thanks! I hope someone can help me out here.

 Thanks in advance! 

whroeder1
13656
whroeder1 2016.07.11 19:58  
  1. Print out your variables, and find out why. In image one, the Low[0] was below Low[1] at the time it opened the order.
  2. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You must RefreshRates between sleeps and server calls.
  4. Using OrdersTotal buy itself means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
/
To add comments, please log in or register