EA with Buystop order send

 

Dear all,


I hve made my own EA. My idea is to check last 9 bars abd if their bodies is above EMA than it should open order when highest high of those 9 bars +3 pips is reached.

When i do a test of strategy (for 3 year period) the result is that there is one pending order sent and it isn't executed despite fact that price should hit my open level and also SL and TP.

void OnTick()
  {
//---
   double Level,PriceClose,PriceOpen,PriceLow,PriceHigh;
   double HighestpriceBar,LowestpriceBar;                                              
   int summa, ticket, openorders, i;
   
   i=0;                       //Starting value
   summa=0;                   //Starting value
  
  //checking EMA Levels for last 9 bars
   for(i=i; i<=9; i++)
   {
   Level=iMA(NULL,0,MaPeriod,0,MODE_EMA,PRICE_TYPICAL,i);    // Set the level
   
   PriceClose = iClose(NULL,NULL,i);                       // Request close price
   PriceOpen = iOpen(NULL,NULL,i);                        // Request open price
   PriceLow = iLow(NULL,NULL,i);                          // Request Low price
   PriceHigh = iHigh(NULL,NULL,i);                        //// Request High price
   
   //checking if candles of last 9 bars are over EMA
      if ((PriceClose>Level)&&(PriceOpen>Level)&&(PriceLow>Level)&&(PriceHigh>Level))
      {
      summa=summa+1;
         if (summa>=8)                               //If True than price is over EMA and is bulish
         {
         HighestpriceBar =iHighest(NULL,0,MODE_HIGH,11,0); // Checking for bar that gives highest high for last 9 bars
         PriceHigh = iHigh(NULL,NULL,HighestpriceBar); //checking highest high for last 9 bars
         openorders=OrdersTotal();                    //checking for orders already open   
            if(openorders<1)                          
 //if no orders than all is set clear for sening a pending Buy order 3 pips above highest high (PriceHigh)          
           {
           ticket = OrderSend(Symbol(),OP_BUYSTOP,0.01,PriceHigh+3*Point,1,PriceHigh-StopLoss*Point,PriceHigh+TakeProfit*Point,0,0,0,Green);
           }
         }
        
         
      }
   }
   
  }

Can you please help me to understand what is wrong in my EA and why it doesn't generate trades while i am testing it on Strategy tester?


The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Use the debugger or print out your variables, including _LastError and prices and . Do you really expect us to debug your code for you?

  3. Move your if(summa… outside your loop, to avoid multiple triggering.

  4. ticket = OrderSend(Symbol(),OP_BUYSTOP,0.01,PriceHigh+3*Point,1,PriceHigh-StopLoss*Point,PriceHigh+TakeProfit*Point,0,0,0,Green);
    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)

  5.          openorders=OrdersTotal();                    //checking for orders already open  
                if(openorders<1) 
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop 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 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
 

Thank you for answer.

1.Done, Thank you!

2. I already printed results on screen. I need help only to understand why in Strategy tester my EA has only generated one sent order and it don't shows the result of it.

3 .Done

4. Thank you - understodd, but this is not main issue for me

5. It was done partly. But i don't know if it is reason why my EA is sending only one order (buy stop) and it isn't giving result if this pending order was executed and what was the result of it. And after this pending order is executed why it isn't opening new one trade?


Here is updated code:


void OnTick()
  {
//---
   double Level,PriceClose,PriceOpen,PriceLow,PriceHigh;
   double HighestpriceBar,LowestpriceBar;                                              
   int summa, ticket, openorders, i;
   
   i=0;                       //Starting value
   summa=0;                   //Starting value
  
  //checking EMA Levels for last 9 bars
   for(i=i; i<=9; i++)
   {
   Level=iMA(NULL,0,MaPeriod,0,MODE_EMA,PRICE_TYPICAL,i);    // Set the level
   
   PriceClose = iClose(NULL,NULL,i);                       // Request close price
   PriceOpen = iOpen(NULL,NULL,i);                        // Request open price
   PriceLow = iLow(NULL,NULL,i);                          // Request Low price
   PriceHigh = iHigh(NULL,NULL,i);                        //// Request High price
   
   //checking if candles of last 9 bars are over EMA
      if ((PriceClose>Level)&&(PriceOpen>Level)&&(PriceLow>Level)&&(PriceHigh>Level))
      {
      summa=summa+1;
               
         
      }
   }
   if (summa>=8)                               //If True than price is over EMA and is bulish
         {
         HighestpriceBar =iHighest(NULL,0,MODE_HIGH,11,0); // Checking for bar that gives highest high for last 9 bars
         PriceHigh = iHigh(NULL,NULL,HighestpriceBar); //checking highest high for last 9 bars
         openorders=OrdersTotal();                    //checking for orders already open   
            
            if(openorders<1)                          
 //if no orders than all is set clear for sening a pending Buy order 3 pips above highest high (PriceHigh)          
           {
           ticket = OrderSend(Symbol(),OP_BUYSTOP,0.01,PriceHigh+30*Point,1,PriceHigh-StopLoss*Point,PriceHigh+TakeProfit*Point,0,MagicNumber,0,Green);
           }
         }
  }


2020.03.04 16:36:18.807 EURUSD,M15: 1609250 tick events (4104 bars, 1610251 bar states) processed in 0:00:03.438 (total time 0:00:06.219)
2020.03.04 16:36:18.807 2020.03.02 23:59:54  EMA open OnTester returns 0.00000000000000
2020.03.04 16:36:15.401 2020.01.02 06:00:00  EMA open EURUSD,M15: open #1 buy stop 0.01 EURUSD at 1.12283 sl: 1.12053 tp: 1.12353 ok
2020.03.04 16:36:15.370 2020.01.01 00:00:00  EMA open inputs: TakeProfit=100; Lots=0.1; StopLoss=200; MaPeriod=100; MagicNumber=7;

Reason: