Converting an indicator

pancakeman since: 18 Dec 2020;

  07 Apr 2021, 10:29
Converting an indicator

Hi guys, I'm trying to convert a small part of a indicator from tradingview to ctrader. The pine script is:

wvf = ((highest(close, pd)-low)/(highest(close, pd)))*100

plot(wvf*-1, title='wvf')

Here's my code so far:

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 NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public int pd { get; set; }

        [Output("Main")]
        public IndicatorDataSeries wvf { get; set; }


        protected override void Initialize()
        {
            wvf = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            wvf = ((Math.Max(Bars.ClosePrices[index], pd) - Bars.LowPrices[index]) / (Math.Max(Bars.ClosePrices[index], pd))) * 100;
        }
    }
}

I'm getting the error "Error CS0029: Cannot implicitly convert type 'double' to 'cAlgo.API.IndicatorDataSeries'" and I'm not sure how I should go about the "wvf*-1" part. Any advice will be much appreciated, thanks! 

JerryTrader since: 06 Jan 2021;

  07 Apr 2021, 11:12

Hi,

You should assign your result to wvf[index].
Regarding wvf * -1, it just inverts the plot.

You should be ok with :

public override void Calculate(int index)
{
    wvf[index] = -((Math.Max(Bars.ClosePrices[index], pd) - Bars.LowPrices[index]) / (Math.Max(Bars.ClosePrices[index], pd))) * 100;
}
amusleh's avatar

amusleh since: 01 Mar 2021;

  07 Apr 2021, 11:16

Hi,

You should do it like this:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter("Period", DefaultValue = 10)]
        public int Period { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            var highest = Bars.ClosePrices.Maximum(Period);

            Result[index] = (highest - Bars.LowPrices[index]) / highest * 100;
        }
    }
}

Please learn C# basics and read cTrader Automate API references before trying to develop a cBot/indicator for cTrader.

Community Developer | Spotware

pancakeman since: 18 Dec 2020;

  08 Apr 2021, 05:40

Thanks for the responses, much appreciated!