Please fix the code for this cBot

Created at 15 Jan 2023, 21:09
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!
Algorithmic_Robot's avatar

Algorithmic_Robot

Joined 28.07.2022

Please fix the code for this cBot
15 Jan 2023, 21:09


Hi, Please help me.

I've been trying to fix the code with the help of an AI online. I was not succesful.

1.The cBot when I run it in the optimization program on M1 bars from Server (open prices) performs really well. It can even make more than 1000% profit a Month. On M5, on M15, on H1,..

But when I try to optimize or backtest it on Data: tick data from server, the cBot does not make profit.

(yes, I did try to increase or put the live spread data on the M1 bars from server backtest settings)

How so that If the cBot is making such great profit on H1 backtested on M1 bars from server it won't make profit when backtested on tick data (accurate)?

2. The cBot will only open new positions on XAU/USD (Gold) / USD/MXN  on smaller timeframe than M15. It won't open any positions on smaller time frames bellow M15 on EUR/USD or other currency pairs.  Is there any way to modify the code so the cBot will open positions also on EUR/USD on smaller timeframes? Does this have anything to do with the take profit and stop loss and pending orders that the cBot uses (in pips)? (I think the problem is in the Velocity Indicator.. Because when I put the Velocity Indicator on eur/usd it is totally different than on Gold - on the same time frame)

*This is a cAlgo robot that uses Fibonacci bands and velocity indicators to determine when to open trades. The robot uses the OnBar() method to check if the velocity of the market is rising or falling, and if so, it opens a trade using the Abreposicoes() method. The robot also has a number of parameters that can be adjusted by the user, such as the volume of the trade, the period of the ATR and EMA indicators, and the period of the velocity indicator. Additionally, the robot has a number of helper methods such as Carregar_fibo() that loads the Fibonacci bands into a list and OrganizarVetores() that sorts and reverses the list.* It works with creating pending orders. It also cancels the pending order according to market condition.

(I think someone from Portugal made the cBot because of the language)

***Can someone modify the code so it will check on bars open price  only? Or however it should be modified because the cBot breaks accounts in backtest program on Tick data  from Server (accurate) and the cBot makes more than 1000% profit on M1 bars from server open prices even if I backtest it on H1 timeframe. ?!

(in the backtest program we only have M1 bars from Server - open prices ! Why? We should have close prices too. I hope we get the 'M1 bars - close price' option too.

 

*** Can someone modify the code so the cBot will also open new positions on eur/usd and other currency pairs on smaller timeframes (smaller than M15)? Now it only opens new positions on xau/usd 

 

Thank you

 

*****There can be multiple reasons why the robot performs well on one backtest setting but not on another, even when the testing instrument and time frame is the same. Some possible explanations are:

  • The robot's logic may be optimized for the specific data that is used in the "M1 bars from Server (open prices)" setting. For example, it may be using the open prices of each bar as a key input for its calculations and decisions, which would be different from the tick data.
  • The robot may be sensitive to the number of price updates that it receives and the frequency of the updates. In the "Tick Data (accurate)" setting, the robot will receive many more updates than in the "M1 bars from Server (open prices)" setting, which could lead to different results.
  • The robot may be using some other data that is not present in the "Tick Data (accurate)" setting that is important for its calculations and decisions.
  • The robot may be using technical indicators that only work well on a specific kind of data, like the robot in question is using Fibonacci Bands and Velocity indicators, these indicators may work optimally on M1 bars from Server (open prices) but not on tick data.
  • The robot may be using a fixed stop loss or take profit value, which may be optimal for the data used in the "M1 bars from Server (open prices)" setting but not for the "Tick Data (accurate)" setting.

*****It is not specified in the provided code that the robot only opens trades on Gold and not on other currency pairs or on smaller time frames. However, it could be the case that the robot's parameters are not set optimally for smaller time frames or for other currency pairs, or the robot's logic does not account for those cases. It is also possible that the robot is not properly handling the differences in volatility and market conditions for different currency pairs and time frames, which could lead to poor performance in those cases. It is also possible that the robot is running on a strategy that is only meant for gold specifically. It is also possible that the fibo bands and velocity indicators are not working optimally on smaller time frames.

 

This is a backtest  result for the 5 minute time frame on Gold made on backtest settings M1 bars from Server (open prices)

The bot will not make profit on Tick data.

 

 

THIS IS THE CODE FOR THIS cBot. PLEASE HELP ME MODIFY IT OR DO ANYTHING TO IT SO IT WILL BE PROFITABLE ON TICK DATA.  

 

 

//#reference: ..\Indicators\FibonacciBands.algo
//#reference: ..\Indicators\Velocidade.algo
// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//
// -------------------------------------------------------------------------------


using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using System.IO;
using System.Collections.Generic;

using System.Linq;
using System.Runtime.InteropServices;
using System.Globalization;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class SR_Fibo : Robot
    {
        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set; }
        [Parameter(DefaultValue = 21)]
        public int AtrPeriod { get; set; }
        [Parameter(DefaultValue = 55)]
        public int EmaPeriod { get; set; }
        [Parameter(DefaultValue = 10)]
        public int VelPeriod { get; set; }

        List<double> fibo;
        int idx;

        Velocidade tendencia;
        FibonacciBands fbands;

        double stop, gain, price;
        PendingOrder ordemc, ordemv;
        Position posicao;
        private void Seta_variaveis(TradeType oper)
        {
            Carregar_fibo();

            if (oper == TradeType.Buy)
                price =Precos(fibo, Symbol.Ask, oper);
            else
                price = Precos(fibo, Symbol.Bid, oper);
            stop = Stopcalc(oper, fibo);
            gain = Gaincalc(oper, fibo);

        }

        protected override void OnPendingOrderCreated(PendingOrder newPendingOrder)
        {
            if (newPendingOrder.TradeType == TradeType.Sell)
                ordemv = newPendingOrder;
            else
                ordemc = newPendingOrder;

        }

        protected override void OnBar()
        {
            Carregar_fibo();
            Trade.DeletePendingOrder(ordemv);
            Trade.DeletePendingOrder(ordemc);

            if (tendencia.velocidade.LastValue > 0 && Functions.IsRising(tendencia.velocidade))
                Abreposicoes(TradeType.Buy);
            if (tendencia.velocidade.LastValue < 0 && Functions.IsFalling(tendencia.velocidade))
                Abreposicoes(TradeType.Sell);



        }
        protected override void OnStart()
        {

            tendencia = Indicators.GetIndicator<Velocidade>(MarketSeries.Close, VelPeriod);
            fbands = Indicators.GetIndicator<FibonacciBands>(EmaPeriod, AtrPeriod);

            fibo = new List<double>();
            Carregar_fibo();
            OrganizarVetores();
            Abreposicoes(TradeType.Buy);
            Abreposicoes(TradeType.Sell);
            Print("OK!!!");
        }

        protected override void OnTick()
        {

        }
        private void OrganizarVetores()
        {
            fibo.Sort();
            fibo.Reverse();


        }

        private void Carregar_fibo()
        {
            fibo.Clear();
            fibo.Add(fbands.UpperBand4.LastValue);
            fibo.Add(fbands.UpperBand3.LastValue);
            fibo.Add(fbands.UpperBand2.LastValue);
            fibo.Add(fbands.UpperBand1.LastValue);
            fibo.Add(fbands.LowerBand1.LastValue);
            fibo.Add(fbands.LowerBand2.LastValue);
            fibo.Add(fbands.LowerBand3.LastValue);
            fibo.Add(fbands.LowerBand4.LastValue);
        }

        private void Abreposicoes(TradeType oper)
        {

            Seta_variaveis(oper);
            if (oper == TradeType.Buy)
                Trade.CreateBuyLimitOrder(Symbol, Volume, price, stop, gain, null);
            if (oper == TradeType.Sell)
            {
                Trade.CreateSellLimitOrder(Symbol, Volume, price, stop, gain, null);

            }
        }

        private double Stopcalc(TradeType oper, List<double> sr)
        {
            OrganizarVetores();
            if (oper == TradeType.Sell)
                sr.Sort();
            if (idx < sr.Count - 1 && Math.Abs(sr[idx] - Symbol.Bid) / Symbol.PipSize > 5)
                return sr[idx + 1];
            else if (oper == TradeType.Buy)
                return sr[idx] - 20 * Symbol.PipSize;
            else if (oper == TradeType.Sell)
                return sr[idx] + 20 * Symbol.PipSize;



            return 0;
        }

        private double Gaincalc(TradeType oper, List<double> sr)
        {
            OrganizarVetores();
            if (oper == TradeType.Sell)
                sr.Sort();

            return sr[idx - 1];
        }




        private double Precos(List<double> sr, double price, TradeType oper)
        {
            OrganizarVetores();
            if (oper == TradeType.Buy)
            {
                for (var i = 1; i < sr.Count() - 1; i++)
                {
                    if (tendencia.velocidade.LastValue > 0 && price - sr[i] > 10 * Symbol.PipSize)
                        if (sr[i] < price)
                        {
                            idx = i;
                            return sr[i];
                            break;
                        }


                    if (tendencia.velocidade.LastValue < 0 && price - sr[i] > 10 * Symbol.PipSize)
                        if (sr[i] < price)
                        {

                            idx = i + 1;
                            return sr[i + 1];
                            break;


                        }

                }

            }
            if (oper == TradeType.Sell)
            {

                sr.Sort();

                for (var i = 1; i < sr.Count() - 1; i++)
                {

                    if (tendencia.velocidade.LastValue < 0 && sr[i] - price > 10 * Symbol.PipSize)
                    {

                        if (sr[i] > price)
                        {

                            idx = i;
                            return sr[i];
                            break;
                        }
                    }
                    if (tendencia.velocidade.LastValue > 0 && sr[i] - price > 10 * Symbol.PipSize)
                        if (sr[i] > price)
                        {

                            idx = i + 1;
                            return sr[i + 1];
                            break;


                        }
                }

            }

            return 0;
        }


        protected override void OnPositionOpened(Position posicaoaberta)
        {
            posicao = posicaoaberta;
            Print("Nova posição aberta\n");

        }

        protected override void OnPositionClosed(Position posicaoaberta)
        {
            posicao = null;


        }
        protected override void OnStop()
        {
            Trade.DeletePendingOrder(ordemv);
            Trade.DeletePendingOrder(ordemc);
        }
    }
}


@Algorithmic_Robot
Replies