My bot doesn't work even successfully compiled! please help

Created at 02 May 2024, 17:15
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

My bot doesn't work even successfully compiled! please help
02 May 2024, 17:15


Hello,

I am a beginner in Calgo, but I could successfully coded an indicator and a bot that is based on it, but the bot doesn't excute any positions eventhough its successfully created. I will post both the indicator and the bot anyone may help.

 

The Indicator: 

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true)]
    public class dlagema : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Period", DefaultValue = 3)]
        public int Period { get; set; }

        [Parameter("Periodema", DefaultValue = 100)]
        public int Periodema { get; set; }

        [Parameter("tp_Period", DefaultValue = 6)]
        public int tp_Period { get; set; }

        [Parameter("tp_Periodema", DefaultValue = 10)]
        public int tp_Periodema { get; set; }

        [Parameter("Multiplier", DefaultValue = 4)]
        public int M { get; set; }

        [Parameter("Length", DefaultValue = 1000)]
        public int Length { get; set; }

        [Parameter("ema100period", DefaultValue = 1000)]
        public int ema100period { get; set; }

        [Output("dlagema", LineColor = "White", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Result { get; set; }

        [Output("tp", LineColor = "Red", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries ResultBase { get; set; }

        [Output("UpperBand", LineColor = "Blue", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries UpperBand { get; set; }

        [Output("LowerBand", LineColor = "Red", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries LowerBand { get; set; }

        [Output("ema100", LineColor = "blue", LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries sema100 { get; set; }

        private ExponentialMovingAverage ema1000;
        private ExponentialMovingAverage ema100;
        private ExponentialMovingAverage EMA1;
        private ExponentialMovingAverage EMA2;
        private ExponentialMovingAverage tpEMA1;
        private ExponentialMovingAverage tpEMA2;
        private AverageTrueRange atr;

        protected override void Initialize()
        {
            EMA1 = Indicators.ExponentialMovingAverage(Source, Period);
            EMA2 = Indicators.ExponentialMovingAverage(EMA1.Result, Periodema);

            tpEMA1 = Indicators.ExponentialMovingAverage(Source, tp_Period);
            tpEMA2 = Indicators.ExponentialMovingAverage(tpEMA1.Result, tp_Periodema);

            ema100 = Indicators.ExponentialMovingAverage(Source, Periodema);
            atr = Indicators.AverageTrueRange(1000, MovingAverageType.Exponential);

            // Initialize ema1000 with ATR instead of Source
            ema1000 = Indicators.ExponentialMovingAverage(atr.Result, 14); 
        }

        private bool ShouldCalculateBands(int index, IndicatorDataSeries dLagemaIndicator)
        {
            if ((Bars.ClosePrices[index - 1] > dLagemaIndicator[index - 1] && Bars.ClosePrices[index - 2] < dLagemaIndicator[index - 2]) ||
                (Bars.ClosePrices[index - 1] < dLagemaIndicator[index - 1] && Bars.ClosePrices[index - 2] > dLagemaIndicator[index - 2]))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override void Calculate(int index)
        {
            double em = ema100.Result[index];
            double ema1Value = EMA1.Result[index];
            double ema2Value = EMA2.Result[index];
            double tpEma1Value = tpEMA1.Result[index];
            double tpEma2Value = tpEMA2.Result[index];
            double difference = ema1Value - ema2Value;
            double tpDifference = tpEma1Value - tpEma2Value;
            double dlagemaValue = ema2Value - difference;
            double tpValue = tpEma2Value - tpDifference;
            double xatr = atr.Result[index];
            double ema1000Value = ema1000.Result[index];

            double upperBand;
            double lowerBand;

            if (ShouldCalculateBands(index, Result))
            {
                upperBand = M * ema1000Value + em;
                lowerBand = em - M * ema1000Value ;
            }
            else
            {
                // If conditions are not met, copy the previous values
                upperBand = UpperBand[index - 1];
                lowerBand = LowerBand[index - 1];
            }

            Result[index] = dlagemaValue;
            ResultBase[index] = tpValue;
            sema100[index] = em;
            UpperBand[index-1] = upperBand;
            LowerBand[index] = lowerBand;
        }
    }
}

 

__________________________________________________________________________________________________________________________________

The bot is here: 

_________________________________________________________________________________________________________________________________

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DlagemaRobot : Robot
    {
        private dlagema dLagemaIndicator;
        private double lotSize = 0.1;
        private Position currentPosition;

        protected override void OnStart()
        {
            // Initialize the dLagema indicator
            dLagemaIndicator = Indicators.GetIndicator<dlagema>(Bars.ClosePrices, 3, 100, 6, 10, 4, 1000);
        }

        protected override void OnBar()
        {
            int index = Bars.ClosePrices.Count - 1;

            // If there's no open position
            if (currentPosition == null)
            {
                // Buy condition
                if (Bars.ClosePrices[index - 1] > dLagemaIndicator.Result[index - 1] && Bars.ClosePrices[index - 2] < dLagemaIndicator.Result[index - 2])
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, lotSize, "Open BUY Position");
                }
                // Sell condition (triggered if price closes below dLagema)
                else if (Bars.ClosePrices[index - 1] < dLagemaIndicator.Result[index - 1] && Bars.ClosePrices[index - 2] > dLagemaIndicator.Result[index - 2])
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, lotSize, "Open SELL Position");
                }
            }
            // If there's an open position
            else
            {
                // Close BUY position if conditions are met
                if (currentPosition.TradeType == TradeType.Buy &&
                    (Bars.HighPrices[index] > dLagemaIndicator.UpperBand[index] ||
                     Bars.ClosePrices[index] < dLagemaIndicator.ResultBase[index] ||
                     Bars.ClosePrices[index] < dLagemaIndicator.Result[index]))
                {
                    ClosePosition();
                }
                // Close SELL position if conditions are met
                else if (currentPosition.TradeType == TradeType.Sell &&
                         (Bars.LowPrices[index] < dLagemaIndicator.LowerBand[index] ||
                          Bars.ClosePrices[index] > dLagemaIndicator.ResultBase[index] ||
                          Bars.ClosePrices[index] > dLagemaIndicator.Result[index]))
                {
                    ClosePosition();
                }
            }
        }

        public void ClosePosition()
        {

        }

        [Obsolete("Use OnPositionOpened(Position) instead.")]
        protected override void OnPositionOpened(Position openedPosition)
        {
            currentPosition = openedPosition;
        }
    }
}


@DR.SHADI_JARRAR
Replies

firemyst
03 May 2024, 20:58

Let's start simple. Does your indicator work as expected?


@firemyst