Simple way to avoid repetitive code?

 

Hi Everyone


What is the simplest way to set variables in order to avoid the repetitiveness, please? (total means OrdersTotal)

if(total==1 && Bid-PriceS>HT){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*2,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==2 && Bid-PriceS>HT*2){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*3,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==3 && Bid-PriceS>HT*3){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*4,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==4 && Bid-PriceS>HT*4){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*5,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==5 && Bid-PriceS>HT*5){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*6,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==6 && Bid-PriceS>HT*6){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*7,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==7 && Bid-PriceS>HT*7){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*8,Bid,5,0,TPS,"Original",0,0,Red); 
     }
   if(total==8 && Bid-PriceS>HT*8){
      ticket=OrderSend(Symbol(),OP_SELL,Lots*9,Bid,5,0,TPS,"Original",0,0,Red); 
     }
 
trader3000: What is the simplest way to set variables in order to avoid the repetitiveness, please? (total means OrdersTotal)

Learn to code.

Not tested, not compiled, just typed.

for(int count=1; count < 9; ++count){
   RefreshRates();
   if(total==count && Bid-PriceS>HT*count){
        ticket=OrderSend(Symbol(),OP_SELL,Lots*(count+1),Bid,5,0,TPS,"Original",0,0,Red); 
     }
}   // for

Not tested, not compiled, just typed.

After Sleep and between server calls the market will have changed. You must update your Predefined Variables or series arrays before using any of them — you must call RefreshRates.
 

Depending on what is the 'PriceS'. Looks like this is the price of the lowest sell order.

If you just want to simplify this code, then there is a dependency here:

if(total > 0 && total < 9 && Bid - PriceS > HT * total)
  {
   double orderLots = Lot * (total + 1); // Should be normalized
   // ticket=OrderSend(...)
  }

But still, it's not good.


I think something like this would be a little better:

const int MAX_ORDERS_NUMBER = 8;

void addingGridOrders(double priceOfNextOrder, double lotOfNextOrder, int ordersNumber)
  {
   if(ordersNumber < 1 || ordersNumber >= MAX_ORDERS_NUMBER)
      return;
   if(Bid < priceOfNextOrder)
      return;
   ticket=OrderSend(Symbol(),OP_SELL,lotOfNextOrder,Bid,5,0,TPS,"Original",0,0,Red); 
  }

where

priceOfNextOrder = NormalizeDouble(PriceS + HT * ordersNumber, Digits());
lotOfNextOrder = Lot * (ordersNumber + 1);

This is not code ready to use. I'm just trying to convey the idea to you

 
Vladislav Boyko #:

Depending on what is the 'PriceS'. Looks like this is the price of the lowest sell order.

If you just want to simplify this code, then there is a dependency here:

But still, it's not good.


I think something like this would be a little better:

where


This is not code ready to use. I'm just trying to convey the idea to you

The point is to reduce the condition to this:

if(Bid < priceOfNextOrder)
   return;

That is, if the price at which the next grid order should be added is reached, then we add it.

I just flipped the condition to avoid nesting. It could look like this:

if(Bid >= priceOfNextOrder)
   ticket=OrderSend(Symbol(),OP_SELL,lotOfNextOrder,Bid,5,0,TPS,"Original",0,0,Red);

And the price of the next order is most conveniently calculated where you get the 'PriceS'

 
Thank you for the help guys, much appreciated
 
Comments that do not relate to this topic, have been moved to "Off-topic MT4/mql4 questions.".
Reason: