Questions from Beginners MQL4 MT4 MetaTrader 4 - page 143

 
Лауреат:
How to make a function which would close an open position by its own bridge after a specified number of candles have passed in the time history.
int numBars = 5;
void OnTick()
{
     for(int i = OrdersTotal(); i >= 0; i--)
     {
          if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
          if( OrderSymbol() != _Symbol)                continue;
          if( OrderMagicNumber() != Magic)             continue;
          if( OrderOpenTime() > Time[numBars-1] )      continue; 
          
          /* work */
     }
}
 
sviter-pro:
I'm experimenting!!! logic is specific!!! can you help me?

What's specific about it?

Look at the last position, and open the opposite position.

 
Konstantin Nikitin:

I'll check it out.

 
sviter-pro:

I'll check okay.

 

Hello. Please help with the function. On one account, there are several EAs to trade. Each EA may open no more than one trade per day. I need a function that would return true if the EA has already had a trade, and false if there was no trade. The check should be done by magic on current date as I understand it. If you can help me, I plan to build this function in each of my EA, which will need such a check. If i can help, i am planning to build it in each of my EAs where i need such a check.

 
Kolya32:

Hello. Please help with the function. On one account, it is supposed to trade with several EAs. Each EA may open no more than one trade per day. We need a function that returns true if there was a trade, and false if there was no trade. The check should be done by magic on current date as I understand it. If you can help me, I plan to build this function in each of my EA, which will need such a check. If i can help, i am planning to build it in each of my EAs where i need such a check.

Everything has already been done before us )) See here.

 

Looks like I need the isTradeToDay() function. Well, I'll experiment) Thank you.

I have corrected the function for myself by commenting out int mn=-1 and replacing mn with Magic. After running the EA with this function in the tester, I saw a big disadvantage: the time of testing the EA increased FOR YEARS! Now I have decided to test the Expert Advisor with this feature on a demo account. I have 5 Expert Advisors in my account and I'm worried that trades will be opened with a lag...

bool isTradeToDay(string sy="", int op=-1 /*,int mn=-1*/) {
int i, k=OrdersHistoryTotal();

if (sy=="0") sy=Symbol();
for (i=0; i<k; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
if (OrderSymbol()==sy || sy=="") {
if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
if (op<0 || OrderType()==op) {
if (Magic<0 || OrderMagicNumber()==Magic) {
if (TimeDay(OrderOpenTime())==Day()
&& TimeMonth(OrderOpenTime())==Month()
&& TimeYear(OrderOpenTime())==Year()) return(True);
}
}
}
}
}
}
k=OrdersTotal();
for (i=0; i<k; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==sy || sy=="") {
if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
if (op<0 || OrderType()==op) {
if (Magic<0 || OrderMagicNumber()==Magic) {
if (TimeDay(OrderOpenTime())==Day()
&& TimeMonth(OrderOpenTime())==Month()
&& TimeYear(OrderOpenTime())==Year()) return(True);
}
}
}
}
}
}
return(False);
}

 

You need to mark the breakeven level of the grid on the chart.

Profit in pips:

double current_Prof_Pts = current_Prof_Bux/current_summ_lot/(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE)/_Point)

Breakeven position for the BUY grid

zero = Bid-current_Prof_Pts*_Point

(For SELL, respectively: Ask+current_Prof_Pts*_Point)

Result: seems to be true, but always jumps around the true value by about 3 points.

Can you tell me where the error is?

 
Kolya32:

Looks like I need the isTradeToDay() function. Well, I will experiment) Thank you.

I've found a big disadvantage: the time of testing an EA has increased EXACTLY FIVE TIMES!

Yes, of course. The function is written in a universal way. If you access it with every tick, then the loop goes through the entire account history. And the account history may be quite deep (I've seen cases where there were more than 100 000 orders). To speed up the execution, you don't need to run the loop on the entire history every time. It is enough to do it only once at the very beginning and remember the index of the last order processed. The next time, you need to go through the loop only for the orders that have an index greater than the stored index. After that, remember the index of the last order again, and so on.

 
Igor Zakharov:

You need to mark the breakeven level of the grid on the chart.

Profit in pips:

Breakeven position for the BUY grid

(For SELL, respectively: Ask+current_Prof_Pts*_Point)

Result: seems to be true, but always jumps around the true value by about 3 points.

Please advise, where is the error?

The floating spread is to blame.