return more value from function...is possible?

Questions about MultiCharts .NET and user contributed studies.
shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

return more value from function...is possible?

Postby shivax » 28 Jul 2014

hi, i ask to expert people in mc.net

Code: Select all

namespace PowerLanguage
{
namespace Function
{
public class ProvaCurrentBar2 : FunctionSimple<int>
{
public ProvaCurrentBar2(CStudyControl master) : base(master){}

public int gary;

protected override int CalcBar(){

gary=4;
}



protected override void Create(){
m_current_bar = new VariableObject<int>(this);
}

}


}
}

Code: Select all

namespace PowerLanguage.Indicator{
public class Prova_ : IndicatorObject {
public Prova_(object _ctx):base(_ctx){}
private IPlotObject plot1;

private Function.ProvaCurrentBar2 Contabar;

protected override void Create() {

Contabar = new Function.ProvaCurrentBar2(this);

protected override void CalcBar(){

Output.WriteLine("Gary " + Contabar.gary);
}
}
}
DISPLAY : gary0
gary0
gary0

(i don't want use Return because in a future i must return more value..not one)
Why Not display gary4?
Thank you for help me

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: return more value from function...is possible?

Postby JoshM » 29 Jul 2014

I cannot say I understand your post, but have you looked into the out parameter modifier for getting multiple values from a method?

PS: If you use the [ code ] and [ /code ] tags (see the accompanying button in the 'Post a reply' window), your code is easier to read for others.

shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

Re: return more value from function...is possible?

Postby shivax » 29 Jul 2014

Ok .. thanks for the help .... I rephrase the question

See that code below :

Code: Select all


//FUNCTION

using System;
using System.Drawing;
using System.Linq;


namespace PowerLanguage
{
namespace Function
{
public class ProvaCurrentBar2 : FunctionSimple<int>
{
public ProvaCurrentBar2(CStudyControl master) : base(master){}
private VariableObject<int> m_current_bar;
public VariableObject<int> gary;

protected override int CalcBar(){
gary.Value=4; //IS AN example
}



protected override void Create(){
m_current_bar = new VariableObject<int>(this);
}

}


}
}

Code: Select all

//INDICATOR study

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

namespace PowerLanguage.Indicator{
public class Prova_ : IndicatorObject {
public Prova_(object _ctx):base(_ctx){}
private IPlotObject plot1;

private Function.ProvaCurrentBar2 Contabar;

protected override void Create() {
// create variable objects, function objects, plot objects etc.

Contabar = new Function.ProvaCurrentBar2(this);
}
protected override void StartCalc() {
// assign inputs

}
protected override void CalcBar(){
// indicator logic

Output.WriteLine("Gary " + Contabar.gary);
}
}
}


The question is :

I tried to return a value to the indicator but I do not see the result ....
The code is correct?
Attachments
Output.png
Output.png (24.41 KiB) Viewed 804 times

User avatar
Henry MultiСharts
Posts: 9066
Joined: 25 Aug 2011
Has thanked: 1255 times
Been thanked: 2911 times

Re: return more value from function...is possible?

Postby Henry MultiСharts » 29 Jul 2014

You can use any of the prebuilt functions for reference.
There are two ways of returning a value from a function:
1) using Return operator in CalcBar method. Only one value can be returned this way.
2) if you need to return multiple values - you can create multiple Public variables and by changing their values in the function read these values from an indicator or signal.

But in order to have the function executed and the necessary values calculated - you need to have the function calculated by calling the CalcBar method.

it can be done indirectly by calling the current function value:
int _var = MyFunction[0];

Or directly using the Call method:
int _var = MyFunction.Call();

These calls are missing in your code.

shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

Re: return more value from function...is possible?

Postby shivax » 29 Jul 2014

Thanks you very ....But...it give me an error :

Code: Select all


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

namespace PowerLanguage.Indicator{
public class Prova_ : IndicatorObject {
public Prova_(object _ctx):base(_ctx){}
private IPlotObject plot1;

private Function.ProvaCurrentBar2 Contabar;

protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));

Contabar = new Function.ProvaCurrentBar2(this);
}
protected override void StartCalc() {
// assign inputs

}
protected override void CalcBar(){

int _var = Contabar.gary[0];


Output.WriteLine("Gary " + _var);
}
}
Attachments
screenshot_210.png
screenshot_210.png (15.61 KiB) Viewed 804 times

User avatar
Henry MultiСharts
Posts: 9066
Joined: 25 Aug 2011
Has thanked: 1255 times
Been thanked: 2911 times

Re: return more value from function...is possible?

Postby Henry MultiСharts » 30 Jul 2014

Here is the proper code:

Code: Select all

protected override void CalcBar()
{
Contabar.Call();
int _var = Contabar.gary.Value;
Output.WriteLine("Gary " + _var);
}

shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

Re: return more value from function...is possible?

Postby shivax » 30 Jul 2014

Code: Select all



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

namespace PowerLanguage.Indicator{
public class Prova_ : IndicatorObject {
public Prova_(object _ctx):base(_ctx){}
private IPlotObject plot1;

private Function.ProvaCurrentBar2 Contabar;

protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));

Contabar = new Function.ProvaCurrentBar2(this);
}
protected override void StartCalc() {
// assign inputs

}
protected override void CalcBar()
{
Contabar.Call();
int _var = Contabar.gary.Value;
Output.WriteLine("Gary " + _var);
}
}
}

it give me same error you last code ( see pics below)

(it's ok text formatting now? )


Another question;

I am converting my indicator made ​​in powerlanguage in mc.net..

Is an indicator very long .. and i want break it up into many functions ... I ask this question because different functions must return more value to be used in the indicator ...... and I would not want that will be an anomaly for mc.net (the functions should always be call in calc_bar method(in the indicator) ...because should always give me values ​​in real time)

i thought I'd make the heart of the program in the functions and use the indicator (that will call function) to displaying result...
Attachments
Immagine.png
Immagine.png (25.77 KiB) Viewed 800 times

User avatar
Henry MultiСharts
Posts: 9066
Joined: 25 Aug 2011
Has thanked: 1255 times
Been thanked: 2911 times

Re: return more value from function...is possible?

Postby Henry MultiСharts » 01 Aug 2014

The error appears because "gary" variable is not initialized in Create() method of the function.

Please clarify your additional question. I'm not sure I am following.

shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

Re: return more value from function...is possible?

Postby shivax » 02 Aug 2014

Thank for your answer...
i explain you in brief

Using 2 return values ​​for the class function in the indicator that you described , the program is stable or is better to avoid it and to return only 1 value through Return () .. Thanks

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: return more value from function...is possible?

Postby JoshM » 02 Aug 2014

Using 2 return values ​​for the class function in the indicator that you described , the program is stable or is better to avoid it and to return only 1 value through Return () .. Thanks
It's very likely more efficient to return multiple values from one function (with using public fields like Henry mentioned) than using a separate function for each value. This latter option will give more redundant code, more function class instances, and (as I understand it) also more CPU usage since every function is updated on each bar.

It would be more efficient to have one function update, than several functions that are highly similar.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: return more value from function...is possible?

Postby JoshM » 02 Aug 2014

(...)
2) if you need to return multiple values - you can create multiple Public variables and by changing their values in the function read these values from an indicator or signal.

But in order to have the function executed and the necessary values calculated - you need to have the function calculated by calling the CalcBar method.
Thanks Henry, I didn't knew that.

Here's an example Shivax for how to implement what Henry is talking about (public fields in the function and calling `Call()` in the indicator). (If it can help you, I already made them following Henry's comments so figured I might as well post them here to).

Indicator:

Code: Select all

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

namespace PowerLanguage.Indicator
{
public class ForumExample_AccessFunctionFields : IndicatorObject
{
private ForumExample_MultiplePublicFields myFunction;

public ForumExample_AccessFunctionFields(object _ctx) : base(_ctx) { }

protected override void Create()
{
myFunction = new ForumExample_MultiplePublicFields(this);
}

protected override void CalcBar()
{
if (Bars.LastBarOnChart && (Bars.Status == EBarState.Close))
{
myFunction.Call();

Output.WriteLine("Getting values from the function:");
Output.WriteLine("Add: {0}, Subtract: {1}, Multiply: {2}",
myFunction.Add,
myFunction.Subtract,
myFunction.Multiply);
}
}
}
}
Function:

Code: Select all

using System;
using System.Drawing;
using System.Linq;

namespace PowerLanguage
{
namespace Function
{
public sealed class ForumExample_MultiplePublicFields : FunctionSimple<System.Double>
{
private double add, subtract, multiply;

public double Add
{
get { return add; }
}

public double Subtract
{
get { return subtract; }
}

public double Multiply
{
get { return multiply; }
}

public ForumExample_MultiplePublicFields(CStudyControl _master) : base(_master) { }
public ForumExample_MultiplePublicFields(CStudyControl _master, int _ds) : base(_master, _ds) { }

protected override System.Double CalcBar()
{
add = 2 + 4;
subtract = 2 - 4;
multiply = 2 * 8;

return default(System.Double);
}
}
}
}
Gives as ouput:

Code: Select all

Getting values from the function:
Add: 6, Subtract: -2, Multiply: 16

shivax
Posts: 90
Joined: 02 Sep 2013
Has thanked: 37 times

Re: return more value from function...is possible?

Postby shivax » 02 Aug 2014

thank you very much


Return to “MultiCharts .NET”