Calling custom indicator to cBot

Created at 15 May 2020, 01:19
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!
PE

peetman23

Joined 02.04.2019

Calling custom indicator to cBot
15 May 2020, 01:19


Could you please help with error? I'm new to programming. I have selected the custom indicator in the "Manage References"

Error CS0103: The name 'pvto' does not exist in the current context

 

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, AccessRights = AccessRights.None)]
    public class PriceVolumeTrend_OptimizedcBot : Robot
    {
        //[Parameter(DefaultValue = 0.0)]
        //public double Parameter { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }
        public Indicator pvto;


        protected void Initialize()
        {
            pvto= Indicators.GetIndicator<PriceVolumeTrend_Optimized>();
        }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {

            // Put your core logic here
            if (pvto(LastResult) < -9)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (pvto(LastResult) > 9)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

        protected override void OnStop()
        {
            // Put your initialization logic here
        }


        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SamplePVT", SymbolName, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SamplePVT", SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(10000);

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SamplePVT");
        }
    }
}


//-----------------------------//

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PriceVolumeTrend_Optimized : Indicator
    {

        //[Parameter(DefaultValue = 0.0)]
        //public double Parameter { get; set; }

        [Parameter("Price")]
        public DataSeries Source { get; set; }


        [Output("Linha", Color = Colors.Lime, Thickness = 1)]
        public IndicatorDataSeries PVTO { get; set; }

        private PriceVolumeTrend pvt;


        protected override void Initialize()
        {
            // Initialize and create nested indicators

            pvt = Indicators.PriceVolumeTrend(Source);

        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            PVTO[index] = pvt.Result[index] * pvt.Result[index - 4];
        }
    }
}

 


@peetman23
Replies

PanagiotisCharalampous
15 May 2020, 08:33

Hi peetman23,

Can you please post the complete cBot code as well as the indicator code so that we can reproduce this issue?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

peetman23
15 May 2020, 11:01

RE:

PanagiotisCharalampous said:

Hi peetman23,

Can you please post the complete cBot code as well as the indicator code so that we can reproduce this issue?

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

I've updated the initial post.


@peetman23

PanagiotisCharalampous
15 May 2020, 16:08

Hi peetman23,

You need to change 

 public Indicator pvto;

to

 public PriceVolumeTrend_Optimized pvto;

and

            // Put your core logic here
            if (pvto(LastResult) < -9)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (pvto(LastResult) > 9)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }

to

            // Put your core logic here
            if (pvto.PVTO.LastValue < -9)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (pvto.PVTO.LastValue > 9)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }

for the cBot to build. 

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

peetman23
15 May 2020, 16:29

RE:

PanagiotisCharalampous said:

Hi peetman23,

You need to change 

 public Indicator pvto;

to

 public PriceVolumeTrend_Optimized pvto;

and

            // Put your core logic here
            if (pvto(LastResult) < -9)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (pvto(LastResult) > 9)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }

to

            // Put your core logic here
            if (pvto.PVTO.LastValue < -9)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (pvto.PVTO.LastValue > 9)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }

for the cBot to build. 

Best Regards,

Panagiotis 

Join us on Telegram

 

 

Thank you. I was able to build the cBot, but when I try to backtest it throws the following:
Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.


@peetman23

PanagiotisCharalampous
15 May 2020, 16:39

Hi peetman23,

There are many issues in your code like the fact that the indicator needs be initialized in OnStart (Initialize is not used anywhere in the cBot), correct parameters are not passed to the indicator and volume seems really wrong. You need to fix all these issues and any others that might be there before the cBot starts working properly.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous