API Account.Equity and Account.Balance return wrong value in ctrader 4.2.15 and later update

Created at 31 Jul 2022, 19:38
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!
GO

goodday

Joined 30.03.2020

API Account.Equity and Account.Balance return wrong value in ctrader 4.2.15 and later update
31 Jul 2022, 19:38


Hi, I today just update to 4.2.15 and test the 4.3.9  found
1) Account.Equity in ctrader backtest return value of  Account.Balance instead.  And Account.Balance return wrong value.

Print("Account.Equity = " + Account.Equity);
Print("Account.Balance = " + Account.Balance);

This always return wrong in backtesting and don't know in live trading will it same or not ... Please check.

2) In backtest screen for new update version is showing account balance which is not useful as equity as before since we need to see the equity draw down during the test.





















3) The order of the events occur at the same time in the Log is sorting in wrong direction to the time sorting order.. This make confusing when we want to check the working order of the cbot.


4)  The comments section in the code editing windows in Gray color please... Green color make it hard to indicated.


Thanks.


@goodday
Replies

PanagiotisCharalampous
02 Aug 2022, 11:35

Hi goodday,

  1. Can you please provide us with a complete cBot, broker and symbol where we can reproduce this?
  2. We will fix this
  3. We will fix this
  4. We won't change this. Green comments are pretty standard.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 


@PanagiotisCharalampous

goodday
14 Aug 2022, 04:43 ( Updated at: 19 Aug 2022, 23:28 )

RE:

PanagiotisCharalampous said:

Hi goodday,

  1. Can you please provide us with a complete cBot, broker and symbol where we can reproduce this?
  2. We will fix this
  3. We will fix this
  4. We won't change this. Green comments are pretty standard.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 

1.) My Broker is Pepperstone
     ctrader version from Pepperstone now is 4.2.20
     I tested with cross platfrom downloaded from ctrader website and it has same issue.

This is the test cBot code. I backtest with XAUUSD at m1 using ticks data from broker. When backtest, you can compare the value of Account.Equity and Account.Balance in the log to the bottom info of backtest screen. it return wrong value.
this make the line...    var NowProfit = Account.Equity - StartEqty;  in CheckTargetProfit() miscalculated all the time.

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using cAlgo.API.Requests;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class TestTest : Robot
    {
        const string Bgroup = "Basic Setting";

        [Parameter("Bot Label", Group = Bgroup, DefaultValue = "Goodday")]
        public string BotLabel { get; set; }

        [Parameter("Basic Lot", Group = Bgroup, DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double BasicLot { get; set; }

        [Parameter("Pair Distance", Group = Bgroup, DefaultValue = 20.0, MinValue = 0, Step = 1)]
        public double PairDis { get; set; }

        [Parameter("Start At", Group = Bgroup, DefaultValue = 2, MinValue = 0, Step = 1)]
        public int StartAt { get; set; }

        [Parameter("Target Profit ($)", Group = Bgroup, DefaultValue = 5, MinValue = 1, Step = 0.01)]
        public double TargetPorfit { get; set; }

        private double StartEqty;
        private List<Position> DList;
        private List<int> CloseList;
        private bool Closing;
        private bool isOpen;
        private int OpenCount;
        private int PairNum;


        protected override void OnStart()
        {
            Print("ON START...");
            Closing = false;
            isOpen = false;
            OpenCount = 0;
            PairNum = 0;
            StartEqty = Account.Equity;
            Print("StartEqty = " + StartEqty);
            CloseList = new List<int>();
            DList = new List<Position>();
        }

        protected override void OnTick()
        {
            OpenTrade();
            ExcuteTrade();
            CheckTargetProfit();
        }

        protected override void OnBar()
        {

        }

        private void OpenTrade()
        {
            var Allpos = Positions.FindAll(BotLabel, SymbolName);

            if (Allpos.Count() == 0)
            {
                OpenPos();
            }
            if (Allpos.Count() > 0)
            {
                Position HiPos = Allpos.OrderByDescending(i => i.EntryPrice).FirstOrDefault();
                Position LoPos = Allpos.OrderByDescending(i => i.EntryPrice).LastOrDefault();
                if (HiPos != null || LoPos != null)
                {
                    if (Symbol.Ask > HiPos.EntryPrice + PairDis * Symbol.PipSize || Symbol.Bid < LoPos.EntryPrice - PairDis * Symbol.PipSize)
                    {
                        OpenPos();
                    }
                }
            }
        }

        private void OpenPos()
        {
            if (Closing == false && isOpen == false)
            {
                var volumeBase = Symbol.QuantityToVolumeInUnits(BasicLot);
                ++PairNum;
                var Tocom = PairNum.ToString();
                OpenCount = 0;
                isOpen = true;
                ExecuteMarketOrderAsync(TradeType.Buy, Symbol.Name, volumeBase, BotLabel, null, null, Tocom, OpenRes);
                ExecuteMarketOrderAsync(TradeType.Sell, Symbol.Name, volumeBase, BotLabel, null, null, Tocom, OpenRes);
            }
        }

        private void OpenRes(TradeResult tradeResult)
        {
            if (tradeResult.IsSuccessful)
            {
                ++OpenCount;
                if (OpenCount == 2)
                {
                    OpenCount = 0;
                    isOpen = false;
                }
            }
        }

        private void ExcuteTrade()
        {
            if (Closing == false)
            {
                var Allpos = Positions.FindAll(BotLabel, SymbolName);
                Position positionBL = Allpos.Where(p => !DList.Contains(p)).OrderBy(i => i.NetProfit).FirstOrDefault();
                if (positionBL != null && -positionBL.Pips >= PairDis * StartAt)
                {
                    var Toclose = Allpos.Where(p => p.Comment == positionBL.Comment && p.TradeType != positionBL.TradeType).FirstOrDefault();
                    if (Toclose != null)
                    {
                        var tradeResult = ClosePosition(Toclose);
                        if (tradeResult.IsSuccessful)
                        {
                            DList.Add(positionBL);
                        }
                    }
                }
            }
        }

        private void CheckTargetProfit()
        {
            if (Closing == false && DList.Count() != 0)
            {
                Print("Account.Equity = " + Account.Equity);
                Print("Account.Balance = " + Account.Balance);

                //*** The problem is here because Account.Equity return wrong value

                var NowProfit = Account.Equity - StartEqty;

                Print("NowProfit = " + NowProfit);

                if (NowProfit >= TargetPorfit)
                {
                    ReSetTrade();
                }
            }
        }

        private void ReSetTrade()
        {
            Closing = true;
            var Allpos = Positions.FindAll(BotLabel, SymbolName);
            foreach (var pos in Allpos)
            {
                CloseList.Add(pos.Id);
                ClosePositionAsync(pos, ReSetRes);
            }
        }

        private void ReSetRes(TradeResult tradeResult)
        {
            var position = tradeResult.Position;
            if (tradeResult.IsSuccessful)
            {
                CloseList.Remove(position.Id);
                if (CloseList.Count == 0)
                {
                    OnStart();
                }
            }
        }
    }
}




 


@goodday

goodday
14 Aug 2022, 09:38 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi goodday,

  1. Can you please provide us with a complete cBot, broker and symbol where we can reproduce this?
  2. We will fix this
  3. We will fix this
  4. We won't change this. Green comments are pretty standard.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 

And I found this today ... as you can see the Unr. Net Profit is wrong volue too. You can see in the test there are 4 positions left
and the sum of all positions net profit is -13.52$ but showing 10.37$ 


@goodday

goodday
14 Aug 2022, 10:22 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

And Hi PanagiotisCharalampous

I also test to add this line to check the value of Account.UnrealizedNetProfit
                Print("Account.UnrealizedNetProfit = " + Account.UnrealizedNetProfit );
and the result is this

I notice that Account.UnrealizedNetProfit from API still retrun the right value but in backtest info screen show the wrong value.
but Account.Equity always retrun wrong value both in API and in backtest info screen. Please check and fix. Thanks...
I also notice some wrong or abnormal display of positions on the backtesting chat that confuse the test. I will post it here after i carefuly inspect it.
Best regard.


@goodday