Dark Theme
Light Theme
Trading Articles
Article Categories
Article Tools
Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to
register in order to view the content of the threads and start contributing to our community.
It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Multicharts Indicator Days over/under SMA
Updated May 29, 2015
trending_up
3,949 views
thumb_up
2 thanks given
group
2 followers
forum
16 posts
attach_file
0 attachments
Multicharts Indicator Days over/under SMA
May 22nd, 2015, 10:37 AM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hello,
I am a new Multicharts user and PowerLanguage is my first programming language,
so it is very hard at the Moment
This is my first code and I don´t have a solution, maybe somebody can help me.
I try to track the Days when the Price is over or under the EMA and plot it under the Chart.
For Example Price is 3 Days over EMA then +3 -> next Day Price is under EMA = -1
This code works only for Days over EMA or under EMA, but not together.
Where is the mistake or what can I do better?
Code
{
Mai 2015
This Indicator tracks the Days when price is over or under the SMA.
}
inputs:
Price ( Close ),
Length ( 21 ),
Displace ( 0 ) ;
variables:
var0 ( 0 ),
move ( 0 ),
OverEmav (0),
UnderEmav (0);
var0 = Average( Price, Length );
//if Close > var0 then OverEmav = 1;
//if OverEmav >= 1 then move = move +1;
//If close < var0 then move = 0;
If close < var0 then UnderEmav = -1;
If UnderEmav <= -1 then move = move -1;
if close > var0 then move = 0;
plot1(move,"Days over/under EMA");
This should be very easy, but I have no idea how to solve it.
Thank you all.
Timo
Can you help answer these questions from other members on NexusFi?
Best Threads (Most Thanked) in the last 7 days on NexusFi
May 22nd, 2015, 11:35 AM
Posts: 2,436 since Apr 2013
Thanks Given: 484
Thanks Received: 1,629
Hi Timo,
you can add a check for the previous bar to reset the move value when there is change in the relation of price to the MA.
Code
If Close < var0 then
begin
//check if previous bar was also below the average, otherwise set the counter to -1
if close[1] < var0[1] then
move = move -1
else
move = -1;
end
else
if Close > var0 then
begin
//... do the same in here for the case where the close is above the ma
end ;
On a separate matter I would suggest to start using meaningful variable names right. Most of the build in code is not a good example in this case as it uses lots
of var1, var2 or Value1 variable names which makes code much harder to read.
Regards,
ABCTG
May 22nd, 2015, 05:08 PM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hello ABCTG,
thank you for these Tips!
It looks so easy now, but I had no idea how to solve it.
Here is my code:
Code
{
Mai 2015
This Indicator tracks the Bars over or under the SMA.
}
inputs:
Price ( Close ),
SmaLength ( 21 ),
Displace ( 0 ) ;
variables:
SMAv ( 0 ),
move ( 0 );
SMAv = Average( Price, SmaLength );
if Close < SMAv then
begin
//check if previous bar was also below the average, otherwise set the counter to -1
if close[1] < SMAv [1] then
move = move -1
else
move = -1;
end;
if Close > SMAv then
begin
//... do the same in here for the case where the close is above the ma
if close [1] > SMAv [1] then
move = move +1
else
move = +1;
end;
plot1(move,"Days over/under SMA");
If Plot1 >=1 Then
SetPlotColor(1, Green);
If Plot1 <= -1 Then
SetPlotColor(1, red);
It works and I used better variables. Or could I do something else better?
Thanks again for your help.
Best regards,
Timo
May 22nd, 2015, 06:57 PM
Posts: 2,436 since Apr 2013
Thanks Given: 484
Thanks Received: 1,629
Hi Timo,
it looks good to me. I don't see any chances that would gain something.
As you increment and decrement a counter (the variable move in this case) in your
code you might at some point stumble over something similar like
Which (as programmers are lazy ) is just a quicker way of writing
I just thought to point that out for the future, in case you ever come across a similar code expression.
Regards,
ABCTG
May 24th, 2015, 03:21 PM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hallo ABCTG,
thanks for your comments, really appreciate it!
Best regards,
Timo
May 27th, 2015, 06:48 AM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hello ABCTG,
maybe you can give me another tip how to solve this problem.
I would like to track the Highest Value of the Days Over or Under the SMA .
I guess I can do this with arrays, correct?
I wrote this code to test programming with arrays, but I dont get a solution for my problem:
Code
inputs:
Price ( Close ),
SmaLength ( 21 ),
Displace ( 0 ) ;
variables:
ArrayCounter ( 0 ),
SMAv ( 0 ),
HiMove ( 0 ),
LoMove ( 0 ),
move ( 0 );
Array:
HiOverSMA[1000] ( 0 ),
LoUnderSMA[1000] ( 0 );
SMAv = Average( Price, SmaLength );
If currentbar = 1 then ClearPrintLog;
if Close < SMAv then
begin
//check if previous bar was also below the average, otherwise set the counter to -
Value1 = text_new( D, T, L, NumtoStr( BarNumber, 0 ));
text_setstyle( Value1, 2, 0);
ArrayCounter = ArrayCounter +1;
If ArrayCounter = 1001 then ArrayCounter = 1;
LoUnderSMA[ ArrayCounter ] = BarNumber;
Print( D, T," ArrayCounter ", ArrayCounter," Barnumber ", BarNumber," , ",LoUnderSMA[ 1 ]," , ",LoUnderSMA[ 2 ]," , ",LoUnderSMA[ 3 ]," , ",LoUnderSMA[ 4 ]," , ",LoUnderSMA[ 5 ]);
if close [1] < SMAv [1] then
move = move -1
else
move = -1;
end;
In Excel I had this study in 5 minutes, but in Multicharts I am not able to get these information in
hours, very frustrating.
What is the best way to do this in Multicharts?
Thank you.
Best regards,
Timo
May 27th, 2015, 07:13 AM
Posts: 2,436 since Apr 2013
Thanks Given: 484
Thanks Received: 1,629
Hi Timo,
what exactly do you mean with Highest Value of the Days Over or Under the SMA ?
If you are just looking for one value (the highest high of the days above the SMA) you could use a variable and always update it to a higher high. When you want to do this for every swing where price is above the SMA simply reset this variable's value when the price is below the SMA.
Something that should get you started is this code:
Code
if Price > SMA then
begin
if High > TrackingHi then
TrackingHi = High;
end
else
TrackingHi = -999999;
Regards,
ABCTG
May 27th, 2015, 10:23 AM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hi ABCTG,
no, I try to store the highest Value of the indicator. For Example:
Here the SPY was 73 Days over the SMA , and I try to store this Indicator value,
not a high of a price.
Best regards,
Timo
May 27th, 2015, 10:33 AM
Posts: 2,436 since Apr 2013
Thanks Given: 484
Thanks Received: 1,629
Hi Timo,
that's no different than storing a price value. You'd use a variable and update its value when the "move" value is larger than the stored value. Simply adapt the code from my previous post.
Regards,
ABCTG
May 27th, 2015, 10:35 AM
Aschaffenburg
Experience: Beginner
Platform: NinjaTrader, Multicharts
Trading: ES
Posts: 15 since Feb 2012
Thanks Given: 4
Thanks Received: 1
Hi ABCTG,
thanks for your really fast answer! I try it later this evening.
Thanks again.
Timo
Last Updated on May 29, 2015