Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator code not plotting

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

    Indicator code not plotting

    I am not from a C# background.

    Can somebody please explain why the below is not plotting (neither Value[0] nor closeGBPUSD5m[0] work).



    Code:
        public class zUSDtrend : Indicator
        {
            //private double closeEURUSD5m;
            private Series<double> closeGBPUSD5m;
            //private double closeUSDJPY5m;
            //private double closeEURUSD60m;
            //private double closeGBPUSD60m;
            //private double closeUSDJPY60m;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                 = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "zUSDtrend";
                    Calculate                                   = Calculate.OnBarClose;//PriceChange;
                    IsOverlay                                   = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    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;
                    SMA5mPeriod                 = 8;
                    SMA60mPeriod                    = 8;
                    AddPlot(Brushes.Orange, "closeGBPUSD5m");
                    //AddPlot(new Stroke(Brushes.Goldenrod, 2), PlotStyle.Bar, "closeGBPUSD5m");
                    //AddPlot(Brushes.Orange, "closeEURUSD5m");
                    //AddPlot(Brushes.Orange, "closeUSDJPY5m");
                    //AddPlot(Brushes.Orange, "closeGBPUSD60m");
                    //AddPlot(Brushes.Orange, "closeEURUSD60m");
                    //AddPlot(Brushes.Orange, "closeUSDJPY60m");
                  
                }
                else if (State == State.Configure)
                {
                    AddDataSeries("GBPUSD", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
                    //AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
                    //AddDataSeries("USDJPY", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
                    //AddDataSeries("GBPUSD", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
                    //AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
                    //AddDataSeries("USDJPY", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
                }
                else if (State == State.DataLoaded)
                {
                    closeGBPUSD5m = new Series<double>(this);
                }
            }
            protected override void OnBarUpdate()
            {
                //Add your custom indicator logic here.
                //if (CurrentBar < 1 + SMA5mPeriod)
                //{
                    closeGBPUSD5m[0] = Closes[1][0];
                    Print("XXX  " + Closes[1][0]);
                    //Value[0] = Closes[1][0];
                  
                    //closeEURUSD5m = Closes[2][0];
                    //closeUSDJPY5m = Closes[3][0];
                    //closeGBPUSD60m = Closes[4][0];
                    //closeEURUSD60m = Closes[5][0];
                    //closeUSDJPY60m = Closes[6][0];
                //}
            }
            #region Properties​
    Attached Files
    Last edited by Apm123; 09-01-2024, 08:27 AM.

    #2
    When testing you should use a faster bar frequency so that you don't have to wait minutes for OnBarUpdate to fire. And put your code in a try catch block... catch (Exception ex) {Print(ex.message + ex.stacktrace);}

    Also, at the start, and between every line in OnBarUpdate, use Print("a"), Print("b"), etc, so you know the last line which processed successfully.

    I see that it is commented out, but you have: if (CurrentBar < 1 + SMA5mPeriod)
    I think that should be > instead of <
    If you use <, the line should be followed by: return; else {code to process}​

    Here is my best tip for any C# programmer with less than stellar capabilities... Google your inquiry leading with "w3schools" for C# tips because most other sites are way too esoteric and send you down needless rabbit holes. Leave the bunnies to do their thing in private. That's what I always say . w3schools gets me on track over 90% of the time. And I use Google to get to the right help pages on Ninjatrader. Search using "Ninjatrader help" and the important keywords. Google is much better than searching within the site. (any site really... use site name plus keywords and you're there). Everybody trashes google, but it still works great for me. Bing sends me to the marketplace. They always assume I want to buy stuff.

    Comment


      #3
      I know it takes a little time, but if you're not using Studio to debug, Print is really all we have. Use it.

      My errors always surprise me. Print("name of variable: " + variable.ToString()); Often a property or variable that I think should have been set is not set or not set as intended, and that often triggers an error or an unintended outcome. Any variable or property that you can get ToString() you can Print.

      I clutter up a method with 10 or 12 Prints or more, find the problem, and am on my way. Be sure to remove them or comment them out because they will keep printing and you want your Output Window to stay clean.

      Comment


        #4
        Thanks c2injacator.

        With CurrentBar im only just learning now that NT has two referencing systems (right to left and left to right....)....and yes printing and looking at control panel log seems to be the way to go.

        I have since got this one plotting.... Very much a feel-in-the dark exercise when compared to SQL/DAX/VBA/others....just keep bumping around and getting bruised until you come out alive...hopefully....

        Comment


          #5
          Exactly. It took me a long time to get a grasp of the basics. Some things seem obvious in retrospect, but that is why we keep plugging away, to reinforce whatever works. My main challenge is really just staying focused. Some aspects of this work are tedious and so staying on track is a big deal for me. I do my best work in the earlier part of the day and with a good night's sleep... I've flip-flopped from my earlier days of being a hard core night person. Congratulations on getting "this one plotting"... wtg!!!

          You're right... the Print function is really our only tool to find our mistakes. You can use Print(Time[0].ToLongTimeString()); if you are getting a huge number of items, to get to exactly where something might be failing.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          650 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          370 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          109 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          574 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          577 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X