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

Custom function outside of OnBarUpdate()

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

    Custom function outside of OnBarUpdate()

    Hello!

    I have a custom function in the region Helpers.

    I have:
    HTML Code:
    protected override void OnBarUpdate()
    {
      myCust1[pos] = myCustom(EMA(Close, 21), pos);
      Print("myCust1" + " Size " + ":  " + myCust1);
    }
    HTML Code:
    #region Helpers
     private double myCustom(double price, int pos)
      {
          double val = Math.Exp(-0.7*3.14159)
          double CD = val*price + myCust1[pos+1] + myCust1[pos+2];
          return(CD);            
      }
     #endregion​

    A Print() function in OnBarUpdate() pups out the following error:
    myCust1 Size : NinjaTrader.NinjaScript.Series`1[System.Double]
    I was expecting to have values of myCust1, but nothing like that is appearing.

    Could someone please points out where I am mistaking?

    Thanks in advance.
    Last edited by Stanfillirenfro; 11-23-2023, 08:36 AM.

    #2
    Originally posted by Stanfillirenfro View Post
    Hello!

    I have a custom function in the region Helpers.

    I have:
    HTML Code:
    protected override void OnBarUpdate()
    {
    myCust1[pos] = myCustom(EMA(Close, 21), pos);
    Print("myCust1" + " Size " + ": " + myCust1);
    }
    HTML Code:
    #region Helpers
    private double myCustom(double price, int pos)
    {
    double val = Math.Exp(-0.7*3.14159)
    double CD = val*price + myCust1[pos+1] + myCust1[pos+2];
    return(CD);
    }
    #endregion​

    A Print() function in OnBarUpdate() pups out the following error:

    I was expecting to have values of myCust1, but nothing like that is appearing.

    Could someone please points out where I am mistaking?

    Thanks in advance.
    maybe this will help: https://ninjatrader.com/support/help...ustomevent.htm
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    Comment


      #3
      Many thanks Sidlercom80 for your reply.

      The link did not help me unfortunately.

      I have already written successfully some codes by myself with custom functions in the region Helpers.
      This time, my function is a bit particular because I have the same variable
      Code:
      myCust1[x]
      in the function of the region Helpers as well as in protected override void OnBarUpdate(). I am just not figuring out right now how to get the variable out from the region Helpers to make my code funtional.

      Any help?

      Many thanks in danvance

      Comment


        #4
        Hello Stanfillirenfro,

        Your method parameters require a double as the first parameter.

        private double myCustom(double price, int pos)

        Yet when you call the method you are supplying an indicator plot series and not a double.

        myCustom(EMA(Close, 21), pos);

        Supply a double instead by providing an index to select a specific bar double value in the series.

        myCustom(EMA(Close, 21)[0], pos);
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Many thanks NinjaTrader_ChelseaB for your help.

          When I have, myCustom(EMA(Close, 21)[0], pos);​ the code works properly and I have values. But when I previously calculate a variable and pass it as parameter to the constructor, I receive an error message:
          Code:
          Conversion of double in Series not possible!
          double Smooth1 = (EMA(Close, 21)[pos]); In this case, my Smooth1 is a double
          myCustom(Smooth1, pos);

          What is wrong in this case?

          Thanks in advance!​

          Comment


            #6
            Hello Stanfillirenfro,

            May I see a screenshot of the error message and effected line of code?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NinjaTrader_ChelseaB for your message!

              Below is the piece of code which creates the problem:

              HTML Code:
              myRSI = RSI(Smooth1, 21, 3)[pos];
              myCust1[pos] = myCustom(myRSI, 21, pos);​
              The error message is attached.
              It is impossible to hoover the mouse on the line and to cash the whole line.

              Thanks again!
              Attached Files

              Comment


                #8
                Hello Stanfillirenfro,

                The myCustom method you've posted in post # 1 does not have a parameter for an indicator.

                Try myCust1[pos] = myCustom(myRSI[0], pos);​
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Many thanks NinjaTrader_ChelseaB for your reply.

                  The problem now is caused by the line:
                  Code:
                  myRSI = RSI(Smooth1, 21, 3)[pos];
                  It is written that this function has an invalid argument.
                  The first argument of the function ist calculated as followed:
                  Code:
                  double Smooth1 = (EMA(Close, 21)[pos]);
                  The fisrt argument is therfore a double.
                  In the documentation, we have the following for RSI():
                  Code:
                  // Prints the current value of a 20 period RSI using high price type
                  double value = RSI(High, 20, 3)[0];
                  Print("The current RSI value is " + value.ToString());​
                  The first argument is also a double. But what is wrong between the RSI of the documentaion and myRSI?

                  Comment


                    #10
                    Hello Stanfillirenfro,

                    There is no overload that accepts a double, int, and int as the parameters.

                    Below is a link to the help guide.


                    myRSI is a double and not a series.

                    Are you wanting to supply the EMA as a series?

                    RSI(ISeries<double> input, int period, int smooth)

                    RSI(EMA(Close, 21), 21, 2))

                    Are you trying to supply the Smooth1 as the smooth?

                    RSI(int period, int smooth)

                    RSI(21, Smooth1)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Many thanks NinjaTrader_ChelseaB for your help.

                      I had to submit the EMA as Series.

                      The problem is solved.

                      Many thanks again!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Haiasi, 04-25-2024, 06:53 PM
                      2 responses
                      16 views
                      0 likes
                      Last Post Massinisa  
                      Started by Creamers, Today, 05:32 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post Creamers  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      12 responses
                      1,785 views
                      0 likes
                      Last Post Leafcutter  
                      Started by poplagelu, Today, 05:00 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post poplagelu  
                      Started by fx.practic, 10-15-2013, 12:53 AM
                      5 responses
                      5,407 views
                      0 likes
                      Last Post Bidder
                      by Bidder
                       
                      Working...
                      X