data isn't read correctly?

Created at 23 Apr 2024, 22:18
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!
NA

nafewhossain03

Joined 14.08.2023

data isn't read correctly?
23 Apr 2024, 22:18


if (LastResult.Position.GrossProfit > 0 && LastResult.Position.NetProfit > 0)
                {
                    _lastTradeType = LastResult.Position.TradeType;
                    _lastVolume = 1000;
                    _doubleCount = 0;
                }
                else if ( LastResult.Position.GrossProfit < 0 && LastResult.Position.NetProfit < 0)
                {
                    VolAmount();
                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                }

the logic is that it should open a trade in the opposite direction if the profit is negative, but sometimes it executes the “if (LastResult.Position.GrossProfit > 0 && LastResult.Position.NetProfit > 0)” part even if the last trade was a losing trade. i'm using tick data on the backtest section


@nafewhossain03
Replies

PanagiotisCharalampous
24 Apr 2024, 05:14

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis


@PanagiotisCharalampous

nafewhossain03
24 Apr 2024, 16:25

RE: data isn't read correctly?
PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;
using System.Threading;

namespace cAlgo
{
    [Robot(AccessRights = AccessRights.None)]
    public class SinglePositionBot : Robot
    {
        private double StopLossPips = 10;
        private double TakeProfitPips = 20;

        private TradeType _lastTradeType;
        private double _lastVolume;
        private int _doubleCount;

        protected override void OnStart()
        {
            if (SymbolName == "EURUSD")
            {
                StopLossPips = 10;
                TakeProfitPips = 20;
            }
            else if (SymbolName == "GBPUSD")
            {
                StopLossPips = 13.5;
                TakeProfitPips = 27;
            }
            else if (SymbolName == "USDJPY")
            {
                StopLossPips = 11;
                TakeProfitPips = 22;
            }
            else if (SymbolName == "USDCHF")
            {
                StopLossPips = 15;
                TakeProfitPips = 30;
            }
            
            
            _lastTradeType = TradeType.Buy;
            _lastVolume = 1000;
            
            Positions.Opened += OnPositionOpened;
           
            ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);
            
        }
        protected override void OnTick()
        {
            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)
            {
                if (LastResult.Position.GrossProfit > 0)
                {
                    _lastVolume = 1000;
                    _lastTradeType = LastResult.Position.TradeType;
                    _doubleCount = 0;
                }
                else if ( LastResult.Position.GrossProfit < 0)
                {
                    VolAmount();
                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                }
                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);
                
            }
        }
        
        
        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)
            {
                ClosePosition(LastResult.Position);
            }
        }
        
        private double VolAmount()
        {
            _lastVolume = LastResult.Position.VolumeInUnits * 2;
            _doubleCount++;

            if (_doubleCount >= 12)
            {
                _lastVolume = 1000;
                _doubleCount = 0;
            }
            
            return _lastVolume;
        }
        
    }
}

@nafewhossain03

PanagiotisCharalampous
25 Apr 2024, 06:00

RE: RE: data isn't read correctly?

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis


@PanagiotisCharalampous

nafewhossain03
25 Apr 2024, 16:40 ( Updated at: 25 Apr 2024, 17:06 )

RE: RE: RE: data isn't read correctly?

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

the symbols are eurusd, gbpusd starting from 7/11/2022 utc+2 5min timeframe.
on eurousd the 161st trade

on gbpusd the 3247th and 3311th trade

 


@nafewhossain03

PanagiotisCharalampous
26 Apr 2024, 06:16

RE: RE: RE: RE: data isn't read correctly?

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

the symbols are eurusd, gbpusd starting from 7/11/2022 utc+2 5min timeframe.
on eurousd the 161st trade

on gbpusd the 3247th and 3311th trade

 

Trade 161 looks correct to me


@PanagiotisCharalampous

nafewhossain03
27 Apr 2024, 21:19 ( Updated at: 27 Apr 2024, 21:27 )

RE: RE: RE: RE: RE: data isn't read correctly?

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

the symbols are eurusd, gbpusd starting from 7/11/2022 utc+2 5min timeframe.
on eurousd the 161st trade

on gbpusd the 3247th and 3311th trade

 

Trade 161 looks correct to me

mine is like this

could it be that you have different data? i'm using fusion market as broker, 22.5 per million as commission or 2.25 per lot, 4.5 roundturn


@nafewhossain03

PanagiotisCharalampous
28 Apr 2024, 15:33

RE: RE: RE: RE: RE: RE: data isn't read correctly?

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

the symbols are eurusd, gbpusd starting from 7/11/2022 utc+2 5min timeframe.
on eurousd the 161st trade

on gbpusd the 3247th and 3311th trade

 

Trade 161 looks correct to me

mine is like this

could it be that you have different data? i'm using fusion market as broker, 22.5 per million as commission or 2.25 per lot, 4.5 roundturn

Hi there,

The problem in your logic is here

                if (LastResult.Position.GrossProfit > 0)
                {
                    _lastVolume = 1000;
                    _lastTradeType = LastResult.Position.TradeType;
                    _doubleCount = 0;
                }
                else if ( LastResult.Position.GrossProfit < 0)
                {
                    VolAmount();
                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                }

You are determining the size of the next trade based on an open position's profit, which changes on each tick. You should check historical trades using the History collection instead.

Best regards,

Panagiotis


@PanagiotisCharalampous

nafewhossain03
28 Apr 2024, 22:48

RE: RE: RE: RE: RE: RE: RE: data isn't read correctly?

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

nafewhossain03 said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code and information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

using cAlgo.API;using System.Threading;namespace cAlgo{    [Robot(AccessRights = AccessRights.None)]    public class SinglePositionBot : Robot    {        private double StopLossPips = 10;        private double TakeProfitPips = 20;        private TradeType _lastTradeType;        private double _lastVolume;        private int _doubleCount;        protected override void OnStart()        {            if (SymbolName == "EURUSD")            {                StopLossPips = 10;                TakeProfitPips = 20;            }            else if (SymbolName == "GBPUSD")            {                StopLossPips = 13.5;                TakeProfitPips = 27;            }            else if (SymbolName == "USDJPY")            {                StopLossPips = 11;                TakeProfitPips = 22;            }            else if (SymbolName == "USDCHF")            {                StopLossPips = 15;                TakeProfitPips = 30;            }                                    _lastTradeType = TradeType.Buy;            _lastVolume = 1000;                        Positions.Opened += OnPositionOpened;                       ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                    }        protected override void OnTick()        {            if (Positions.Find("SinglePositionBot", SymbolName) == null && Symbol.Spread == 0)            {                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }                ExecuteMarketOrder(_lastTradeType, SymbolName, _lastVolume, "SinglePositionBot", StopLossPips, TakeProfitPips);                            }        }                        private void OnPositionOpened(PositionOpenedEventArgs args)        {            if (args.Position.StopLoss == null && LastResult.Position.SymbolName == SymbolName)            {                ClosePosition(LastResult.Position);            }        }                private double VolAmount()        {            _lastVolume = LastResult.Position.VolumeInUnits * 2;            _doubleCount++;            if (_doubleCount >= 12)            {                _lastVolume = 1000;                _doubleCount = 0;            }                        return _lastVolume;        }            }}

Hi there,

We still need information on how to reproduce this (Symbol, timeframe, dates) so that we can advise.

Best regards,

Panagiotis

the symbols are eurusd, gbpusd starting from 7/11/2022 utc+2 5min timeframe.
on eurousd the 161st trade

on gbpusd the 3247th and 3311th trade

 

Trade 161 looks correct to me

mine is like this

could it be that you have different data? i'm using fusion market as broker, 22.5 per million as commission or 2.25 per lot, 4.5 roundturn

Hi there,

The problem in your logic is here

                if (LastResult.Position.GrossProfit > 0)                {                    _lastVolume = 1000;                    _lastTradeType = LastResult.Position.TradeType;                    _doubleCount = 0;                }                else if ( LastResult.Position.GrossProfit < 0)                {                    VolAmount();                    _lastTradeType = LastResult.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;                }

You are determining the size of the next trade based on an open position's profit, which changes on each tick. You should check historical trades using the History collection instead.

Best regards,

Panagiotis

ok thanks, have a great day


@nafewhossain03