Calucating exact Profit and Loss

 

Hi,

I am having problems in calcuating the exact expected profit and loss.

Below is the code.

Even if my position closes at exactly the specified TP and SL I seeing variation in actual profit and loss.

I believe I accounted the spread in the below code.

Please verify and help me.

double tloss=0;    //Target Loss
double tprofit=0; //Target Profit

//pprice = TP price
//lprice = SL price
//Long Position
      ppips = NormalizeDouble((pprice-Ask)/(Point*10),1);        //Example: 1.30000 - 1.291000 = 10.0 pips
      lpips = NormalizeDouble((Ask-lprice)/(Point*10),1);
//Short Position
      ppips = NormalizeDouble((Bid-pprice)/(Point*10),1);
      lpips = NormalizeDouble((lprice-Bid)/(Point*10),1);

doublr target = 10;
double maxPerLot   = (dd*MathAbs(lpips))/Point * MarketInfo( Symbol(), MODE_TICKVALUE );

double  LotStep = MarketInfo( Symbol(), MODE_LOTSTEP ),
        marginFree   = AccountFreeMargin(),
        marginPerLot = MarketInfo( Symbol(), MODE_MARGINREQUIRED ),
        size =  MathFloor(  MathMin ( marginFree / marginPerLot , target / maxPerLot ) / LotStep )*LotStep;

        if (size < MarketInfo( Symbol(), MODE_MINLOT )) return(0.0);

        tloss  = -size * maxPerLot;
        tprofit = MathAbs((ppips/lpips)*tloss);

return(size);
 
Are you taking the SPREAD into the calculation?
 

Ahh, I see you've written you did, but actually not. You can open a short position at the BID price, but closing is possible only at the ASK.

 
deVries:
I think the OP wants to calculate potential profit prior to placing the order.
 
RaptorUK:
I think the OP wants to calculate potential profit prior to placing the order.

Thanks in that case my posting was wrong so I deleted it. Good weekend Simon
Reason: