MQL4 - automated forex trading   /  

Forum

Stop trading after losses

Back to topics list To post a new topic, please log in or register

avatar
5
sasuke 2006.10.23 21:24 
I'm trying to figure out how to get my EA to sleep for X amount of time after it has made N number of losses and then continue to trade as usual when it wakes up. Can anyone help me with this coding?
article

Video interview with Rashid Umarov

The main conclusion is that it is insufficient to have a profitable strategy: it is necessary to strengthen its advantages and smooth its disadvantages. This is the point in money management: You should gain the largest profit from your approach, your Expert Advisor, your system with minimal risk and in maximal amounts, without being short of it. This is the most important thing, to my mind.


avatar
120
oldman 2006.10.25 09:17 
I would count each time it closed for a loss, put that in a global variable, check that variable each tic and sleep(x) when var reaches your max losses..
You may have to run through your history to find the closed orders and see what profit they made,
Set your terminal history for "last three days" (shouldn't use "today" ,.... might roll over and miss one)
You might want to play with this to insure your trades are not gone from history before your max losses occur... ie, you lost one tuesday, one wednesday and one saturday... if you had last three days history only, then you would only retrieve one trade...

for instance (and i am rough in this, bare with me)

int losses,maxlosses,h,history;
int x =milliseconds to sleep;
int init() {
history=HistoryTotal();
GlobalVariableSet("history",history); //// we reset these each time we start the ea.
GlobalVariableSet("losses",0); //// We now know how many we have in history presently
}

init start() {

h=HistoryTotal();
history=GlobalVariableGet("history",history); // we need to get this each tic
if(h>history) { ///// we have a new addition to history, lets check if a loss. ....
GlobalVariableGet("losses", losses);
OrderSelect(h-1, SELECT_BY_POS, MODE_HISTORY);
if(OrderProfit() < 0) {
losses++;
GlobalVariableSet("losses", losses);
if(losses>maxlosses) {
GlobalVariableSet("losses", 0); /// we found the number we were looking for so we reset this
///for next time
sleep(x);
}
}

GlobalVariableSet("history",h); /// reset the history to current trades when it wakes up.
}

} //// end of this little messed up script. I would not use it until someone a little more knowledgeable in this remarks on it... shouldn't be too long.


avatar
5
sasuke 2006.10.25 19:53 
oldman, Thanks for your help. I'll be waiting to see what others have to suggest.

avatar
120
oldman 2006.10.25 23:28 
It appears from trial and error that historytotal() -1 is the last order closed. . for some reason.
I modified the script above to reflect that.

avatar
18
asmocon 2006.10.31 00:10 

Hopefully this might be something useful. I know that you are wanting it to wait for so long after "multiple losses," but the way we do it is to have it wait after a single loss. And the wait time can easily be optimized because it is based on the candles drawn.

First:
extern int Shift = 1;


Then:
WaitTime=iTime(NULL,0,Shift);

Finally:

total=OrdersTotal();
if(total>=1 && OrderProfit()<0)
{
CloseTime=iTime(NULL,0,0);
}
if(total<1 && WaitTime<=CloseTime)
{
Pause=true;
}
else
{
Pause=false;
}


Basically, it assigns "shift" as a variable. Then, you can optimize how small-large you want the "shift" to be. we did this because we found that a lot of times after a full stop-loss trade, it would end up opening another trade that would lose. This way, after any loss it forces the EA to wait however long "shift" is set for. And the shift is the number of candles. Basically, if you have a shift of 2 and you are attached to a 30minute chart, it would wait for 2 full candles to process before it started to look at options for opening a positions. In the very least, hope this might be able to help in some manner.

Back to topics list  

To add comments, please log in or register