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

Variable (Int) referenced through a Double

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

    Variable (Int) referenced through a Double

    Having trouble with the following code example. In this example, I would like my Int for StdDev (Variable1) to be adjusted for the value returned for ATR


    double atr = ATR(Variable2)[0];

    double stddev = StdDev(Variable1 + atr)[0];

    ---

    I have tried converting to an int as such, but it gives me a circular reference and crashes:

    double atr = Variable1 + ATR(Variable2)[0];
    int variable3 = (int)atr

    double donchianchannel= StdDev(variable3)[0];

    #2
    Hello contrlr,

    Welcome to the forums!

    You are correct in that you can cast a double as an int to have the value converted and assigned to a variable of type int. There will be data loss, however, as integers do not contain decimal values.

    I believe there may be some confusion behind what you are trying to accomplish.

    double atr = ATR(Variable2)[0];

    double stddev = StdDev(Variable1 + atr)[0];
    Here you are getting the ATR return value (type double) and then you are adding it to Variable1 (type int?) When you are only giving StdDev() one argument, it assumes the argument is of type int and is the period used for the Standard Deviation calculation. The calculation then uses the default price type as you have not specified your own input. With your syntax, you are adding a double to an int. The result would not be intended for this method.

    What period are you expecting to use for StdDev() when you say you "would like my Int for StdDev (Variable1) to be adjusted for the value returned for ATR?"

    The syntax for these methods are publicly available in our help guide. I will provide a link to the ATR syntax and the StdDev() syntax for your reference. We also have some documentation that covers C# syntax used with NinjaScripts that may be worth reviewing.

    ATR - https://ninjatrader.com/support/help..._range_atr.htm

    StdDev - https://ninjatrader.com/support/help...ion_stddev.htm

    Programming Concepts - https://ninjatrader.com/support/help...g_concepts.htm

    I look forward to being of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      ATR and StdDev were examples used strictly for name space. I am not concerned with int rounding or calculating to the nearest whole number, but more so that my variable for StdDev could be altered based on the value of another indicators value.

      I understand I could recode each base indicator to accept double values, but for my purposes, there are multiple indicators I'd like to use to evaluate for alteration of my Variable1, and a simple nested conversion would save time.

      So I am looking to combine my Variable1 (Int) with the value of some Indicator (Double) into one referable Int for use in the base Indicator (ex StdDev)

      Comment


        #4
        Hello contrlr,

        Thanks for your reply.

        Type casting (which you have already demonstrated and I have copied below) is what you are looking for. I've added a semicolon to end the line which I believe was merely a typo while copying your code.

        double atr = Variable1 + ATR(Variable2)[0];
        int variable3 = (int)atr;
        A publicly available resource on type casting can be found here: https://stackoverflow.com/questions/...ing-in-c-sharp
        JimNinjaTrader Customer Service

        Comment


          #5
          I think I have located the problem. In my actual model there is one more variable, Shown below as Variable 4 (I checked assigning it both double and int). When I go and optimize this variable, I get a crash. However, optimizing Variable1 and Variable2 do not give me a crash, and they give different results.

          double atr = Variable1 + (ATR(Variable2)[0] / Variable4);
          int variable3 = (int)atr;

          double donchianchannel= StdDev(variable3)[0];

          I've used this language before, with no problems. The only difference is in this instance it is later conversion to an int.

          Comment


            #6
            Originally posted by contrlr View Post
            I think I have located the problem. In my actual model there is one more variable, Shown below as Variable 4 (I checked assigning it both double and int). When I go and optimize this variable, I get a crash. However, optimizing Variable1 and Variable2 do not give me a crash, and they give different results.

            double atr = Variable1 + (ATR(Variable2)[0] / Variable4);
            int variable3 = (int)atr;

            double donchianchannel= StdDev(variable3)[0];

            I've used this language before, with no problems. The only difference is in this instance it is later conversion to an int.
            I do not see how you can take the Standard Deviation of one variable, but that aside, your problem probably lies elsewhere anyway. You are dividing by Variable4, without accounting for a possible division by zero. Anywhere in code that you do division, you must arrange to ensure that you handle a possible division by zero.

            Comment


              #7
              The Variable is for the period, I want to add/subtract from the period based off another indicators value. Min allowed for Variable4 is 1

              Comment


                #8
                Hello contrlr,

                Without the full code, we are only limited to guessing the cause of the issue you are experiencing and if the variables used in calculation have valid values.

                Modifying your code to use whole numbers in place of the variables used shows the NinjaScript "works" and does not crash.
                Code:
                double atr = 1 + (ATR(14)[0] / 2); 
                int variable3 = (int)atr;
                Print(variable3);
                double donchianchannel= StdDev(variable3)[0];
                The sure-fire way to resolve these issues is to debug the code by adding prints for the variables used in calculation, and to see how far the code gets before it crashes at a problematic section of code.

                I've included a link to our forum thread on debugging tips: https://ninjatrader.com/support/foru...ead.php?t=3418

                Other development tips can be found here: https://ninjatrader.com/support/foru...splay.php?f=31
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jim, I was able to work around this. I added a Math.Round, but I still needed to separate the division out into another double, not really sure why:

                  double atr = (ATR(Variable2)[0]);
                  double atr1 = atr/Variable4;
                  double atr2 = Math.Round(atr1);
                  int atr3= (int)atr2;
                  int variable3 = Variable1 + atr3;

                  double donchianchannel= StdDev(variable3)[0];
                  Last edited by contrlr; 10-05-2017, 03:48 PM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post quantismo  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  8 responses
                  168 views
                  0 likes
                  Last Post jeronymite  
                  Started by cre8able, Today, 04:22 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X