How to calculate when the order if profitable ?

 

Hey guys, I wonder if anyone already did this calculation before... I want to know when one order is gonna be profitable so I can draw a line on the screen.


The following code calculates the exact profit of one order...


OrderSelect (ticket, SELECT_BY_TICKET, MODE_TRADES);

profit = OrderProfit () + OrderSwap () + OrderCommission ();


But how can I calculate the exact price one opened order will be profitable (by profitable I mean profit > 0) ? Do I need to know the leverage? I searched for the OrderProfit ( ) source code, but couldn't find it!


Thanks!

 

I'm not sure if this is what you mean, but if you want to find the price where you would stand to gain x amount of pips, you can solve the equation you would use to calculate how many pips you gained.

I haven't worked with commissions before, but if your broker uses spreads, then you'd have something like:

//going short
pip_gain = sell_price - (close_price + spread)
close_price = sell_price - pip_gain - spread //solved

//going long
pip_gain = close_price - (buy_price + spread)
close_price = pip_gain + buy_price + spread //solved

Also, assume that buy_price and sell_price are the price that's shown on the MT4 charts (i..e, the Bid only). That's why the spread adjustment is there.

Whatever you're looking for, I don't think you need the leverage. Leverage shouldn't affect profitability, just how the profit scales. Negative pips is a loss whether you have 100:1 leverage or 1000000:1 leverage. Or so I've been told.

 
marcoblbr:


Hey guys, I wonder if anyone already did this calculation before... I want to know when one order is gonna be profitable so I can draw a line on the screen.


The following code calculates the exact profit of one order...



But how can I calculate the exact price one opened order will be profitable (by profitable I mean profit > 0) ? Do I need to know the leverage? I searched for the OrderProfit ( ) source code, but couldn't find it!


Thanks

Is this what you were looking for? https://docs.mql4.com/trading/OrderProfit

Again, not sure exactly what you mean but if it is where will an order that is currently in loss reach profit, try replacing OrderProfit() with https://docs.mql4.com/trading/OrderOpenPrice

V

 

Thanks for the help! I was able to calculate the approximate price, but not the exact value... Also, I can't use OrderProfit ( ) in this case since I don't want to know the profit for the current price! I think that if I know the OrderOpenPrice ( ) maybe I don't need to know the spread! This function is almost working...


// calculates the minimum price when the sum of all individual profits of all opened orders will be positive

double calculate_profit_price ()
{
   double price, profit, sum_profit;
   int i;
   
   for (price = 0; ; price += 0.0010)
   {
      sum_profit = 0;
      
      for (i = 0; i < number_orders_have; i++)  // suppose you know number_orders_have 
      {
         OrderSelect (ticket [i], SELECT_BY_TICKET, MODE_TRADES);
            
         profit = (price - OrderOpenPrice ()) * 100000 * OrderLots (); 

         if (OrderType () == OPSELL)
            profit = -profit;

         sum_profit += profit;
      }

      if (sum_profit > 0)
         return (price);
   }   
}


I know that what I'm doing is calculating the price when the number of pips is positive!!! This value is almost the correct price. I tested it! But still it's not the correct price since I need to add OrderSwap ( ) + OrderCommission ( ) for each order!!


Any hints on how to update this function to calculate the profit in dollars instead of pips? Or how OrderProfit ( ) calculates the profit ? I need to do that so I can add OrderSwap ( ) + OrderCommission ( ). I wonder if the calculation is the same or depends on USD being the base currency or not (EURUSD, USDCHF) ?

 
  1. profit = (price - OrderOpenPrice ()) * 100000 * OrderLots (); 
    

    On IBFX mini account one lot is 10K not 100, tickValue EURUSD is $1 not $10. Don't put constants in your code!


  2. if (!OrderSelect (ticket, SELECT_BY_TICKET, MODE_TRADES)) ...;
    profit         = OrderProfit() + OrderSwap() + OrderCommission();
    changePerPoint = OrderLots() * MarketInfo( Symbol(), MODE_TICKVALUE );
    if (OrderType() == OP_BUY) { int DIR=1; } else DIR=-1;
    breakEven      = OrderClosePrice() -DIR* profit / changePerPoint * point;

 

Great WHRoeder, thanks a lot!! It worked! I'm using this code now...


profit = (price - OrderOpenPrice ()) * OrderLots () * MarketInfo (Symbol (), MODE_TICKVALUE) / Point;

if (OrderType () == OPSELL)
   profit = -profit;

profit += OrderSwap () + OrderCommission ();
    
Reason: