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

own variables

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

    own variables

    Hello,

    I would like to use my own variables to calculate prices an set "If-statements" with this
    variables. For example:

    mypricesvariable1 = (Close[2] + Close[1] + Close[0]) / 3;
    mypricesvariable2 = (Close[3] + Close[2] + Close[1]) / 3;

    If (
    mypricesvariable1 > mypricesvariable2)
    {
    Value.Set(0)
    }
    else
    Value.Set(1)
    ================================================

    How can I use this variables and how can I declare this variables as double

    For reply many thanks. :-)

    #2
    Hello chartvisor,

    It looks like you have all the code you need already you just need to define mypricesvariable1 and mypricesvariable2 as double values.

    If this is a in line check ( you only need these values for the if statement and no where else in the script) then you could define them right where they are by doing the following:

    Code:
    double mypricesvariable1  = (Close[2] + Close[1] + Close[0]) / 3;
    double mypricesvariable2 = (Close[3] + Close[2] + Close[1]) / 3;
    If (mypricesvariable1 > mypricesvariable2)
    {
    Value.Set(0)
    }
    else
    Value.Set(1)
    This would set the two variables as doubles that could be accessed in the current scope but not from anything beyond the method they currently are in.

    If you want to define a variable that will be used in the entirety of the script, you would need to define these in the variables section at the top by doing something like the following:

    Code:
    private double mypricesvariable1 = 0;
    private double mypricesvariable2 = 0;

    Then these would be set later in the OnBarUpdate or wherever you are setting these values.

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      thank you for your reply.

      I tryed it but it´s not running. In my Ouput Window (Print) is follow message

      Error on calling 'OnBarUpdate' method for indicator 'Trenddetector1H' on bar 0: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. There is not a Instance ?

      The Compiler have no errors ?

      What can I do?


      I have here my Code...


      protected override void OnBarUpdate()
      {
      Plot0.Set(Close[0]);

      double mypricesvariable1 = (Open[0] + Close[0] / 2);
      double mypricesvariable2 = (Open[59] + Close[59] / 2);
      Print(mypricesvariable1);


      if (mypricesvariable1 > mypricesvariable2)
      {
      Value.Set(1);
      }
      else
      Value.Set(0);

      }

      Comment


        #4
        Hello,

        The error you are getting is because the script is being executed when there is not enough bars available.

        You currently have Open[59] used, this would require at least 59 bars of data to be loaded before this code is executed. Another way to look at it would be on the first bar, bar 0, you are trying to access the value of Open bar -59 which does not exist.

        You could append this to the top before the rest of the code in OnBarUpdate to correct this.

        Code:
        protected override void OnBarUpdate()
        {
        if(CurrentBar < 59)
        return;
        
        Plot0.Set(Close[0]);
        
        double mypricesvariable1 = (Open[0] + Close[0] / 2);
        double mypricesvariable2 = (Open[59] + Close[59] / 2); 
        Print(mypricesvariable1);
        
        
        if (mypricesvariable1 > mypricesvariable2)
        {
        Value.Set(1);
        }
        else
        Value.Set(0); 
        
        }
        This simply checks if the current bar being processed is bar 59 or above. if it is not, then don't process below it.

        Here is the documentation on CurrentBar


        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you very much,

          to load the data of 59 bars need any time, than the valueSet comes.
          The script is running if 59 bars are loaded.

          Is this correct ?

          Comment


            #6
            Hello chartvisor,

            You are correct in thinking that the script would be started on the 59th bar. As the code currently is written nothing will work until 59 bars are reached.

            Another way doing this would be to only enclose the items that require 59 bars in this statement like this :

            Code:
            protected override void OnBarUpdate()
                    {
                        Plot0.Set(Close[0]);
            
                        if (CurrentBar >= 59)
                        {
                            double mypricesvariable1 = (Open[0] + Close[0] / 2);
                            double mypricesvariable2 = (Open[59] + Close[59] / 2);
                            Print(mypricesvariable1);
            
            
                            if (mypricesvariable1 > mypricesvariable2)
                            {
                                Value.Set(1);
                            }
                            else
                            {
                                Value.Set(0);
                            }
                        }
                    }
            This would allow your plot that is using Close[0] to plot all the way from the start of the bars but then only start processing the rest of the calculations once 59 bars is hit.

            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Yes its running. If more than 10 Bars ago I must need "if CurrentBar < ... return; "

              Thank you very much for your fast help. :-)

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by alifarahani, Today, 09:40 AM
              6 responses
              23 views
              0 likes
              Last Post alifarahani  
              Started by Waxavi, Today, 02:10 AM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by Kaledus, Today, 01:29 PM
              5 responses
              13 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Waxavi, Today, 02:00 AM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by gentlebenthebear, Today, 01:30 AM
              3 responses
              17 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X