Kindly check my code, i have spent two weeks figuring it out and now i am exhausted so posting this one.

 
This function is taking only one trade, when the positions is greater than 1 then it is supposed to take entry on every 200 points.
example. first buy trade was taken when market was at 1.5000 now the second trade is supposed to be taken when price reaches 1.4980. 

i am attaching my ea code file as well. 
Files:
28nov.mq5  13 kb
 
//+------------------------------------------------------------------+
//| Take Trades Function 1                                           |
//+------------------------------------------------------------------+

void trade()
   {
      double MyRsiValue = NormalizeDouble(rsiArray[0],2);
      Comment(MyRsiValue);
      if (MyRsiValue >= Overbought)
        {
         // Implement your overbought trading logic here
         // For example, you can open a sell trade
         // OrderSend(Symbol(), OP_SELL, LotSize, Ask, 3, 0, 0, "", 0, clrNONE);
         double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         double Sl = StopLoss==0 ? 0 : Bid + StopLoss * _Point;
         double Tp = Bid - TakeProfit * _Point;
         
         trade.Sell(LotSize,_Symbol,Bid,Sl,Tp);
        }
      else if (MyRsiValue <= Oversold)
        {
         // Implement your oversold trading logic here
         // For example, you can open a buy trade
         // OrderSend(Symbol(), OP_BUY, LotSize, Bid, 3, 0, 0, "", 0, clrNONE);
         double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         double Sl = StopLoss==0 ? 0 : Ask - StopLoss * _Point;
         double Tp = Ask + TakeProfit * _Point;
         
         trade.Buy(LotSize,_Symbol,Ask,Sl,Tp);
        }
         if(PositionsTotal() >1)
         {  
                 int totalPositions = PositionsTotal(); // Get the total number of open positions
         
                 // Iterate through all open positions to get their information
                 for (int i = 0; i < totalPositions; i++)
                 {
                    ulong ticket= PositionGetTicket(i);
                    double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
          
                 if (PositionSelectByTicket(ticket) == POSITION_TYPE_BUY && Ask <= def_SetNewPrice)
                 {   
                     double Sl = StopLoss==0 ? 0 : Ask - StopLoss * _Point;
                     double Tp = Ask + TakeProfit * _Point;
          
                     if(trade.Buy(LotSize,_Symbol,Ask,Sl,Tp))
                     {
                        MyArray[LastRow][def_SetNewPrice] = true;
                     }
                     
                 }
                 double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         
                 if (PositionSelectByTicket(ticket) == POSITION_TYPE_SELL && Bid <= MyArray[LastRow][def_NextDayPrice])
                 { 
                     double Sl = StopLoss==0 ? 0 : Bid + StopLoss * _Point;
                     double Tp = Bid - TakeProfit * _Point;
         
                     if(trade.Sell(LotSize,_Symbol,Bid,Sl,Tp))   
                     {
                        MyArray[LastRow][def_SetNewPrice] = true;
                     }
                 }
                 if(PositionsTotal() >= 2)             
                  { 
                    double NewTakeProfit = NormalizeDouble(positionInfo[0][2] + TakeProfit *_Point,_Digits);
                    //double NewTakeProfit = positionInfo[0][2] + TakeProfit;
                    trade.PositionModify(ticket,0,NewTakeProfit);
                  }
   }
    }
      
}

 

My advices for newbies:

  1. Before learning to code in MQL5 learn to search as there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you! Either with google (forgives typps) "site:mwl5.com WhatIwant" or here:
        => articles: https://www.mql5.com/en/articles
        => codebase: https://www.mql5.com/en/code
        => generally: https://www.mql5.com/en/search
  2. In your case "entry on every 200 points " I would search for grid trading.
  3. If your program does not do what it should use the debugger to control your variables on every step: https://www.metatrader5.com/en/metaeditor/help/development/debug
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
Fernando Carreiro #:
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

Thank you fernando :) i hope to get some help regarding my issue i am facing. 

 

Bro I'm noticing a lot of ternary operators and long if statements, have you tried simplifying the code? 

Sometimes that works for me, I start again and keep it stupid simple (KISS), dead simple. From there once I have everything working in the simplest way possible I slowly make it more sophisticated.

Also "def_SetNewPirce" and  "def_NextDayPirce" aren't defined in the code snippet you've provided, I'm assuming they are functions, the error may lie in them. Instead of calling sperate functions, try setting those values at the top of trade so we can be sure the error lies here in trade and nowhere else. Sometimes it's easier for me to create one monolith function and if everything works, I start breaking it down and separating into separate functions.

This tp definition isn't explicit, I would add brackets around the operation I want done first, just to ensure the computer is going to calculate it the same way it's happening in my head. I don't know if you want the addition done first or if TakeProfit should be multiplied first then added to ask, it may help to be more explicit. I think by default in MQL5 multiplication will be handled before addition, if that's what you intended then that's great!
double Tp = Ask + TakeProfit * _Point;


I don't understand what this logic is doing, but I think it could be simpler. The beauty of keeping the code simple is that errors become easier to spot just from glancing at the code. 

MyArray[LastRow][def_SetNewPrice] = true;

Hope this helps.

 
Gamuchirai Zororo Ndawana #:

Bro I'm noticing a lot of ternary operators and long if statements, have you tried simplifying the code? 

Sometimes that works for me, I start again and keep it stupid simple (KISS), dead simple. From there once I have everything working in the simplest way possible I slowly make it more sophisticated.

Also "def_SetNewPirce" and  "def_NextDayPirce" aren't defined in the code snippet you've provided, I'm assuming they are functions, the error may lie in them. Instead of calling sperate functions, try setting those values at the top of trade so we can be sure the error lies here in trade and nowhere else. Sometimes it's easier for me to create one monolith function and if everything works, I start breaking it down and separating into separate functions.

This tp definition isn't explicit, I would add brackets around the operation I want done first, just to ensure the computer is going to calculate it the same way it's happening in my head. I don't know if you want the addition done first or if TakeProfit should be multiplied first then added to ask, it may help to be more explicit. I think by default in MQL5 multiplication will be handled before addition, if that's what you intended then that's great!


I don't understand what this logic is doing, but I think it could be simpler. The beauty of keeping the code simple is that errors become easier to spot just from glancing at the code. 

Hope this helps.

thank you brother, i will try to code the simpler version of my ea to see where the issue is. 

however i added the complete code file as well. 

Reason: