Indicator using averages as an input  [SOLVED]

Questions about MultiCharts and user contributed studies.
synonym
Posts: 70
Joined: 20 Feb 2009
Has thanked: 10 times
Been thanked: 3 times

Indicator using averages as an input

Postby synonym » 07 Jun 2018

Hi
I'm a completely new to MC and coding and i'd really appreciate it if someone could advise on how i'd make an indicator that takes the 3 sma of the high, the 5sma of low and the 5 sma of close, adds them together and then divides by 3?

Also, one that does similar, but where the final figure would be subtracted from an sma and then plotted as a histogram and centred on 0?

Thank you
Syn

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Indicator using averages as an input  [SOLVED]

Postby JoshM » 11 Jun 2018

I'm a completely new to MC and coding and i'd really appreciate it if someone could advise on how i'd make an indicator that takes the 3 sma of the high, the 5sma of low and the 5 sma of close, adds them together and then divides by 3?
If I understand you correctly, that would look like:

Code: Select all

Variables:
highAvg(0),
lowAvg(0),
closeAvg(0),
plotValue(0);

highAvg = AverageFC(High, 3);
lowAvg = AverageFC(Low, 5);
closeAvg= AverageFC(Close, 5);

plotValue = (highAvg + lowAvg + closeAvg) / 3;

plot1(plotValue);
(I didn't really follow your second question.)

synonym
Posts: 70
Joined: 20 Feb 2009
Has thanked: 10 times
Been thanked: 3 times

Re: Indicator using averages as an input

Postby synonym » 11 Jun 2018

Thank you for your reply. That's really helpful JoshM. I'll check your site out., it's looks looks very interesting and incredibly helpful.

Sorry, my 2nd question could have been clearer. It was about a similar indicator, but where the sma is subtracted rather than added. I can see how i can easily amend the code you've kindly given me to do that.

The final points on my 2nd question about is being able to plot the result as a histogram rather than a line, which i now know is an option in the formatting indicator box when inserting it in a chart. And i was a little tired when i initially posted. If the indicator gives positive AND negative figures in it's results then the indicator will of course fluctuate around zero...which is what i want.

Thanks again Josh. I really appreciate it.
Cheers
Syn.

asyx
Posts: 27
Joined: 30 Apr 2011
Location: Europe
Has thanked: 57 times
Been thanked: 6 times

Re: Indicator using averages as an input

Postby asyx » 14 Jul 2018

Hello,
I have a similar question.

I do not want e.g. today to be included but only the days before.
I tried to take the average of high(1), high(2),....etc. this works, but is not really flexible.
That is way I tried : high(1), length (3) ....for example but this does not work.
Then I ran out of ideas. Could you please point e in the right direction?

Thanking you,

asyx

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Indicator using averages as an input

Postby TJ » 14 Jul 2018

Hello,
I have a similar question.

I do not want e.g. today to be included but only the days before.
I tried to take the average of high(1), high(2),....etc. this works, but is not really flexible.
That is way I tried : high(1), length (3) ....for example but this does not work.
Then I ran out of ideas. Could you please point e in the right direction?

Thanking you,

asyx

not sure what you are talking about...

please make examples.

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Indicator using averages as an input

Postby Xyzzy » 15 Jul 2018

I do not want e.g. today to be included but only the days before.
I tried to take the average of high(1), high(2),....etc. this works, but is not really flexible.
That is way I tried : high(1), length (3) ....for example but this does not work.
What you want is the the average as of yesterday. To do that, you can use:

Code: Select all

Average(Close, 100)[1]
The [1] portion indicates that MC should use the average that was calculated yesterday, not today's average. You can also use other values to "go back in time" further.

asyx
Posts: 27
Joined: 30 Apr 2011
Location: Europe
Has thanked: 57 times
Been thanked: 6 times

Re: Indicator using averages as an input

Postby asyx » 31 Jul 2018

Thanks, Xyzzy and TJ.

e.g. compare today´s high with average of last 5 highs.

Graphically I could solve this with "Displace". I overlookd that - thanks.

If coding I have the following so far. More flexible than my previous attempts but it is still quite static:

Code: Select all


inputs: Price( High ), Length( 5 ) ;

variables:
var0( 1 ),
var1( 0 ),
var2( 0 ),
var3( 0 ) ;

var1 = var0;
var0 = AverageFC( Price, Length ) ;
var2 = High[0];
var3 = ((var2 / var1)-1)*100;

Plot1(var3,"Comp.");
...with static I mean, what if I want to compare today with the average of 5 highs 10 days ago or average of last year etc.....

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Indicator using averages as an input

Postby Xyzzy » 31 Jul 2018

Note that you can use the "[x]" notation for functions too. You can get rid of var0 and use:

Code: Select all


var1 = AverageFC( Price, Length )[1];
var2 = High[0];
var3 = ((var2 / var1)-1)*100;
Then, var1 is set to the average from the prior bar.


Return to “MultiCharts”