Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

BarColor

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    BarColor

    I'm trying to change the color of the bars using a couple of simple rules. When I load it up the bars don't change color. Can you help me?


    --------START CODE --------
    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    [Description("Gives brighter green when going up for sure, gives brighter red when going down the tube")]
    public class MoreDepthBars : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    double barClosePrice = Close[0];
    double barOpenPrice = Open[0];
    double barHighPrice = High[0];
    double barLowPrice = Low[0];

    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    if (Open[0] < Close[0])
    {BarColor = Color.Green;}
    if (Open[0] < Close[0] && Close[0] > High[0])
    {BarColor = Color.Lime;}
    if (Open[0] > Close[0])
    {BarColor = Color.Maroon;}
    if (Open[0] > Close[0] && Close[0] < Low[0])
    {BarColor = Color.Red;}
    if (Open[0] == Close[0])
    {BarColor = Color.White;}
    Plot0.Set(Close[0]);
    }

    #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("")]
    [Category("Parameters")]
    public int MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private MoreDepthBars[] cacheMoreDepthBars = null;

    private static MoreDepthBars checkMoreDepthBars = new MoreDepthBars();

    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    public MoreDepthBars MoreDepthBars(int myInput0)
    {
    return MoreDepthBars(Input, myInput0);
    }

    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    public MoreDepthBars MoreDepthBars(Data.IDataSeries input, int myInput0)
    {
    checkMoreDepthBars.MyInput0 = myInput0;
    myInput0 = checkMoreDepthBars.MyInput0;

    if (cacheMoreDepthBars != null)
    for (int idx = 0; idx < cacheMoreDepthBars.Length; idx++)
    if (cacheMoreDepthBars[idx].MyInput0 == myInput0 && cacheMoreDepthBars[idx].EqualsInput(input))
    return cacheMoreDepthBars[idx];

    MoreDepthBars indicator = new MoreDepthBars();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.MyInput0 = myInput0;
    indicator.SetUp();

    MoreDepthBars[] tmp = new MoreDepthBars[cacheMoreDepthBars == null ? 1 : cacheMoreDepthBars.Length + 1];
    if (cacheMoreDepthBars != null)
    cacheMoreDepthBars.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheMoreDepthBars = tmp;
    Indicators.Add(indicator);

    return indicator;
    }

    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.MoreDepthBars MoreDepthBars(int myInput0)
    {
    return _indicator.MoreDepthBars(Input, myInput0);
    }

    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    public Indicator.MoreDepthBars MoreDepthBars(Data.IDataSeries input, int myInput0)
    {
    return _indicator.MoreDepthBars(input, myInput0);
    }

    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.MoreDepthBars MoreDepthBars(int myInput0)
    {
    return _indicator.MoreDepthBars(Input, myInput0);
    }

    /// <summary>
    /// Gives brighter green when going up for sure, gives brighter red when going down the tube
    /// </summary>
    /// <returns></returns>
    public Indicator.MoreDepthBars MoreDepthBars(Data.IDataSeries input, int myInput0)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.MoreDepthBars(input, myInput0);
    }

    }
    }
    #endregion
    --------END CODE --------

    #2
    Hi Infinity,

    You will need to debug your code as per this tip here: http://www.ninjatrader-support.com/v...ead.php?t=3418

    Also, generally you shouldn't declare these variables in the Initialize() method:
    Code:
    protected override void Initialize()
            {
                double barClosePrice = Close[0];
                double barOpenPrice = Open[0];
                double barHighPrice = High[0];
                double barLowPrice = Low[0];
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hey Josh,

      it's been so long since I programmed anything I've forgotten all the basics! Where do you declare the variables?

      Basically the code is meant to change the color of the bar depending on the relationship between the current bar and previous bar's open, close, high and low.

      if (Open[0] < Close[0])
      {BarColor = Color.Green;}
      if (Open[0] < Close[0] && Close[0] > High[0])
      {BarColor = Color.Lime;}
      if (Open[0] > Close[0])
      {BarColor = Color.Maroon;}
      if (Open[0] > Close[0] && Close[0] < Low[0])
      {BarColor = Color.Red;}
      if (Open[0] == Close[0])
      {BarColor = Color.White;}

      Comment


        #4
        Generally you would just declare them as privates inside the "Variables" section which is above the Initialize() method.
        Code:
        private double var1 = 2.5;
        In your case you don't even need to use variables. To check the previous bar's Close you can just do Close[1].
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        383 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        1 view
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Started by judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Working...
        X