Publish a Trading Robot in MQL5 Code Base and Let Millions of Traders Know About You
MQL4.com is moving to MQL5.com. Sign up for the new web portal for traders!

EA lot size % changer with balance change

Back to topics list To post a new topic, please log in or register
avatar
11
paulme33 2010.02.06 22:15
 

i have Ea i wrote a EA and would like to add script to it so that i can input a % of the balance to use in buying lots.

So $1000 could have a lot size of 0.1 and then increase it to 0.2 when the balance is $2000 and between those amounts? but i would like to be able to change the settings of the %.

i have tried to add some code but it didnt work. could some one who knows what they are doing help?

Attached files:
  newradxhsamewslaectcthankbuy.mq4  (9.28 KB)
 
DON’T KILL THE MESSENGER! GENERALISED FOREX FORECAST FOR 20 – 24 APRIL 2015
It is well known that you do not kill the messenger, even if he brings bad news. And so we, ladies and gentlemen, are those very messengers who generalise and bring to you the opinions of 35 analysts from world leading banks and broker companies, as well as forecasts based on different methods of technical and graphical analysis. We collect all this in a table and try to deduce some sort of averaged indications. Regarding last week’s forecast, we have to admit that the forecaster proposes, but the market disposes. Just like the old saying goes. And so last week, not only did 30% of the analysts fall through, or simply speaking make inaccurate forecasts, but also... 91% of the indicators! Only 6% of the analysts hit the bull’s eye (if you do not take into account the 58% who simply did not know, wisely refusing to comment at all). As an explanation we can only offer the Easter holidays (what else?
avatar
48
joetrader 2010.02.06 22:38 #
 

This is what I use:

Extern double PercentRisked=2

double LotsBuy()
{
double StopLossBuyCalc =(Ask-BuyStop+(Ask-Bid)+SLFromFractal*Point)*(1/Point);
double RiskPercent = (PercentRisked*0.01);
double DollarsPerLot,FirstDollarsPerLot,SecondDollarsPerLot,BuyLots;

DollarsPerLot = (MarketInfo(Symbol(),MODE_TICKVALUE)*10);
BuyLots = NormalizeDouble(((AccountBalance()* RiskPercent)/(StopLossBuyCalc*0.1)/DollarsPerLot),2);
return (BuyLots);
}

 
avatar
10576
WHRoeder 2010.02.07 00:54 #
 
//+------------------------------------------------------------------+
//| Lot size computation.                                            |
//+------------------------------------------------------------------+
double  LotSize(double SL_points, bool modifying = false) {
    /* This function computes the lot size for a trade.
     * Explicit inputs are SL relative to bid/ask (E.G. SL=30*points,) and if
     * returned lot size will be used in a new order or be used to modify an
     * pending order (trade count-1.) Implicit inputs are the MM mode, the MM
     * multiplier and currently filled or pending trade count (used to reduce
     * available balance.) Mode, multiplier, adjusted account balance determined
     * the maximum dollar risk allowed. StopLoss determines the maximum dollar
     * risk possible per lot. Lots=maxRisk/maxRiskPerLot
     **************************************************************************/
    double  ab  = AccountBalance();
    switch(MMMode_F0M1G2) {
    case MMMODE_FIXED:
        atRisk  = MMMultplier;
        break;
    case MMMODE_MODERATE:
        // See https://www.mql5.com/en/articles/1526 Fallacies, Part 1: Money
        // Management is Secondary and Not Very Important.
        atRisk  = MathSqrt(MMMultplier * ab)/ab;    // % used/trade ~const.
        atRisk  = MathSqrt(MMMultplier * ab
                            * MathPow( 1 - atRisk, OrdersTotal()-modifying ));
        break;
    case MMMODE_GEOMETRICAL:
        atRisk  = MMMultplier * ab
                            * MathPow(1 - MMMultplier, OrdersTotal()-modifying);
        break;
    }
    double  maxLossPerLot   = SL_points/Point
                            * MarketInfo( Symbol(), MODE_TICKVALUE );
    // IBFX demo/mini       EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000
    // 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/standard   EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000
    //                                  $1.00/point or $10.00/pip.
    double  LotStep = MarketInfo( Symbol(), MODE_LOTSTEP ),
    // atRisk / maxLossPerLot = number of lots wanted. Must be rounded/truncated
    // to nearest lotStep size.
    //
    // However, the broker doesn't care about the atRisk/account balance. They
    // care about margin. margin used=lots used*marginPerLot and that must be
    // less than free margin available.
    marginFree   = AccountFreeMargin(),                     // Free Margin
    marginPerLot = MarketInfo( Symbol(), MODE_MARGINREQUIRED ),
    // So I use, the lesser of either.

    // I don't use MODE_MAXLOT as OpenNow handles that.
    size =  MathFloor(  MathMin ( marginFree / marginPerLot
                                , atRisk     / maxLossPerLot )
                        / LotStep )*LotStep;        // truncate
    if (size < MarketInfo( Symbol(), MODE_MINLOT )) {
        // Multiple orders -> no free margin -> LotSize(SL=4.1)=0
        // [risk=9.48USD/40.80, margin=10.62/1334.48, MMM=1x1, coat=0]
        //       0.23                  0.007
        Print(
            "LotSize(SL=", DoubleToStr(SL_points/pips2dbl, Digits.pips), ")=",
            size, " [risk=",    atRisk, AccountCurrency(),  "/", maxLossPerLot,
                    ", margin=",    marginFree,             "/", marginPerLot,
                    ", MMM=",       MMMode_F0M1G2,          "x", MMMultplier,
                    ", coat=",      OrdersTotal(),  // Count Of active trades
                "]" );
        return(0.0);    // Risk limit.
    }
    atRisk  = size * maxLossPerLot; // Export for Comment()
    return(size);
}   // LotSize



avatar
11
paulme33 2010.02.07 01:59 #
 

could you paste it in to my code and test it and repost it on here? or email it to me as i have no luck in adding it? please..

paulsmith101@hotmail.com

 
Back to topics list  

To add comments, please log in or register