PIP price per position

 

Hi,

I'm trying to calculate one PIP price for closed position, and then calculate position profit, but results I'm getting always differ from OrderProfit(). Please help me correct my code.
By PIP I always mean 1/10000 LOT price (even if there is 5 digits precision), I'm testing it in Strategy Tester on GBPUSD, account currency is set to GBP (to avoid currency rates) and I set spread for 20 (which is 2 PIP for my code):

double p = 10 * Point(); // for 5 digits precision
double lot_price = AccountLeverage() * MarketInfo(Symbol(), MODE_MARGINREQUIRED);
double pip_price = lot_price * OrderLots() * p;
double spread = 2 * p;

int dir = 0;
switch(OrderType()) {
    case OP_BUY: case OP_BUYLIMIT: case OP_BUYSTOP: dir = 1; break;
    case OP_SELL: case OP_SELLLIMIT: case OP_SELLSTOP: dir = -1; break;
}

double res = (dir * (OrderClosePrice() - OrderOpenPrice()) - spread) / p;

string str = StringFormat("%6.2lf x %4.2lf = %6.2lf (%6.2lf)\n", res, pip_price, res * pip_price, OrderProfit());
Print(str);

Exmaple output (OrderLots() == 0.02):

 50.00 x 0.20 =  10.00 (  5.74)
 29.00 x 0.20 =   5.80 (  3.42)
 18.00 x 0.20 =   3.60 (  2.21)
  9.00 x 0.20 =   1.80 (  1.21)
-38.00 x 0.20 =  -7.60 ( -3.97)
-39.00 x 0.20 =  -7.80 ( -4.08) 

 

Regards 

 
veedoo:I'm trying to calculate one PIP price for closed position,

By PIP I always mean 1/10000 LOT price (even if there is 5 digits precision), I

There is no such thing as PIP price or LOT price. There is only a change in price, lots size, and value of the currency per point. The latter depends on the pair and the account currency's exchange rates.
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
Reason: