Problem with a study - don't understand the error  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
jorsde
Posts: 10
Joined: 01 Apr 2014
Has thanked: 5 times

Problem with a study - don't understand the error

Postby jorsde » 06 Apr 2014

Hi

I'm trying to create an study that will plot 3 lines on the price chart
1. A line of the open price
2. A line of the mid range ((high-low)/2)
3. An EMA

The code I'm using complies but I when I try to add the indicator to a chart I get an error (See attached).
I've attached the code and the screenshot

What is the problem ?

Code: Select all

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

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Market_Status : IndicatorObject
{
private VariableObject<Double> m_Open;
private VariableObject<Double> m_Mid;
private VariableObject<Double> m_TodaysHigh;
private VariableObject<Double> m_TodaysLow;
private VariableObject<Double> m_TodaysOpen;
private VariableObject<Int32> m_Counter;

private Mov_Avg_Exponential m_FEMA;

private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;

public Market_Status(object ctx):
base(ctx) {}

protected override void Create()
{
m_Mid = new VariableObject<Double>(this);
m_TodaysHigh = new VariableObject<Double>(this);
m_TodaysLow = new VariableObject<Double>(this);
m_TodaysOpen = new VariableObject<Double>(this);

m_FEMA = new Mov_Avg_Exponential(this); //http://www.multicharts.com/discussion/viewtopic.php?f=19&t=12593

Plot1 = AddPlot(new PlotAttributes("Open", EPlotShapes.Line ,Color.Blue, Color.Empty, 2,0,true));
Plot2 = AddPlot(new PlotAttributes("Mid", EPlotShapes.Line, Color.Aqua, Color.Empty, 2, 0, true));
Plot3 = AddPlot();
m_FEMA = (Mov_Avg_Exponential)AddIndicator("EMA"); // Use EMA
m_FEMA.length = 10;
}

protected override void CalcBar()
{
EResolution resolution = Bars.Info.Resolution.Type;
if (resolution == EResolution.Quarter || EResolution.Week <= resolution && resolution <= EResolution.Year)
return;

if (Bars.Time[0].Date != Bars.Time[1].Date)
{
m_TodaysHigh.Value = Bars.High[0];
m_TodaysLow.Value = Bars.Low[0];
m_TodaysOpen.Value = Bars.Open[0];

m_Mid.Value = ((m_TodaysHigh.Value - m_TodaysLow.Value) + m_TodaysLow.Value);
Plot1.Set(0, m_Open.Value); //First line is the open of the day
Plot2.Set(0, m_Mid.Value); //Second line is the mid level of the day
Plot3.Set(m_FEMA.Plots[0].Values[0]); //Third line is the EMA
}
}
}
}
Attachments
Screenshot.jpg
The error Screenshot
(87.03 KiB) Downloaded 641 times

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

Re: Problem with a study - don't understand the error

Postby Henry MultiСharts » 07 Apr 2014

Hello jorsde,

Please use the following code:

Code: Select all

protected override void Create()
{
m_Mid = new VariableObject<Double>(this);
m_TodaysHigh = new VariableObject<Double>(this);
m_TodaysLow = new VariableObject<Double>(this);
m_TodaysOpen = new VariableObject<Double>(this);

//m_FEMA = new Mov_Avg_Exponential(this); //http://www.multicharts.com/discussion/viewtopic.php?f=19&t=12593

Plot1 = AddPlot(new PlotAttributes("Open", EPlotShapes.Line, Color.Blue, Color.Empty, 2, 0, true));
Plot2 = AddPlot(new PlotAttributes("Mid", EPlotShapes.Line, Color.Aqua, Color.Empty, 2, 0, true));
Plot3 = AddPlot();

m_FEMA = (Mov_Avg_Exponential)AddIndicator("Mov_Avg_Exponential"); // Use EMA
m_FEMA.length = 10;
}

jorsde
Posts: 10
Joined: 01 Apr 2014
Has thanked: 5 times

Re: Problem with a study - don't understand the error

Postby jorsde » 07 Apr 2014

I've tried it and got an error.
See attached.
What is wrong ?

Code: Select all

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

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Market_Status : IndicatorObject
{
private VariableObject<Double> m_Open;
private VariableObject<Double> m_Mid;
private VariableObject<Double> m_TodaysHigh;
private VariableObject<Double> m_TodaysLow;
private VariableObject<Double> m_TodaysOpen;
private VariableObject<Int32> m_Counter;

private Mov_Avg_Exponential m_FEMA;

private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;

public Market_Status(object ctx):
base(ctx) {}

protected override void Create()
{
m_Mid = new VariableObject<Double>(this);
m_TodaysHigh = new VariableObject<Double>(this);
m_TodaysLow = new VariableObject<Double>(this);
m_TodaysOpen = new VariableObject<Double>(this);

//m_FEMA = new Mov_Avg_Exponential(this); //http://www.multicharts.com/discussion/viewtopic.php?f=19&t=12593

Plot1 = AddPlot(new PlotAttributes("Open", EPlotShapes.Line, Color.Blue, Color.Empty, 2, 0, true));
Plot2 = AddPlot(new PlotAttributes("Mid", EPlotShapes.Line, Color.Aqua, Color.Empty, 2, 0, true));
Plot3 = AddPlot();

m_FEMA = (Mov_Avg_Exponential)AddIndicator("Mov_Avg_Exponential"); // Use EMA
m_FEMA.length = 10;
}

protected override void CalcBar()
{
EResolution resolution = Bars.Info.Resolution.Type;
if (resolution == EResolution.Quarter || EResolution.Week <= resolution && resolution <= EResolution.Year)
return;

if (Bars.Time[0].Date != Bars.Time[1].Date)
{
m_TodaysHigh.Value = Bars.High[0];
m_TodaysLow.Value = Bars.Low[0];
m_TodaysOpen.Value = Bars.Open[0];

m_Mid.Value = ((m_TodaysHigh.Value - m_TodaysLow.Value) + m_TodaysLow.Value);
Plot1.Set(0, m_Open.Value); //First line is the open of the day
Plot2.Set(0, m_Mid.Value); //Second line is the mid level of the day
Plot3.Set(m_FEMA.Plots[0].Values[0]); //Third line is the EMA
}
}
}
}
Attachments
Error.jpg
Error
(47.88 KiB) Downloaded 635 times

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

Re: Problem with a study - don't understand the error

Postby Henry MultiСharts » 07 Apr 2014

There are objects in your code that are declared but not created. When you try to access them in your code you get the error. For example m_Open.

jorsde
Posts: 10
Joined: 01 Apr 2014
Has thanked: 5 times

Re: Problem with a study - don't understand the error

Postby jorsde » 07 Apr 2014

Thank you,
It is working now but it looks like just two straight lines, probably because it starts from the beginning of the history and not the current session.

Is there a way to force the calculation to be only for the current session (today) and not earlier ?

Code: Select all

using System;
using System.Drawing;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Market_Status : IndicatorObject
{
private VariableObject<Double> m_Open;
private VariableObject<Double> m_Mid;
private VariableObject<Double> m_TodaysHigh;
private VariableObject<Double> m_TodaysLow;
private VariableObject<Double> m_TodaysOpen;

private Mov_Avg_Exponential m_FEMA;

private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;

public Market_Status (object ctx):
base(ctx) {}

protected override void Create()
{
m_Open = new VariableObject<Double>(this);
m_Mid = new VariableObject<Double>(this);
m_TodaysHigh = new VariableObject<Double>(this);
m_TodaysLow = new VariableObject<Double>(this);
m_TodaysOpen = new VariableObject<Double>(this);

//m_FEMA = new Mov_Avg_Exponential(this); //http://www.multicharts.com/discussion/viewtopic.php?f=19&t=12593

Plot1 = AddPlot(new PlotAttributes("Open", EPlotShapes.Line, Color.Blue, Color.Empty, 2, 0, true));
Plot2 = AddPlot(new PlotAttributes("Mid", EPlotShapes.Line, Color.Aqua, Color.Empty, 2, 0, true));
Plot3 = AddPlot();

m_FEMA = (Mov_Avg_Exponential)AddIndicator("Mov_Avg_Exponential"); // Use EMA
m_FEMA.length = 10;
}

protected override void CalcBar()
{

m_TodaysHigh.Value = Bars.High[0];
m_TodaysLow.Value = Bars.Low[0];

m_TodaysOpen.Value = Bars.Open[0];
m_Mid.Value = ((m_TodaysHigh.Value - m_TodaysLow.Value) + m_TodaysLow.Value);
Plot1.Set(0, m_Open.Value); //First line is the open of the day
Plot2.Set(0, m_Mid.Value); //Second line is the mid level of the day
Plot3.Set(m_FEMA.Plots[0].Values[0]); //Third line is the EMA
}
}
}
Attachments
Chart Issue.JPG
Lines
(87.99 KiB) Downloaded 637 times

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

Re: Problem with a study - don't understand the error  [SOLVED]

Postby Henry MultiСharts » 08 Apr 2014

Time filtering has been recently discussed in the following thread.


Return to “MultiCharts .NET”