Converting an indicator

Created at 07 Apr 2021, 10:29
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!
PA

pancakeman

Joined 18.12.2020

Converting an indicator
07 Apr 2021, 10:29


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! 


@pancakeman
Replies

JerryTrader
07 Apr 2021, 11:11 ( Updated at: 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;
}

 


@JerryTrader

amusleh
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.


@amusleh

pancakeman
08 Apr 2021, 05:40

Thanks for the responses, much appreciated! 


@pancakeman