int maxEntriesToLookAt=10; bool found=false; for(int n=OrdersHistoryTotal()-1; n>=0; n--) { if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break; if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=true; break; } } if(found) Print("Magic order exists in recent history!");I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.)
I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.)
void hedger() { int buy_trades_number = _get_number_of_trades(OP_BUY); int sell_trades_number = _get_number_of_trades(OP_SELL); int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY); int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL); int maxEntriesToLookAt=10; bool found=false; for(int n=OrdersHistoryTotal()-1; n>=0; n--) { if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break; if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=true; break; } } if(found) Print("Magic order exists in recent history!"); if (buy_trades_number == 6 && numberofhedgedbuys == 0) {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);} if (sell_trades_number == 6 && numberofhedgedsells == 0) {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);} }
Yes! exactly what I was looking for, however, this doesnt work although seemingly it should. I also tried reversing the conditions by using != and sticking in a continue but that didnt work. Do you see the error here that I dont see?
essentially what Im trying to do is see if there is an order in the last 10 trades with a magic number of 333 or 334 and if there is, I want to break out of the function
I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.
void hedger() { int buy_trades_number = _get_number_of_trades(OP_BUY); int sell_trades_number = _get_number_of_trades(OP_SELL); int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY); int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL); int found=0; for(int n=OrdersHistoryTotal()-1; n>=0; n--) { if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break; if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=1; break; } } if(found) Print("Magic order exists in recent history!"); else; if (buy_trades_number == 7 && numberofhedgedbuys == 0 && found==0) {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);} if (sell_trades_number ==7 && numberofhedgedsells == 0 && found==0) {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);} }
does this look fine to you? Or is it bad code practice?
...
essentially what Im trying to do is see if there is an order in the last 10 trades with a magic number of 333 or 334 and if there is, I want to break out of the function
This is easy. I wonder why you didn't see it:
if(found) return; // was: Print("Magic order exists in recent history!");
Or, to take your else construct into account:
if(found) Print("Magic order exists in recent history!"); else { if (buy_trades_number == 7 && numberofhedgedbuys == 0) {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);} if (sell_trades_number == 7 && numberofhedgedsells == 0) {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);} }
Note there's some really helpful tool to fix the code indentation (formatting of white space etc to improve readability without changing the logic), it's called Styler. You'll find it in the Tools menu of the editor.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have a separate function, seen below, that applies a trailing stop loss to these 50 lot trades. The problem is that once the trailing stop loss closes out one of these trades, it automatically opens up another. So I had an idea to use OrdersHistoryTotal() in order to scan the last 20 historical trades and make sure there wasnt one with a magic number of 333 or 334 before placing a 50 lot trade. Any ideas on how to do this guys? I suck at creating loops.
trailing stop loss code^
how I count my buys and sells code^
how my onTick code looks^