What is the most stable way to code a function to retrieve the last trades closed @ tp ONE BY ONE from history pool?
ordo: This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time:
if(OrdersHistoryTotal() > prevhistorytotal) { for(int i=OrdersHistoryTotal()-1; i>=0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); if(PipsProfit(OrderTicket()) >= TPLevel_pips)
You're half way there
- Check your return codes (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
- filter out just that EAs orders order accounting - MQL4 forum
- I use this:
double ocp = OrderClosePrice(); bool isTP = MathAbs(OrderTakeProfit() - ocp) < MathAbs(OrderStopLoss() - ocp);
- Find latest.
datetime lastCloseDT = 0; double lastCloseTP; for(int i=OrdersHistoryTotal()-1; i>=0; i--) if( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber() == MY_MAGIC && OrderSymbol() == Symbol() && PipsProfit(OrderTicket()) >= TPLevel_pips && lastCloseDT < OrderCloseTime() ){ lastCloseDT = OrderCloseTime(); lastCloseTP = OrderTakeProfit(); } if(lastCloseDT == 0) ... // None
&& PipsProfit(OrderTicket()) >= TPLevel_pips
Passing the ticket to that function is suspect. You shouldn't do an OrderSelect in the function when you call the function inside an OrderSelect loop. Also since SL/TP are market orders, that comparison may not be true.
ordo:
Hello dear coders,
This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time:
You could try something along the OrderProfit() Comparison
for(int i = OrdersHistoryTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Access to history failed with error (",GetLastError(),")") break; } if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true) //MODE_HISTORY selects closed and cancelled { if(OrderMagicNumber() == MagicNumber) //if you don't want these filters and want ALL orders from all symbols then get rid of these 2 lines if(OrderSymbol()== Symbol()) ///// { if(OrderProfit()<0) //trade was a losing trade meaning negative profit Print("Order LotSize was ",OrderLots()," Increasing to ", mlots," Ticket#", OrderTicket()); break; // break stops counting back as soon as something is found } if(OrderProfit() > 0 && OrderProfit() == TPlevels) //trade was a winner and matches the TP level //TPlevels could be your extern double TP*pips2double or whatever you use for 3/5 Digit brokers //with break finds only 1 and exits, remove the break to keep counting back and further filtering code can be added below { Print("Ticket Number ",OrderTicket()," Closed by TPlevel "," TPlevel ",TPlevel, "=" OrderProfit ",OrderProfit()); break; } } } } //I would put this function in my OrderEntry function //And call it with something like double OrderCheck() or some such function
I only suggest this because I'm noobish coder and this is what I can understand.
These gurus have much better and simpler methods I have no doubt, but this would work for me and I would use it as a function() to keep my head on straight. If code gets too long I get confused too easy.
Hope this gives you some ideas for what you want
Agent86: I only suggest this because I'm noobish coder and this is what I can understand.
This will fail
OrderProfit() == TPlevels
- Slippage after TP triggers and becomes a market order
- The == operand. - MQL4 forum
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello dear coders,
This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time: