Problmes with referring custom indicator's to cBot causing none executed trade in Backtesting

Created at 27 Aug 2023, 04:49
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!
JXTA's avatar

JXTA

Joined 23.08.2023

Problmes with referring custom indicator's to cBot causing none executed trade in Backtesting
27 Aug 2023, 04:49


Hi guys, I am having problems by using(referring) my custom indicator's calculation result in(to) my cBot, I can't execute any trade in "Backtesting" but there are no errors showing up every time I build the code, I have checked lots of things I dont think is the cBot's error because when I use the built in indicator(RSI) it execute order and selling orders in Backtesting perfectly fine, so I think it's probably my calculation's format is doing wrongly?

I am a newbie on csharp, I will provide my indicator code and cBot code below for you guys to check, please help me out 🤝

This is the indicator code

```
using cAlgo.API;

namespace cAlgo
{
   [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
   
   public class JINGXUANSOUL : Indicator
   {
       [Parameter("Source")]
       public DataSeries Source { get; set; }

       [Parameter(DefaultValue = 27)]
       public int Periods { get; set; }

       [Parameter(DefaultValue = 3)]
       public int SSSmooth { get; set; }

       [Parameter(DefaultValue = 3)]
       public int DDSmooth { get; set; }

       [Output("K", LineColor = "Blue", PlotType = PlotType.Line)]
       public IndicatorDataSeries SS { get; set; }

       [Output("D", LineColor = "Red", PlotType = PlotType.Line)]
       public IndicatorDataSeries DD { get; set; }

       [Output("J", LineColor = "Green", PlotType = PlotType.Line)]
       public IndicatorDataSeries KK { get; set; }
       
       [Output("KD", LineColor = "Green", PlotType = PlotType.Line)]
       public IndicatorDataSeries KD { get; set; }
       
       
       
       
       protected override void Initialize()
       {
           IndicatorArea.DrawHorizontalLine("OverBoughtH"    , 100   , Color.Orange);
           IndicatorArea.DrawHorizontalLine("OverBoughtL"    , 90    , Color.Orange);
           IndicatorArea.DrawHorizontalLine("OverSoldH"      , 10    , Color.Orange);
           IndicatorArea.DrawHorizontalLine("OverSoldL"      , 0     , Color.Orange);
       }

       public override void Calculate(int index)
       {
           var highestHigh = Source.Maximum(Periods);
           var lowestLow = Source.Minimum(Periods);
           var currentClose = Bars.ClosePrices;

           double rsv = 0;
           if (highestHigh != lowestLow)
               rsv = (currentClose[index] - lowestLow) / (highestHigh - lowestLow) * 100;

           var kPrev = index > 0 ? SS[index - 1] : 50;
           var dPrev = index > 0 ? DD[index - 1] : 50;

           var ss = (rsv + (SSSmooth - 1) * kPrev) / SSSmooth;
           var dd = (ss + (DDSmooth - 1) * dPrev) / DDSmooth;
           var kk = 3 * ss - 2 * dd;
           var kd = ((ss+dd)/2);

           SS[index] = ss;
           DD[index] = dd;
           KK[index] = kk;
           KD[index]  = kd;
       }
   }
}
```

and this is the cBot 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.None)]
    public class RelativeStrengthIndexSample : Robot
    {
        [Parameter("Volume (Lots)", DefaultValue = 0.1)]
        public double VolumeInLots { get; set; }
        
        [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
        public double StopLossInPips { get; set; }
        
        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public double TakeProfitInPips { get; set; }
        
        [Parameter("Label", DefaultValue = "Sample")]
        
        private JINGXUANSOUL _jxsoul;
        
        public string Label { get; set; }
          
        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }
        protected override void OnStart()
        {
            _jxsoul = Indicators.GetIndicator<JINGXUANSOUL>();
        }
        protected override void OnBarClosed()
        {
            if (_jxsoul.KD.Last(1)>90)
            {
                ClosePositions(TradeType.Buy);
                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInLots, Label, StopLossInPips, TakeProfitInPips);
            }
            
            else if (_jxsoul.KD.Last(1)<10)
            {
                ClosePositions(TradeType.Sell);
                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInLots, Label, StopLossInPips, TakeProfitInPips);
            }
        }
        
        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;
                ClosePosition(position);
            }
        }
        
    }
}
 


@JXTA
Replies

PanagiotisChar
27 Aug 2023, 05:54 ( Updated at: 28 Aug 2023, 04:42 )

Hi there,

Check your log and you will see an exception. You do not initialize BotPositions anywhere

Aieden Technologies

Need help? Join us on Telegram


 


@PanagiotisChar

JXTA
07 Sep 2023, 00:00 ( Updated at: 07 Sep 2023, 05:44 )

RE: Problmes with referring custom indicator's to cBot causing none executed trade in Backtesting

PanagiotisChar said: 

Hi there,

Check your log and you will see an exception. You do not initialize BotPositions anywhere

Aieden Technologies

Need help? Join us on Telegram


 

Thanks Panagiotis


@JXTA