Exit Buy Orders

How it works:

  • It selects the number of orders and then subtracts 1 from that number and assigns that number to (i) i.e. 7 orders become 6. then the loop will start from 6 and run 7 times 6,5,4,3,2,1,0 and stop.
  • As it starts it selects order 6. If it is not able to then it will jump to the else statement and print the reason why in the journal.
  • Next it checks to see if it is a buy order. If it is not it moves on, decrements (i– changes it to 5).
  • If it is a buy it checks the Magicnumber.If it’s incorrect it does i– and goes to 5.
  • If Magic is good it starts the While loop which is set to true which means it will never stop unless something inside it makes it break out of it’s cycle.
  • It tries to close the order with the parameters obtained from the order it just selected and stores the result, true or false in the result variable.
  • If it did notexitbuys() close it’s gets the last error from the broker and prints it to the journal. Otherwise if it did close properly it simply sets err to zero and exits the while loop. And moves on to the next order.
  • If the order did NOT close then one of the cases in the switch might match. If it finds a match it will do everything on every line after that until it hits a (break). So any code that matches will run until they hit the break on the case:146 line. It will wait 1000 milliseconds then refresh the rates and then bump (i)  up and break out of switch, and then break out of the while and retry that order.
  • For instance if order 4 does not close it will get bumped up to 5 here and then when it jumps out of the switch and the while loop and gets to the end of this for loop iteration the 5 will get changed back to 4 and the order will run again. Hopefully closing this time with the rates refreshed.
  • When the loop completes 7 runs. 6,5,4,3,2,1,0 and comes back for a run at -1 it will stop and all orders will be closed.

Note: The Code found here is a little different than what is in the Lesson 26 video. I added err 129 and also added another break; to break out of the while after breaking out of the switch.

5 thoughts on “exitbuys()

  1. Ajay Karkhane

    Hi Jim,
    Its really great to see all your videos and learn,
    From lesson of making EA i tried with my own hand and typed the EA in MQL4 .I applied to daily chart and did the back testing but it does not open any order ,
    I could not understand the reason for not opening any trade.can you pls explain to me if order send function shown in your video one year back is changed in these days or do we need to do any modification to old code for opening the orders.


    1. johmcg64

      Did you check you lot size? The min live trading lot size is sometimes bigger than demo account lot size. Also, the lot stop needs to be correct. In code:

      Print(“Minimum permitted amount of a lot=”,MarketInfo(Symbol(),MODE_MINLOT));
      Print(“Step for changing lots=”,MarketInfo(Symbol(),MODE_LOTSTEP));
      Print(“Maximum permitted amount of a lot=”,MarketInfo(Symbol(),MODE_MAXLOT));

      Jim told me in a video that an individual was practice trading with .01 size but live trading had a minumum lot size of .05.

  2. ahcmos

    Hi Jim i learn yours vdo it is basic to development but i can’t make it
    by the way thanks a lot for yours vdo i will try hard to make money in forex market
    thanks you

  3. Zepplin

    JD you can use the tags to put these mql code in, this will keep all the identation, some hightlights etc, I think it would be nice, by the way, congratulations for your work, really good and helpful.

Leave a Reply