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

How to change strategy parameters from within code

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

    How to change strategy parameters from within code

    Hello. I'm sorry, if this thread duplicate same question. I search on forum before ask.

    So, I want to change strategy parameters from within code if it runs on specific instruments.

    I made simple strategy - look at screenshot, please.

    And I tried solution proposed by koganam from here: http://ninjatrader.com/support/forum...12&postcount=1

    But parameters are changing only in Initialise().
    In OnBarUpdate() and in StrategyAnalyzer nothing changing.

    Is the way to do this correct?
    Attached Files
    Last edited by fx.practic; 10-09-2015, 06:09 AM.
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    Thank you for your post.

    You would need to check what instrument is being ran against in Initialize() and then change the parameter.

    For example:
    Code:
            protected override void Initialize()
            {			
    			if(Instrument.FullName == "AAPL")
    				myInput0 = 12;
            }

    Comment


      #3
      Hello, Patrick. Thank You for quick answer.

      Did You see screenshot? I did exactly as You said - don't work.

      I made short video, look it please: http://screencast.com/t/FxumpLJy1

      SmaPeriod == 30 in Initilize(), but SmaPeriod == 5 again in OnBarUpdate();
      Last edited by fx.practic; 10-09-2015, 06:56 AM.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        Hello fx.practic,

        Thank you for your update.

        Try the simple line as a test (without the switch and cases).
        Code:
                protected override void Initialize()
                {			
        			if(Instrument.FullName == "AAPL")
        				myInput0 = 12;
                }

        Comment


          #5
          Done: http://screencast.com/t/Qz9DW3PeFEP

          Instrument name checking properly in both versions, because SmaPeriod was changed in Initialize()
          Attached Files
          Last edited by fx.practic; 10-09-2015, 07:43 AM.
          fx.practic
          NinjaTrader Ecosystem Vendor - fx.practic

          Comment


            #6
            And I also add Print( "OnStartUp(): SmaPeriod = "+ SmaPeriod ); to OnStartUp()
            Attached Files
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment


              #7
              Originally posted by fx.practic View Post
              Hello. I'm sorry, if this thread duplicate same question. I search on forum before ask.

              So, I want to change strategy parameters from within code if it runs on specific instruments.

              I made simple strategy - look at screenshot, please.

              And I tried solution proposed by koganam from here: http://ninjatrader.com/support/forum...12&postcount=1

              But parameters are changing only in Initialise().
              In OnBarUpdate() and in StrategyAnalyzer nothing changing.

              Is the way to do this correct?
              Very different situation.

              For what you want to do, you would have to put your switch statement in OnStartUp(), not Initialize().

              Further, as you are setting/overriding the smaPeriod value in code, there is really no need to have it in the PropertyGrid anyway.

              Comment


                #8
                Thank You,

                I hope there was another way.

                As I see, I have now to re-assign new values to parameters twice: in Initialize() before adding indicators
                Code:
                Add( SMA( SmaPeriod ));
                and in OnStartUp() to change values for run-time;

                Also, I'd like to see changed values in grid to have control - without control mistakes are guarantee. May be I'll draw fixed text on chart with actual parameters.
                fx.practic
                NinjaTrader Ecosystem Vendor - fx.practic

                Comment


                  #9
                  Originally posted by fx.practic View Post
                  Thank You,

                  I hope there was another way.

                  As I see, I have now to re-assign new values to parameters twice: in Initialize() before adding indicators
                  Code:
                  Add( SMA( SmaPeriod ));
                  and in OnStartUp() to change values for run-time;

                  Also, I'd like to see changed values in grid to have control - without control mistakes are guarantee. May be I'll draw fixed text on chart with actual parameters.
                  Code:
                  Add( SMA( SmaPeriod ));
                  is not really an assignation: it is more a declaration. Something has to be assigned because that is what the class demands in its constructor. What value is assigned is really not important in your context. You could just as easily have used:
                  Code:
                  Add( SMA(10));
                  with no effect to your processing, as that value is going to be overridden anyway. Context does matter.

                  Further, because of how you are writing the code, the PropertyGrid does not have any control. You are better off using something else as a viewer, like the Output Window, or writing the value as text to the chart. Using the PropertyGrid as a viewer requires 2 clicks to reveal a modal window, which once open, allows you to do nothing else until closed.
                  Last edited by koganam; 10-09-2015, 11:29 AM.

                  Comment


                    #10
                    Originally posted by koganam View Post
                    Code:
                    Add( SMA( SmaPeriod ));
                    is not really an assignation: it is more a declaration. Something has to be assigned because that is what the class demands in its constructor. What value is assigned is really not important in your context. You could just as easily have used:
                    Code:
                    Add( SMA(10));
                    with no effect to your processing, as that value is going to be overridden anyway. Context does matter.
                    Can I override SMA(10) in OnStartUp() ?
                    Or how can I change it parameters propeties?
                    I don't understand the nature of this code at all:
                    Code:
                    Add( SMA(10) );
                    I tried
                    Code:
                    Add( SMA(10) );
                    SMA(10) = SMA(20);
                    but get error:
                    "The left-hand side of an assignment must be a variable, property or indexer."
                    I don't understand: if
                    SMA(10) is object - why I can't reassign it?

                    Then I tried this:
                    Code:
                    [COLOR=Blue]private [/COLOR]SMA Sma_Obj;
                    Sma_Obj = SMA(10);
                    Add( Sma_Obj );
                    Sma_Obj = SMA( 20 );

                    This compiled, but on-screen sma period didn't changed.
                    Are there any correct ways to do it?

                    Last edited by fx.practic; 10-09-2015, 02:14 PM.
                    fx.practic
                    NinjaTrader Ecosystem Vendor - fx.practic

                    Comment


                      #11
                      Originally posted by fx.practic View Post
                      Can I override SMA(10) in OnStartUp() ?
                      Or how can I change it parameters propeties?
                      I don't understand the nature of this code at all:
                      Code:
                      Add( SMA(10) );
                      I tried
                      Code:
                      Add( SMA(10) );
                      SMA(10) = SMA(20);
                      but get error:
                      "The left-hand side of an assignment must be a variable, property or indexer."
                      I don't understand: if
                      SMA(10) is object - why I can't reassign it?

                      Then I tried this:
                      Code:
                      [COLOR=Blue]private [/COLOR]SMA Sma_Obj;
                      Sma_Obj = SMA(10);
                      Add( Sma_Obj );
                      Sma_Obj = SMA( 20 );

                      This compiled, but on-screen sma period didn't changed.
                      Are there any correct ways to do it?

                      What that code does it to create a new object and assign it to the old object, which technically should be kosher. I do not know where you are checking the period for Sma_Obj and not getting the expected value, so I cannot comment. If you want to change the period of the Sma_Obj, then just change it.
                      Code:
                      Sma_Obj.Period = 20;

                      Comment


                        #12
                        Oh, This is so obviously!
                        O!!!!!!!!!!!!!!
                        Thanks a lot!

                        I leave here simple working strategy example - if someone will need it.
                        Attached Files
                        Last edited by fx.practic; 10-10-2015, 03:09 AM.
                        fx.practic
                        NinjaTrader Ecosystem Vendor - fx.practic

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by bortz, 11-06-2023, 08:04 AM
                        47 responses
                        1,603 views
                        0 likes
                        Last Post aligator  
                        Started by jaybedreamin, Today, 05:56 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post jaybedreamin  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        18 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        12 views
                        0 likes
                        Last Post Javierw.ok  
                        Working...
                        X