Cannot implicitly convert type error

Questions about MultiCharts .NET and user contributed studies.
Gubbar924
Posts: 6
Joined: 12 Mar 2015

Cannot implicitly convert type error

Postby Gubbar924 » 12 Mar 2015

Hello, I am not a programmer, but I've almost figured my coding, with one exception. I get this error message on one line of my code:

Code: Select all

Cannot implicitly convert type 'double' to 'PowerLanguage.ISeries<double>'
Within the public class section, I have:

Code: Select all

private XAverage EMA1;
Within the StartCalc() procedure I have the following:

Code: Select all

EMA1 = new XAverage(this);

double dbl3 = (Bars.Close[0] - (dbl1+ dbl2)/2);

EMA1.Price = dbl3;
It is at this last line that I get the error message stating Cannot implicitly convert type 'double' to 'PowerLanguage.ISeries<double>'

I'm not familiar with powerlanguage so having some trouble typecasting.
Any suggestions would be welcome, thank you.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Cannot implicitly convert type error

Postby JoshM » 15 Mar 2015

It is at this last line that I get the error message stating Cannot implicitly convert type 'double' to 'PowerLanguage.ISeries<double>'
It looks like you're trying to assign a value to the EMA function, while you probably want to store that into a `VariableSeries<T>`.

Since you didn't explicitly say what you're trying to achieve, I guess you want to do something like the following (see the comments in the code for the explanation):

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.Collections.Generic;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Snippet_Indicator : IndicatorObject
{
public Snippet_Indicator(object _ctx) : base(_ctx) { }

// This is the function that calculates the EMA
private XAverage EMA1;

// The variable series holds the calculated EMA value for each bar
private VariableSeries<double> emaData;

// This is the line plot for visual confirmation
private IPlotObject myPlot;

protected override void Create()
{
// Here we create a new instance of the EMA function
EMA1 = new XAverage(this);

// And here we make a variable series
emaData = new VariableSeries<double>(this);

// Here we create the plot, named 'EMA' and set to a blue line
myPlot = AddPlot(new PlotAttributes("EMA", EPlotShapes.Line, Color.Blue));
}

protected override void StartCalc()
{
// Here we set the EMA function settings
// The EMA function is calculated on the closing price of the bars
EMA1.Price = Bars.Close;

// And the EMA has a lenght of 10 bars
EMA1.Length = 10;
}

protected override void CalcBar()
{
// Here we set the current value of the variable series (named `emaData`)
// to the current value calculated by the EMA function (`EMA1[0]`).
// Because this is done on every bar, the variable series is filled with historical
// values once we arrive at the last bar.
emaData.Value = EMA1[0];

// Here we set the plot line to the current stored value of the EMA (`emaData[0]`).
// Should we want to plot the EMA value of the previous bar, we'd have used `emaData[1]` and so on.
myPlot.Set(emaData[0]);
}

}
}

Gubbar924
Posts: 6
Joined: 12 Mar 2015

Re: Cannot implicitly convert type error

Postby Gubbar924 » 15 Mar 2015

Thank you for replying JoshM.
Yes in a nutshell this is exactly what I'm trying to do, except that I'm having to dynamically set the value of EMA1.price. Because EMA1 is an XAverage (which is an ISeries), and the formula that I am using to get the value of EMA1.price actually returns a double, this is where I keep running into issues.

What I'm trying to do on a bigger picture is convert a script from NT to Multicharts .Net.
Here is the original script (if you're familiar with NT scripts, I've taken out all of the NT specific coding that is added to the bottom of each script)

Code: Select all

namespace NT.Indicator
{
[Description("Stochastic Momentum Index by NT Indicator")]
public class NTISMI : Indicator
{
#region Variables
private int q = 13;
private int r = 25;
private int s = 2;
private int avgPeriod = 3;

private DataSeries rdiff;
private DataSeries diff;
#endregion



protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "SMI"));
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Avg"));

Add(new Line(Color.Gray, -40, "Lower"));
Add(new Line(Color.Gray, 40, "Upper"));

rdiff = new DataSeries(this);
diff = new DataSeries(this);

Overlay = false;
}



protected override void OnBarUpdate()
{
double min_low = MIN(Low, q)[0];
double max_high = MAX(High, q)[0];

rdiff.Set(Close[0] - (max_high + min_low)/2);
diff.Set(max_high - min_low);

double avgrel = EMA(EMA(rdiff, r), s)[0];
double avgdiff = EMA(EMA(diff, r), s)[0];


SMI.Set(avgrel/(avgdiff/2)*100);
Avg.Set(EMA(SMI, avgPeriod)[0]);
}
what I have so far, it compiles without any errors, but it errors out at runtime with the following error message:
Unaccessible property(method): CreateSeriesVar. Construct only.
and finally, here is what I have so far as the code

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
public class TGSMI : IndicatorObject
{
private IPlotObject plot1;
private IPlotObject plot2;
private IPlotObject plot3;
private IPlotObject plot4;
private IPlotObject plot5;

private VariableSeries<Double> SMI;
private VariableSeries<Double> Avg;

private XAverage EMA1;
private XAverage EMA2;
private XAverage EMA3;
private XAverage EMA4;
private XAverage EMA5;

private ISeries<double> rdiff;
private ISeries<double> diff;
private ISeries<double> avgrel1;
private ISeries<double> avgdiff;

[Input]
public int q { get; set; }

[Input]
public int r { get; set; }

[Input]
public int s { get; set; }

[Input]
public int avgPeriod { get; set; }

[Input]
public double OverBought { get; set; }

[Input]
public double OverSold { get; set; }


public TGSMI(object _ctx):base(_ctx)
{
OverBought = 80;
OverSold = 20;
q = 13;
r = 25;
s = 2;
avgPeriod = 3;
}


protected override void Create()
{
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("ZeroLine", EPlotShapes.Line, Color.Black, Color.Empty, 1, 0, false));
plot2 = AddPlot(new PlotAttributes("OverBought", EPlotShapes.Line, Color.Blue, Color.Empty, 1, 0, false));
plot3 = AddPlot(new PlotAttributes("OverSold", EPlotShapes.Line, Color.Blue, Color.Empty, 1, 0, false));
plot4 = AddPlot(new PlotAttributes("SMI", EPlotShapes.Line, Color.Green, Color.Empty, 1, 0, false));
plot5 = AddPlot(new PlotAttributes("Avg", EPlotShapes.Line, Color.Red, Color.Empty, 1, 0, false));

SMI = new VariableSeries<Double>(this);
Avg = new VariableSeries<Double>(this);

EMA1 = new XAverage(this);
EMA2 = new XAverage(this);
EMA3 = new XAverage(this);
EMA4 = new XAverage(this);
EMA5 = new XAverage(this);
}


protected override void StartCalc()
{
// assign inputs
}


protected override void CalcBar()
{
// indicator logic
plot1.Set(0);
plot2.Set(40);
plot3.Set(-40);


int q = 13;
int r = 25;
int s = 2;
int avgPeriod = 3;

double minlow1 = Bars.Low.Lowest(q, 0);
double maxhigh1 = Bars.High.Highest(q, 0);

rdiff = CreateSeriesVar<double>((Bars.Close[0] - (maxhigh1 + minlow1)/2));
diff = CreateSeriesVar<double>((maxhigh1 - minlow1));

EMA1.Price = CreateSeriesVar<double>(rdiff[0]);
EMA1.Length = r;
EMA2.Price = CreateSeriesVar<double>(EMA1[0]);
EMA2.Length = s;
avgrel1 = CreateSeriesVar<double>(EMA2[0]);

EMA3.Price = CreateSeriesVar<double>(diff[0]);
EMA3.Length = r;
EMA4.Price = CreateSeriesVar<double>(EMA1[0]);
EMA4.Length = s;
avgdiff = CreateSeriesVar<double>(EMA4[0]);

//SMI = (avgrel1[0]/(avgdiff[0]/2)*100);
double smi = (avgrel1[0]/(avgdiff[0]/2)*100);

EMA5.Price = CreateSeriesVar<double>(SMI[0]);
EMA5.Length = avgPeriod;

//Avg = EMA5[0];
double avg = EMA5[0];



plot4.Set(smi);
plot5.Set(avg);
}
}
}
Im no coder, so I wouldnt even know where I'm going wrong.
Thanks again for checking in!

Gubbar924
Posts: 6
Joined: 12 Mar 2015

Re: Cannot implicitly convert type error

Postby Gubbar924 » 29 May 2015

bump please
any other takers?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Cannot implicitly convert type error

Postby Henry MultiСharts » 01 Jun 2015

Hello Gubbar924,

Programming assistance is beyond our technical support. Please contact us directly in case you are interested in a custom programming services.


Return to “MultiCharts .NET”