How to use custome indicator in CBOt?

Created at 12 Jan 2024, 05:41
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!
LF

lfdomingo143

Joined 27.05.2021

How to use custome indicator in CBOt?
12 Jan 2024, 05:41


Please help with my code. I want to use KeltnerChannelATR instead KelnterChannel Default.
using cAlgo.API;
using cAlgo.API.Indicators;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class KeltnerChannelsSample : Robot
    {
        
        private double _volumeInUnits;
        private KeltnerChannels _keltnerChannels;
        private bool stopLossTriggered;
        private DateTime lastTradeTime;
        private int tradesExecuted;

        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }

        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { get; set; }

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

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

        [Parameter("Keltner Channel Period", DefaultValue = 20)]
        public int KcPeriod { get; set; }

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

        [Parameter("Multiplier", DefaultValue = 2.0)]
        public double Multiplier { get; set; }

        [Parameter("Moving Average Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("Start Trading Hour", DefaultValue = 0, MinValue = 0, MaxValue = 23)]
        public int StartTradingHour { get; set; }

        [Parameter("Max Trades Per Day", DefaultValue = 1, MinValue = 1)]
        public int MaxTradesPerDay { get; set; }

        public Position[] BotPositions
        {
            get { return Positions.FindAll(Label); }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _keltnerChannels = Indicators.KeltnerChannels(KcPeriod, MaType, AtrPeriod, MaType, Multiplier);
        }

        protected override void OnBar()
        {
            if (Server.Time.Day != lastTradeTime.Day)
            {
                // Reset for a new day
                lastTradeTime = Server.Time;
                stopLossTriggered = false;
                tradesExecuted = 0;
            }

            if (Server.Time.Hour < StartTradingHour)
                return; // Do not trade before the specified hour

            var upperBand = _keltnerChannels.Top.Last(1);
            var lowerBand = _keltnerChannels.Bottom.Last(1);

            if (!stopLossTriggered && tradesExecuted < MaxTradesPerDay)
            {
                if (Bars.LowPrices.Last(1) <= lowerBand)
                {
                    if (BotPositions.Length == 0)
                    {
                        ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLoss, TakeProfit);
                        tradesExecuted++;
                    }
                }
                else if (Bars.HighPrices.Last(1) >= upperBand)
                {
                    ClosePositions(TradeType.Buy);
                }
            }
            else
            {
                stopLossTriggered = false;
                ClosePositions(TradeType.Sell);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;
                ClosePosition(position);
            }
        }
    }
}


@lfdomingo143
Replies

PanagiotisCharalampous
12 Jan 2024, 07:29

Hi there, 

Please explain what exact help to you need. Nobody will do the job for you but we are happy to help you do it yourself

Best regards,

Panagiotis


@PanagiotisCharalampous

lfdomingo143
12 Jan 2024, 07:51 ( Updated at: 12 Jan 2024, 12:43 )

RE: How to use custome indicator in CBOt?

PanagiotisCharalampous said: 

Hi there, 

Please explain what exact help to you need. Nobody will do the job for you but we are happy to help you do it yourself

Best regards,

Panagiotis

Hello!

Thank you for your reply.
I need to figure out how can I use/reference my code to custom indicators specifically KeltnerChannel ATR (average true range) instead of Keltner Channels (Simple) readily available indicator.


@lfdomingo143

PanagiotisCharalampous
12 Jan 2024, 12:47

RE: RE: How to use custome indicator in CBOt?

lfdomingo143 said: 

PanagiotisCharalampous said: 

Hi there, 

Please explain what exact help to you need. Nobody will do the job for you but we are happy to help you do it yourself

Best regards,

Panagiotis

Hello!

Thank you for your reply.
I need to figure out how can I use/reference my code to custom indicators specifically KeltnerChannel ATR (average true range) instead of Keltner Channels (Simple) readily available indicator.

Hi again,

Check the video below, it should be helpful


@PanagiotisCharalampous