Hello,
I've been struggling with this for a quite a while, and I get some error that just don't make sense to me, Hopefully some of you pros can help me out and find what I'm missing. I want to assign the ticket number to each open order after removal and then also get the open price of the oldest order. In my point of view I have this programmed EXACTLY the same for buys and sells, but if works properly for sells, and not for buys. Last week it was the opposite, it worked for buys and not for sells, so this drives me mad!!
this is my output when I print it now. At the moment of testing this I have 2 sells and 2 buys open, I should see Signal buy start price "opening price of the buy order", but its just returning 0.
Also, I might think the problem is in the loops as tickets doesn't reach index 0?
I search for orderselect error #4051, but could not find any useful information about how to fix that. Any help here plz?
I wonder, why do you want to assign a ticket number to the open order?
This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations.
This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations.
There is no need to put ordertickets in an array. The terminal already gives you access to open orders. Just do an orderselect loop to check the situation.
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum?
Next time post in the correct place. The moderators will likely move this thread there soon. -
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ ⋮ tickets[i] = OrderTicket();
You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays. -
if(OrderSelect(orders[0], SELECT_BY_TICKET==true))
The second argument is SELECT_BY_TICKET. It is not a bool.
- www.mql5.com
This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations.
- docs.mql4.com
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum?
Next time post in the correct place. The moderators will likely move this thread there soon. -
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays.
- The second argument is SELECT_BY_TICKET. It is not a bool.
Hello, thank you for answer, sorry about the post in the wrong group, will post in the correct place next time.
So you recomend I make this removal update on the ontick funcion just once? So that I avoind making the update in OnInit?
"You need i for the position list index and two others for the buy/sell arrays. " I think this might be the problem, I added 1 more sell and one more buy. and I get this as a result
Index i is indeed skipping through the orders. What do you mean with using two others? How can I approach that?
Thank you!
How can I use a magic number for this situation?
-
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum?
Next time post in the correct place. The moderators will likely move this thread there soon. -
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays.
- The second argument is SELECT_BY_TICKET. It is not a bool.
I fixed it thank you!!
How can I use a magic number for this situation?
When the EA open the trade using order send,
static int MAGIC_NUMBER_GOES_HERE = 12345; OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",MAGIC_NUMBER_GOES_HERE,0,clrGreen);
if the Magic Number was included, the EA will recognize the trade by check for OrderMagicNumber().
For example:
if (buypositions > 0){ for(int i =buypositions-1; i>=0; i--){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ if(_Symbol==OrderSymbol()){ if(OrderType()==OP_BUY && OrderMagicNumber == MAGIC_NUMBER_GOES_HERE){ //<<---------- check for magic number according to the one set earlier tickets[i] = OrderTicket(); Print("ticket ", i, " ", tickets[i]); } } } else{ Print("OrderSelect failed error # ", GetLastError()); }; } }
So, each time the EA was reinit, it will check only the trade opened with magic number.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I've been struggling with this for a quite a while, and I get some error that just don't make sense to me, Hopefully some of you pros can help me out and find what I'm missing. I want to assign the ticket number to each open order after removal and then also get the open price of the oldest order. In my point of view I have this programmed EXACTLY the same for buys and sells, but if works properly for sells, and not for buys. Last week it was the opposite, it worked for buys and not for sells, so this drives me mad!!
this is my output when I print it now. At the moment of testing this I have 2 sells and 2 buys open, I should see Signal buy start price "opening price of the buy order", but its just returning 0.
Also, I might think the problem is in the loops as tickets doesn't reach index 0?
I search for orderselect error #4051, but could not find any useful information about how to fix that. Any help here plz?