I used the ratio study to create an indicator. But when I tried to add the EMA programming to the indicator I get an error because my indicator is a variable series but the EMA is calling for an Iseries. How can I create and EMA based on the output of the ratio spread?
spread.Value = (dataseries1[0] / dataseries2[0]);
I try to enter spread.value into an ema but it fails.
Thanks
ema of an indicator
- Kate MultiCharts
- Posts: 561
- Joined: 21 Oct 2020
- Has thanked: 7 times
- Been thanked: 139 times
Re: ema of an indicator
Hello beggar,
Here's an example based on the standard Mov_Avg_Exponential indicator:
Here's an example based on the standard Mov_Avg_Exponential indicator:
Code: Select all
using System;
using System.Drawing;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Mov_Avg_Exponential2 : IndicatorObject
{
private XAverage m_xaverage1;
private VariableSeries<Double> m_avgexp;
private IPlotObject Plot1;
public Mov_Avg_Exponential2(object ctx) :
base(ctx){
length = 9;
}
private VariableSeries<double> spread { get; set; }
[Input]
public int length { get; set; }
[Input]
public int displace { get; set; }
protected override void Create(){
spread = new VariableSeries<double>(this);
m_xaverage1 = new XAverage(this);
m_avgexp = new VariableSeries<Double>(this);
Plot1 =
AddPlot(new PlotAttributes("AvgExp", 0, Color.Blue,
Color.Empty, 0, 0, true));
}
protected override void StartCalc(){
m_xaverage1.Price = spread;
m_xaverage1.Length = length;
}
protected override void CalcBar(){
spread.Value = Bars.Close[0] / BarsOfData(2).Close[0];
m_avgexp.Value = m_xaverage1[0];
if (((displace >= 0)
|| Bars.CurrentBar > Math.Abs(displace))){
Plot1.Set(displace, m_avgexp.Value);
}
}
}
}
Re: ema of an indicator
Thank you. I am currently having another issue. I am trying to access previous values of the custom standard deviation and divide by it. I am getting errors stating that I am trying to divide by zero zscore.Value = spread[0] - m_xaverage1[1] / m_sdev[1]
How can I access previous values. Thanks
How can I access previous values. Thanks
Code: Select all
using System;
using System.Drawing;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
// [SameAsSymbol(true)]
public class Mov_Avg_Exponential2 : IndicatorObject
{
private XAverage m_xaverage1;
private VariableSeries<Double> m_avgexp;
private VariableSeries<Double> m_sdev { get; set; }
private VariableSeries<Double> m_lowerband;
private VariableSeries<Double> m_upperband;
private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;
private IPlotObject Plot4;
public Mov_Avg_Exponential2(object ctx) :
base(ctx){
length = 15;
lengthSma = 15;
numdevsdn = -2;
numdevsup = 2;
}
private VariableSeries<double> spread { get; set; }
private VariableSeries<double> zscore;// { get; set; }
[Input]
public int length { get; set; }
[Input]
public int lengthSma { get; set; }
[Input]
public int displace { get; set; }
[Input]
public double numdevsup { get; set; }
[Input]
public double numdevsdn { get; set; }
protected override void Create(){
m_lowerband = new VariableSeries<Double>(this);
m_upperband = new VariableSeries<Double>(this);
spread = new VariableSeries<double>(this);
zscore = new VariableSeries<double>(this);
m_xaverage1 = new XAverage(this);
m_avgexp = new VariableSeries<Double>(this);
m_sdev = new VariableSeries<double>(this);
Plot1 =
AddPlot(new PlotAttributes("AvgExp", 0, Color.Red,
Color.Empty, 0, 0, true));
Plot2 =
AddPlot(new PlotAttributes("Spread", 0, Color.Black,
Color.Empty, 0, 0, true));
Plot3 =
AddPlot(new PlotAttributes("Deviation", 0, Color.Yellow,
Color.Empty, 0, 0, true));
Plot4 =
AddPlot(new PlotAttributes("spp", 0, Color.Orange,
Color.Empty, 0, 0, true));
}
protected override void StartCalc(){
m_xaverage1.Price = spread;
m_xaverage1.Length = length;
}
protected override void CalcBar(){
m_upperband.Value = (m_avgexp.Value + (numdevsup*m_sdev.Value));
m_lowerband.Value = (m_avgexp.Value + (numdevsdn*m_sdev.Value));
spread.Value = Bars.Close[0] /BarsOfData(2).Close[0];
m_avgexp.Value = m_xaverage1[0];
m_sdev.Value = spread.StandardDeviationCustom(lengthSma, 1);
zscore.Value = spread[0] - m_xaverage1[1] / m_sdev[1];
if (((displace >= 0)
|| Bars.CurrentBar > Math.Abs(displace))){
Plot1.Set(displace, m_sdev.Value);
// Plot2.Set(displace, spread.Value);
// Plot3.Set(displace, (spread.Value - m_avgexp.Value) / m_sdev.Value);
// Plot1.Set(displace, m_upperband.Value);
// Plot2.Set(displace, m_lowerband.Value);
// Plot3.Set(displace, m_avgsma.Value);
Plot4.Set(displace, m_sdev.Value);
}
}
}
}
- Kate MultiCharts
- Posts: 561
- Joined: 21 Oct 2020
- Has thanked: 7 times
- Been thanked: 139 times
Re: ema of an indicator
Hi,
Standard deviation value can be rather small. You are also referencing the previous value which is 0 for the first bar.
Try checking if the divisor is close to 0. For example,
Standard deviation value can be rather small. You are also referencing the previous value which is 0 for the first bar.
Try checking if the divisor is close to 0. For example,
Code: Select all
if ( Math.Abs(m_sdev[1]) < 0.0000001) {..}