Discrepancy in Swap Calculation with cBot

Created at 15 Jun 2024, 15:48
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
G.

G.A.F

Joined 04.08.2022

Discrepancy in Swap Calculation with cBot
15 Jun 2024, 15:48


Hi everyone, I am encountering a discrepancy in the swap calculation for a cBot I have written in cTrader. Below is the code for the cBot:

 

using System; 

using cAlgo.API; 

using cAlgo.API.Collections;

 using cAlgo.API.Indicators; 

using cAlgo.API.Internals; 

namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None, AddIndicators = true)] 

public class swapmanager : Robot 

{

 [Parameter(DefaultValue = "Hello world!")] 

public string Message { get; set; } 

protected override void OnStart()

 { 

ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.LotSize);

 } 

protected override void OnBar()

 { 

foreach (var pos in Positions)

 {

 if (pos.Swap < 0)

 { 

Print("pos.swap: ", pos.Swap);

 Print("SwapLong (pips): ", Symbol.SwapLong); 

Print("Formula Swap: ", CalculateSwap(Symbol.SwapLong, Symbol.Digits, Symbol.LotSize));

 pos.Close(); 

Stop(); 

}

 }

 private double CalculateSwap(double swap, int pipPosition, double volume) 

{ return swap * Math.Pow(10, -pipPosition) * volume; } protected override void OnStop() 

{ // Handle cBot stop here } } }

 

 

 

Here is the log output:

 

06/03/2024 00:00:00.000 | Info | CBot instance [swap manager, EURUSD, h1] started. 06/03/2024 00:00:00.000 | Trade | Executing Market Order to Buy 100000 EURUSD 06/03/2024 00:00:00.000 | Trade | → Executing Market Order to Buy 100000 EURUSD SUCCEEDED, Position PID1 06/03/2024 21:00:00.000 | Info | pos.swap: -68,32 06/03/2024 21:00:00.000 | Info | SwapLong (pips): -2,445 06/03/2024 21:00:00.000 | Info | Formula Swap: -2,4450000000000003 06/03/2024 21:00:00.000 | Trade | Closing position PID1 (Volume: 100000) 06/03/2024 21:00:00.000 | Trade | → Closing position PID1 (Volume: 100000) SUCCEEDED 06/03/2024 21:00:00.000 | Info | CBot instance [swap manager, EURUSD, h1] stopped.

 

 

As you can see, the pos.swap value is -68,32, while my calculated swap using the formula swap * Math.Pow(10, -pipPosition) * volume gives -2,4450000000000003.

Am I missing something in the swap calculation? Any help to understand and resolve this discrepancy would be greatly appreciated.

Thank you!


@G.A.F
Replies

PanagiotisCharalampous
16 Jun 2024, 06:27

Hi there,

See the correct formula below

        private double CalculateSwap(double swap, double volume)
        { 
            return AssetConverter.Convert(Symbol.SwapLong * Symbol.PipSize * volume, Symbol.QuoteAsset, Account.Asset ); 
        }

Best regards,

Panagiotis


@PanagiotisCharalampous

G.A.F
16 Jun 2024, 13:17

RE: Discrepancy in Swap Calculation with cBot

PanagiotisCharalampous said: 

Hi there,

See the correct formula below

        private double CalculateSwap(double swap, double volume)        {             return AssetConverter.Convert(Symbol.SwapLong * Symbol.PipSize * volume, Symbol.QuoteAsset, Account.Asset );         }

Best regards,

Panagiotis

thanks a lot now is working Fine

 


@G.A.F