Try
for (counter1=0; counter1<=OrdersTotal()-1; counter1++) { if (OrderSelect(counter1,SELECT_BY_POS)==true) { if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) { Calc_trailing(); selected_order_typ=OrderType(); selected_order_ticket=OrderTicket(); selected_lots=OrderLots(); selected_sl=OrderStopLoss(); selected_tp=OrderTakeProfit(); liveorder=true; last_profit=false; break; } else { liveorder=false; } } }
liveorder=false; // Didn't find any for (counter1=0; counter1<=OrdersTotal()-1; counter1++) { if (OrderSelect(counter1,SELECT_BY_POS)==true) { if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) { Calc_trailing(); selected_order_typ=OrderType(); selected_order_ticket=OrderTicket(); selected_lots=OrderLots(); selected_sl=OrderStopLoss(); selected_tp=OrderTakeProfit(); liveorder=true; // Found one or many last_profit=false; // } else { // liveorder=false; } } }
Thx for helping.
I have gave in variables liveorder=false
I correct the code as above with break, and it seems it's working.
if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) { liveorder=true; } else liveorder=false;
With the break version. If it finds an order with symbol/MN you get true. If it finds any that don't match you get false. If it doesn't find any orders, you get liveorder being undefined.for (counter1=0; counter1<=OrdersTotal()-1; counter1++) {
Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- With the break version. If it finds an order with symbol/MN you get true. If it finds any that don't match you get false. If it doesn't find any orders, you get liveorder being undefined.
- Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
Thx WHRoeder! It's clear why is better the decrement in loop. I am defined the liveorder bollean in the beginning as false.
Now I have another problem. I like to get the profit of the last closed order from the history. I have tried many ways, but I got always wrong function.
for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) { if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket) continue; break; } if (liveorder==false) { if (order_close_by_indi==false && OrderTicket()==selected_order_ticket && last_profit==false) { close_order_profit=OrderProfit() + OrderSwap() + OrderCommission(); selected_order_ticket=0; last_profit=true; . . . . .
What is that I know before the for... lop begin? I know the orderticket & symbol. This position will be closed. If the closing not with trigger came, the order_close_by_indi=false. The last_profit boolean represent the last position profit we got or not yet.
So.., is it good thinking to try to get with OrderTicket condition the last closed position, or not? If we get it, the last_profit will be true.
But this way is not working well. What would be the fault?
Sorry, I don't understand your post.
But, here you don't use counter2 in your loop, so you try to select the same order position every time
for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) { if (OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket) continue; break; }
Sorry, I don't understand your post.
But, here you don't use counter2 in your loop, so you try to select the same order position every time
Hi GumRai!
What is you don't understand? I try to explain it:
I like to get the last closed order profit. I think to get is the easiest way when I loop trought the history and with if... conditions I try to get it. In history each closed trade I examine with OrderTicket. I know the OrderTicket because of this ticket belong to the earlier live trade.
But with this code I don't get the right trade from the history.
I tried this one too:
if (OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)==true) { if (OrderTicket()==selected_order_ticket) { profit=OrderProfit()...........
Sorry, I don't understand your post.
But, here you don't use counter2 in your loop, so you try to select the same order position every time
Yep sorry I made wrong copy-paste.
for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) { if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket) continue; break; }
With this I get always the first in history, this is the deposit.
Hi GumRai!
What is you don't understand? I try to explain it:
I like to get the last closed order profit. I think to get is the easiest way when I loop trought the history and with if... conditions I try to get it. In history each closed trade I examine with OrderTicket. I know the OrderTicket because of this ticket belong to the earlier live trade.
But with this code I don't get the right trade from the history.
I tried this one too:
If you know the ticket number, then just select that ticket
OrderSelect(ticket,SELECT_BY_TICKET); if(OrderCloseTime()!=0) Profit=OrderProfit();
I see you mentioned order_close_by_indi should know this already, however OrderProfit() does not know this
for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) { if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket) continue; break; } ?? seems like you don't mean to exit for loop here // next if statement outside of for loop if (liveorder==false) { //if OrderTotal()=0 nothing will happen here if (order_close_by_indi==false && OrderTicket()==selected_order_ticket && last_profit==false) { close_order_profit=OrderProfit() + OrderSwap() + OrderCommission(); //OrderProfit() might be a negative number here meaning (loss), unless order_close_by_indi==false already knows this selected_order_ticket=0; last_profit=true; . . . . .
Such as:
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true)
if(magic.......symbol etc etc.
{
If(OrderProfit()>0)
{
blah blah some more code
last_profit=true;
break; //keeps it from counting on and on, just last order
}
If(OrderProfit()<0)last_profit==false;
{
more code etc etc
last_profit=false;
break;
}
Also I'm not sure how this gets you what you want
for (counter1=0; counter1<=OrdersTotal()-1; counter1++)
As WHRoader pointed out what is the value for (live_order) if OrderTotal() = 0 and exits the for loop ?
Anyhow that is about all I know
- 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, please help me. My EA opens many order, not only one per pair.
I don't know what is the problem in my code.
In this session I am checking the order, if found live order the liveorder=true would block to open more than one order per pair. But doesn't work:
If liveorder=false comes this condition:
Thx for help
Cheers