Topic: how to set max number of losers per autotrade session?

If you leave FST with MT4 to autotrade your strategy, is there a way to set it to stop in case of a number of losses, or total loss of some amount? For example, I'd like it to function as after 3 consecutive losses on a strategy, it stops entering trades for that strategy. Other strategies on other instances of FST would not be affected. Then, I can reset the counter when I come back later on. This would prevent the problem of leaving it on autotrade while doing something else (like sleeping) and finding the next morning the strategy had a whole bunch of losing trades.
I know there is the Enter Once indicator, which could limit it to one trade per day (therefore no more than one loss possible) as a workaround.
This might be something to be done in MT4, not FST; if so, any suggestions where to go to start researching how to do it?

Re: how to set max number of losers per autotrade session?

MT4, I think I can start here:

Re: how to set max number of losers per autotrade session?

Maybe it could work as:
counter start is a parameter (presumably user enters 0 at start of automated trading)
EA counts the losses as they occur;
after x losses, it disconnects FST (or sends command to FST to stop automated trading)

4 (edited by krog 2010-10-19 03:45:39)

Re: how to set max number of losers per autotrade session?

This post may have better code:

But then would I have to create a new Expert Advisor, or insert into the MT4-FST expert advisor (MT4-FST Expert.mq4)? After trying a couple of times, I think an MT4 chart can only take one EA at a time, so may have to edit MT4-FST Expert.mq4.

I think it should be in MT4-FST Expert.mq4, can't add another EA. Since it handles connecting to FST, it should also handle disconnecting FST.

Should I put the count-to-max-losses-then-disconnect modification in the start() function or the Server() function?

Re: how to set max number of losers per autotrade session?

I made an example how you can protect your account from further loss.

This code checks the account equity and if it drops below a predefined value, it stops the expert.
If you want, you can  to promote the hard-coded variable to an external variable, in order to set it when starting the expert. You can also write another condition for stopping the trade.

Find the beginning of the server cycle at line 302:

while (!IsStopped())

and add this code below it.

int minAccountEquity = 2500; 
if (AccountEquity() < minAccountEquity)
   message = "Account equity dropped down below " + minAccountEquity + ". Expert was stopped.";
   PostMessageA(WindowHandle(Symbol(), Period()), WM_COMMAND, 33050, 0);
   return (-1);

You have to set the minimum equity in the code. The current value is 2500.

Re: how to set max number of losers per autotrade session?

Thank you Mr. Popov. Ah yes, now I see how to do it -- in Server(), in the while loop, return a -1 to stop trading. I'll get that working, then from there I think I'll have a good idea how to customize the Expert.

Re: how to set max number of losers per autotrade session?

I think this has it, but I've only tested over a short time sample:
for a parameter of how many losses before disconnecting, in external variables:

extern int MaxLosses = 3;

then to initialize the loss count for each session (when I attach the expert and leave it to run for the night):

// declare somewhere outside any function
int ordersHistoryInit = 0;
// set in init function for starting point
int init() {
     ordersHistoryInit = OrdersHistoryTotal();

Down in the Server() function, this code checks the loss count, and disconnects FST if the max has been hit:

int Server() {
    while (!IsStopped())
        // count previous orders, be sure less than max number of allowed losses
        // if more, then return -1 to end server
         int LossCount=0;
         int OrdHistory=OrdersHistoryTotal();
         if(OrdHistory>0) {
           for(int cnt=OrdHistory-ordersHistoryInit; cnt>=1; cnt--){
                 if (OrderSymbol()==Symbol() && OrderMagicNumber()==Expert_Magic) {
                    if (OrderProfit()>=0) {
                    else {
         if (LossCount >= MaxLosses) {
            message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + "Max Losses (" + LossCount + "/" + MaxLosses + ") hit, disconnecting FST";
            Print("Max Losses hit: Loss Count: " + LossCount + " MaxLosses: " + MaxLosses);
            return (-1);

Also in the Server() function, to display how many losses there are up to now:

          if (time > tickTime) {
               if (tickResponce == 1)
                  FST_Connected = true;
                  TimeLastPing  = TimeLocal();
                  message = expertID + TimeToStr(TimeLocal(), TIME_DATE | TIME_SECONDS) + " Forex Strategy Trader is connected." + "\n";
                  // add count to display info here
                  message = message + "Loss Count = " + LossCount + " / Max Loss = " + MaxLosses;

I'll try this out for the week, check if it works or has bugs.

Also, the way this is coded, it will count consecutive losses, not total losses in the session. This is best if your limit is larger than your stop. If your stop is larger than your limit, it would not be so good, as you could have the case of win-loss-win-loss-win-loss ... n, which would not disconnect and could run a large drawdown.

So far in my short testing, this code was able to run separate counts for 2 instances of FST running on 2 different crosses.

Further, I think this requires removing and attaching the Expert for each session, in order to set the max number of losses, and to reset the count.