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

Setting Default Values for Parameters

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

    Setting Default Values for Parameters

    Hello NT guys,

    I've been able to call out parameters in my Ninjascripts that show up in the Strategy Analyzer. I would now like to set up default values and still be able to change them in the Strategy Analyzer. The following code is how I'm getting the parameters to show up:

    Code:
    [NinjaScriptProperty]
    [Description("imethod")]
    [Category("Parameters")]
    public int imethod
    { get; set; }
    
    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;
    Would appreciate any guidance.

    Thanks,
    Ben

    #2
    Hello harr5754,

    Thanks for your post.

    You could set up a NinjaScript property like in the code you shared. To set a default value for the NinjaScript properties, you would assign a value to the property in OnStateChange() when the state reaches State.SetDefaults.

    For example, if you are creating an int property named imethod and you want to set a default value of 5 for imethod, you would do the following.

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            imethod = 5;
        }
    }
    This could be seen by setting up user input variables in the Strategy Builder and clicking the 'View code' button the view the code that accomplishes this.

    See this help guide for more information about creating user input variables in the Strategy Builder: https://ninjatrader.com/support/help...ariablesScreen

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      It still doesn't want to show default values when I pull it up in Strategy Analyzer. He is more of the script I'm working on just in case I buggered it up elsewhere:

      Code:
       public class EntryTest01TradeThePullback : Strategy
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Entry #1 – Trade The Pullback";
      Name = "EntryTest01TradeThePullback";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      IsInstantiatedOnEachOptimizationIteration = true;
      [COLOR=#2980b9][B]entry1_sl = 5;
      entry1_slx = 20;
      bse = 5;
      imethod = 1;[/B][/COLOR]
      }
      else if (State == State.Configure)
      {
      
      }
      }
      
      [NinjaScriptProperty]
      [Description("entry1_sl")]
      [Category("Parameters")]
      public int entry1_sl
      { get; set; }
      
      [NinjaScriptProperty]
      [Description("entry1_slx")]
      [Category("Parameters")]
      public int entry1_slx
      { get; set; }
      
      [NinjaScriptProperty]
      [Description("bse")]
      [Category("Parameters")]
      public int bse
      { get; set; }
      
      [NinjaScriptProperty]
      [Description("imethod")]
      [Category("Parameters")]
      public int imethod
      { get; set; }
      
      protected override void OnBarUpdate()
      {
      if (CurrentBar < BarsRequiredToTrade)
      return;
      
      if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
      EnterLong();
      if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
      EnterShort();
      
      if(imethod == 1 && BarsSinceEntryExecution() >= bse)
      {
      ExitLong();
      ExitShort();
      }
      
      //exit after specified number of bars, ONLY if position is currently profitable
      if (imethod == 2 && BarsSinceEntryExecution() >= bse
      && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency) > 0)
      {
      ExitLong();
      ExitShort();
      }
      
      //exit after specified number of bars, ONLY if position is currently losing
      if (imethod == 3 && BarsSinceEntryExecution() >= bse
      && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency) < 0)
      {
      ExitLong();
      ExitShort();
      }
      Looking forward to your response,
      Thanks,
      Ben

      Comment


        #4
        I think that there may be an issue, if you saved your workspace and exited Ninja, so as long as you don't switch strategies in Strategy Analyzer, if you had any different values before setting those default, you will still see previous values, even though script has different default values.

        If that is the case, switch to different strategy and back to this strategy, it should help. Or you could just close NT8->delete all the files in strategyanalyzerlogs folder->Reload NT8. Unless, you saved a default template with different values than what are in the script, you should be good to go.

        Comment


          #5
          I tried deleted all the files in the strategyanalyzerlogs folder and I am stilling getting "0's" coming up for the parameters instead of the ones in the script. I have not saved any default templates either. Strange.

          Comment


            #6
            Originally posted by harr5754 View Post
            I tried deleted all the files in the strategyanalyzerlogs folder and I am stilling getting "0's" coming up for the parameters instead of the ones in the script. I have not saved any default templates either. Strange.
            I think I know what your problem is. You don't have Properties region.

            YOu should have this code in your script:

            Code:
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="entry1_sl")]
            public int entry1_sl
            { get; set; }
            
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="entry1_slx")]
            public int entry1_slx
            { get; set; }
            
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="bse")]
            public int bse
            { get; set; }
            
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name="imethod")]
            public int imethod
            { get; set; }
            Tell me if it helped.

            Comment


              #7
              Hello harr5754,
              Use the below example for properties:-
              Code:
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Period", Order=1, GroupName="Parameters")]
              public int Period
              { get; set; }
              Hope it helps!

              Comment


                #8
                Hi UltraNIX,

                I double checked the code I was running through the analyzer and it was different,...my mistake. It works now.

                However, I do have a question about the Properties Region you mentioned. Should I be putting all the blocks starting with "[NinjaScriptProperty]" down in the properties region?

                I appreciate yours and Brandon's help.
                Ben

                Comment


                  #9
                  Originally posted by harr5754 View Post
                  Hi UltraNIX,

                  I double checked the code I was running through the analyzer and it was different,...my mistake. It works now.

                  However, I do have a question about the Properties Region you mentioned. Should I be putting all the blocks starting with "[NinjaScriptProperty]" down in the properties region?

                  I appreciate yours and Brandon's help.
                  Ben
                  I think it's enough to just paste my code. And if you added more parameters, be sure, you have corresponding [NinjaScriptProperty] properties. If you don't, you will get 0's again

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Jonker, Today, 01:19 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jonker
                  by Jonker
                   
                  Started by futtrader, Today, 01:16 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post futtrader  
                  Started by Segwin, 05-07-2018, 02:15 PM
                  14 responses
                  1,789 views
                  0 likes
                  Last Post aligator  
                  Started by Jimmyk, 01-26-2018, 05:19 AM
                  6 responses
                  838 views
                  0 likes
                  Last Post emuns
                  by emuns
                   
                  Started by jxs_xrj, 01-12-2020, 09:49 AM
                  6 responses
                  3,294 views
                  1 like
                  Last Post jgualdronc  
                  Working...
                  X