MQL5 EA - No Results for Lot Sizing

 
double maxRiskAmount = 100000 * 0.02;
double pipValue = 10 * SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);

//Not Working
if (signal =="sell" && PositionsTotal()<1)
{
double SL = NormalizeDouble((SARvalue - Bid)*_Point,2);
double riskperpip = maxRiskAmount/SL;
double lot = NormalizeDouble(riskperpip/pipValue,2);
trade.Sell(lot,NULL,Bid,(SARvalue),(Bid - ((SARvalue - Bid)*5)),NULL);
}

//Working
if (signal =="buy" && PositionsTotal()<1)
{
double SL = NormalizeDouble((Ask - SARvalue)*_Point,2);
double riskperpip = maxRiskAmount/SL;
double lot = NormalizeDouble(riskperpip/pipValue,2); 
trade.Buy(0.10,NULL,Bid,(SARvalue),(Ask+ ((Ask - SARvalue)*5)),NULL);
}


Hi All, 

I wanted my EA to auto calculate the lot sizing based on a fixed 2% on my account balance of ($100,000) against the stop loss I get the the ParabolicSar. 

I am not sure what I did wrong here but my EA will not run unless i replace the "lot" variable to an actual lot size like "0.10". 


Please do advise. Thank you.

 
kandasami:


Hi All, 

I wanted my EA to auto calculate the lot sizing based on a fixed 2% on my account balance of ($100,000) against the stop loss I get the the ParabolicSar. 

I am not sure what I did wrong here but my EA will not run unless i replace the "lot" variable to an actual lot size like "0.10". 


Please do advise. Thank you.

Try my way:

if(Long==true&&BuyOrders<1){
        SL=0;if(StopLoss==true){SL=(Ask-SAR[0])/_Point;}
        double LotSize=MathRound((Risk*((Balance/SL)/Tick)/100)/minLot)*minLot;
        if(!trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,LotSize,Ask,SAR[0],Ask+(SL*5)*_Point,CustomComment)){
                Print("PositionOpen error ",trade.ResultRetcode());
                return;
                }
        }
if(Short==true&&SellOrders<1){
        SL=0;if(StopLoss==true){SL=(SAR[0]-Bid)/_Point;}
        double LotSize=MathRound((Risk*((Balance/SL)/Tick)/100)/minLot)*minLot;
        if(!trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,LotSize,Bid,SAR[0],Bid-(SL*5)*_Point,CustomComment)){
                Print("PositionOpen error ",trade.ResultRetcode());
                return;
                }
        }
input double Risk=2.0;
 
David Diez:

Try my way:

Hi David, 


Thanks for helping. 

Mind if I ask what does minLot mean? 

Does it mean the min lot I can select for the particular symbol ie. (0.01) to ensure that even if the calculation results in anything lesser than (0.01), it would still trigger a (0.01) lot size?


Thanks. 

Reason: