OrderHistory

 

Good day, Ive been trying to write a script where if (iSAR>Ask && LastClosedOrder=Buy) to open a sell order. I have this part where I get order history. When i run the script it seems like it does not get last closed order instead it gets all of the history. i see this because even if last closed order is Sell, it goes ahead and opens order because in the history there is still buy orders. Please help with this if i have the lines wrong or...

if (MySARValue>Ask)
   {
   
  for(int i=OrdersHistoryTotal()-1;i>=0;i--)
    {
    //---- check selection result
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true &&OrderType()==OP_BUY)
      {
    
    OrderSend(Symbol(),OP_SELL,0.01,Bid,3,stoploss2,takeprofit2,"SELL",EXPERT_MAGIC,0,clrRed);  
   
   Comment("SELL--",MySARValue);
      }


  

 
  1. Why did you post your MT4 question in the Root / 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.

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  3. Do not assume history has only closed orders.
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool).

  5. You are looping through all orders and opening on each buy order seen. Find the last closed order (№ 3.) See it is a buy. I assume you already have a check for open orders (not posted.)

  6. Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

Reason: