Robot Backtesting doesnt work for all pairs!

Created at 17 May 2024, 09:44
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!
DR

DR.SHADI_JARRAR

Joined 02.05.2024

Robot Backtesting doesnt work for all pairs!
17 May 2024, 09:44


Hello,

 

I have build a robot that works as intended in backtesting for indecies only(euro50,Germany40,Japan225,US30…) … but backtesting doesnt work when trying the robot in other forex and commodity pairs! .. Does anybody know why?

Thanks


@DR.SHADI_JARRAR
Replies

PanagiotisCharalampous
17 May 2024, 10:41

Hi there,

Can you please provide more information about the issue? What do you mean when you say it doesn't work? Can you share the source code as well as steps to reproduce?

Best regards,

Panagiotis


@PanagiotisCharalampous

DR.SHADI_JARRAR
17 May 2024, 21:30

RE: Robot Backtesting doesnt work for all pairs!

PanagiotisCharalampous said: 

Hi there,

Can you please provide more information about the issue? What do you mean when you say it doesn't work? Can you share the source code as well as steps to reproduce?

Best regards,

Panagiotis

simply the cbot doesnt excute any positions in all pairs except in indices. here is the result of backtesting when used with Japan225 for example:

 

On the other hand, for usdjpy backtesting this is the result: 

 

 

 

Here is the code of the cbot: 

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Linq;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class YarabRobot : Robot
    {
                // تحديد الثوابت والمراجع
        private Yarabindicator _Yarabindicator;
        private bool tradeExecutedThisHour = false;
        private double initialLotSize = 0.01;
        private double currentLotSize = 0.01; 
        private bool previousPositionInLoss = false; 
        private bool IsNewHour()
        {
            return Server.Time.Minute == 0 && Server.Time.Second == 0;
        }

                // استدعاء المؤشر
        protected override void OnStart()
        {
            // Initialize the dLagema indicator
            _Yarabindicator = Indicators.GetIndicator<Yarabindicator>(3, 100, 60,1, 7,17);
        }

                // طريقة البار لفتح الصفقات

                 protected override void OnBar()
        {
                    int index = MarketSeries.Close.Count - 1;
                    
            // السماح بفتح الصفقات لمرة واحدة فقط بمجرد بداية الساعة                 
                if (IsNewHour())
                tradeExecutedThisHour = false;
            
            // اشتري بهذه الشروط واعلن اتمام فتح صفقة
            if ((Bars.ClosePrices[index-1] > _Yarabindicator.DLagEMAs[index-1]&&Bars.ClosePrices[index-2] < _Yarabindicator.DLagEMAs[index-2]) )
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, currentLotSize, "Open BUY Position", null,null,null, "Buy");
                tradeExecutedThisHour = true;
            }
            // بيع بهذه الشروط واعلن اتمام فتح صفقة
            else if ((Bars.ClosePrices[index-1] < _Yarabindicator.DLagEMAs[index-1]&&Bars.ClosePrices[index-2] > _Yarabindicator.DLagEMAs[index-2]))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, currentLotSize, "Open SELL Position",null, null,null, "Sell");
                tradeExecutedThisHour = true;  
                
            }
         }    
         
         
                // طريقة التيك لاغلاق الصفقات
        protected override void OnTick()
        {
                    int index = Bars.ClosePrices.Count - 1;


            // أغلق صفقة الشراء باحدى الحالتين التاليتين
            if (Bars.HighPrices[index ] > _Yarabindicator.sr[index ]||Bars.LowPrices[index ] < _Yarabindicator.DLagEMAs2[index ] )
            {
                var buyPositions = Positions.Where(p => p.TradeType == TradeType.Buy).ToList();
                foreach (var position in buyPositions)
                    
                    if (position.GrossProfit < 0)
                    {
                    currentLotSize=currentLotSize*1;
                    ClosePosition(position);
                    }
                    else if (position.GrossProfit > 0)
                    {
                    currentLotSize=initialLotSize;
                    ClosePosition(position);
                    }
            }

            // أغلق صفقة الشراء باحدى الحالتين التاليتين
            if (Bars.LowPrices[index ] < _Yarabindicator.sr[index ]||Bars.HighPrices[index ] > _Yarabindicator.DLagEMAs2[index ])
            {
                var sellPositions = Positions.Where(p => p.TradeType == TradeType.Sell).ToList();
                foreach (var position in sellPositions)
 
                    if (position.GrossProfit < 0)
                    {
                    currentLotSize=currentLotSize*1;
                    ClosePosition(position);
                    }
                    else if (position.GrossProfit > 0)
                    {
                    currentLotSize=initialLotSize;
                    ClosePosition(position);
                    }
            }     
        }
       }
      }
 


@DR.SHADI_JARRAR

Elon.Musk
17 May 2024, 21:48

RE: Robot Backtesting doesnt work for all pairs!

PanagiotisCharalampous said: 

Hi there,

Can you please provide more information about the issue? What do you mean when you say it doesn't work? Can you share the source code as well as steps to reproduce?

Best regards,

Panagiotis

Guys i got reason of Slowness … rebuild your Algo based on .net 6.0 and Enjoy :-) 


@Elon.Musk

PanagiotisCharalampous
18 May 2024, 06:44

RE: RE: Robot Backtesting doesnt work for all pairs!

DR.SHADI_JARRAR said: 

PanagiotisCharalampous said: 

Hi there,

Can you please provide more information about the issue? What do you mean when you say it doesn't work? Can you share the source code as well as steps to reproduce?

Best regards,

Panagiotis

simply the cbot doesnt excute any positions in all pairs except in indices. here is the result of backtesting when used with Japan225 for example:

 

On the other hand, for usdjpy backtesting this is the result: 

 

 

 

Here is the code of the cbot: 

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Linq;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class YarabRobot : Robot
    {
                // تحديد الثوابت والمراجع
        private Yarabindicator _Yarabindicator;
        private bool tradeExecutedThisHour = false;
        private double initialLotSize = 0.01;
        private double currentLotSize = 0.01; 
        private bool previousPositionInLoss = false; 
        private bool IsNewHour()
        {
            return Server.Time.Minute == 0 && Server.Time.Second == 0;
        }

                // استدعاء المؤشر
        protected override void OnStart()
        {
            // Initialize the dLagema indicator
            _Yarabindicator = Indicators.GetIndicator<Yarabindicator>(3, 100, 60,1, 7,17);
        }

                // طريقة البار لفتح الصفقات

                 protected override void OnBar()
        {
                    int index = MarketSeries.Close.Count - 1;
                    
            // السماح بفتح الصفقات لمرة واحدة فقط بمجرد بداية الساعة                 
                if (IsNewHour())
                tradeExecutedThisHour = false;
            
            // اشتري بهذه الشروط واعلن اتمام فتح صفقة
            if ((Bars.ClosePrices[index-1] > _Yarabindicator.DLagEMAs[index-1]&&Bars.ClosePrices[index-2] < _Yarabindicator.DLagEMAs[index-2]) )
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, currentLotSize, "Open BUY Position", null,null,null, "Buy");
                tradeExecutedThisHour = true;
            }
            // بيع بهذه الشروط واعلن اتمام فتح صفقة
            else if ((Bars.ClosePrices[index-1] < _Yarabindicator.DLagEMAs[index-1]&&Bars.ClosePrices[index-2] > _Yarabindicator.DLagEMAs[index-2]))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, currentLotSize, "Open SELL Position",null, null,null, "Sell");
                tradeExecutedThisHour = true;  
                
            }
         }    
         
         
                // طريقة التيك لاغلاق الصفقات
        protected override void OnTick()
        {
                    int index = Bars.ClosePrices.Count - 1;


            // أغلق صفقة الشراء باحدى الحالتين التاليتين
            if (Bars.HighPrices[index ] > _Yarabindicator.sr[index ]||Bars.LowPrices[index ] < _Yarabindicator.DLagEMAs2[index ] )
            {
                var buyPositions = Positions.Where(p => p.TradeType == TradeType.Buy).ToList();
                foreach (var position in buyPositions)
                    
                    if (position.GrossProfit < 0)
                    {
                    currentLotSize=currentLotSize*1;
                    ClosePosition(position);
                    }
                    else if (position.GrossProfit > 0)
                    {
                    currentLotSize=initialLotSize;
                    ClosePosition(position);
                    }
            }

            // أغلق صفقة الشراء باحدى الحالتين التاليتين
            if (Bars.LowPrices[index ] < _Yarabindicator.sr[index ]||Bars.HighPrices[index ] > _Yarabindicator.DLagEMAs2[index ])
            {
                var sellPositions = Positions.Where(p => p.TradeType == TradeType.Sell).ToList();
                foreach (var position in sellPositions)
 
                    if (position.GrossProfit < 0)
                    {
                    currentLotSize=currentLotSize*1;
                    ClosePosition(position);
                    }
                    else if (position.GrossProfit > 0)
                    {
                    currentLotSize=initialLotSize;
                    ClosePosition(position);
                    }
            }     
        }
       }
      }
 

You seem to use lots instead of units as volume. That's probably the reason.


@PanagiotisCharalampous