void CloseAll () { int totalorders = OrdersTotal(); int cnt = 0; for (cnt = totalorders ; cnt >=0 ; cnt--) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES); if(Symbol()!=OrderSymbol()) RefreshRates(); if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet); if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet); if(OrderType()>OP_SELL) OrderDelete(OrderTicket()); } //return (0); } int Buy (int nth_level) { double Lots [5] = {0.01 , 0.02 , 0.04 , 0.07 , 0.10}; int ticket_num = OrderSend (Symbol(),OP_BUY,Lots[nth_level],Ask,3,0,0,"",12345,0,Green); return (ticket_num); } int Sell (int nth_level) { double Lots [5] = {0.01 , 0.02 , 0.04 , 0.07 , 0.10}; int ticket_num = OrderSend (Symbol(),OP_SELL,Lots[nth_level],Bid,3,0,0,"",12345,0,Red); return (ticket_num); }
Replace
for (cnt = totalorders ; cnt >=0 ; cnt--) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES); if(Symbol()!=OrderSymbol()) RefreshRates(); if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet); if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet); if(OrderType()>OP_SELL) OrderDelete(OrderTicket()); }
to
for (cnt = totalorders-1 ; cnt >=0 ; cnt--) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES); if(Symbol()!=OrderSymbol()) RefreshRates(); if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet); if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet); if(OrderType()>OP_SELL) OrderDelete(OrderTicket()); }
Wow!!! thanks! i'll improve this EA...
Always test return codes, make the EA compatible with other EA's (including itself on other charts)
for (cnt = totalorders-1 ; cnt >=0 ; cnt--) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES);to
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and my pair.
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 everyone, i need a little help on finding the error on this code, namely: the orderclose error 4051 and ordersend error 4051
this is a hedging EA that takes 5 levels of lots: 0.01, 0.02, 0.04, 0.07, 0.1
if the previous order takes a -80 point loss, the robot hedges and takes the opposite position.
if the total profit makes it up to $1, then it closes all the orders...