SWAP calculation during Backtest

cTrader Automate
2
Votes

uwescheffold since: 22 Nov 2020;

  16 Jan 2021, 13:43
SWAP calculation during Backtest

As we see a column named SWAp ind History Window of realtime trades, I would suggest the same column in Backtest History window. 

Since the information of accumulated swap is essential for backtest analysis. I checked that the backtest is calculated and taken into account of the "Gross" result column. But the "Net"  column only takes care of the commissions, I think this is realy missleading and confusing.  

lukas.ourada since: 11 Mar 2019;

  19 Jan 2021, 12:25

Hi, I needed something similar. I don't know why it can't be automated in the cTrader API, but I think the broker probably changes these rates and they are not in the historical data between Ticks. I solved temporarily by creating my own configurable parameter swapLong , swapShort and to OnTimer () write this code:

if (Time.Hour == 23 && Time.Minute == 0 && Time.Second == 0)
 {
   if (Time.DayOfWeek == DayOfWeek.Wednesday)
   {
    ZjistiObjemObchodu();

    double a = selllots / 100000 * SwapShort * PipValue* 3;
    double b = buylots / 100000 * SwapLong * PipValue* 3;
    double ab = a + b;

    SwapCelkem += ab;

    Print("Today was a three-day swap, the total is now paid " + Math.Round((SwapCelkem), 2) + Account.Currency);
                       

    }
    else if (Time.DayOfWeek == DayOfWeek.Saturday)
    {
        Print("--------------- Today is Saturday ---------------");

    }
    else if (Time.DayOfWeek == DayOfWeek.Sunday)
    {
         Print("--------------- Today is Sunday ---------------");
    }
    else
    {

    ZjistiObjemObchodu();

    double a = selllots / 100000 * SwapShort * HodnotaPipu;
    double b = buylots / 100000 * SwapLong * HodnotaPipu;
    double ab = a + b;

    SwapCelkem += ab;

    Print("Swap total is: " + Math.Round((SwapCelkem), 2) + Account.Currency);

   
    }



}


void ZjistiObjemObchodu()
        {
            buylots = 0;
            selllots = 0;

            foreach (var position in Positions)
            {
                if (position.SymbolCode == Symbol.Code)
                {
                    double lot = position.VolumeInUnits;

                    if (position.TradeType == TradeType.Buy)
                    {
                        buylots += lot;
                    }

                    if (position.TradeType == TradeType.Sell)
                    {
                        selllots += lot;
                    }
                }
            }


        }

it calculates the total swap which you set in the parameters according to your broker (its in cTrader/trade/ panel symbol details) and writes it to the console. At the same time, it is also in the SwapTotal variable, so you can use it to get the actual real account equity and balance by subtraction it.

maybe it will help

Sorry for my bad english :-)

Lukas