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

New to NinjaScript - Printing to NT8 Output Window

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

    New to NinjaScript - Printing to NT8 Output Window

    Hi guys, I'm pretty new to Ninja and using the free version but so far like the platform and see myself using it once I get some strategies converted / tested / simulated. At the minute I've got some really basic stuff I'm struggling with and I'm not sure why so hopefully someone here can help send me in the right direction. I'm starting off with some very basic stuff such as attempting to add in indicator to a chart and print to the output window. Below is a copy of what I have so far.

    I'm attempting to add the chart volume indicator to panel 2 as per the help guides https://ninjatrader.com/support/help.../nt8/?tips.htm but nothing is being added to the chart window. Secondly I believe I have correctly defined a regression channel with a 1 standard deviation upper and lower bound however when opening the output window it's blank. The strategy does not have a symbol defined within the code so would that be part of the reason that there are no outputs when output window is opened from the ninjascript editor?

    The strategy is currently running on 2 charts but neither is showing volume or the regression channel outputs

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class StratAttempt1 : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"First strat attempt";
    Name = "StratAttempt1";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = true;
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0.25;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.ByStrategyPosition;
    BarsRequiredToTrade = 6000;

    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    }
    else if (State == State.DataLoaded)
    {

    AddChartIndicator(VOL());
    VOL().Panel = 2;
    double price = Close[0];
    int period = 6900;
    int stdev = 1;
    double RegChannelUpper = RegressionChannel(period, stdev).Upper[0];
    double RegChannelLower = RegressionChannel(period, stdev).Lower[0];
    Print("Regression channel value - upper -" + RegChannelUpper + " - lower - " + RegChannelLower);
    Print("Price" + price.ToString());



    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;


    }
    }
    }
    Last edited by itsthefriz; 07-02-2021, 12:53 PM.

    #2
    Hello,

    Thank you for the post.

    From what you provided you should be seeing an error in the control center log tab. You are accessing data from State.DataLoaded. The Close[0] would need removed.

    Could you try removing the Close[0] from State.DataLoaded then refresh the script?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse, it compiled fine with the Close[0] however I have removed that and print price line.

      I saved the strategy, removed it from the two charts, then added them back to the charts and I'm still not seeing any updates in the output window. Is the right way to view the output window to have the strategy running on a chart and hit NinjaScript Output from the NinjaScript Editor?

      Edit: I noticed that the enable button is not checked in the NT strategy tab. I commented out the regression channel code and now I'm seeing the volume. It appears that might have been part of my problem, and the regression channel needs to be edited
      Last edited by itsthefriz; 07-02-2021, 10:14 AM.

      Comment


        #4
        Hello,

        Thank you for the post.

        If you still have the other regression channel parts in DataLoaded that would also be incorrect, you would need to move any bar/price/plot related logic to OnBarUpdate. Have you moved the prints to OnBarUpdate?

        One way to also approach this would be to use the strategy builder to form a condition using the indicator and then click View code, that will put the code in the correct states/OnBarUpdate.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          So I have moved the regression channel to on bar update and script is as follows. It compiles and enables but I am still unable to see where the print statement for the regression channel is printing to. I am in the script and selecting NinjaScript output but nothing appears in the output window.

          #region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion

          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class Strat1 : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"First Strategy";
          Name = "Strat1";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = true;
          MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0.25;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.ByStrategyPosition;
          BarsRequiredToTrade = 7000;

          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
          }
          else if (State == State.DataLoaded)
          {

          AddChartIndicator(VOL());
          VOL().Panel = 2;




          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;
          int period = 6900;
          int stdev = 1;
          double RegChannelUpper = RegressionChannel(6900, 1).Upper[0];
          double RegChannelLower = RegressionChannel(period, stdev).Lower[0];
          Print("Regression channel value - upper -" + RegChannelUpper + " - lower - " + RegChannelLower);


          }
          }
          }

          Comment


            #6
            Hello,

            Thank you for the post.

            I tried the script however I see prints. Have you tried to remove and re apply the strategy then re enable the new instance?



            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I see the prints now, I may not have been waiting long enough for it to appear. Thanks for the help!

              Comment


                #8
                Hi Jesse, I have another question for you if you don't mind. I mentioned the other day that I'm working on converting some strategies to run in NT and one of them has an average candle size function which is computed across the past 50 bars. I see in the documentation that you can return the High and Low of bars with the bar number in brackets however I was wondering if there's an easy way to set this up to calculate the average bar size without having to set up a for loop?

                Comment


                  #9
                  Hello itsthefriz,

                  In NinjaScript you often don't need any kind of loops due to the forward processing through data. A script always starts at bar 0 and works through the data so you can calculate values as the script progresses through the past data.

                  To calculate something for the past 50 bars you could use a series or plot for that purpose.
                  In an indicator with a plot you could do a simple calculation, for example:

                  Code:
                  Value[0] = High[0] - Low[0];
                  That would assign the high minus low for every bar as a plot. Now to do something for the past 50 bars you could use a indicator to work with that data. For example if you wanted to now average that you could use a custom indicator or an existing one like the SMA:

                  Code:
                  double averaged = SMA(Value, 50)[0];
                  that will return the average of a 50 period SMA using the High - Low data.


                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Jesse, using the SMA to get high - low is a pretty good idea however I'm getting an error on compile which states NinjaTrader.NinjaScript.ISeries<double>, int)' has some invalid arguments and the next line is Argument 1: cannot convert from 'double' to NinjaTrader.NinjaScript.ISeries<double>
                    Last edited by itsthefriz; 07-12-2021, 03:55 PM.

                    Comment


                      #11
                      Hi Jesse, just following up on this again since the 50 period SMA using high-low data is failing to compile and I was curious if you might know why

                      Comment


                        #12
                        Hello itsthefriz,

                        Can you provide the syntax you tried which you are having trouble with?

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hi Jesse, here's the syntax

                          double MyCandleSize_min_ER = High[0] - Low[0];
                          double AverageCandle_min_ER = SMA(MyCandleSize_min_ER, BarsBack_min_ER)[0];

                          Comment


                            #14
                            Hello itsthefriz,
                            MyCandleSize_min_ER is double but SMA would need double series for calculation so the error. You can convert MyCandleSize_min_ER to double series to fix this.
                            Hope it helps!

                            Comment


                              #15
                              Hello itsthefriz,

                              s.kinra is correct, the SMA is expecting a series of data to perform calculations over. To make a series you can see the following link and its sample, you would then assign the high minus low to the series and pass the series to the SMA.





                              I look forward to being of further assistance.

                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by MarianApalaghiei, Today, 10:49 PM
                              3 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by XXtrader, Today, 11:30 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post XXtrader  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              4 responses
                              28 views
                              0 likes
                              Last Post love2code2trade  
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post funk10101  
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              37 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X