NexusFi: Find Your Edge


Home Menu

 





basic color coding according to direction of a moving average


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one stephenszpak with 4 posts (1 thanks)
    2. looks_two Fat Tails with 2 posts (4 thanks)
    3. looks_3 Big Mike with 1 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 4,260 views
    2. thumb_up 5 thanks given
    3. group 2 followers
    1. forum 7 posts
    2. attach_file 4 attachments




 
Search this Thread

basic color coding according to direction of a moving average

  #1 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356

Hi

I'm just looking to color code momentum bars (created by eman).

The code I wrote below either doesn't work as written or there is
something more I need to do in the indicator script.

As you can see all that is needed is to compare the current value of a
simple moving average of value 2, to what it was in the previous bar. Is it more (yellow),
or the same (red), or less(blue)?



if (SMA(2)[0] > SMA(2)[1])

BarColor=Color.Yellow;

else if (SMA(2)[0]==SMA(2)[1])

BarColor=Color.Red;


else if (SMA (2)[0]<SMA(2)[1])
BarColor = Color.Blue;

Screenshots below show original graph and graph modified in Paint to show what I'm going for.
The cyan moving average is a 2 simple.

Attached Thumbnails
Click image for larger version

Name:	original graph.png
Views:	220
Size:	89.7 KB
ID:	39554   Click image for larger version

Name:	modified  in Paint.png
Views:	242
Size:	129.8 KB
ID:	39555  
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
Build trailing stop for micro index(s)
Psychology and Money Management
 
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102



stephenszpak View Post
Hi

I'm just looking to color code momentum bars (created by eman).

The code I wrote below either doesn't work as written or there is
something more I need to do in the indicator script.

As you can see all that is needed is to compare the current value of a
simple moving average of value 2, to what it was in the previous bar. Is it more (yellow),
or the same (red), or less(blue)?



if (SMA(2)[0] > SMA(2)[1])

BarColor=Color.Yellow;

else if (SMA(2)[0]==SMA(2)[1])

BarColor=Color.Red;


else if (SMA (2)[0]<SMA(2)[1])
BarColor = Color.Blue;

Screenshots below show original graph and graph modified in Paint to show what I'm going for.
The cyan moving average is a 2 simple.


Maybe the problem is caused by checking two double values for equality. As both values are calculated, there may be rounding errors, which cause the oscillations.

Try to replace

 
Code
if (SMA(2)[0]==SMA(2)[1])
with

 
Code
if (Math.Abs(SMA(2)[0]-SMA(2)[1]) < 0.25*TickSize
If the difference is smaller than 0.25*TickSize, the two values must be identical.


If you think about it, there is an even easier solution. Comparing SMA(2)[0] and SMA(2)[1] is the same as
comparing Close[0] with Close[2], as both SMA(2)[0] and SMA(2)[1] are calculated by using Close[1], so you simply can throw it away and your code would look like this


 
Code
if (Close[0] > Close[2])
     BarColor=Color.Yellow;
else if (Close[0]==Close[2])
     BarColor=Color.Red;
else if (Close[0]<Close[2])
BarColor = Color.Blue;
In this case the comparison Close[0]== Close[2] should work, as the values are not calculated but just collected from the datafeed. Not sure but you can try.

Reply With Quote
  #4 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356

Thanks Fat Tails. I'll get back to you on this. The code was so simple I
thought sure it would work.

Started this thread Reply With Quote
  #5 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356

You forgot a parenthesis in the first line, but it still doesn't work. I never
program so I don't remember/know about the generic code that NT has
built in. Not even sure if it is important or not.

Some of my program is below:

protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
CalculateOnBarClose=true;
Overlay = true;
PriceTypeSupported=false;

}



if (Close[0] > Close[2])
BarColor=Color.Yellow;
else if (Close[0]==Close[2])
BarColor=Color.Red;
else if (Close[0]<Close[2])
BarColor = Color.Blue;


#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}

[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion

Started this thread Reply With Quote
  #6 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102

Works here with all NinjaTrader default types.

I cannot try it though on these funny Momentum No-Gap bars, as I do not have them installed.

Attached Files
Elite Membership required to download: PaintBarSMA2.zip
Reply With Quote
  #7 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,398 since Jun 2009
Thanks Given: 33,173
Thanks Received: 101,537

Add some Print() statements and check your output window to find the flawed logic.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356

Ok Fat Tails it works! Thanks for the program.

Attached Thumbnails
Click image for larger version

Name:	program works!.png
Views:	273
Size:	90.1 KB
ID:	39567  
Started this thread Reply With Quote
Thanked by:




Last Updated on May 29, 2011


© 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