NexusFi: Find Your Edge


Home Menu

 





I need help with code to add color to candles/bars ?? -- Ninjatrader 8


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one SJunior with 6 posts (0 thanks)
    2. looks_two zt379 with 3 posts (2 thanks)
    3. looks_3 EDGE with 2 posts (2 thanks)
    4. looks_4 DavidHP with 1 posts (0 thanks)
    1. trending_up 906 views
    2. thumb_up 4 thanks given
    3. group 4 followers
    1. forum 10 posts
    2. attach_file 1 attachments




 
Search this Thread

I need help with code to add color to candles/bars ?? -- Ninjatrader 8

  #1 (permalink)
SJunior
Goiânia/Goiás/Brazil
 
Posts: 5 since Jun 2023
Thanks Given: 4
Thanks Received: 0

My knowledge in NT8 programming is very little and I need to add the following situation in the source code, but I have already tried some alternatives, but without success.

Change the candlesticks/bars chains, when:

HS = (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0]);
color Lime

LS = (d18[0] < 0 && dO18[0] > 0 && d18[0] < d38[0] && dO18[0] > d38[0]);
color red

H = (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0] && d18[0] > d208[0] && dO18[0 ] < d208[0]);
color cyan

L = (d18[0] < 0 && dO18[0] > 0 && d18[0] < d38[0] && dO18[0] > d38[0] && d18[0] < d208[0] && dO18[0 ] > d208[0]);
color magenta


=======================================================


Below is the complete and working code, but without the candles/bars coloring coding.


=====================================================


//
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion

namespace NinjaTrader.NinjaScript.Indicators.mah_Indicators
{
public class Indicators : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "SJunior";
Name = "SJunior";
IsSuspendedWhileInactive = true;

AddPlot(new Stroke(Brushes.LightPink, 4), PlotStyle.Line, "d38");
AddPlot(new Stroke(Brushes.DarkGreen, 4), PlotStyle.Line, "d208");
AddPlot(new Stroke(Brushes.Cyan, 1), PlotStyle.Bar, "d18");
AddPlot(new Stroke(Brushes.Yellow, 1), PlotStyle.Bar, "dO18");
AddPlot(new Stroke(Brushes.LightCyan, 1), PlotStyle.Line, "osc");
AddPlot(new Stroke(Brushes.Goldenrod, 1), PlotStyle.Line, "srs");
AddPlot(new Stroke(Brushes.Goldenrod, 1), PlotStyle.Line, "sri");
AddPlot(new Stroke(Brushes.Yellow, 1), PlotStyle.Line, "nvel_0");


}
}

protected override void OnBarUpdate()
{
if (CurrentBar < 20) return;

d38[0] = (((SMA(1)[0]-SMA(8)[0])/SMA(8)[0])*100);
d208[0] = (((SMA(20)[0]-SMA(8)[0])/SMA(8)[0])*100);
d18[0] = (((SMA(1)[0]-SMA(8)[0])/SMA(8)[0])*100);
dO18[0] = (((SMA(1)[1]-SMA(8)[0])/SMA(8)[0])*100);
osc[0] = (d38[0]-d208[0]);
srs[0] = (((2*StdDev(8)[0])/SMA(8)[0])*100);
sri[0] = (((-2*StdDev(8)[0])/SMA(8)[0])*100);
nivel_0[0] = 0;



if (d208[0] <= 0 && d208[0] < d208[1])
PlotBrushes[1][0] = Brushes.Green;
else if (d208[0] >= 0 && d208[0] > d208[1])
PlotBrushes[1][0] = Brushes.Red;
else if (d208[0] <= 0 && d208[0] > d208[1])
PlotBrushes[1][0] = Brushes.Magenta;
else if (d208[0] >= 0 && d208[0] < d208[1])
PlotBrushes[1][0] = Brushes.Orange;


if (osc[0] >= 0 && osc[0] > osc[1])
PlotBrushes[4][0] = Brushes.Green;
else if (osc[0] <= 0 && osc[0] < osc[1])
PlotBrushes[4][0] = Brushes.Red;
else if (osc[0] >= 0 && osc[0] < osc[1])
PlotBrushes[4][0] = Brushes.Magenta;
else if (osc[0] <= 0 && osc[0] > osc[1])
PlotBrushes[4][0] = Brushes.Orange;



}

#region Properties

[Browsable(false)]
[XmlIgnore]
public Series<double> d38
{
get { return Values[0]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> d208
{
get { return Values[1]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> d18
{
get { return Values[2]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> dO18
{
get { return Values[3]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> osc
{
get { return Values[4]; }
}


[Browsable(false)]
[XmlIgnore]
public Series<double> srs
{
get { return Values[5]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> sri
{
get { return Values[6]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> nivel_0
{
get { return Values[7]; }
}

[Browsable(false)]
[XmlIgnore]
public bool HiE
{
get; set;
}

[Browsable(false)]
[XmlIgnore]
public bool LoE
{
get; set;
}

[Browsable(false)]
[XmlIgnore]
public bool Hi
{
get; set;
}

[Browsable(false)]
[XmlIgnore]
public bool Lo
{
get; set;
}

#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private mah_Indicators.Indicators[] cacheIndicators;
public mah_Indicators.Indicators Indicators()
{
return Indicators(Input);
}

public mah_Indicators.Indicators Indicators(ISeries<double> input)
{
if (cacheIndicators != null)
for (int idx = 0; idx < cacheIndicators.Length; idx++)
if (cacheIndicators[idx] != null && cacheIndicators[idx].EqualsInput(input))
return cacheIndicators[idx];
return CacheIndicator<mah_Indicators.Indicators>(new mah_Indicators.Indicators(), input, ref cacheIndicators);
}
}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.mah_Indicators.Indicators Indicators()
{
return indicator.Indicators(Input);
}

public Indicators.mah_Indicators.Indicators Indicators(ISeries<double> input )
{
return indicator.Indicators(input);
}
}
}

namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.mah_Indicators.Indicators Indicators()
{
return indicator.Indicators(Input);
}

public Indicators.mah_Indicators.Indicators Indicators(ISeries<double> input )
{
return indicator.Indicators(input);
}
}
}

#endregion

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
NexusFi Journal Challenge - April 2024
Feedback and Announcements
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
18 thanks
The Program
18 thanks
  #2 (permalink)
 
DavidHP's Avatar
 DavidHP 
Isla Mujeres, MX
Legendary Market Wizard
 
Experience: Advanced
Platform: NinjaTrader
Broker: Ninjatrader / Optimus Futures / AmpFutures
Trading: ES / 6E / 6B / CL
Frequency: Every few days
Duration: Minutes
Posts: 1,609 since Aug 2009
Thanks Given: 11,328
Thanks Received: 2,743

You should post the request here:



Sent using the nexusfi.com mobile app

Rejoice in the Thunderstorms of Life . . .
Knowing it's not about Clouds or Wind. . .
But Learning to Dance in the Rain ! ! !
Follow me on Twitter Reply With Quote
  #3 (permalink)
 
EDGE's Avatar
 EDGE 
Saint Louis, Mo., USA
 
Experience: Advanced
Platform: NinjaTrader, Tradestation
Broker: Amp/CQG, Velocity/TT, Kinetick, TS
Trading: Anything That Moves..
Frequency: Daily
Duration: Minutes
Posts: 209 since Aug 2010
Thanks Given: 98
Thanks Received: 392



SJunior View Post
Change the candlesticks/bars chains

While NT's help guide is not perfect, it does contain a wealth of knowledge, and searchable topics..

https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?barbrushes.htm


Please be safe in this crazy world!

Reply With Quote
Thanked by:
  #4 (permalink)
SJunior
Goiânia/Goiás/Brazil
 
Posts: 5 since Jun 2023
Thanks Given: 4
Thanks Received: 0


EDGE View Post
While NT's help guide is not perfect, it does contain a wealth of knowledge, and searchable topics..


Please be safe in this crazy world!



Yes, but I couldn't do the coding of this function in my indicator.

Could you help me with this coding?

Reply With Quote
  #5 (permalink)
 
EDGE's Avatar
 EDGE 
Saint Louis, Mo., USA
 
Experience: Advanced
Platform: NinjaTrader, Tradestation
Broker: Amp/CQG, Velocity/TT, Kinetick, TS
Trading: Anything That Moves..
Frequency: Daily
Duration: Minutes
Posts: 209 since Aug 2010
Thanks Given: 98
Thanks Received: 392


SJunior View Post
Yes, but I couldn't do the coding of this function in my indicator. Could you help me with this coding?

//BarBrushes[x] where x = number of bars back

//Only if condition1 true set current bar color blue
if(yourCondition1TrueFalse) BarBrushes[0] = Brushes.Blue;

//Only if condition2 true set previous bar color yellow
if(yourCondition2TrueFalse) BarBrushes[1] = Brushes.Yellow;


Be Safe!

Reply With Quote
Thanked by:
  #6 (permalink)
 zt379 
UK London
 
Platform: NT
Posts: 2,031 since Sep 2009
Thanks Given: 1,544
Thanks Received: 1,924


SJunior View Post
Yes, but I couldn't do the coding of this function in my indicator.

Could you help me with this coding?

@SJunior

Hi.

I've attached "SJunior Bar Colours NT8"

However it seems the conditions you set out for each bar colour aren't met in order to colour the bars.

If you look at the code and reduce each bar colour criteria to it's minimum you'll see it does colour the bars.

But as you add each further criteria then the bar colours can't be generated

For example using the criteria for your HS:

 
Code
{
if (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])
{	
BarBrush =  UpColor1;
}
If you break this down in to smaller sections ie:
by moving the last ) bracket and commenting out the remainder of the line with // :
 
Code
if (d18[0] > 0) // && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])
 
Code
 if (d18[0] > 0)
will colour the bars

And then if you add the further criteria it still colours the bars but less frequently
 
Code
if (d18[0] > 0 && dO18[0] < 0 )
Then add the next criteria it colours even fewer bars and when you get the whole criteria it doesn't colour any bars
 
Code
if (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])

So unless I'm misunderstanding something the criteria you have for each bar colour is too complicated to actually generate a bar colour.

Anyway I hope this helps at least to see the code I've added in On Bar Update and Properties and hopefully you can move forward in some way with it.

Kind regards

Attached Files
Elite Membership required to download: SJunior Bar Colours NT8.zip
Reply With Quote
Thanked by:
  #7 (permalink)
SJunior
Goiânia/Goiás/Brazil
 
Posts: 5 since Jun 2023
Thanks Given: 4
Thanks Received: 0


zt379 View Post
@SJunior

Hi.

I've attached "SJunior Bar Colours NT8"

However it seems the conditions you set out for each bar colour aren't met in order to colour the bars.

If you look at the code and reduce each bar colour criteria to it's minimum you'll see it does colour the bars.

But as you add each further criteria then the bar colours can't be generated

For example using the criteria for your HS:

 
Code
{
if (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])
{	
BarBrush =  UpColor1;
}
If you break this down in to smaller sections ie:
by moving the last ) bracket and commenting out the remainder of the line with // :
 
Code
if (d18[0] > 0) // && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])
 
Code
 if (d18[0] > 0)
will colour the bars

And then if you add the further criteria it still colours the bars but less frequently
 
Code
if (d18[0] > 0 && dO18[0] < 0 )
Then add the next criteria it colours even fewer bars and when you get the whole criteria it doesn't colour any bars
 
Code
if (d18[0] > 0 && dO18[0] < 0 && d18[0] > d38[0] && dO18[0] < d38[0])

So unless I'm misunderstanding something the criteria you have for each bar colour is too complicated to actually generate a bar colour.

Anyway I hope this helps at least to see the code I've added in On Bar Update and Properties and hopefully you can move forward in some way with it.

Kind regards




Thanks for the help, it didn't work, but it helped with other details that I needed to put in the code.

I had to develop another formula for the code to work.

I also had to remove "else" to get it to work as I needed it to.

Below is the change I made.


if (ColorBars1)
{
if (closePrice > openPrice && closePrice > SMA(3)[0] && closePrice > SMA(8)[0] && openPrice < SMA(3)[0] && openPrice < SMA(8)[0])
{
BarBrush = UpColor1;
}

if (closePrice < openPrice && closePrice < SMA(3)[0] && closePrice < SMA(8)[0] && openPrice > SMA(3)[0] && openPrice > SMA(8)[0])
{
BarBrush = DnColor1;
}
if (closePrice > openPrice && closePrice > SMA(3)[0] && closePrice > SMA(8)[0] && openPrice < SMA(3)[0] && openPrice < SMA(8)[0] && closePrice > SMA(20)[0] && openPrice < SMA(20)[0])
{
BarBrush = UpColor2;
}
if (closePrice < openPrice && closePrice < SMA(3)[0] && closePrice < SMA(8)[0] && openPrice > SMA(3)[0] && openPrice > SMA(8)[0] && closePrice < SMA(20)[0] && openPrice > SMA(20)[0])
{
BarBrush = DnColor2;

Reply With Quote
  #8 (permalink)
SJunior
Goiânia/Goiás/Brazil
 
Posts: 5 since Jun 2023
Thanks Given: 4
Thanks Received: 0


EDGE View Post
//BarBrushes[x] where x = number of bars back

//Only if condition1 true set current bar color blue
if(yourCondition1TrueFalse) BarBrushes[0] = Brushes.Blue;

//Only if condition2 true set previous bar color yellow
if(yourCondition2TrueFalse) BarBrushes[1] = Brushes.Yellow;


Be Safe!




Thank's for your help, I combined your information with others and managed to solve the problem.

Reply With Quote
  #9 (permalink)
 zt379 
UK London
 
Platform: NT
Posts: 2,031 since Sep 2009
Thanks Given: 1,544
Thanks Received: 1,924

@SJunior

Glad you've made progress.

In your code you'll need to replace the instances of

closeprice with
 
Code
Close[0]

and openPrice with
 
Code
Open[0]

For example:

 
Code
if (closePrice > openPrice && closePrice > SMA(3)[0] && closePrice > SMA(8)[0] && openPrice < SMA(3)[0] && openPrice < SMA(8)[0])

should be:

 
Code
if (Close[0] > Open[0] && Close[0] > SMA(3)[0] && Close[0] > SMA(8)[0] && Open[0] < SMA(3)[0] && Open[0] < SMA(8)[0])

kind regards

Reply With Quote
Thanked by:
  #10 (permalink)
SJunior
Goiânia/Goiás/Brazil
 
Posts: 5 since Jun 2023
Thanks Given: 4
Thanks Received: 0



zt379 View Post
@SJunior

Glad you've made progress.

In your code you'll need to replace the instances of

closeprice with
 
Code
Close[0]

and openPrice with
 
Code
Open[0]

For example:

 
Code
if (closePrice > openPrice && closePrice > SMA(3)[0] && closePrice > SMA(8)[0] && openPrice < SMA(3)[0] && openPrice < SMA(8)[0])

should be:

 
Code
if (Close[0] > Open[0] && Close[0] > SMA(3)[0] && Close[0] > SMA(8)[0] && Open[0] < SMA(3)[0] && Open[0] < SMA(8)[0])

kind regards




Thank you for your help.

So that I understand, why does [0] have to be used and when do I have to change it to [1] or [2] or [3]......?

Reply With Quote




Last Updated on June 30, 2023


© 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