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 not 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.