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

Monthly Open Price Indicator

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

    Monthly Open Price Indicator

    Hello,

    I'm trying to build a very simple indicator that will visually print a line indicating the open price of the current (or at the time, current) month on a chart.

    The problem I'm having is that the current month line is correct but the line for the previous month's don't represent the then current month's open but rather the prior month's open. To say it another way, if I put this indicator on a current live chart, the line for today properly shows today's open which is also the month open but yesterday's line isn't last month's open but rather January's.

    Code so far below. Would appreciate any help!

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class Monthly_Open : Indicator
        {
            private double monthOpenPrice;        
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "Monthly_Open";
                    Calculate                                    = Calculate.OnPriceChange;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = false;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
    
                    AddPlot(new Stroke(Brushes.LightGray, 1), PlotStyle.Line, "Monthly_Open");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Month, 1);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress == 1)
                {                
                    monthOpenPrice = Open[0];
                }
    
                if (BarsInProgress == 0)
                {            
                    Monthly_Open[0] = monthOpenPrice;
                }
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Monthly_Open
            {
                get { return Values[0]; }
            }
            #endregion
        }
    }

    #2
    Hello fiddich,
    Thanks for your post.

    I checked on my end and I believe this would be expected due to the the way the monthly bar is closing. Visually you can just set the Displacement property to "1" and it will fix the plot. If you wanted to use that value for anything else you would need to create some logic to track the amount of days in each month and then change the bars ago value used for the calculation. Perhaps by comparing DateTime values?

    Does the following snippet achieve what you are after?

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Calculate = Calculate.OnBarClose;
            IsOverlay = true;
            BarsRequiredToPlot = 0;
            Displacement = 1;
            AddPlot(Brushes.Red,"PreviousMonthOpenPlot");
        }
        else if (State == State.Configure)
        {
            AddDataSeries(BarsPeriodType.Month, 1);
        }
    }
    
    protected override void OnBarUpdate()
    {
        if(CurrentBars[1]<1)return;
        Value[0] = Opens[1][0];
    }
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Josh.

      The displacement didn't seem to do it. One of the things I'm seeking to do is have it show the month open price while that month bar is still open, so in current time. I thought that using Calculate.OnPriceChange would allow me to achieve that but it only seems to work for the current live bar.

      Perhaps a picture would help - I've attached here a snippet from the current GC daily chart. The current bar shows the indicator printing at 1315.5, which is correct because it's today's open and thus this month's current open. But the previous value of the indicator for February says 1290.5 which is actually the January month open, not February. I would expect the line for last month to be at 1326, February 1st's open price.

      Does that make more sense?

      Thanks.
      Attached Files

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      59 views
      0 likes
      Last Post DynamicTest  
      Started by ScottWalsh, Today, 06:52 PM
      4 responses
      36 views
      0 likes
      Last Post ScottWalsh  
      Started by olisav57, Today, 07:39 PM
      0 responses
      7 views
      0 likes
      Last Post olisav57  
      Started by trilliantrader, Today, 03:01 PM
      2 responses
      21 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      10 views
      0 likes
      Last Post cre8able  
      Working...
      X