Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Newbie Question

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

    Newbie Question

    If I made this code in builder strategy but want to be able to run it through over 300 stocks in market analyzer. I need the code to give me a 1 or -1.

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 256)
    return;

    // Set 1
    if ((CrossAbove(EMA1, EMA2, 1))
    && (CrossAbove(EMA2, EMA3, 1))
    && (CrossAbove(EMA1, Close, 1)))
    {
    trueFalse = 1;
    }
    else if (trueFalse == 0)

    Plot0.Set(trueFalse);


    But I get error plot doesn't exist in the current content

    #2
    protectedoverridevoidOnStateChange()
    {
    if(State==State.SetDefaults)
    {
    Name="Examples Indicator";
    // Lines are added to the Lines collection in order
    AddPlot(Brushes.Orange,"Plot1");// Stored in Plots[0]
    AddPlot(Brushes.Blue,"Plot2");// Stored in Plots[1]
    }
    }

    // Dynamically change the primary plot's color based on the indicator value
    protectedoverridevoidOnBarUpdate()
    {
    if(Value[0]>70)
    {
    Plots[0].Brush=Brushes.Blue;
    Plots[0].Width=2;
    }
    else
    {
    Plots[0].Brush=Brushes.Red;
    Plots[0].Width=2;
    }
    }

    Comment


      #3
      Hello GCO77,

      Thanks for your first post in the forums!

      The code you are showing would not be produced by the NT8 Strategy Builder.

      Is the code you are showing from the NinjaTrader7 Strategy wizard and are you trying to code this for NinjaTrader7?

      Comment


        #4
        Originally posted by NinjaTrader_PaulH View Post
        Hello GCO77,

        Thanks for your first post in the forums!

        The code you are showing would not be produced by the NT8 Strategy Builder.

        Is the code you are showing from the NinjaTrader7 Strategy wizard and are you trying to code this for NinjaTrader7?
        It was an older code I had for NinjaTrade 7 and since now I have NinjaTrader 8 I am trying to make a simple cross over code that can put 1 or -1 so it can be used to as a scanner for market analyzer. I am sure on the new strategy builder how to add this option of just indicating 1 or -1 and transferring it over to an custom indicator that can be used in market analyzer.
        Last edited by GCO77; 01-28-2020, 08:54 AM.

        Comment


          #5
          Hello GCO77,

          Thanks for clarifying.

          As member Emma1 advised, you would need to use AddPlot() in state.SetDefaults. However to read this in the Market analyzer, you would need to create a corresponding public output that the market analyzer can use. This would be created in #region properties.

          So for example in State.SetDefaults you might have:

          AddPlot(Brushes.Transparent, "SignalPlot"); // no need to show the plot so use Transparent


          In OnBarUpdate() you would need:

          if ((CrossAbove(EMA1, EMA2, 1))
          && (CrossAbove(EMA2, EMA3, 1))
          && (CrossAbove(EMA1, Close, 1)))
          {
          trueFalse = 1;
          }
          else if (trueFalse == 0);

          SignalPlot[0] = trueFalse; // assign to the plot.



          In #Region Properties (this would be outside of the OnBarUpdate braces { }) you would need:

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

          Comment


            #6
            Originally posted by Emma1 View Post
            protectedoverridevoidOnStateChange()
            {
            if(State==State.SetDefaults)
            {
            Name="Examples Indicator";
            // Lines are added to the Lines collection in order
            AddPlot(Brushes.Orange,"Plot1");// Stored in Plots[0]
            AddPlot(Brushes.Blue,"Plot2");// Stored in Plots[1]
            }
            }

            // Dynamically change the primary plot's color based on the indicator value
            protectedoverridevoidOnBarUpdate()
            {
            if(Value[0]>70)
            {
            Plots[0].Brush=Brushes.Blue;
            Plots[0].Width=2;
            }
            else
            {
            Plots[0].Brush=Brushes.Red;
            Plots[0].Width=2;
            }
            }

            Thanks for helping. Cheers

            Comment


              #7
              Originally posted by NinjaTrader_PaulH View Post
              Hello GCO77,

              Thanks for clarifying.

              As member Emma1 advised, you would need to use AddPlot() in state.SetDefaults. However to read this in the Market analyzer, you would need to create a corresponding public output that the market analyzer can use. This would be created in #region properties.

              So for example in State.SetDefaults you might have:

              AddPlot(Brushes.Transparent, "SignalPlot"); // no need to show the plot so use Transparent


              In OnBarUpdate() you would need:

              if ((CrossAbove(EMA1, EMA2, 1))
              && (CrossAbove(EMA2, EMA3, 1))
              && (CrossAbove(EMA1, Close, 1)))
              {
              trueFalse = 1;
              }
              else if (trueFalse == 0);

              SignalPlot[0] = trueFalse; // assign to the plot.



              In #Region Properties (this would be outside of the OnBarUpdate braces { }) you would need:

              [Browsable(false)]
              [XmlIgnore]
              public Series<double> SignalPlot
              {
              get { return Values[0]; }
              }
              Thanks it complied properly but it surely isn't working properly.

              Click image for larger version  Name:	Screen.jpg Views:	0 Size:	45.4 KB ID:	1085381

              It should display 1 but not matter what it displays 0
              Last edited by GCO77; 01-28-2020, 10:36 AM.

              Comment


                #8
                Hello GCO77,

                Thanks for your post.

                The signal will only be value for the duration of the bar, so if your data series in the market analyzer column is like 1 minute bars then the cross would only show for 1 minute as the cross occurs. This could be even less if you are using the Calculate mode of Calculate.OnEachtick or Calculate.OnPriceChange.

                I would suggest adding a number of instruments to the analyzer to as that would increase the odds of seeing the 1. (You might also set cell conditions to set the cell color if a 1 or 0)

                Comment


                  #9
                  trueFalse= 1, that means ' trueFalse is assigned to 1.' and below that trueFalse is equal to 0; check this https://ninjatrader.com/support/help...sic_syntax.htm

                  Comment


                    #10
                    Hello GCO77,

                    As member Emma1 rightly advises your logic is in need of a slight change.

                    Where your code shows:

                    {
                    trueFalse = 1;
                    }
                    else if (trueFalse == 0);


                    You would want to have this as:

                    {
                    trueFalse = 1;
                    }
                    else
                    {
                    trueFalse = 0; // set to zero if coss conditions are not true.
                    }


                    I misread what you had as reflecting that

                    Comment


                      #11
                      Originally posted by NinjaTrader_PaulH View Post
                      Hello GCO77,

                      As member Emma1 rightly advises your logic is in need of a slight change.

                      Where your code shows:

                      {
                      trueFalse = 1;
                      }
                      else if (trueFalse == 0);


                      You would want to have this as:

                      {
                      trueFalse = 1;
                      }
                      else
                      {
                      trueFalse = 0; // set to zero if coss conditions are not true.
                      }


                      I misread what you had as reflecting that
                      Thanks here is my code and I am using it on eod of data on market analyzer and it still reading as zero even when I changed to say greater than even though all moving averages have met the requirements and I see them on the market analyzer.

                      Sorry for rehashing some of my previous points.

                      Cheers


                      PHP Code:
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                          public class TripleMov : Indicator
                          {
                      
                              private EMA EMA1;
                              private EMA EMA2;
                              private EMA EMA3;
                              private double trueFalse = 0;
                      
                              protected override void OnStateChange()
                              {
                                  if (State == State.SetDefaults)
                                  {
                                      Description                                    = @"Enter the description for your new custom Indicator here.";
                                      Name                                        = "TripleMov";
                                      AddPlot(Brushes.Transparent, "SignalPlot");
                                      Calculate                                    = Calculate.OnBarClose;
                                      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;
                                  }
                                  else if (State == State.Configure)
                                  {
                                  }
                              else if (State == State.DataLoaded)
                                  {                
                                      EMA1                = EMA(Close, 15);
                                      EMA2                = EMA(Close, 20);
                                      EMA3                = EMA(Close, 45);
                                  }
                              }
                      
                      
                              protected override void OnBarUpdate()
                              {
                                  if (BarsInProgress != 0)
                      return;
                      
                      if (CurrentBars[0] < 256)
                      return;
                      
                      // Set 1
                      if ((EMA1[0] > EMA2[0])
                                       && (EMA2[0] > EMA3[0])
                                       && (EMA1[0] > Close[0]))
                      {
                      trueFalse = 1;
                      }
                      else
                      {
                      trueFalse = 0; // set to zero if coss conditions are not true.
                      } // assign to the plot.);
                                  }    
                              #region Properties
                              [Browsable(false)]
                              [XmlIgnore()]
                              public Series<double> SignalPlot
                              {
                                  get { return Values[0]; }
                              }
                      #endregion 
                      
                      Last edited by GCO77; 01-28-2020, 09:26 PM. Reason: Forget something

                      Comment


                        #12
                        Hello GCO77,

                        Thanks for your reply.

                        It looks like you are missing assigning the variable to the plot.

                        SignalPlot[0] = trueFalse; // assign to the plot.

                        Comment


                          #13
                          Originally posted by Emma1 View Post
                          trueFalse= 1, that means ' trueFalse is assigned to 1.' and below that trueFalse is equal to 0; check this https://ninjatrader.com/support/help...sic_syntax.htm
                          thanks for the info

                          Comment


                            #14
                            Thank you as well for the question

                            Comment


                              #15
                              Originally posted by NinjaTrader_PaulH View Post
                              Hello GCO77,

                              Thanks for your reply.

                              It looks like you are missing assigning the variable to the plot.

                              SignalPlot[0] = trueFalse; // assign to the plot.

                              Sorry I still can't get this to work in any way. I reinstalled my old Ninjatrader 7 and found some old scripts and they work perfectly. Transferring these to Ninjatrader 8 is a bit of a headache.

                              Basically at wits ends I have tried everything to get this to work and it hasn't.
                              Old Ninja Trader 7 Script for context.


                              PHP Code:
                              // This namespace holds all indicators and is required. Do not change it.
                              namespace NinjaTrader.Indicator
                              {
                                  /// <summary>
                                  /// Enter the description of your new custom indicator here
                                  /// </summary>
                                  [Description("Enter the description of your new custom indicator here")]
                                  public class EMACrossover : 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()
                                      {
                                          Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                                          Overlay                = false;
                                      }
                              
                                      /// <summary>
                                      /// Called on each bar update event (incoming tick)
                                      /// </summary>
                                      protected override void OnBarUpdate()
                                      {
                                         if(  EMA(50)[0] > EMA(100)[0] )
                              {
                                Plot0.Set(1);
                              }
                              else if (  EMA(100)[0] > EMA(50)[0]  )
                              {
                                Plot0.Set(-1);
                              }
                                      }
                              
                                      #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
                                  } 
                              
                              Last edited by GCO77; 01-30-2020, 12:44 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              93 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              48 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              31 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              34 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X