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

Need Function Advice

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

    Need Function Advice

    Hello NT,

    I'm trying to build a simple indicator that does something to the effect:

    "If the date is 13-Mar-14 color that bar green."

    I'm sure there are functions to do this in Ninjascript. Would appreciate your help.

    Thanks
    Gennaker

    #2
    Hello Gennaker,

    Yes, you can compare the date of the bar (Time[0]) with a date time object in a branching command (if statement).


    You can color the bar with the BarColor object.


    I would recommend using the Strategy Wizard to generate this code with the point and click interface.
    Understand the fundamentals of basic strategy creation by developing an actual strategy using the NinjaTrader Strategy Wizard.3:23 Strategy Wizard Overview7:...


    Below is a link to a general post with resources for learning NinjaScript.
    Last edited by NinjaTrader_ChelseaB; 08-06-2017, 11:12 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      Thanks for the road map to learning ninjascript. Look forward to checking it out. In the meantime I tried using the wizard to mark a bar from July 19, 2017 without success. Here is a code snippet from the the wizard:

      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (ToTime(Time[0]) == ToDay(2017, 7, 19))
      {
      DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Lime);
      }
      }
      Would appreciate some advice on where I'm going wrong.

      Thanks
      Gennaker

      Comment


        #4
        Hello Gennaker,

        I overlooked there is a limitation with the Strategy Wizard in NinjaTrader 7 that was resolved in NinjaTrader 8.


        The Strategy Wizard is unable to make this comparison correctly as it compares a time object with a date object.

        I am happy to assist you in manually correcting the script since there is this limitation.

        The code needed is
        Code:
        protected override void OnBarUpdate()
        {
        // Condition set 1
        if (ToDay(Time[0] == ToDay(2017, 7, 19))
        {
        DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Lime);
        }
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,

          I tried using the edit you suggested but no luck. It was showing an error until I added another ")" after (ToDay(Time[0] but it still did not mark any bars after I compiled it and ran it.

          I downloaded NT8 and tried using the wizard, but still no luck. Here is the code that was generated by the strategy wizard:

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "ColorMyReversal";
          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;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          }
          }

          protected override void OnBarUpdate()
          {
          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if (Times[0][0].Date == new DateTime(2017, 7, 19))
          {
          BarBrush = Brushes.Yellow;
          }

          }
          }
          Thanks
          Gennaker

          Comment


            #6
            Hello Gennaker,

            I would expect this code to color the bars for July 17th 2017 yellow.

            May I have a screenshot of your chart with the data box showing (so we may see what date is in view on the chart)?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              I was getting ready to make a screenshot for you and noticed the bar I wanted marked was now yellow. It wasn't there yesterday.

              When I removed the strategy and added it back again, the yellow bar went away. I'm guessing when the new day occurred the strategy was forced to start from the beginning (?). Is there another way to prompt the strategy to run?

              Thanks for your help
              Ben
              Last edited by Gennaker; 08-08-2017, 08:24 PM. Reason: another question

              Comment


                #8
                Hello Ben,

                I'm not certain that I am understanding.

                If the script is enabled, and this code is the only code in the script, then the bar(s) on July 17th should be colored yellow.

                If the script is re-enabled, these bar(s) should still be colored yellow as this would be in the historical data.

                Can you provided a screenshot showing that the script is enabled and that the bars on this date are not colored yellow?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea

                  There should be a couple of screen shots attached. The first one shows the bar not colored yellow (arrow is pointing to it).

                  The bar is colored yellow when I check the "enable" button. Second screen shot.

                  I'm trying to understand why I have to keep checking the enable button every time I open the chart.

                  Thanks,
                  Ben
                  Attached Files

                  Comment


                    #10
                    If you close a chart with a Strategy on it, It is disabled because the chart is closed and so is the strategy.
                    when you create a new chart and add the strategy, it has to be enabled because Strategies are not meant to wake up enabled. If you want it to display all the time turn it into an Indicator. If you also want to use the value in a Strategy as well then use AddChartIndicator() inside your Strategy.

                    Comment


                      #11
                      Hello Ben,

                      JerryWar is correct, if a strategy is applied to a chart this becomes attached to this chart only. If the chart is closed, the strategy instance is disabled and removed.

                      If a strategy is not enabled then it will not be performing any actions such as coloring bars or placing orders.

                      Strategies do not self enable.

                      You will need to either leave the chart open, or don't add the strategy to the chart and add the strategy to Strategies tab of the Control Center instead.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chelsea,

                        I watched your strategy wizard 301 video tutorial and ninjascript 401 tutorial and was having success in using the wizard to generate code and copying it over to an unlocked copy.

                        I was using the bar color for a while and then experimented with using up arrows instead. I didn't like the up arrows because they were down at the bottom of the chart. When I tried to copy over the bar color again I got the attached system error message saying my color was not defined.

                        Would appreciate your advice on how to correct this.

                        Thanks,
                        Ben
                        Attached Files

                        Comment


                          #13
                          Hello Gennaker,

                          In the screen shot you provided you are using a lowercase Y for yellow.

                          Using the editor to modify scripts requires some basics in coding and I would suggest the following resources to aid you in the learning process. The helpguide is going to be a great resource for you.



                          The following will introduce you to the strategy builder;

                          Strategy Builder Intro: 7 Minutes
                          An overview of the Strategy Builder feature in the NinjaTrader 8 platform.NinjaTrader supports over 40,000 traders around the globe with best-in-class techno...


                          Strategy Builder NT8:
                          Understand the fundamentals of basic strategy creation by developing an actual strategy using the NinjaTrader Strategy Builder.2:45 Opening a Strategy Builde...


                          See the NinjaScript Editor Youttube,
                          Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.


                          A link to our Help Guide NinjaScript section can be found here,


                          Click here to see our NinjaScript Reference Samples:


                          Please let us know if you need further assistance.
                          Alan P.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by warreng86, 11-10-2020, 02:04 PM
                          5 responses
                          1,356 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by Perr0Grande, Today, 08:16 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post Perr0Grande  
                          Started by elderan, Today, 08:03 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post elderan
                          by elderan
                           
                          Started by algospoke, Today, 06:40 PM
                          0 responses
                          10 views
                          0 likes
                          Last Post algospoke  
                          Started by maybeimnotrader, Today, 05:46 PM
                          0 responses
                          12 views
                          0 likes
                          Last Post maybeimnotrader  
                          Working...
                          X