The cBot is not entering trades.

Created at 01 Mar 2024, 18:01
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!
TE

telefonowelolo

Joined 01.03.2024

The cBot is not entering trades.
01 Mar 2024, 18:01


Hi, I'm seeking help with my cBot. Apparently, it is not entering any trades, and I don't know why. I would also like to ask how to determine the timeframe the cBot is using or, specifically, what timeframe (the one that i am looking at?)the indicator has. Here is my cBot: 

//#reference: ..\Indicators\Indicatorbbb.algo


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;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class BOTbbb : Robot
    {
        private Indicatorbbb Meini;

        [Parameter("Volume (Units)", DefaultValue = 10000, MinValue = 0)]
        public double VolumeInUnits { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 0)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 40, MinValue = 0)]
        public int TakeProfit { get; set; }

        protected override void OnStart()
        {
            Meini = Indicators.GetIndicator<Indicatorbbb>(2, MovingAverageType.WilderSmoothing);
        }

        protected override void OnBar()
        {

            if (Meini.haOpen.Last(1) < Meini.haClose.Last(1) &&
                Meini.haOpen.Last(0) > Meini.haClose.Last(0))
            {
                // Enter a long trade
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, "Long Trade", StopLoss, TakeProfit);
            }
        }
    }
}


@telefonowelolo
Replies

PanagiotisCharalampous
02 Mar 2024, 07:30

Hi there,

The problem is here

Meini.haOpen.Last(0) > Meini.haClose.Last(0)

On bar opening the values will always be equal. Try executing the code in OnBarClosed() instead.

Regarding the timeframe, the Algo class has a Timeframe parameter, you can user it.

Best regards,

Panagiotis

 


@PanagiotisCharalampous

telefonowelolo
02 Mar 2024, 09:59 ( Updated at: 03 Mar 2024, 07:09 )

RE: The cBot is not entering trades.

PanagiotisCharalampous said: 

Hi there,

The problem is here

Meini.haOpen.Last(0) > Meini.haClose.Last(0)

On bar opening the values will always be equal. Try executing the code in OnBarClosed() instead.

Regarding the timeframe, the Algo class has a Timeframe parameter, you can user it.

Best regards,

Panagiotis

 

Hi PanagiotisCharalampous

I followed your instructions, but my bot is still not working. And I reconstructed it so it looks like this 


//#reference: ..\Indicators\John.algo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class HAK : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 1, MinValue = 1)]
        public int InitialVolume { get; set; }

        [Parameter("Initial Stop Loss", DefaultValue = 40)]
        public int SL { get; set; }

        [Parameter("Initial Take Profit", DefaultValue = 40)]
        public int TP { get; set; }

        public bool isAlreadyInTrade;
        public IndicatorDataSeries haClose { get; set; }
        public IndicatorDataSeries haOpen { get; set; }
        public John HAJ;
        string label = "HAK";

        protected override void OnStart()
        {
            Positions.Closed += OnPositionClosed;
            HAJ = Indicators.GetIndicator<John>(10, MovingAverageType.Simple);
            isAlreadyInTrade = false;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            isAlreadyInTrade = false;
        }

        protected override void OnBar()-----------------tried OnBarClosed()
        {
            int index = MarketSeries.Close.Count - 2;

            if (!isAlreadyInTrade)
            {
                Print("Current value of Kolor: " + HAJ.Kolor);
                
                if (HAJ.Kolor)
                {
                    
                    ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, SL, TP);
                    isAlreadyInTrade = true;
                }
                //else
                //{
                    
                    //ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, SL, TP);
                    //isAlreadyInTrade = true;
                //}
            }
        }
    }
}

I found out that the bot somehow thinks the kolor is always false

Indicator:

public Colors Color;
        public bool Kolor { get; set; }

Color = (haOpen[index] > haClose[index]) ? Colors.Red : Colors.LimeGreen;
                Kolor = (haClose[index] > haOpen[index]);
 


@telefonowelolo

PanagiotisCharalampous
03 Mar 2024, 07:15

RE: RE: The cBot is not entering trades.

telefonowelolo said: 

PanagiotisCharalampous said: 

Hi there,

The problem is here

Meini.haOpen.Last(0) > Meini.haClose.Last(0)

On bar opening the values will always be equal. Try executing the code in OnBarClosed() instead.

Regarding the timeframe, the Algo class has a Timeframe parameter, you can user it.

Best regards,

Panagiotis

 

Hi PanagiotisCharalampous

I followed your instructions, but my bot is still not working. And I reconstructed it so it looks like this 


//#reference: ..\Indicators\John.algo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class HAK : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 1, MinValue = 1)]
        public int InitialVolume { get; set; }

        [Parameter("Initial Stop Loss", DefaultValue = 40)]
        public int SL { get; set; }

        [Parameter("Initial Take Profit", DefaultValue = 40)]
        public int TP { get; set; }

        public bool isAlreadyInTrade;
        public IndicatorDataSeries haClose { get; set; }
        public IndicatorDataSeries haOpen { get; set; }
        public John HAJ;
        string label = "HAK";

        protected override void OnStart()
        {
            Positions.Closed += OnPositionClosed;
            HAJ = Indicators.GetIndicator<John>(10, MovingAverageType.Simple);
            isAlreadyInTrade = false;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            isAlreadyInTrade = false;
        }

        protected override void OnBar()-----------------tried OnBarClosed()
        {
            int index = MarketSeries.Close.Count - 2;

            if (!isAlreadyInTrade)
            {
                Print("Current value of Kolor: " + HAJ.Kolor);
                
                if (HAJ.Kolor)
                {
                    
                    ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, SL, TP);
                    isAlreadyInTrade = true;
                }
                //else
                //{
                    
                    //ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, SL, TP);
                    //isAlreadyInTrade = true;
                //}
            }
        }
    }
}

I found out that the bot somehow thinks the kolor is always false

Indicator:

public Colors Color;
        public bool Kolor { get; set; }

Color = (haOpen[index] > haClose[index]) ? Colors.Red : Colors.LimeGreen;
                Kolor = (haClose[index] > haOpen[index]);
 

Hi there,

I do not see my instructions being followed anywhere, neither I can help with partial code. You don't seem to use any output IndicatorDataSeries, therefore your Calculate() method is probably not evaluated.

Best regards,

Panagiotis


@PanagiotisCharalampous

... Deleted by UFO ...