# Converting Indicator to Function: Return results don't match  [SOLVED]

Questions about MultiCharts and user contributed studies.
eneilson
Posts: 2
Joined: 22 Feb 2011

### Converting Indicator to Function: Return results don't match

I have a stochastic Indicator that I would like to convert into a simple callable Function. When I move the code into the Function, however, the returned values don't match the values generated by the Indicator. I'm passing my values into the function by NumericRef.

The working indicator code is posted first and then the indicator+function code (Both the RVI and Trigger return values are "sort of off" from what they should be). It looks like something is happening in the regression.

Functioning Indicator Code with correct values:

Code: Select all

`//Stochastic Relative Vigor IndexInputs:   Length(8);Vars:      Num(0),      Denom(0),      count(0),      RVI(0),      MaxRVI(0),      MinRVI(0);      Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6; Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6; Num=0; Denom=0; For count=0 to Length-1 Begin    Num=Num+Value1[count];    Denom=Denom+Value2[count]; end; If Denom<>0 then RVI=Num/Denom; MaxRVI=Highest(RVI, Length); MinRVI=Lowest(RVI, Length); If MaxRVI<>MinRVI then Value3=(RVI-MinRVI)/(MaxRVI-MinRVI); Value4=(4*Value3+3*Value3[1]+2*Value3[2]+Value3[3])/10; Value4=2*(Value4-0.5); if LastBarOnChart thenbeginprint("--------");print("Correct Value: Stoc1RVI:");print(Value4);end;Plot1(Value4, "RVI");Plot2(.96*(Value4[1]+.2), "Trigger");Plot3(0, "Ref");`

Simple Indicator with called function (RVI and Trigger return values are not correct):

Code: Select all

`//Stochastic Relative Vigor IndexInputs:   Length(8);Vars:      RVI(0),      Trigger(0),      Ref(0);//RVI, Trigger are passed by reference      Value1 = StochasticRVI(Length, RVI, Trigger);Plot1(RVI, "RVI");Plot2(Trigger, "Trigger");Plot3(Ref, "Ref");`

Code: Select all

`//Stochastic Relative Vigor IndexInputs:   Length(NumericSimple),RVI(NumericRef), Trigger(NumericRef);Vars:      Num(0),      Denom(0),      count(0),      MaxRVI(0),      MinRVI(0),      Ref(0);      Value1=((Close-Open)+2*(Close[1]-Open[1])+2*(Close[2]-Open[2])+(Close[3]-Open[3]))/6; Value2=((High-Low)+2*(High[1]-Low[1])+2*(High[2]-Low[2])+(High[3]-Low[3]))/6; Num=0; Denom=0; For count=0 to Length-1 Begin    Num=Num+Value1[count];    Denom=Denom+Value2[count]; end; If Denom<>0 then RVI=Num/Denom; MaxRVI=Highest(RVI, Length); MinRVI=Lowest(RVI, Length); If MaxRVI<>MinRVI then Value3=(RVI-MinRVI)/(MaxRVI-MinRVI); Value4=(4*Value3+3*Value3[1]+2*Value3[2]+Value3[3])/10; Value4=2*(Value4-0.5); if LastBarOnChart thenbeginprint("Incorrect value: Stoc2RVI:");print(Value4);end;RVI = Value4;Trigger = .96*(Value4[1]+.2);StochasticRVI = 1;`

Any help is appreciated.

Thanks,
E

TJ
Posts: 6572
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 968 times
Been thanked: 1901 times

### Re: Converting Indicator to Function: Return results don't m  [SOLVED]

Tip #1:

Do not use generic variable names.

ie. Value1, Value2, condition1, etc.,

Variable names are free; you can create a meaningful name so that you can reference it during debug.

It will save you plenty of hair.
These users thanked the author TJ for the post:
JoshM

eneilson
Posts: 2
Joined: 22 Feb 2011

### Re: Converting Indicator to Function: Return results don't m

Do not use generic variable names.

That was indeed a good tip. Switching out the ValueX variable names with locally declared ones fixed the problem and the data matches. Do the scratch variables have scope outside of the function?

It might be a good idea to add a warning in the docs about the potential of these scratch variables to be corrupted.

Thanks,
E