Backtesting indexes don´t show results

Created at 11 Feb 2024, 09: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!
OL

oliver.r.m.92

Joined 11.04.2023

Backtesting indexes don´t show results
11 Feb 2024, 09:38


Hello! When I do backtesting with a cBot on indexes it does not show any results. Does anyone know the reason?


@oliver.r.m.92
Replies

PanagiotisCharalampous
12 Feb 2024, 06:41

Hi there,

There is probably an issue with your cBot's logic. Please share your cBot code, your broker and the backtesting parameters so that we can reproduce and advise accordingly.

Best regards,

Panagiotis


@PanagiotisCharalampous

oliver.r.m.92
12 Feb 2024, 17:19 ( Updated at: 13 Feb 2024, 06:51 )

RE: Backtesting indexes don´t show results

PanagiotisCharalampous said: 

Hi there,

There is probably an issue with your cBot's logic. Please share your cBot code, your broker and the backtesting parameters so that we can reproduce and advise accordingly.

Best regards,

Panagiotis

Hi Panagiotis! The broker im using its ICMarkets, the parameters wich I tryed to test the Cbot on US500 are the shame on the code.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class MACDcBot : Robot
    {
                 
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.12, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
        
        //SL & TP
        [Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 50)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 30)]
        public int TakeProfitInPips { get; set; }
        
        //MA               
        [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSeries { get; set; }
        
        [Parameter("MA Type", Group = "Moving Average")]
        public MovingAverageType MAType { get; set; }
        
        [Parameter("MA Periods", Group = "Moving Average", DefaultValue = 200)]
        public int MAPeriods { get; set; }
        
        //MACD
        [Parameter("Period", DefaultValue = 9)]
        public int Period { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }
        
        private bool isPositionOpen;
        private MacdCrossOver _MACD;
        private MovingAverage SMA;
        private const string label = "Trade ON";
        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            // Actualizar el estado de la posición abierta
            isPositionOpen = false;                 
        }
        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
        
        protected override void OnStart()
        {
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
            SMA = Indicators.MovingAverage(SourceSeries, MAPeriods, MAType);
            isPositionOpen = false;
            Positions.Closed += OnPositionsClosed;
        }

        protected override void OnBar()
        {           
            if (IsSignalLineCrossover)
            {


                if (!isPositionOpen && _MACD.Histogram.Last(1) > (0) && _MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1) && Bars.HighPrices.Last(1) > SMA.Result.Last(2))
                {                 
                   ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label,StopLossInPips, TakeProfitInPips);
                   isPositionOpen = true;
                }
                
                if (!isPositionOpen && _MACD.Histogram.Last(1) < (0) && _MACD.MACD.Last(2) > _MACD.Signal.Last(2) && _MACD.MACD.Last(1) < _MACD.Signal.Last(1) && Bars.LowPrices.Last(1) < SMA.Result.Last(2))
                {               
                   ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
                   isPositionOpen = true;                                                            
                }
            }
            //Zero cross over
            else
            {
                if (!isPositionOpen && _MACD.MACD.Last(1) > 0 && _MACD.MACD.Last(2) < 0)
                {
                    //up
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label,StopLossInPips, TakeProfitInPips);
                    isPositionOpen = true;
                }
                else if (!isPositionOpen && _MACD.MACD.Last(1) < 0 && _MACD.MACD.Last(2) > 0)
                {
                    //Down
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label,StopLossInPips, TakeProfitInPips);  
                    isPositionOpen = true;
                }
                
            }  
        }
    }
}

 


@oliver.r.m.92

PanagiotisCharalampous
13 Feb 2024, 07:19

Hi Oliver,

Check your log and you will discover the reason. You need to normalize the volume as below

        private double VolumeInUnits
        {
            get { return Symbol.NormalizeVolumeInUnits(Symbol.QuantityToVolumeInUnits(Quantity)); }
        }

Best regards,

Panagiotis


@PanagiotisCharalampous

oliver.r.m.92
13 Feb 2024, 14:24

RE: Backtesting indexes don´t show results

PanagiotisCharalampous said: 

Hi Oliver,

Check your log and you will discover the reason. You need to normalize the volume as below

        private double VolumeInUnits        {            get { return Symbol.NormalizeVolumeInUnits(Symbol.QuantityToVolumeInUnits(Quantity)); }        }

Best regards,

Panagiotis

Perfect, thak you! That was the error, is the code you have provided me the one that should always be used, whether indexes, forex or anything that is operated? The one that was already in my code works only with forex, is it equally correct or have I been using it wrong?

 


@oliver.r.m.92

PanagiotisCharalampous
13 Feb 2024, 14:41

RE: RE: Backtesting indexes don´t show results

oliver.r.m.92 said: 

PanagiotisCharalampous said: 

Hi Oliver,

Check your log and you will discover the reason. You need to normalize the volume as below

        private double VolumeInUnits        {            get { return Symbol.NormalizeVolumeInUnits(Symbol.QuantityToVolumeInUnits(Quantity)); }        }

Best regards,

Panagiotis

Perfect, thak you! That was the error, is the code you have provided me the one that should always be used, whether indexes, forex or anything that is operated? The one that was already in my code works only with forex, is it equally correct or have I been using it wrong?

 

Hi Oliver,

You can use what I have sent for all cases.

Best regards,

Panagiotis


@PanagiotisCharalampous