Does OrderProfit() take into account the spread?

 

Hi there,

I'll go straigth to the point. I have a partial closing function which closes a given percentage of all trades when a certain relationship between profits and initial risk is met.

The thing is, it is closing many more short positions than long positions -Around 15% difference-. I wonder if anyone knows why.

The only thing that comes to my mind is an OrderProfit() issue I am not aware of. Here is the code:

void PartialClose()
{  
   // Lotstep
   double l_lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
   int vp = 0; if(l_lotstep == 0.01) vp = 2; else vp = 1;
   
   // Iterate all trades
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); int l_type = OrderType();
      if((l_type == OP_BUY || l_type == OP_SELL) && OrderMagicNumber() >= MagicNumber && OrderSymbol() == Symbol())
      {
         // Pips gained for now
         double PipProfit, PipStopLoss;    // This is actually not calculated in pips, but plain currency
         
         // Calculate pips for stoploss
         if(l_type == OP_BUY)
         {
            // If this trade is losing or free
            if(Bid < OrderOpenPrice()) continue;
            
            // Profit and so forth
            PipProfit = Bid - OrderOpenPrice();
            PipStopLoss = (OrderOpenPrice() - LastOrderStopLoss[l_type]) / StopDivisor;
            
         } else if(l_type == OP_SELL) {
         
            // If this trade is losing or free
            if(Ask > OrderOpenPrice()) continue;
         
            // Profit and so forth
            PipProfit = OrderOpenPrice() - Ask;
            PipStopLoss = (LastOrderStopLoss[l_type] - OrderOpenPrice()) / StopDivisor;
         }
         
         // Read comment from trade
         string Com = OrderComment();
         double LOTS = OrderLots();
       
         // Partial close
         if(PartialClose_Enabled &&
            PipProfit > PipStopLoss && 
            StringFind(Com, "from #", 0) == -1 && 
            LOTS > MarketInfo(Symbol(), MODE_MINLOT))
         {
            // Close
            double halflots = NormalizeDouble(LOTS * PercentageToClose, vp);
            
            // Close half position
            if(halflots >= MarketInfo(Symbol(), MODE_MINLOT))
               if(!OrderClose(OrderTicket(), halflots, OrderClosePrice(), 6, Gold))
                  Print(ShortName +" (OrderModify Error) "+ ErrorDescription(GetLastError()));
         }
      }
   }
}

Is something wrong in my profit/risk calculation using OrderProfit()?

Many thanks in advance. Cheers.

 
flaab:

Is something wrong in my profit/risk calculation using OrderProfit()?

No idea, you don't show any code containing OrderProfit()
 
RaptorUK:
No idea, you don't show any code containing OrderProfit()

OMG is true that was the old version!!! I am a mess xDD I know what it is, OrderOpenPrice() does not take spread into account!!!
 

For a Buy you open at the Ask for a Sell you open at the Bid.

OrderOpenPrice is the price you opened at. Spread has nothing to do with it.

If you want to know the Bid you opened at, you must subtract the spread from buy orders.

It is your stop loss that must be adjusted for the spread for sell orders when you're basing it off the chart (Mt4 charts are Bid charts.)

 
WHRoeder:

For a Buy you open at the Ask for a Sell you open at the Bid.

OrderOpenPrice is the price you opened at. Spread has nothing to do with it.

If you want to know the Bid you opened at, you must subtract the spread from buy orders.

It is your stop loss that must be adjusted for the spread for sell orders when you're basing it off the chart (Mt4 charts are Bid charts.)

Thanks a lot, I have succesfully solved it with this tip.
Reason: