Need help with Back Tester

[Deleted]  

Can't understand why my back tester just stops all of the sudden??? I took a quick video of my setup...

please take a look and let me know if you see anything wrong.

Thanks!

[Deleted]  

>
William Roeder
23491
William Roeder  
zzuegg
1502
zzuegg  

"stopped because of Stop Out" is wirtten in the journal.

Seems that you get a Margin Call...

[Deleted]  
Why would I get a margin call if I was making a profit?
zzuegg
1502
zzuegg  
well i suggest you start reading what a margin call is and how the margin used and free margin is calculated... note that you would require some free margin to open a position...
William Roeder
23491
William Roeder  
rortiz77:
Why would I get a margin call if I was making a profit?
The two are independent. Some free margin to open a position is only half the problem. As the market move against the position(s), free margin drops. Once it reaches zero - margin call. If you max out your margin and the market drops by one point, margin call.

You must prevent margin call by reducing lot size for the last open order.

    perLotPerPoint  = PointValuePerLot();
    total.risk      = 0;
    oo.lots         = 0;                    // _> multiple open.
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number          // my magic number
    &&  OrderSymbol()      == Symbol() ){           // and period and symbol
        oo.lots += OrderLots();
        perPoint        = OrderLots()*perLotPerPoint,
        eRisk           = DIR*(OrderOpenPrice()-OrderStopLoss())*perPoint;
        if (eRisk > 0)  total.risk += eRisk;    // Total balance reduction.
...
double LotSize(double risk){
    ... compute size ...
    double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
            LotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
            perLotPerPoint  = PointValuePerLot(),
            maxLossPerLot   = risk * perLotPerPoint,
            size = at.risk / maxLossPerLot; // Must still round to LotStep.
    /*---- Compute lot size based on account balance and MM mode*/}
    /* The broker doesn't care about the at.risk/account balance. They care
     * about margin. Margin used=lots used*marginPerLot and that must be less
     * than free margin available. Using the lesser of size vs
     * AccountFreeMargin / MODE_MARGINREQUIRED should have been sufficient, but
     * the tester was generating error 134 even when marginFree should have been
     * OK. So I also use AccountFreeMarginCheck < 0 which agrees with the
     * tester. Reported at https://www.mql5.com/en/forum/128506
     *
     * Second problem, after opening the new order, if free margin then drops to
     * zero we get a margin call. In the tester, the test stops with: "EA:
     * stopped because of Stop Out" So I make sure that the free margin
     * after is larger then the equity risk so I never get a margin call. */
    EA.status = "SL>AE";    // Assume size < minLot
    while (true){   // Adjust for broker, test for margin, combine with TEF...
        size = MathFloor(size/LotStep)*LotStep;
        if (size < minLot){     Print("LotSize(SL=",
            DoubleToStr(risk/pips2dbl, Digits.pips), " pips)=", size,
            "<", minLot, " ", EA.status, ", MMM=", Money.Management.F0M1G2,
            "@", Money.Management.Multiplier);
            at.risk=0;  return(0);  }

        at.risk = size * maxLossPerLot;                 // Export for Comment
        if (at.risk+total.risk > maxRisk){
            size *= (maxRisk-total.risk)/at.risk;
            EA.status = "MaxRisk";  continue;   }

        double  AFMC = AccountFreeMarginCheck(Symbol(), op.code, size),
                eRisk   = risk*(oo.lots+size)*perLotPerPoint;
        if (AFMC <= eRisk){ size *= 0.95;   EA.status = "Free Margin";
            continue;   }   // Prevent margin call if new trade goes against us.
        return(size);   // We're good to go.
    }
    /*NOTREACHED*/
}   // LotSize
double PointValuePerLot() { // Value in account currency of a Point of Symbol.
    /* In tester I had a sale: open=1.35883 close=1.35736 (0.00147)
     * gain$=97.32/6.62 lots/147 points=$0.10/point or $1.00/pip.
     * IBFX demo/mini       EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000
     * IBFX demo/standard   EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000
     *                                  $1.00/point or $10.00/pip.
     *
     * https://www.mql5.com/en/forum/127584 CB: MODE_TICKSIZE will usually return the
     * same value as MODE_POINT (or Point for the current symbol), however, an
     * example of where to use MODE_TICKSIZE would be as part of a ratio with
     * MODE_TICKVALUE when performing money management calculations which need
     * to take account of the pair and the account currency. The reason I use
     * this ratio is that although TV and TS may constantly be returned as
     * something like 7.00 and 0.00001 respectively, I've seen this
     * (intermittently) change to 14.00 and 0.00002 respectively (just example
     * tick values to illustrate). */
    return(  MarketInfo(Symbol(), MODE_TICKVALUE)
           / MarketInfo(Symbol(), MODE_TICKSIZE) ); // Not Point.
}