How to calculate upcoming swap for SYMBOL_SWAP_MODE_INTEREST_OPEN swap mode?

 

I'm trying to code an EA which needs to asses the value of upcoming swap in order to decide if it is feasible to open a trade in the evening.

I was able to get calculations for some of the swap modes working correctly, however SYMBOL_SWAP_MODE_INTEREST_OPEN swap mode calculation fails for me.

Whatever I do, for larger swap values (SYMBOL_SWAP_LONG, SYMBOL_SWAP_SHORT of order of magnitude equal to 20-30) there is always a noticeable discrepancy with that the MT5 server produces.

Is there anyone to let me know the formula I should use to calculate upcoming swap for SYMBOL_SWAP_MODE_INTEREST_OPEN swap mode?

Your answer is much appreciated.

 
roman.k.v.2:

I was able to get calculations for some of the swap modes working correctly, however SYMBOL_SWAP_MODE_INTEREST_OPEN swap mode calculation fails for me.

//+------------------------------------------------------------------+
//| Calculate the open interst swap value per one holding day,       |
//| for a trade, in account currency.                                |
//+------------------------------------------------------------------+
double CalculateOpenInterestSwap(ENUM_ORDER_TYPE ordertype, string symbol, double volume)
  {
   double swap = 0.0;
//---
   ENUM_SYMBOL_SWAP_MODE swap_mode = (ENUM_SYMBOL_SWAP_MODE)SymbolInfoInteger(symbol, SYMBOL_SWAP_MODE);

   // as annual interest, using the current or open price
   if(swap_mode == SYMBOL_SWAP_MODE_INTEREST_OPEN)
     {
      // Calculates the trade's notional value, in account currency.
      const double TickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
      const double TickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
      const double Price = SymbolInfoDouble(symbol, SYMBOL_ASK);
      const double TradeNotionalValue = volume * Price * TickValue / TickSize;

      if(ordertype == ORDER_TYPE_BUY)
         swap = TradeNotionalValue * SymbolInfoDouble(symbol, SYMBOL_SWAP_LONG) / 100 / 360;
      if(ordertype == ORDER_TYPE_SELL)
         swap = TradeNotionalValue * SymbolInfoDouble(symbol, SYMBOL_SWAP_SHORT) / 100 / 360;
     }
//---
   return (swap);
  }


// usage:
double swap_long = CalculateOpenInterestSwap(ORDER_TYPE_BUY, Symbol(), volume) * num_days;
double swap_short = CalculateOpenInterestSwap(ORDER_TYPE_SELL, Symbol ,volume) * num_days;

Please check the full code at https://www.mql5.com/en/code/28029

Forex Calculators
Forex Calculators
  • www.mql5.com
Margin Calculator, Point Value Calculator, Position Size Calculator, Profit Calculator and Swap Calculator.