Close an open order with the new bars

 

Hello, i need to decide if to close or not an operation of buy/sell after 10 more sticks appears after the buy/sell operation.

 

I want to take an average of that 10 new sticks so then i decide if i should close the operation or not.

I know how to do with the previous bars before the buy/sell operation but im very confused on how could i with that new bars, i would need something like a loop which wait for every bar with a counter  but i dont know if any function exists for that and i would appreciate your help guys.

 

Thanks for all and best regards, 

 
espina88:

I want to take an average of that 10 new sticks so then i decide if i should close the operation or not.

Then use the standard Moving Average indicator with a period of 10. The function for that is "iMA()":

MetaQuotes even supplies an example EA for using Moving averages. You will find it in the "Experts" folder and has a name of "Moving Average.mq4".

Besides that, there are plenty of examples of EAs in the CodeBase that use Moving Averages.

 

You can use Bars or iBars()

if (i != Bars)
   {
   i=Bars;
   // new bar
   }
 
FMIC:

Then use the standard Moving Average indicator with a period of 10. The function for that is "iMA()":

MetaQuotes even supplies an example EA for using Moving averages. You will find it in the "Experts" folder and has a name of "Moving Average.mq4".

Besides that, there are plenty of examples of EAs in the CodeBase that use Moving Averages.

So if i put this function like this: 

 

for(int i=0; i<10;i++)
   {
      Average =  iMA(NULL,PERIOD_CURRENT,10,8,MODE_SMA,PRICE_CLOSE,i);

  }

 

It will automatically wait for every stick which is being created and it will loop for the next 10 sticks? I need something like that.

 

I think this code just works for the already created bars and i want for the new ones 

 
Tecuciztecatl:

You can use Bars or iBars()

 

Thats functions just return the number of bars of the chart, so imagine there are 450 bars in my chart, i want a function that loops until 10 more new sticks are generated so then i could average that new 10 bars,
 
espina88:
Thats functions just return the number of bars of the chart, so imagine there are 450 bars in my chart, i want a function that loops until 10 more new sticks are generated so then i could average that new 10 bars,

Well, then add to the bars ten)

if (i < Bars)
   {
   i=Bars+10;
   // every tenth bar
   }

or

void WaitNewBar()
   {
   int i=Bars;
   while (i==Bars && !IsStopped())
      {Sleep (100);}
   }
 
  static datetime order_bar_time=0;
  static datetime bar_time=0;
  bool new_bar=false;
  
  //Condition to open order
    {
    //Open order
    order_bar_time=Time[0];
    }
  
  datetime this_bar_time=Time[0];
  if(this_bar_time!=bar_time)
    {
    new_bar=true;
    bar_time=this_bar_time;
    }
  
  if(new_bar)
    {
    if(iBarShift(Symbol(),0,order_bar_time)==11)  //10 completed bars since the order was opened
       {                                          //not including the order open bar
       
       //Calculate the average
       //Whatever action you require
       
       }
    }
.
 
Tecuciztecatl:

Well, then add to the bars ten)

or



It has been said, explained, again and again that Bars or iBars is not a reliable way to check for new bar. Number of bars can change will you are still on the same bar.

Only time is reliable, see Gumrai code.

 
Tecuciztecatl:

Well, then add to the bars ten)

if (i < Bars)
   {
   i=Bars+10;
   // every tenth bar
   }

or

void WaitNewBar()
   {
   int i=Bars;
   while (i==Bars && !IsStopped())
      {Sleep (100);}
   }
  1. Predefined Variables - MQL4 Reference do not update without a RefreshRates - Timeseries and Indicators Access - MQL4 Reference
  2. Either add it (and can't use the tester,) or remember values and return from OnInit and await the next tick.
angevoyageur:

It has been said, explained, again and again that Bars or iBars is not a reliable way to check for new bar. Number of bars can change will you are still on the same bar.

Only time is reliable, see Gumrai code.

Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
 
GumRai:
.

Thanks for your reply,

 

I have tried this iBarShift but when i integrated to my program it crashed and gets stop, so i decided to make a little program to trace with the same result:

 

int Cont = 0;

static datetime time=0;

void OnTick()

  {

//---

   if(Cont == 0)

   { time = OrderTime();}

   Cont++; 

   

   Order(time);   


  }

//+------------------------------------------------------------------+


void Order(datetime time){

   int cont = 0;

   int indeX = iBarShift(Symbol(),0,time);

       while( indeX  <= 11 ) {

       

         if( Cont == 10 ){

         

            Comment("We are in 10");

         }

         cont++;

      } 

}



datetime OrderTime(){

   datetime time  = Time[0];

   return time;

}

 

When i make it work on strategy tester it is just stop the simulation and nothing happens. Is the first that i have that problem maybe im missing something 


 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. What part of "either add it (and can't use the tester,)" was unclear?
Reason: