Please help a new colleague girl! How to stop the opening of new orders for X minutes after loss?

 

Hi!

I'm a newcomer programmer and something is really complicate at the time.

I am trying to develope a function that

- in case of orders that are closed with a loss bigger than XX pips or

- orders that are still opened but loosing more than XX pips

doesn't allow to send other orders for 30 minutes.

I tried but with no result..


Here there is the code where order are sent:


if(type==OP_BUY)
   { if (PAL == false || TimeCurrent( ) > (Restart + (Pause * 60))){
      for(c=0;c<NumberOfTries;c++)
      {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,aStopLoss,aTakeProfit,TicketComment,MagicNumber,0,Green);
         err=GetLastError();
         if(err==0)
         { 
            if(ticket>0) break;
         }
         else
         {
            if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
            {
               Sleep(5000);
               continue;
            }
            else //normal error
            {
               if(ticket>0) break;
            }  
         }
      }  } 
   }


and here where are closed:


 for(cnt=total-1;cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber)
      {
         switch(OrderType())
         {
            case OP_BUY      :
               for(c=0;c<NumberOfTries;c++)
               {
                  ticket=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
                  
                  // Set the restart time after big loss
                  if (MathAbs(OrderOpenPrice() - OrderClosePrice()) > TickToSleep)
                      {Restart = OrderCloseTime() + (pause * 60)};
                  
                  err=GetLastError();
                  if(err==0)
                  { 
                     if(ticket>0) break;
                  }
                  else
                  {
                     if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
                     {
                        Sleep(5000);
                        continue;
                     }
                     else //normal error
                     {
                        if(ticket>0) break;
                     }  
                  }
               }   
               break;

the code after // Set the restart time after big loss doesn't work..... sigh.

And I have no idea how to check if between open orders there are ones loosing more than XX pips.

Please, can anyone help me?

Andrea

 
nuspy wrote >>

Hi!

I'm a newcomer programmer and something is really complicate at the time.

I am trying to develope a function that

- in case of orders that are closed with a loss bigger than XX pips or

- orders that are still opened but loosing more than XX pips

doesn't allow to send other orders for 30 minutes.

I tried but with no result..

Here there is the code where order are sent:

and here where are closed:

the code after // Set the restart time after big loss doesn't work..... sigh.

And I have no idea how to check if between open orders there are ones loosing more than XX pips.

Please, can anyone help me?

Andrea

It is not a good idea to use OrderClosePrice() for trades that is still.

Your OrderSelect function looks in the pool of trades that are open or pending (MODE_TRADES). Try using (MODE_HISTORY) to look at trades that are closed and then use OrderProfit() to determine the number of pips lost or gained.

When you are looking at the open trades do not use the MathAbs function as this will remove the plus or minus (profit or loss) value of your calculation. Look at the difference between OrderOpenPrice and Ask/Bid and do the calculation seperately beteen OP_BUY or OP_SELL

Use temporary Print() or Comment() functions is the code to debug.

Good Luck

whocares

Reason: