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

Help, I need somebody...

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

    Help, I need somebody...

    Hello,

    I want to make an indicator but there is a big problem;

    the idea is that when a situation occur, like a line cross above another, a variable, well let's call it flag, equals 1.
    When an another situation occurs, the flag = -1.

    The problem is that i've got to declare this variable, typically int flag = 0;
    and when a situation on a bar a situation occurs, flag = 1 or -1, but the next bar, it equals 0 (due to the declaration).
    I want that the value of flag remains until a new situation or event.

    Is that possible? I knew that is possible in Strategy with user variable but in an indicator?

    Thank U a lot!!!

    (Sorry for my english!! )

    #2
    Hello Romeau,

    Welcome to our forums.

    A variable will use its last state. You don't have to declare a default value for the variable.
    When you are declaring the variable use:

    Code:
    int myInt;
    instead of

    Code:
    int myInt = 1;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanx but it doesn't work.
      Here is my code:


      if (Close[0]>KotHigh)
      {
      flag = 1;
      }
      if (Close[0]<KotLow)
      {
      flag = 0;
      }

      and the error:

      Indicator\Kot.cs Use of unassigned local variable 'flag' CS0165 - click for info 63 8

      I think that the two events don't appear often so there is no value for flag...

      Arrrrrr

      Comment


        #4
        Can you post the whole code segment you're using? It may be the variable is not available within the scope you're trying to access it from.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          protected override void OnBarUpdate()
          {
          double KotHigh = SMA(High,periodSma)[0] + (Aa * ATR(Bb)[0]);
          double KotLow = SMA(Close,periodSma)[0] - (Aa * ATR(Bb)[0]);

          int flag;
          double signal;






          if (Close[0]>KotHigh)
          {
          flag = 1;
          }
          if (Close[0]<KotLow)
          {
          flag = 0;
          }

          while (flag != 1 || flag != 0)
          {
          flag = 0;
          }




          if (flag==1)
          {
          signal=KotLow;
          }
          else
          {
          signal=KotHigh;
          }


          // Use this method for calculating your indicator values. Assign a value to each
          // plot below by replacing 'Close[0]' with your own formula.
          Plot0.Set(signal);

          Comment


            #6
            OOpppss, forget the last post :



            protected override void OnBarUpdate()
            {
            double KotHigh = SMA(High,periodSma)[0] + (Aa * ATR(Bb)[0]);
            double KotLow = SMA(Close,periodSma)[0] - (Aa * ATR(Bb)[0]);

            int flag;
            double signal;






            if (Close[0]>KotHigh)
            {
            flag = 1;
            }
            if (Close[0]<KotLow)
            {
            flag = 0;
            }




            if (flag==1)
            {
            signal=KotLow;
            }
            else
            {
            signal=KotHigh;
            }


            // Use this method for calculating your indicator values. Assign a value to each
            // plot below by replacing 'Close[0]' with your own formula.
            Plot0.Set(signal);

            Comment


              #7
              Move your variables declaration from OnBarUpdate() to the variables region.

              Variables region is colored in grey and you have to click the arrow to expand this section. Click the arrow and then add those declarations.


              Code:
               
              #region Variables
              // Wizard generated variables
              // User defined variables (add any user defined variables below.
              int flag;
              double signal;
              #endregion
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                It works fine!!!!!

                Thank you Ryan!!!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,605 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                8 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                4 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                13 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X