Who can help me please to add "Open new trade in the each new candle" for my EA! New comment

 

Hello programmers! 

I am a new programming learner & am very new in MQL4 language...

I need to add one command to my EA, But I'm tired of my failed attempts  :(

I just need to add one command to the EA:

Open new trade in the each new candle      //if current candle has met the same conditions//

That's all...


Thank you in advance for your reply and help, any help is greatly apriciated. 


Tayseer...




//+------------------------------------------------------------------+

//|                                                Tayseer_Renko.mq4 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.80"

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

extern int MagicNumber=269903;

extern double Lots=0.1;

extern double StopLoss=0;

extern double TakeProfit=0;

extern int TrailingStop=0;

extern int Slippage=3;
//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

  {
   double MyPoint=Point;

   if(Digits==3 || Digits==5) MyPoint=Point*10;

   double TheStopLoss=0;

   double TheTakeProfit=0;

   if(TotalOrdersCount()==0)

     {
      int result=0;

      if((Open[1]<Close[1])) // Here is my open buy rule

        {
         result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Tayseer",MagicNumber,0,Blue);

         if(result>0)

           {
            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

            if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
           }

         return(0);
        }

      if((Open[1]>Close[1])) // Here is my open Sell rule

        {
         result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Tayseer",MagicNumber,0,Red);

         if(result>0)

           {
            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;

            if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
           }

         return(0);
        }
     }

   for(int cnt=0;cnt<OrdersTotal();cnt++)

     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()<=OP_SELL && 

         OrderSymbol()==Symbol() && 

         OrderMagicNumber()==MagicNumber)

        {
         if(OrderType()==OP_BUY)

           {
            if((Open[1]>Close[1])) //here is my close buy rule

              {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

            if(TrailingStop>0)

              {
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);
                    }
                 }
              }
           }

         else

           {
            if((Open[1]<Close[1])) // here is my close sell rule

              {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

            if(TrailingStop>0)

              {
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);
                    }
                 }
              }
           }
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int TotalOrdersCount()

  {
   int result=0;

   for(int i=0;i<OrdersTotal();i++)

     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if(OrderMagicNumber()==MagicNumber) result++;
     }

return(result);}
//+------------------------------------------------------------------+

 

Do not open multiple duplicate topics.

Your other 2 topics have been deleted.

 
Keith Watford:

Do not open multiple duplicate topics.

Your other 2 topics have been deleted.

Ok, sorry, as i sayed, i am new here, but thank you for warrning... 
 

14115844: I am a new programming learner & am very new in MQL4 language...

Open new trade in the each new candle      //if current candle has met the same conditions//

  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. Don't double post.

  3. Format your code so it can be read. MetaEditor → Tools → Styler (Control+,) and then edit your post.
              Messages Editor

  4. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  5. Start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  6. if((Open[1]>Close[1])){ //here is my close buy rule
       OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
    }
                  <<< if the close was successfull, what happens below?
    if(TrailingStop>0)  
    Check your return codes for errors, and report them including GLE/LE. Don't look at it 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 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
  7. for(int i=0;i<OrdersTotal();i++){
       OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
       if (OrderMagicNumber()==MagicNumber) result++;}
    Code breaks if you put it on another chart without change the MN.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
 
//+------------------------------------------------------------------+

//|                                                Tayseer_Renko.mq4 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.80"

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

extern int MagicNumber=269903;

extern double Lots=0.1;

extern double StopLoss=0;

extern double TakeProfit=0;

extern int TrailingStop=0;

extern int Slippage=3;
//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

  {
   double MyPoint=Point;

   if(Digits==3 || Digits==5) MyPoint=Point*10;

   double TheStopLoss=0;

   double TheTakeProfit=0;

   if(TotalOrdersCount()==0)

     {
      int result=0;

      if((Open[1]<Close[1])) // Here is my open buy rule

        {
         result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Tayseer",MagicNumber,0,Blue);

         if(result>0)

           {
            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

            if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
           }

         return(0);
        }

      if((Open[1]>Close[1])) // Here is my open Sell rule

        {
         result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Tayseer",MagicNumber,0,Red);

         if(result>0)

           {
            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;

            if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
           }

         return(0);
        }
     }

   for(int cnt=0;cnt<OrdersTotal();cnt++)

     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()<=OP_SELL && 

         OrderSymbol()==Symbol() && 

         OrderMagicNumber()==MagicNumber)

        {
         if(OrderType()==OP_BUY)

           {
            if((Open[1]>Close[1])) //here is my close buy rule

              {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

            if(TrailingStop>0)

              {
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);
                    }
                 }
              }
           }

         else

           {
            if((Open[1]<Close[1])) // here is my close sell rule

              {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

            if(TrailingStop>0)

              {
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);
                    }
                 }
              }
           }
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int TotalOrdersCount()

  {
   int result=0;

   for(int i=0;i<OrdersTotal();i++)

     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if(OrderMagicNumber()==MagicNumber) result++;
     }

return(result);}
//+------------------------------------------------------------------+
William Roeder
:

  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. Don't double post.

  3. Format your code so it can be read. MetaEditor → Tools → Styler (Control+,) and then edit your post.
              Messages Editor

  4. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  5. Start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  6. Check your return codes for errors, and report them including GLE/LE. Don't look at it 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 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
  7. Code breaks if you put it on another chart without change the MN.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles



Thank you for replaying,

I am using this EA on the RENKO CHART,

And it's working wiht out any error,

But my problem is that i need to open new order with each new candle if has met the same conditions,

Thank you in advance for your reply and help, any help is greatly apriciated. 


Tayseer...

 
Keith Watford:

Do not open multiple duplicate topics.

Your other 2 topics have been deleted.

Keith, could you please also move mql4 topic to mql4 section when it's obviously mql4 only stuff.
 
Alain Verleyen:
Keith, could you please also move mql4 topic to mql4 section when it's obviously mql4 only stuff.

Moving