for(int i=(OrdersHistoryTotal()-1);i>=0;i--); { OrderSelect(i, SELECT_BY_POS,MODE_HISTORY); if(OrderSymbol()==Symbol() && OrderMagicNumber()==BUY) { //for buy order if(OrderType()==OP_BUY && OrderProfit()>0) last=1; if(OrderType()==OP_BUY && OrderProfit()<0) last=0; } }
- You don't need separate magic numbers for buy and sell. See also Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
- This code sets last=1 when the earliest buy order was profitable.
- Don't use 0 and 1 when you mean boolean.
- 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
Hi,
I see your account deleted.
I have one for checking last 3 order profitable or not, but without buy or sell separation.
I hope this give some idea.
I see your account deleted.
I have one for checking last 3 order profitable or not, but without buy or sell separation.
I hope this give some idea.
// Indicator is profitable by history bool isProfitable_1 = NULL; bool isProfitable_2 = NULL; bool isProfitable_3 = NULL; bool IsLastProfitable(){ bool isProfitable = true; int no = 0; for(int i=OrdersHistoryTotal(); i>=1; i--) { no++; if(OrderSelect(i-1, SELECT_BY_POS,MODE_HISTORY)==true){ if(OrderSymbol()==Symbol()) { bool isProfit = OrderProfit() > 0 ? true : false; if(no == 1) isProfitable_1 = isProfit; else if(no == 2) isProfitable_2 = isProfit; else if(no == 3) isProfitable_3 = isProfit; else return isProfitable_1; } } } return isProfitable_1; } void OnTick() { IsLastProfitable(); }
Your code bool isProfit = OrderProfit() > 0 ? true : false;
Simplified bool isProfit = OrderProfit() > 0;
-
for(int i=OrdersHistoryTotal(); i>=1; i--) { no++; if(OrderSelect(i-1, SELECT_BY_POS,MODE_HISTORY)==true){ if(OrderSymbol()==Symbol())
You are incrementing no even if the order is not valid.
-
Do not assume history has only closed orders.
OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017) -
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)
-
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
I would like to check if my last three closed sell or buy trades were profitable, and change a string value depending on it.
I have this code (MT4):
But it doesn't work for me.
How I should modify it?
Thanks for help.