Hello Can anyone Help me with this code it keep sending OrderCloseBy error 4018

 

i can not figure out what is wrong the buy close well .. but the sell do not close !! as you can see in the photo attached.


here is the code :


//---Reverse Option

      if(OrdersTotal() < 2)

      {

         int ordertype = OrderType();

         

            if(OrderSelect(BBWROrderID,SELECT_BY_TICKET)==true)

            {

            if(ordertype == 0 && Close[1]<red_Hama && red_Hama0 < red_Hama01 && red_Hama < red_Hama1)

            {

            SBWROrderID=OrderSend(NULL,OP_SELL,lotSize*2,Bid,0.3,0,0,"BWR",magic);

            OrderCloseBy(BBWROrderID,SBWROrderID,clrBlue); 

            }

            else if (OrderSelect(SBWROrderID,SELECT_BY_TICKET)==true)

            {

            if(ordertype == 1 && blue_Hama0 > blue_Hama01 && blue_Hama > blue_Hama1 && Close[1]>blue_Hama)

            {

            BBWROrderID=OrderSend(NULL,OP_BUY,lotSize*2,Ask,0.3,0,0,"BWR",magic);

            OrderCloseBy(SBWROrderID,BBWROrderID,clrRed);

            }

            }    

       } 

       } 
Files:
Untitled.png  10 kb
Untitled02.png  63 kb
 
  1. Why did you post your MT4 question in the 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? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.       int ordertype = OrderType();
                if(OrderSelect(BBWROrderID,SELECT_BY_TICKET)==true)
  3. MT4: You can not use any Trade Functions until you first select an order.
    MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)
  4. You select by ticket but do not check if the order is still pending, or has been deleted, or the order closed.

  5. SBWROrderID=OrderSend(NULL,OP_SELL,lotSize*2,Bid,0.3,0,0,"BWR",magic);

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)
MQL5 forum
MQL5 forum
  • www.mql5.com
MQL5: Forum on automated trading systems and strategy testing
 
Ahmed Gouda:

i can not figure out what is wrong the buy close well .. but the sell do not close !! as you can see in the photo attached.


here is the code :


if(orderselect){
if(ordertype){ /*yourcode*/}
}//end of selected
else
if(orderselect){
if(ordertype){ /*yourcode*/}
}//end another selected

Reason: