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

Price Inputs or Variables

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

    Price Inputs or Variables

    I want to use the strategy builder to compare the close of different days and I want the number of days back for one of them to be an input or variable. The strategy builder only allows numbers for days back. How to I insert an input or variable in lieu of a number?

    #2
    Hello harr5754,

    Welcome to the NinjaTrader forums!

    Are you referring to the 'Bars ago' value used for series objects?

    Unfortunately, the Strategy Builder is not quite sophisticated enough to properly ensure that CurrentBars for each series is greater than the value of the input to prevent indexing errors at run time. This means that inputs cannot be used for 'Bars ago' values.

    This would require unlocking the script and coding by hand.

    public int BarsBack { get; set; }

    if (CurrentBars[BarsInProgress] > BarsBack)
    Print(Time[0].ToString() + " | Close[" + BarsBack.ToString() + "]: " + Close[BarsBack].ToString()));

    As a workaround in the Strategy Builder you could make a condition set for each bars ago value you want to use, and have the input value compared in each condition set. So in set 1 the condition checks for the input to be 1, and then uses 1 as the bars ago value in the series comparison.

    A similar approach is used for optimizing start times in the Strategy Builder, linked below.

    Hi guys, how can I optimize a time period? I know how to optimize Stop and Take Profit sizes in the Strategy Analyzer Optimization (Variables), but I want to have the time period (it doesn't matter which Data Series or symbol I use) which is the most profitable with entry time and exit time. Example: I want to make an entry
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      I was trying to use the Strategy Builder to enable the variables in the following code:


      // NOTE: This code NOT OPTIMIZED to enhance readability
      protected override void OnBarUpdate()
      {
      if (CurrentBar < BarsRequiredToTrade)
      return;

      // Input Variables
      int entry2_sl = 5, entry2_slx = 20;
      // entry2_sl = short term trend length
      // entry2_slx = longer term trend length

      if (Close[0] < Close[entry2_sl] && Close[0] > Close[entry2_slx])
      EnterLong();
      if (Close[0] > Close[entry2_sl] && Close[0] < Close[entry2_slx])
      EnterShort();
      }

      I think the variables have to be defined and I'm not sure how to do that.

      Thanks,
      Ben

      Comment


        #4
        Hello Ben,

        The code you have, using variables for barsAgo indexes, cannot be generated or input into the Strategy Builder due to its limitations.

        Inputs and variables can be added on the Inputs and variables page of the Strategy Builder.
        https://ninjatrader.com/support/help...ariablesScreen

        Below is a link to a forum post with helpful information about getting started with NinjaScript and C# and includes a link to the Strategy Builder 301 training video.
        https://ninjatrader.com/support/foru...040#post786040

        In an unlocked script variables can be declared within the scope of the class (outside of any methods).

        private int entry2_sl;

        If you want these to be inputs from the user, use the public access modifier and the NinjaScriptProperty attribute.


        [NinjaScriptProperty]
        public int entry2_sl
        { get; set; }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea,

          I just want to optimize these variables from the Strategy Analyzer. I couldn't tell from your link if I should declare it with a NinjaScriptProperty (your 2nd snippet) attribute or declare it as private (your 1st snippet). Not familiar with Xml Serialized.

          Regards,
          Ben

          Comment


            #6
            Hello Ben,

            For an optimizable input this will need to be public using the NinjaScriptProperty attribute.

            The XmlIgnore() attribute is used for object types that are easily converted to strings (i.e. anything other than string, int, bool, double) such as Brushes.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks Chelsea,

              I was able to optimize the variables for bars back using the [NinjaScriptProperty] snippet you provided. I am now trying to introduce SetProfitTarget and Set StopLoss exits by copying code produced by the Strategy Builder but I am getting a CS0103 error saying the targets and stops do not exist in the current context. It seems I have left something out that would provide the proper context.

              Regards,
              Ben

              Comment


                #8
                Hello Ben,

                That would imply a variable or method has not been declared.

                What is the full error message? This includes a filename, full error message, then a code, then importantly a line number, and a column number. Click on an error message, hit Ctrl + c on the keyboard to copy, and then click in your reply and Ctrl + v to paste.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Here is the error message:
                  Entry02WithTrendPullback.cs The name 'Target' does not exist in the current context CS0103 54 7
                  Here is where I was trying

                  namespace NinjaTrader.NinjaScript.Strategies.KJTrading_9and7
                  {
                  public class Entry02WithTrendPullback : Strategy
                  {
                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  Description = @"Entry #2 - With Trend Pullback";
                  Name = "Entry02WithTrendPullback";
                  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 = 21;
                  // Disable this property for performance gains in Strategy Analyzer optimizations
                  // See the Help Guide for additional information
                  IsInstantiatedOnEachOptimizationIteration = false;
                  Target = 1000;

                  Comment


                    #10
                    Hello harr5754,

                    Where is the Target variable declared in the script?

                    (If you look at strategy builder code, Inputs are declared in the #region Properties within the scope of the class, and private variables are declared at the top within the scope of the class.)

                    May I also confirm you have watched the NinjaScript Editor 401 video I have linked in post #4 that shows copying code from one script to another?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks, Chelsea. Got it working once I found all the declarations hiding in the Properties #region. Your Ninjascript 401 webinar was very helpful. Exactly what I'm trying to do.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by WHICKED, Today, 12:45 PM
                      2 responses
                      16 views
                      0 likes
                      Last Post WHICKED
                      by WHICKED
                       
                      Started by GussJ, 03-04-2020, 03:11 PM
                      15 responses
                      3,272 views
                      0 likes
                      Last Post xiinteractive  
                      Started by Tim-c, Today, 02:10 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Taddypole, Today, 02:47 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post Taddypole  
                      Started by chbruno, 04-24-2024, 04:10 PM
                      4 responses
                      51 views
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Working...
                      X