Hi all,
I am learning how to write EAs. Steep curve, but getting there slowly. I am looking at some of the great coders here for ideas & to learn techniques.
I have coded a dead simple script. I am testing an EA that can open multiple trades. In order to close the trades I wrote a simple script that closes if the comment belongs to that EA & symbol. No problems so far. However, when the script runs on a chart it closes every second trade that belongs to that symbol & with the correct comment.
Is this normal action? Certainly not what I expected! And, of course, the script checks all open orders, finds the correct ones to close & then closes every 2nd. So half the orders are not closed before the script finishes! I can get around this by making it an EA so it keeps going until all orders are closed.
I'm happy to work out what I have done wrong - I just don't want to spend any more hours trying to solve a "problem" only to find that it is the way Empty4 handles things.
Thanks for your help. Cheers,
Mick
Coding question
-
madpipa
- Trader
- Posts: 62
- Joined: Fri Oct 19, 2012 12:59 am
- Location: Gold Coast, Australia
-
phil_trade
Re: Coding question
Himadpipa wrote:Hi all,
I am learning how to write EAs. Steep curve, but getting there slowly. I am looking at some of the great coders here for ideas & to learn techniques.
I have coded a dead simple script. I am testing an EA that can open multiple trades. In order to close the trades I wrote a simple script that closes if the comment belongs to that EA & symbol. No problems so far. However, when the script runs on a chart it closes every second trade that belongs to that symbol & with the correct comment.
Is this normal action? Certainly not what I expected! And, of course, the script checks all open orders, finds the correct ones to close & then closes every 2nd. So half the orders are not closed before the script finishes! I can get around this by making it an EA so it keeps going until all orders are closed.
I'm happy to work out what I have done wrong - I just don't want to spend any more hours trying to solve a "problem" only to find that it is the way Empty4 handles things.
Thanks for your help. Cheers,
Mick
it's always best to publish your code as we can help you better.
you probably loop the trades from 1 to total() instead from total() to 1 step -1 when closing
cheers
Philippe
-
madpipa
- Trader
- Posts: 62
- Joined: Fri Oct 19, 2012 12:59 am
- Location: Gold Coast, Australia
Re: Coding question
Hi Philippe,
Thanks for the prompt reply. You are correct - I did loop from 0 to OrderTotal()-1 . Since my earlier post I added a comment section within the same loop as the OrderClose command to print all ticket numbers to the screen. If I have 6 open trades it prints the 6 numbers correctly. If I then uncomment the OrderClose commands it prints every 2nd order.
As I was writing this reply I just remembered reading something from another member a couple of weeks ago. I wasn't sure what he meant at the time, but now I think he was saying the same as you. I didn't realise the full impact of what looping the wrong way would do. Another lesson learned!!!
Is it better to always loop from total to 1 step -1? Are there exceptions? Or should I usually go from 1 to total & only use the reverse method for order closes? Do you know of anywhere I can read these types of things, or do I have to learn the hard way - like my trading
I have just run the test again with the correction you identified. Spot on - it now works! And I now understand why it was only doing every second trade close. I won't forget again!!
Thanks very much for your help Philippe.
Cheers,
Mick
Thanks for the prompt reply. You are correct - I did loop from 0 to OrderTotal()-1 . Since my earlier post I added a comment section within the same loop as the OrderClose command to print all ticket numbers to the screen. If I have 6 open trades it prints the 6 numbers correctly. If I then uncomment the OrderClose commands it prints every 2nd order.
As I was writing this reply I just remembered reading something from another member a couple of weeks ago. I wasn't sure what he meant at the time, but now I think he was saying the same as you. I didn't realise the full impact of what looping the wrong way would do. Another lesson learned!!!
Is it better to always loop from total to 1 step -1? Are there exceptions? Or should I usually go from 1 to total & only use the reverse method for order closes? Do you know of anywhere I can read these types of things, or do I have to learn the hard way - like my trading
I have just run the test again with the correction you identified. Spot on - it now works! And I now understand why it was only doing every second trade close. I won't forget again!!
Thanks very much for your help Philippe.
Cheers,
Mick
-
phil_trade
Re: Coding question
madpipa wrote:Hi Philippe,
Thanks for the prompt reply. You are correct - I did loop from 0 to OrderTotal()-1 . Since my earlier post I added a comment section within the same loop as the OrderClose command to print all ticket numbers to the screen. If I have 6 open trades it prints the 6 numbers correctly. If I then uncomment the OrderClose commands it prints every 2nd order.
As I was writing this reply I just remembered reading something from another member a couple of weeks ago. I wasn't sure what he meant at the time, but now I think he was saying the same as you. I didn't realise the full impact of what looping the wrong way would do. Another lesson learned!!!
Is it better to always loop from total to 1 step -1? Are there exceptions? Or should I usually go from 1 to total & only use the reverse method for order closes? Do you know of anywhere I can read these types of things, or do I have to learn the hard way - like my trading![]()
I have just run the test again with the correction you identified. Spot on - it now works! And I now understand why it was only doing every second trade close. I won't forget again!!![]()
Thanks very much for your help Philippe.
Cheers,
Mick
Is it better to always loop from total to 1 step -1?
No it's only needed for closing purpose as when you close Ticket #1, Ticket Number 2 becomes #1, and you step +1 so miss it !
For other purpose, you may need to scan from 1 to total()
cheers
Philippe
-
madpipa
- Trader
- Posts: 62
- Joined: Fri Oct 19, 2012 12:59 am
- Location: Gold Coast, Australia
Re: Coding question
Thanks for you time Philippe. Now I understand I hope to never make that mistake again.phil_trade wrote: No it's only needed for closing purpose as when you close Ticket #1, Ticket Number 2 becomes #1, and you step +1 so miss it !
For other purpose, you may need to scan from 1 to total()
cheers
Philippe
Cheers & green pips,
Mick
-
phil_trade
Re: Coding question
your welcome !gamegold1 wrote:Thanks for you time Philippe. Now I understand I hope to never make that mistake again.
What is funny, is that you will make other bugs, as all of us
It is necessary to be aware of that, and always do an audit of your code searching where these bugs are...because they are !