Newbie coding help

 

Hi All,

I need helping with fixing a coding bug. The EA is not doing what it is desired as losing trades are not closing at specified times with certain percent. Any help?? 

Attached is a time based stop loss EA. EA's purpose is to close % of a position after specified time has elapsed of a losing position. There are four time based levels and you can choose to close specified position % at these specified time levels.

Files:
TimeEA.mq4  7 kb
 
bm0899:

Hi All,

I need helping with fixing a coding bug. The EA is not doing what it is desired as losing trades are not closing at specified times with certain percent. Any help?? 

Attached is a time based stop loss EA. EA's purpose is to close % of a position after specified time has elapsed of a losing position. There are four time based levels and you can choose to close specified position % at these specified time levels.

What exact error do you get in the experts or journal tab?
 
mladen:
What exact error do you get in the experts or journal tab?

I don't get any error but the TimeBasedSL function is not working as required (I have put some comments in the code where it is not working, simple code).

 

Many thanks. 

 
bm0899:

I don't get any error but the TimeBasedSL function is not working as required (I have put some comments in the code where it is not working, simple code).

 

Many thanks. 

OK. Will check the function and will get back with the results
 
bm0899:

I don't get any error but the TimeBasedSL function is not working as required (I have put some comments in the code where it is not working, simple code).

 

Many thanks. 

Try this (but also add some lots controlling function to avoid invalid lot size error)
Files:
TimeEA.mq4  7 kb
 
mladen:
Try this (but also add some lots controlling function to avoid invalid lot size error)

Hi Mladen,

Thanks for this, your MQL file does the same thing as mine. As you will see on the photo below that "TimeBasedSLperiodLoss2" should be called only once, but it was called multiple times by the EA (see the EA log photo). 

TimeBasedSLperiodLoss1, TimeBasedSLperiodLoss2, TimeBasedSLperiodLoss3, TimeBasedSLperiodLoss4 - these variables should only be called once, hence I had created the if statement (alreadyExecuted1...etc)

 I can't get my head around how to write code, so that TimeBasedSLperiodLoss2 etc will only be called once. So confused :'(

Files:
 
bm0899:

Hi Mladen,

Thanks for this, your MQL file does the same thing as mine. As you will see on the photo below that "TimeBasedSLperiodLoss2" should be called only once, but it was called multiple times by the EA (see the EA log photo). 

TimeBasedSLperiodLoss1, TimeBasedSLperiodLoss2, TimeBasedSLperiodLoss3, TimeBasedSLperiodLoss4 - these variables should only be called once, hence I had created the if statement (alreadyExecuted1...etc)

 I can't get my head around how to write code, so that TimeBasedSLperiodLoss2 etc will only be called once. So confused :'(

Can you please explain exactly what is your idea about that EA?
 

Purpose of the EA: Once a trade has been entered and the trade is in a LOSS then, after certain amount of time close percent of a position (trade must be still in a loss).

For example: If I enter a trade and after 1min the trade is in a loss then close 25% of the original position size; after 5min close remaining 50% position (trade still in a loss), after 10min close remaining 50% position (trade still in a loss), after 20min close remaining 100% of the position (trade still in a loss).

I hope the above makes sense. I wrote 90% of the code already but I think the code isn't functioning as intended as the logic may not be correct. Any help will be appreciated.

 
bm0899:

Purpose of the EA: Once a trade has been entered and the trade is in a LOSS then, after certain amount of time close percent of a position (trade must be still in a loss).

For example: If I enter a trade and after 1min the trade is in a loss then close 25% of the original position size; after 5min close remaining 50% position (trade still in a loss), after 10min close remaining 50% position (trade still in a loss), after 20min close remaining 100% of the position (trade still in a loss).

I hope the above makes sense. I wrote 90% of the code already but I think the code isn't functioning as intended as the logic may not be correct. Any help will be appreciated.

But you have 4 different time periods (gaps) for that, which means it has to enter the procedure 4 times not once

Also, since it is not possible to write information in an already opened (or partially closed) order, it is not possible to mark somehow that some of the partial closes have been already executed, so there is no foolproof way to do a 4 step partial close procedure without errors (due to a lack of information that can not be substituted in any way(

 

Also, some more clarification :

When a partial close is executed on an order that order becomes a completely different order (for the sake of simplicity, let say that the ticket of the order was originally 1, after the partial close it will not be 1 any more) and that way it is not possible to see what happened with the original order (especially since the comment field is overwritten by the broker in partial close cases 99.999% of times - depends on a broker, but they are happily changing the comment field of the order in that case, and we, the traders, can not do that).


So, each partial close is producing a new order (as far as ticket is concerned) which, as far as the code is concerned, did not exist before, and the code can not "know" what were the steps executed on that particular ticket No

 

Thank you so much for your help on this. I really appreciate this.

How do I fix this? Are you able to fix this on the EA? I don't know how to do this.  

Reason: