Objem1<1 will never be true.
Don't use volume to detect a new bar, use time.
static datetime bar_time=Time[0]; datetime now_time=Time[0]; if(now_time!=bar_time) { bar_time=now_time; //A new bar has opened //Place code here to be executed only when a new bar opens // }
.
Hi,
thank you for your help. I am using "volume", because "time" work only when is price moving at specific time and that is poblem, because price can be static, when i need to close pending orders.
Hi,
thank you for your help. I am using "volume", because "time" work only when is price moving at specific time and that is poblem, because price can be static, when i need to close pending orders.
-
Play videoPlease edit your post.
For large amounts of code, attach it. -
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
- In the presence of multiple orders (one EA multiple charts, multiple EA's,
manual trading)
- You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
Time[0] changes when a new bar opens, Volume[0] can never be <1
If it works your way, then do it your way and you don't need any help.
Garble_buble:
Time[0] changes when a new bar opens, Volume[0] can never be <1
Hi All,
i created a simple script, which cover closing unrealized pending orders when new candle started, but it doesn´t work. Can you please help me with this? I need to solve this problem.
You're doing this in a script?
GumRai is correct - a new bar does not form unless a tick has come in. Therefore volume should never be less than 1.
//Copy and paste and test, hope it helps
static datetime _lastBarTime = 0;
int OnInit()
{
_lastBarTime = iTime(Symbol(),0,0);
return(INIT_SUCCEEDED);
}
bool NewBar()
{
if (iTime(Symbol(),Period(),0) != _lastBarTime)
{
_lastBarTime = iTime(Symbol(), Period(),0);
return (true);
}
else
return (false);
}
int start()
{
if(NewBar()==true)
{
closePendingOrder();
}
void closePendingOrder()
{
for(int a=0;a<OrdersTotal();a++)
{
if(OrderSelect(a,SELECT_BY_POS)==true)
{
if(OrderType()==OP_SELLSTOP || OrderType==OP_BUYSTOP)
{
OrderDelete(OrderTicket(),Violet);
}
}
}
}
-
Play videoPlease edit your post.
For large amounts of code, attach it. -
In the presence of multiple orders (one EA multiple charts, multiple EA's,
manual trading)
- You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi All,
i created a simple script, which cover closing unrealized pending orders when new candle started, but it doesn´t work. Can you please help me with this? I need to solve this problem.
Thanks for any help :-)
int Objem1 = Volume[0];//+------------------------------------------------------------------+
//| Close pending orders |
//+------------------------------------------------------------------+
for(p=0; p <= OrdersTotal(); p++)
if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES)==true && Objem1<1 && OrderMagicNumber()==Magic_number)
{if(OrderType()==OP_BUYSTOP)
{
Delete = OrderDelete(OrderTicket(),0);}}