Download MetaTrader 5

how to compact this code please?

To add comments, please log in or register
Unable to find the right application? Order it!
Brian Kester
818
Brian Kester 2014.12.23 14:59 

Hi, I made this code to create increasing gaps between next orders.

Can someone help me understand how to compact this?  Thank you as always I appreciate it.

(ps i intentionally left out index 0 in the examples)


void gapsteps()
  {    
    double firstgap = GapBetweenOrders * .0001; 
    double gapstep[10]; 
   
     if(UseGapStep && GapType == 2)      //this step = last step added to the step before that 
      {
       gapstep[1] = firstgap;
       gapstep[2] = gapstep[1] + gapstep[1]/2;
       gapstep[3] = gapstep[2] + gapstep[1];
       gapstep[4] = gapstep[3] + gapstep[2];
       gapstep[5] = gapstep[4] + gapstep[3]; 
       gapstep[6] = gapstep[5] + gapstep[4];
       gapstep[7] = gapstep[6] + gapstep[5];
       gapstep[8] = gapstep[7] + gapstep[6]; 
       gapstep[9] = gapstep[8] + gapstep[7];
      }
    else
     if(UseGapStep && GapType == 3)       //this step = last step x2         
      { 
       gapstep[1] = firstgap;
       gapstep[2] = gapstep[1] * 2;
       gapstep[3] = gapstep[2] * 2;
       gapstep[4] = gapstep[3] * 2;
       gapstep[5] = gapstep[4] * 2;
       gapstep[6] = gapstep[5] * 2;
       gapstep[7] = gapstep[6] * 2;
       gapstep[8] = gapstep[7] * 2;
       gapstep[9] = gapstep[8] * 2;
      }
  }
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.12.23 15:07  
Never try array?
Brian Kester
818
Brian Kester 2014.12.23 15:59  
deysmacro:
Never try array?

Yes, it is an array. GapStep[10].


but how can I fill it using less lines?

whroeder1
14470
whroeder1 2014.12.23 16:15  
c3po: fibonacci steps - 8,12,18,27, these blocks take up way too many lines.
  1. gapstep[1] = firstgap;
    gapstep[2] = gapstep[1] + gapstep[1]/2;
    gapstep[3] = gapstep[2] + gapstep[1];
    gapstep[4] = gapstep[3] + gapstep[2];
    :
    Your code doesn't generate 8,12,18,27 it generates 8,12,20,32
  2. Use a for loop
    gapstep[1] = firstgap;                 
    gapstep[2] = gapstep[1] + gapstep[1]/2;
    gapstep[3] = gapstep[2] + gapstep[1];
    gapstep[4] = gapstep[3] + gapstep[2];
    gapstep[5] = gapstep[4] + gapstep[3];
    gapstep[6] = gapstep[5] + gapstep[4];
    gapstep[7] = gapstep[6] + gapstep[5];
    gapstep[8] = gapstep[7] + gapstep[6];
    gapstep[9] = gapstep[8] + gapstep[7];
    gapstep[1] = firstgap;                 
    gapstep[2] = gapstep[1] + gapstep[1]/2;
    for(int i=3; i<10 ++i)
     gapstep[i] = gapstep[i-2] + gapstep[i-2];
    
    gapstep[1] = firstgap;
    gapstep[2] = gapstep[1] * 2;
    gapstep[3] = gapstep[2] * 2;
    gapstep[4] = gapstep[3] * 2;
    gapstep[5] = gapstep[4] * 2;
    gapstep[6] = gapstep[5] * 2;
    gapstep[7] = gapstep[6] * 2;
    gapstep[8] = gapstep[7] * 2;
    gapstep[9] = gapstep[8] * 2;
    gapstep[1] = firstgap;
    for(int i=2; i<10; ++i)
       gapstep[i] = gapstep[i-1] * 2;
    
  3. If the firstgap was a constant 8 you could just create a constant array
    int gapStep[]={8,12,20,32...}
    
    int gapStep[]={8,16,32,64...}
    
  4. For variable size use firstgap * step[]
    double step[]={1, 1.5, 2.5, 4.0...}; //*8=8,12,20,32...
    
    int    step[]={1, 2, 4, 8...};       //*8=8,16,32,64...

Brian Kester
818
Brian Kester 2014.12.23 16:19  
WHRoeder:
  1. Your code doesn't generate 8,12,18,27 it generates 8,12,20,30
  2. Use a for loop





Oh i had a little miscalculation in my example, but you understood the principle i was looking for, Thank you so much WHR.

To add comments, please log in or register