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

Random number

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

    Random number


    I'm not a programmer, but I'm very involved haha. A long time ago I created a random number indicator, now I need it (I deleted it), now I do not know how I did it. number = random generated number.
    I have tried with:

    int number = Random(1,5);
    int number = rand(1,5);
    int number = Random ()% 100 + 1;
    int number = rand()% 100 + 1;

    However, he tells me that "Rand" does not exist in the context, I think it is because "it is not" the "stdlib.h". I repeat I do not know much about programming. And "Random" I do not know what it is, it tells me that "it uses a variable that does not". But I remember, some time ago I managed to do the random number, someone could help me. ? Very thanks and regards.

    #2
    Originally posted by samir View Post
    I'm not a programmer, but I'm very involved haha. A long time ago I created a random number indicator, now I need it (I deleted it), now I do not know how I did it. number = random generated number.
    I have tried with:

    int number = Random(1,5);
    int number = rand(1,5);
    int number = Random ()% 100 + 1;
    int number = rand()% 100 + 1;

    However, he tells me that "Rand" does not exist in the context, I think it is because "it is not" the "stdlib.h". I repeat I do not know much about programming. And "Random" I do not know what it is, it tells me that "it uses a variable that does not". But I remember, some time ago I managed to do the random number, someone could help me. ? Very thanks and regards.
    Code:
    Random rnd = new Random();
    int month = rnd.Next(1, 13); // creates a number between 1 and 12
    int dice = rnd.Next(1, 7); // creates a number between 1 and 6
    int card = rnd.Next(52); // creates a number between 0 and 51

    Comment


      #3
      Hello samir,

      Thank you for your post.

      koganam has hit the nail on the head here. I'd just like to add that there's publicly available documentation from Microsoft on the Random class in C#:

      Represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness.


      Please let us know if we may be of further assistance to you.
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        Very thanks.

        Comment


          #5
          Thank you very much, a separate question. Does anyone know in which area of the documentation should I read in order to make the indicator work in the strategy tester and also in the historical candles?
          The behavior of the indicator does not comply with what I want, which is "The higher the value of the input, the higher the probability of being a 2" (between 1 and 99), 100 is always 2 (probability indicator, or at least that's what I want to do) (and it works well on new candles that appear live) But, in historical candlesticks and strategy tester almost always the indicator is flat and suddenly it has minimal variation, in the new real live candles the indicator moves a lot and works according to what I want.

          Code:
                          Probabilidad                    = 10;
                          AddPlot(Brushes.GreenYellow, "Resultado");
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                                      Random rnd = new Random();
                              int numerogenerado = rnd.Next(1, 101);
                              if (numerogenerado <= Probabilidad)
                      {
                          Value[0] = 2;
                      }
                      else
                      {
                          Value[0] = 1;
                      }
          I appreciate your help, thank you very much. Greetings.
          Last edited by samir; 06-22-2019, 07:43 PM.

          Comment


            #6

            Comment


              #7
              Originally posted by samir View Post
              Thank you very much, a separate question. Does anyone know in which area of the documentation should I read in order to make the indicator work in the strategy tester and also in the historical candles?
              The behavior of the indicator does not comply with what I want, which is "The higher the value of the input, the higher the probability of being a 2" (between 1 and 99), 100 is always 2 (probability indicator, or at least that's what I want to do) (and it works well on new candles that appear live) But, in historical candlesticks and strategy tester almost always the indicator is flat and suddenly it has minimal variation, in the new real live candles the indicator moves a lot and works according to what I want.

              Code:
               Probabilidad = 10;
              AddPlot(Brushes.GreenYellow, "Resultado");
              }
              }
              
              protected override void OnBarUpdate()
              {
              Random rnd = new Random();
              int numerogenerado = rnd.Next(1, 101);
              if (numerogenerado <= Probabilidad)
              {
              Value[0] = 2;
              }
              else
              {
              Value[0] = 1;
              }
              I appreciate your help, thank you very much. Greetings.
              Move the declaration of rnd out of OnBarUpdate(). Declare it at the class level.

              As written you are generating a new random number on each pass instead of using the one that already exists.
              Last edited by koganam; 06-22-2019, 11:00 PM. Reason: Corrected grammar.

              Comment


                #8
                Extremely thanks kogaman.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Stanfillirenfro, Today, 07:23 AM
                8 responses
                23 views
                0 likes
                Last Post Stanfillirenfro  
                Started by DayTradingDEMON, Today, 09:28 AM
                2 responses
                15 views
                0 likes
                Last Post DayTradingDEMON  
                Started by navyguy06, Today, 09:28 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by cmtjoancolmenero, Yesterday, 03:58 PM
                8 responses
                32 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by helpwanted, Today, 03:06 AM
                2 responses
                22 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Working...
                X