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

Unlike statements

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

    Unlike statements

    Example

    -----------
    public class P1: Indicator ========> there is an indicator

    .............
    'instruction '
    ............
    ------------

    What is the difference between

    1)

    public class USingP1: Strategy

    Private P1 var1; ====>I declare a variable of the type above


    2)

    public class USingP1: Strategy

    Add(P1(var1,var2)); ====>I use a variable of the type above




    IN both statements recall an earlier indicator ... but which is the difference?



    Last edited by turbofib; 02-07-2014, 06:18 AM.

    #2
    turbofib, I'm sorry - you mean adding the indicator in for display via the Add()?

    var1 and var2 would be part of the indicator constructor needed for deciding which parameters to use for the call.

    Case 1 is also used to assign the indicator to a variable to be able to call it easier and more efficiently, since you just refer to the var name instead of calling the full overload again in your code.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      ok
      but Why when i call the indicator and pass the value of the variables...it give me error :

      Private P1 var1;


      Print(var1(Input,2).Num);
      // ( "2 " is variable passed because the indicator accepts 1 inputs numerical )
      // (Num is field output)

      Error:the name "var1" does not exist in the current context

      Comment


        #4
        Looks to me like you never assign the indicator to your variable?

        Here's a quick example with a basic SMA -

        Code:
        public class Test : Indicator
            {        
                private SMA sma1;
               
                protected override void Initialize()
                {
                    Overlay				= true;
                }
        				
                protected override void OnBarUpdate()
                {
                    if (CurrentBar == 0)
        	  sma1 = SMA(Close, 20);
        			
                    Value.Set(sma1[0]);
                }
                
            }
        BertrandNinjaTrader Customer Service

        Comment


          #5
          IN A NUTSHELL:

          double g1=0;
          g1=P1(2).Num;
          Print(g1); ==========================>is correct..
          it run correctly

          Private P1 var1;
          Print(var1(2).Num; ============> is not correct...give me an error

          Comment


            #6
            OK ...i understand my error..

            Thanks you

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              Looks to me like you never assign the indicator to your variable?

              Here's a quick example with a basic SMA -

              Code:
              public class Test : Indicator
                  {        
                      private SMA sma1;
                     
                      protected override void Initialize()
                      {
                          Overlay                = true;
                      }
                              
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBar == 0)
                    sma1 = SMA(Close, 20);
                          
                          Value.Set(sma1[0]);
                      }
                      
                  }


              i ask a question :
              when the following instruction "sma1 = SMA(Close, 20);" i can put it in "Initialize" instead in Onbarupdate?

              what changes? I have examples where 'indifferent where I put it .. gives me the same result

              I understand if I put it in Initialize is executed only 1 time ..

              In onbarupdate runs in each bar---then is more efficient algorithm in the first case

              Is correct?



              I
              Last edited by turbofib; 03-03-2014, 03:43 PM.

              Comment


                #8
                Hi turbofib,

                It looks like the first bar of historical data is available at startup.

                I think adding that to OnStartUp() would be a better idea.


                Also, you are using a period of 20 but on the first bar. That would be the same as just using the Close price as there is only going to be one bar in the dataseries..
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  with OnStartUp() do you mean
                  OnBarUpdate()?


                  i dont' undertand you last sentence:
                  "Also, you are using a period of 20 but on the first bar. That would be the same as just using the Close price as there is only going to be one bar in the dataseries.. "

                  you can explain it better?
                  Last edited by turbofib; 03-03-2014, 05:34 PM.

                  Comment


                    #10
                    Hello turbofib,

                    Below is a link to the help guide on OnStartUp().
                    http://www.ninjatrader.com/support/h.../onstartup.htm


                    The SMA is an average (Simple Moving Average). The period will be the number of bars used for the average. So if the period is 20, 20 bars should be used for the average. That is if there are 20 bars of data.

                    If you call SMA(Close, 20)[0] with only 1 bar of data (as in CurrentBar is 0 like at startup) then only 1 bar will be used for the average. In other words it will not really be an average. If you are doing this at startup SMA(Close, 20)[0] will always equal Close[0].

                    Try printing the following in OnBarUpdate():
                    if(CurrentBar == 0)
                    Print(SMA(Close, 20)[0]+" - "+Close[0]);
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      yes...i try it and SMA(Close, 20)[0=Close[0]

                      but i don't understand that :

                      "
                      OnstartUp :

                      This method is called only once immediately prior to the start of your script processing its logic in OnBarUpdate(),
                      "

                      But the instruction of the plot is call to every candle ... not just the first candle ...
                      Then I see him more logical in
                      OnBarUpdate()


                      or not?

                      Comment


                        #12
                        Originally posted by turbofib View Post
                        i ask a question :
                        when the following instruction "sma1 = SMA(Close, 20);" i can put it in "Initialize" instead in Onbarupdate?

                        what changes? I have examples where 'indifferent where I put it .. gives me the same result

                        I understand if I put it in Initialize is executed only 1 time ..

                        In onbarupdate runs in each bar---then is more efficient algorithm in the first case

                        Is correct?



                        I
                        You declared your named instance of the SMA. You now need to intialize it ONCE. If you put the initialization statement in OBU, you are reinitializing your named instance each time the barUpdate event is handled. You need to initialize your named instance in either Initialize(), OnStartUp(), or delimited to (CurrrentBar == 0).

                        Comment


                          #13
                          Hi turbofib,
                          Originally posted by turbofib View Post
                          i ask a question :
                          when the following instruction "sma1 = SMA(Close, 20);" i can put it in "Initialize" instead in Onbarupdate?

                          what changes? I have examples where 'indifferent where I put it .. gives me the same result

                          I understand if I put it in Initialize is executed only 1 time ..

                          In onbarupdate runs in each bar---then is more efficient algorithm in the first case

                          Is correct?
                          You were understanding this correctly (mostly). The Initialize() is run when the indicators window is opened, when an indicator instance is added to a chart and when you click OK on the indicator window. In other words it runs 3 times when you are adding an indicator.

                          OnBarUpdate is run at the close of each bar.

                          You had asked if it would be more efficient to set sma1 = SMA(Close, 20)[0] in Initialize() or OnBarUpdate.
                          This depends on what you are trying to do.

                          If you set this variable in Initialize() that would cause it to be run once only for the first bar and it would not keep updating. In other words its set once and done.
                          That being said, it would be more efficient to put it in OnStartUp() vs Initialize().

                          If you are wanting to set this value on every bar, then it will need to go in OnBarUpdate.

                          Below are a few links to the help guide:
                          Initialize() - http://www.ninjatrader.com/support/h...initialize.htm
                          OnStartUp() - http://www.ninjatrader.com/support/h.../onstartup.htm
                          OnBarUpdate() - http://www.ninjatrader.com/support/h...nbarupdate.htm
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            thanks you all.... you always explain the concept in a comprehensive manner

                            Comment


                              #15
                              if I add an indicator whose parameters change at every bar .. need to put it in onbarupdate ..

                              Ex:
                              ..
                              protected override void OnBarUpdate()
                              {
                              ...
                              sma1 = SMA(Close/CurrentBar, 20);
                              ..
                              }

                              Is correct?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X