how to calculate breakeven price of open positions accounting for swaps

 

Would anyone know how to incorporate into a breakeven price calculation the swaps of the given positions?

I am working on the function below, which calculate the BE price of positions of a given pair in a given direction, but I have not figure out a simple way to add the losses from swaps into that price..


double BreakEvenPrice(string PairName, string PositionType)
  {
   double sum_mult    =0.0;
   double sum_volumes =0.0;

   //--- Check positions of the desire pair and type
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
     //bypass positions that are not of the given pair and type.
      if(PairName!=PositionGetSymbol(i) && (PositionType == "Longs" && PositionGetInteger(POSITION_TYPE)!=POSITION_TYPE_BUY))
         continue;
      //--- for desire position get the price and position volume
      double pos_price  =PositionGetDouble(POSITION_PRICE_OPEN);
      double pos_volume =PositionGetDouble(POSITION_VOLUME);
      
      //--- Sum up values
      sum_mult+=(pos_price*pos_volume);
      sum_volumes+=pos_volume;
     }
     
//--- Prevent division by zero
   if(sum_volumes==0)
      return(0.0);
//--- Return the breakeven price
   return(sum_mult/sum_volumes);
  }
 

I figure out an solution, which may not be the best but I think in principle should work.... Can any expert in the group confirm if this is right?

The idea is to calculate the breakeven price as a simple weighted average of price by volume. Concurrently, then we sum up the costs of swaps for all entries.

Once the cumulative swap is calculated, such a cost is converted to ticks, which are added to the breakeven price in the case of longs, or subtracted in the case of shorts.

Basically, given the volume of the entries, how much the price would have to move to cover the costs of swaps...such as an additional price movement is added to the Breakeven price... code below...


double BreakEvenPrice(string PairName, string PositionType)
  {
  
  double Capital=0;                // Hold variable of the entry price by the volume purchased
  double Volume =0;                // Volume
  double Swap=0;                   // hold the swap cost, which is in the account currency
        
   for (int i = 0; i < PositionsTotal(); i++)
      {
       if (PositionGetSymbol(i) ==PairName  && (PositionType=="Longs" && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
          {
             Capital += PositionGetDouble(POSITION_PRICE_OPEN)*PositionGetDouble(POSITION_VOLUME);
             Volume  += PositionGetDouble(POSITION_VOLUME);
             Swap    += PositionGetDouble(POSITION_SWAP);
          }

       }
       
     //--- Prevent division by zero
     if(Volume==0) {return(0.0);};
      
     //--- Breakeven price without swap costs
     double BE_Price= Capital/Volume;
     
     //--- accounting for swaps: amount price needs to correct to recover costs from swaps
     long nDigits=SymbolInfoInteger(PairName,SYMBOL_DIGITS);
     int PipAdjust = 0; if(nDigits==5 || nDigits==3) {PipAdjust=10;}; if(nDigits==4 || nDigits==2) {PipAdjust=1;}

     double TickValue = SymbolInfoDouble(PairName,SYMBOL_TRADE_TICK_VALUE) * PipAdjust;
     double TickSize  = SymbolInfoDouble(PairName,SYMBOL_TRADE_TICK_SIZE)  * PipAdjust;
     double CostExtension = Swap/(Volume*TickValue)*TickSize; 
     
     BE_Price = PositionType=="Longs" ? BE_Price + CostExtension : BE_Price - CostExtension;
     
     return(BE_Price);
  }
  
 

Hi

The idea with swap divided by volume seems to be correct.

I would only include in the calculations (for both BE and those added costs) the pip value – since the profit depends on the value of pip for the symbol and account currency.

Best Regards

 
I thought the pip value is already included in the calculation with the tickvalue, and tick size, no?
Reason: