# Close an open order with the new bars

44
2016.05.03 14:24

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,

2349
2016.05.03 14:37
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.

70
2016.05.03 15:56

You can use Bars or iBars()

```if (i != Bars)
{
i=Bars;
// new bar
}```
44
2016.05.03 16:28
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

44
2016.05.03 17:01
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,
70
2016.05.03 18:04
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);}
}```
Moderator
9644
2016.05.03 18:58
```  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

}
}```
.
Moderator
30752
2016.05.03 20:53
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.

14532
2016.05.04 12:45
 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);}    }``` Predefined Variables - MQL4 Reference do not update without a RefreshRates - Timeseries and Indicators Access - MQL4 Reference 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
44
2016.05.05 14:59
GumRai:
.

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

14532
2016.05.05 16:40
1.  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?
12