NexusFi: Find Your Edge


Home Menu

 





Multicharts Indicator Days over/under SMA


Discussion in MultiCharts

Updated
    1. trending_up 3,814 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 16 posts
    2. attach_file 0 attachments




 
Search this Thread

Multicharts Indicator Days over/under SMA

  #1 (permalink)
 venatrix 
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

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Deepmoney LLM
Elite Quantitative GenAI/LLM
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
36 thanks
NexusFi site changelog and issues/problem reporting
25 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

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

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 venatrix 
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

Started this thread Reply With Quote
Thanked by:
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

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
 
Code
move += 1 ;
Which (as programmers are lazy ) is just a quicker way of writing
 
Code
move = move + 1 ;
I just thought to point that out for the future, in case you ever come across a similar code expression.

Regards,
ABCTG

Follow me on Twitter Reply With Quote
  #5 (permalink)
 venatrix 
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

Started this thread Reply With Quote
  #6 (permalink)
 venatrix 
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

Started this thread Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

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

Follow me on Twitter Reply With Quote
  #8 (permalink)
 venatrix 
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

Started this thread Reply With Quote
  #9 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

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

Follow me on Twitter Reply With Quote
  #10 (permalink)
 venatrix 
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

Started this thread Reply With Quote




Last Updated on May 29, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts