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

    #16
    Hi Jesse, I apologize but I'm still a little confused about how to 'convert' a double to a series double because adding [0] at the end of either variable doesn't seem to help. Your previous message said;

    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 understand Value is a syntax reserved for An ISeries<double> object but replacing either variable with Value, Values, Value[0] or Values[0] still prevents it from compiling.

    If the problem is conversion then can you try to be a little more specific about conversion from double to series? Should simply changing MyCandleSize_min_OR to MyCandleSize_min_OR[0] be enough? The code below is written in the OnBarUpdate() section of the script

    double MyCandleSize_min_OR[0] = (High[0]-Low[0]);
    double Average_Candle_min_OR = SMA(MyCandleSize_min_OR, 50)[0];

    I tried this but end up with a compiling error that states "Bad array declarator" and "Array size cannot be specified in a variable declaration"

    Is my issue in the way this is initialized and I need to create a private property to set up the Series <T> object? What is the difference between my code above and the example below?

    protected override voidOnBarUpdate()
    {
    // Calculate the range of the current bar and set the value
    myDoubleSeries[0]=(High[0]-Low[0]);

    // Print the current 10 period SMA of range
    Print("Value is "+SMA(myDoubleSeries,10)[0]);


    Sorry for all the questions/confusion, I just have very limited seat time programming in C#
    Last edited by itsthefriz; 07-25-2021, 05:26 PM.

    Comment


      #17
      Hello itsthefriz,
      Before OnStateChange, private Series <double> MyCandleSize_min_ER;
      In DataLoaded, MyCandleSize_min_ER = new Series <double> (this);
      In OnBarUpdate, MyCandleSize_min_ER[0] = High[0] - Low[0];
      Print ("SMA value : " + SMA(MyCandleSize_min_ER, 50)[0]);
      Hope it helps!

      Comment


        #18
        Hello itsthefriz,

        The code you provided is in error because you specify double before the variable name:

        Code:
        [B]double [/B]MyCandleSize_min_OR[0] = (High[0]-Low[0]);
        MyCandleSize_min_OR would need to be defined as a private variable, a private Series<double> MyCandleSize_min_OR; In DataLoaded you would define the series like shown in the help guide sample. Then in OnBarUpdate you use it without specifying double:

        Code:
        MyCandleSize_min_OR[0] = (High[0]-Low[0]);
        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #19
          s.kinra thanks for laying it out with the individual states it's supposed to be in, that helped to make sense of what Jesse was saying. The code is now compiling

          Comment


            #20
            Hi NinjaTrader_Jesse got a new question for you.

            As I mentioned earlier I've been able to get my script to run but initializing it is taking a long time and the output window appears to show the script looping through calculations. I set up a counter to the print output window and it appears to loop ~580 times prior to it being enabled.

            I expect the problem has to do with a calculation of the standard deviation for 200 bars. Initially the standard deviation was set to 5 days worth of 1 min bars previously but it was taking ~30min to enable, hence why I bring this up. Here's a rough idea of the layout;

            Code:
            public class StartStrat : Strategy
            {
            private ATR ATR_min;
            private ATR ATR_fifteen_min;
            private ATR ATR_hour;
            private ATR ATR_four_hour;
            private ATR ATR_week;
            private StdDev svtd_deviation;
            
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"StartStrat";
            Name = "StartStrat";
            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 = 8000;
            
            
            else if (State == State.DataLoaded)
            {
            //ATR Series
            int ATR_period = 9;
            ATR_min = ATR(BarsArray[0], ATR_period);
            ATR_fifteen_min = ATR(BarsArray[1], ATR_period);
            ATR_hour = ATR(BarsArray[2], ATR_period);
            ATR_four_hour = ATR(BarsArray[3], ATR_period);
            ATR_week = ATR(BarsArray[4], ATR_period);
            
            //Standard Deviation
            int std_period = 200;
            std_deviation = StdDev(BarsArray[0], std_period);
            }
            }
            
            protected override void OnBarUpdate()
            {
            if (CurrentBar < BarsRequiredToTrade)
            return;
            int period = 200;
            
            //standard deviation channel series
            double Deviation = Math.Sqrt(std_deviation[0]);
            double STDC_up1 = RegressionChannel(period, Deviation).Upper[0];
            double STDC_low1 = RegressionChannel(period, Deviation).Lower[0];
            Setting both periods to 400 increases the loops to 608.

            Any thoughts on how to optimize this to reduce the loops or is that pretty much required for the calculations?
            Last edited by itsthefriz; 07-26-2021, 04:33 PM.

            Comment


              #21
              Hello itsthefriz,

              It looks like your sample was cut off, I don't see where you are printing.

              If the print is in OnBarUpdate you should see as many prints as there are bars.If thats the case then there is not really any way to reduce that other than reducing the amount of bars loaded. I would otherwise need to see where your print is to better understand if anything could be changed to make it print less.

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

              Comment


                #22
                Hi Jesse,

                Here it is with the print statement.

                Code:
                public class StartStrat : Strategy
                {
                private ATR ATR_min;
                private ATR ATR_fifteen_min;
                private ATR ATR_hour;
                private ATR ATR_four_hour;
                private ATR ATR_week;
                private StdDev svtd_deviation;
                
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"StartStrat";
                Name = "StartStrat";
                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 = 8000;
                
                else if (State == State.Configure)
                {
                AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last); // Array[0], 1 minute
                AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last); // Array[1], 15 minute
                AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last); // Array[2], 60 minute
                AddDataSeries("ES 09-21", Data.BarsPeriodType.Minute, 240, Data.MarketDataType.Last); // Array[3], four hour
                AddDataSeries("ES 09-21", Data.BarsPeriodType.Day, 5, Data.MarketDataType.Last); // Array[4], week
                
                }
                
                else if (State == State.DataLoaded)
                {
                //ATR Series
                int ATR_period = 9;
                ATR_min = ATR(BarsArray[0], ATR_period);
                ATR_fifteen_min = ATR(BarsArray[1], ATR_period);
                ATR_hour = ATR(BarsArray[2], ATR_period);
                ATR_four_hour = ATR(BarsArray[3], ATR_period);
                ATR_week = ATR(BarsArray[4], ATR_period);
                
                //Standard Deviation
                int std_period = 200; // This will be set to 6,900
                std_deviation = StdDev(BarsArray[0], std_period);
                
                int i = 0; // initialize counter
                
                }
                }
                
                protected override void OnBarUpdate()
                {
                if (CurrentBar < BarsRequiredToTrade)
                return;
                int period = 200; // This will be set to 6,900
                
                i = i+1
                Print("i: " +i);
                
                //standard deviation channel series
                double Deviation = Math.Sqrt(std_deviation[0]);
                double STDC_up1 = RegressionChannel(period, Deviation).Upper[0];
                double STDC_low1 = RegressionChannel(period, Deviation).Lower[0];
                My worry is that this is going to take a long time to run. As I mentioned the strategy requires 6,900 bars on the 1m time frame (~5 days worth of 1m bars) and enabling the script with the entire amount of bars takes roughly 45 minutes to start. Given the calculation time to enable this I'm also a bit concerned about calculation time as well.

                Another question regarding BarsRequiredToTrade - I believe it's pulling 8,000 bars from the 1 minute time frame but as you can see I'm pulling in arrays for 5 different time periods. Do you have anything to reference regarding pulling or setting different bar amounts for different time periods?

                Comment


                  #23
                  Hello itsthefriz,

                  The print you have it just within OnBarUpdate so you should only see 1 print per bar. You are also using multiple series so that will add to the amount of times you are calling that logic.

                  The only optimization you may be able to do here would be to further isolate the code you run from OnBarUpdate:

                  if(BarsInProgress == 0)
                  {
                  //code here
                  }

                  That would make the code only execute for the primary series so that would reduce the number of times that code is called.

                  I look forward to being of further assistance.


                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by wzgy0920, 04-20-2024, 06:09 PM
                  2 responses
                  27 views
                  0 likes
                  Last Post wzgy0920  
                  Started by wzgy0920, 02-22-2024, 01:11 AM
                  5 responses
                  32 views
                  0 likes
                  Last Post wzgy0920  
                  Started by wzgy0920, 04-23-2024, 09:53 PM
                  2 responses
                  49 views
                  0 likes
                  Last Post wzgy0920  
                  Started by Kensonprib, 04-28-2021, 10:11 AM
                  5 responses
                  193 views
                  0 likes
                  Last Post Hasadafa  
                  Started by GussJ, 03-04-2020, 03:11 PM
                  11 responses
                  3,235 views
                  0 likes
                  Last Post xiinteractive  
                  Working...
                  X