All trades open at the same time

 

Good day


Could Someone please assist with the following. I would like my EA to open a new trade every 500 points(can be adjusted by user). I have a maximum of 10 trades that can be opened but the problem is that when it should open the second trade at open all the trades at the same time. Thus the InPutTradegat does not open a new trade as per trade gap but opens all at the same time. Please can someone assist


input int                InputMaxTrades            = 10;                 // Max number of trades

input double             InpTradeGap               = 0.005;              // Minimum gap between trades

input ENUM_ORDER_TYPE    InpType                   = ORDER_TYPE_BUY;     // Order type;

input double             InpMinProfit              = 1.00;               // Profit in bas currency

input int                InpMagicNumber            = 19790927;           // Magic number

input string             InpTradeComment           = __FILE__;           // Trade comment

input double             InputVolume               = 0.01;               // Volume per order



struct StradeSum {

     int     count;

     double  profit;

     double  trailPrice;

}; 



CTrade   Trade;

 

int OnInit() {



     Trade.SetExpertMagicNumber(InpMagicNumber);

   

   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)  {





  }



void OnTick(){



    StradeSum sum;

    GetSum(sum);

    

    if (sum.profit>InpMinProfit) { // target reached

        CloseAll();

    } else

    if (sum.count==0)  {  // no trades

       OpenTrade();

    } else

    if (sum.count<InputMaxTrades) {

       if ( InpType==ORDER_TYPE_BUY

            && SymbolInfoDouble(Symbol(),SYMBOL_ASK)<=(sum.trailPrice-InpTradeGap)           

            ) {  // Far enough below

          OpenTrade();

            } else

         if ( InpType==ORDER_TYPE_SELL

            && SymbolInfoDouble(Symbol(), SYMBOL_BID)>=(sum.trailPrice+InpTradeGap)

            ) {  // Far enough above

          OpenTrade();

      }

    } 

     

  }

  

void OpenTrade() {



     double  price = (InpType==ORDER_TYPE_BUY) ?

                       SymbolInfoDouble(Symbol(),SYMBOL_ASK) :

                       SymbolInfoDouble(Symbol(),SYMBOL_BID);

     Trade.PositionOpen(Symbol(), InpType, InputVolume, price, 0, 0, InpTradeComment);

     

}



void CloseAll() {



     int count   = PositionsTotal();

     

     for (int i = count-1; i>=0; i--)  {

       ulong ticket = PositionGetTicket(i);

       if (ticket>0) {

         if ( PositionGetString(POSITION_SYMBOL)==Symbol()

              && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber

              && PositionGetInteger(POSITION_TYPE)==InpType       

              ) {

         Trade.PositionClose(ticket);

         }

       }  

     }



   }

   

void  GetSum(StradeSum &sum) {



   int   count   = PositionsTotal();

   

   sum.count      = 0;

   sum.profit     = 0.0;

   sum.trailPrice = 0.0;

   

   for (int i = count-1; i>=0; i--)  {

       ulong ticket  = PositionGetTicket(i);

       if (ticket>0) {

           if (PositionGetString(POSITION_SYMBOL)==Symbol()

           && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber

           && PositionGetInteger(POSITION_TYPE)==InpType) {

         sum.count++;

         sum.profit  += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);

         if (InpType==ORDER_TYPE_BUY) {

            if (sum.trailPrice==0   ||  PositionGetDouble(POSITION_PRICE_OPEN)<sum.trailPrice) {

               sum.trailPrice = PositionGetDouble(POSITION_PRICE_OPEN);

              }         

           } else

           if (InpType==ORDER_TYPE_SELL) {

             if (sum.trailPrice==0  ||  PositionGetDouble(POSITION_PRICE_OPEN)<sum.trailPrice) {

                sum.trailPrice = PositionGetDouble(POSITION_PRICE_OPEN);

             }

             }

           

           }

       }

   

   }



return;



}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Dawid Zinserling:

...

Please edit your post to use the CODE button (Alt-S)!

Use the CODE button


 
Sergey Golubev #:

Please edit your post to use the CODE button (Alt-S)!


I have done so Thanks. I also attached the file

 
Dawid Zinserling #:I have done so Thanks. I also attached the file

No you have not! Please do it properly with the "</>" code button. As per Forum rules and recommendations, please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S).

NB! Very important! DO NOT create a new post. EDIT your original post.

Forum rules and recommendations
Forum rules and recommendations
  • 2023.03.12
  • www.mql5.com
Each language has its own forum, and it is therefore forbidden to communicate in any language other than that of the forum in question...
 
Hi.Its done
Reason: