MQL4 calculate potential profit at TakeProfit level?

 

Hi,

I'm relatively new to MQL4 and my apologies I cannot find any examples of this or understand how to do it myself.

If I have an Order at a specified OrderTakeProfit and OrderOpenPrice, how do I compute the expected Profit if it closes at the price (assuming the current Swap and Commission, the former of which might change). 

Currently I am using this ,but I am not sure if it is correct?


double PointToUSD (string symbol) {
   return MarketInfo (symbol, MODE_POINT) * (MarketInfo (symbol, MODE_TICKVALUE) / MarketInfo (symbol, MODE_TICKSIZE));
}
...
profitTP += (pointToUSD * (OrderOpenPrice () - OrderTakeProfit ())) + OrderSwap () + OrderCommission ();

UPDATE: maybe

double OrderProfitAtTP () {
   double pl = (OrderType () == OP_BUY) ? (OrderTakeProfit () - OrderOpenPrice ()) : (OrderOpenPrice () - OrderTakeProfit ()); 
   return (pl * (MarketInfo (OrderSymbol (), MODE_TICKVALUE) / MarketInfo (OrderSymbol (), MODE_TICKSIZE)) * 
               OrderLots ()) 
            + OrderCommission() + OrderSwap();
}            

Thanks!

 
  1. The function doesn't return point to USD. Point is a distance, USD is currency. It returns the account currency value of a point. Needs to be renamed.
  2. OOP-OTP works only for sell orders.
  3. Multiplying by point is wrong since you aren't using points in OOP-OTP.

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. 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.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

  4. Total Profit (at close) is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

 

I am using this:

double determinePotentialTradeProfit(ENUM_ORDER_TYPE orderType, double orderOpenPrice, double orderTakeProfitPrice, double orderLots) {
   double   tradeTickValuePerLot    = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE);  //Loss/Gain for a 1 tick move with 1 lot
   double   tickValueBasedOnLots    = tradeTickValuePerLot * orderLots;
   double   priceDifference         = MathAbs(orderOpenPrice - orderTakeProfitPrice);
   int      pointsDifference        = priceDifference / Point;
   double   potentialProfit         = tickValueBasedOnLots * pointsDifference;
   
   if (orderType==OP_BUY) {
      potentialProfit         = orderTakeProfitPrice > orderOpenPrice ? potentialProfit : -potentialProfit;
   } else if (orderType==OP_SELL) {
      potentialProfit         = orderTakeProfitPrice > orderOpenPrice ? -potentialProfit : potentialProfit;
   }
   
   return NormalizeDouble(potentialProfit, 2);

}


gfmc:

Hi,

I'm relatively new to MQL4 and my apologies I cannot find any examples of this or understand how to do it myself.

If I have an Order at a specified OrderTakeProfit and OrderOpenPrice, how do I compute the expected Profit if it closes at the price (assuming the current Swap and Commission, the former of which might change). 

Currently I am using this ,but I am not sure if it is correct?


UPDATE: maybe

Thanks!