ADX based on data X within code.

Questions about MultiCharts .NET and user contributed studies.
eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

ADX based on data X within code.

Postby eunos64 » 15 Sep 2018

Hello,
Here is a chart which have 2 data, for example 5 min and 1 hour.
I want to create a simple ADX indicator based on second data_stream, and want to apply this indicator to my signal.
But, this indicator only show a value based on data 1.
What is wrong or lack with this code ? :( :( :(
Of course, I know "Indicator properties --> Base study on --> select data stream " to show the indicator based on another data stream.

Best regards,

:!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!:

using System;
using System.Drawing;

namespace PowerLanguage.Indicator
{
public class ADX_data_num : IndicatorObject
{
private Function.ADX m_adx1;
private VariableSeries<Double> m_adxvalue;
private IPlotObject Plot1;

public ADX_data_num(object ctx) :
base(ctx){
length = 14;
data_num = 2;
}

[Input]
public int length { get; set; }
[Input]
public int data_num { get; set; }

protected override void Create(){
m_adx1 = new Function.ADX(this, data_num);
m_adxvalue = new VariableSeries<Double>(this);
Plot1 =
AddPlot(new PlotAttributes("ADX", 0, Color.Cyan,
Color.Empty, 0, 0, true));
}

protected override void StartCalc(){
m_adx1.Length = length;
m_adxvalue.DefaultValue = 0;
}


protected override void CalcBar(){
m_adxvalue.Value = m_adx1[0];
Plot1.Set(0, m_adxvalue.Value);
if (PublicFunctions.DoubleGreater(m_adxvalue.Value, m_adxvalue[1])
&&PublicFunctions.DoubleLessEquals(m_adxvalue[1], m_adxvalue[2])){
Alerts.Alert("Indicator turning up");
}
else{
if (PublicFunctions.DoubleLess(m_adxvalue.Value, m_adxvalue[1])
&&PublicFunctions.DoubleGreaterEquals(m_adxvalue[1], m_adxvalue[2])){
Alerts.Alert("Indicator turning down");
}
}
}
}
}

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: ADX based on data X within code.

Postby Henry MultiСharts » 17 Sep 2018

Hello eunos64,

Please refer to the following sample:
viewtopic.php?f=19&t=45848#p100792

eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

Re: ADX based on data X within code.

Postby eunos64 » 18 Sep 2018

Yes, I made this indicator following your FAQ.
Added only a few lines to buit-in ADX indicator.
But nothing have changed whatever value set to data_num.
To make the function calculated on data2. Example for ADX and ATR functions:
CODE: SELECT ALL

ADX _a = new ADX(this,2);
//
double _aa = PublicFunctions.TrueRange(this, 10, 2);
Attachments
20180918USDJPY_ADX2.jpg
(258.97 KiB) Downloaded 762 times

eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

Re: ADX based on data X within code.

Postby eunos64 » 19 Sep 2018

I made another indicator "DMI_Data2".
This works almost good for my needs.
But it looks like calculated by calcbar() every data1 1 hour bar, and this make jagged shape curve.

Is this a defined behavior ?
Is there a easy way to calculate every data2 bar ?
And "m_adx1 = new Function.ADX(this, data_num);" doesn't work. I can't tell why....

Code: Select all

using System.Drawing;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
public class DMI_Data2 : IndicatorObject
{
//2018.9.19

private DirMovement m_dirmovement1;
private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;

public DMI_Data2(object ctx) :
base(ctx)
{
adxtrend = 25;
length = 14;
Datastream_DMI = 2;
}
[Input]
public int length { get; set; }
[Input]
public double adxtrend { get; set; }
[Input]
public int Datastream_DMI { get; set; }

protected override void Create()
{
m_dirmovement1 = new DirMovement(this, Datastream_DMI);

Plot1 =
AddPlot(new PlotAttributes("DMI+", 0, Color.Yellow,
Color.Empty, 0, 0, true));
Plot2 =
AddPlot(new PlotAttributes("DMI-", 0, Color.Blue,
Color.Empty,
0, 0, true));
Plot3 =
AddPlot(new PlotAttributes("ADX", 0, Color.Cyan,
Color.Empty, 0, 0, true));
}

protected override void StartCalc()
{
m_dirmovement1.PriceH = BarsOfData(Datastream_DMI).High;
m_dirmovement1.PriceL = BarsOfData(Datastream_DMI).Low;
m_dirmovement1.PriceC = BarsOfData(Datastream_DMI).Close;
m_dirmovement1.Length = length;

}

protected override void CalcBar()
{
m_dirmovement1.Call();
Plot1.Set(0, m_dirmovement1.DMIPlus.Value);
Plot2.Set(0, m_dirmovement1.DMIMinus.Value);
Plot3.Set(0, m_dirmovement1.ADX.Value);

if (PublicFunctions.DoubleGreater(m_dirmovement1.ADX.Value, adxtrend))
{
if (this.CrossesOver(m_dirmovement1.DMIPlus, m_dirmovement1.DMIMinus))
{
Alerts.Alert("Bullish alert");
}
else
{
if (this.CrossesUnder(m_dirmovement1.DMIPlus, m_dirmovement1.DMIMinus))
{
Alerts.Alert("Bearish alert");
}
}
}
}
}
}
Attachments
20180919USDJPY_DMI.JPG
(142.1 KiB) Downloaded 753 times

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: ADX based on data X within code.

Postby Henry MultiСharts » 20 Sep 2018

eunos64,

A study applied to the base series is always calculated based on the bars of this series. If in the script there are functions that are calculated in the context of the other series - they are called upon each calculation of the script, but their values are fixated on the other specified series bar close. You can limit these calculations by adding BarsOfData(2).Status check to your code, but that will not affect the indicator plot you have.

eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

Re: ADX based on data X within code.

Postby eunos64 » 21 Sep 2018

Thank you Henry !

if (BarsOfData(Datastream_DMI).Status == EBarState.Close)
{
my command
}
This works well.
It's getting better !! :D
Attachments
DMI_data2 on Data2.JPG
(65.16 KiB) Downloaded 736 times

eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

Re: ADX based on data X within code.

Postby eunos64 » 21 Sep 2018

But I noticed another problem.
When I set [input]Datastream_DMI=1, this indicator shows totally different value from DMI indicator base study on data1.
I expect same result each indicator, because BarsOfData(1).Close means Bars.Close. :?
Attachments
DMI_data2 on Data1.png
(369.37 KiB) Downloaded 736 times

eunos64
Posts: 19
Joined: 15 Sep 2013
Has thanked: 10 times
Been thanked: 1 time

Re: ADX based on data X within code.

Postby eunos64 » 21 Sep 2018

Here is a file.
Attachments
DMI_Data2.pln
(1.45 KiB) Downloaded 251 times

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: ADX based on data X within code.

Postby Henry MultiСharts » 26 Sep 2018

eunos64,

Changing the base series number via the study Inputs is not supported. After changing the input the study is not recreated, i.e. the Create() method is not called and the functions are not applied to the new context. The study should be programmed to work with the required data series from the beginning.


Return to “MultiCharts .NET”