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

Get value from indicator

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

    Get value from indicator

    Hi, I have an indicator named "Sim22_DeltaV2", and I'm trying to get the value that it returns on 1 min candles and 15 second candles.

    Here's the code of the strategy:

    HTML Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class test2 : Strategy
        {
            private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_Del taV2 Sim22_DeltaV21;
            private EMA EMA1;
            private SMA SMA1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                      Description = @"Joan strategy";
                      Name = "test2";
                      Calculate = Calculate.OnBarClose;
                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy = false;
                      ExitOnSessionCloseSeconds = 30;
                      IsFillLimitOnTouch = true;
                      MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                      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)
                  {
                       AddDataSeries("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                       AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                       AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                       AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last); }
                   else if (State == State.DataLoaded)
                   {
                         Sim22_DeltaV21 = Sim22_DeltaV2(Close, Sim22_DeltaV2_Type.BidAsk, true, false, false,                                         Sim22_DeltaV2_FilterModeType.None, 1);
                    }
                 }
    
                 protected override void OnBarUpdate()
                 {
                       if (BarsInProgress != 0)
                           return;
                       if (CurrentBars[0] < 1)
                           return;
                       Print(Sim22_DeltaV21.DeltaClose[1]);
                  }
           }
    }
    For example, in the above code, the last print should return 20 as we can see on the indicator of the chart:

    Click image for larger version  Name:	grafico.png Views:	0 Size:	46.3 KB ID:	1235461

    But instead of returning 20, it outputs 105 as we can see on the following image (last value printed):

    Click image for larger version  Name:	numeros.png Views:	0 Size:	78.3 KB ID:	1235462

    Any idea on how can I solve the problem?
    Last edited by speedytrade02; 02-14-2023, 06:25 PM.

    #2
    Hello speedytrade02,

    Thanks for your post.

    In the code you shared, I see the isntantiated indicator is set to calculate based on the primary series that the script is running on.

    Sim22_DeltaV2(Close, Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1);

    You could instantiate the indicator to use the Close of the added series by passing in Closes[1] to calculate based on the added 1-minute series. Or, you could pass in Closes[2] to use the added 15-second series for calculations.

    .//calculate from close price of added 1-minute series
    Sim22_DeltaV21 = Sim22_DeltaV2(Closes[1], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1);

    or

    //calculate from close price of added 15-second series
    ​Sim22_DeltaV21 = Sim22_DeltaV2(Close[2], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1);


    Note that you are also printing out the indicator value of the previous bar since you pass in a bars ago value of 1.

    Print(Sim22_DeltaV21.DeltaClose[1]);

    To get the current bar's delta close value, you would need to pass in a bars ago value of 0.

    Print(Sim22_DeltaV21.DeltaClose[0]);
    ​​​​​​​

    See this help guide page for more information about working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

    Let me know if I may further assist you.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon, thanks for rapid response.

      I've tryied what you've said and I still don't understand where the output comes from.

      With this code:​

      HTML Code:
      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class test2 : Strategy
          {
              private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_Del taV2 Sim22_DeltaV21;
              private EMA EMA1;
              private SMA SMA1;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                        Description = @"Joan strategy";
                        Name = "test2";
                        Calculate = Calculate.OnBarClose;
                        EntriesPerDirection = 1;
                        EntryHandling = EntryHandling.AllEntries;
                        IsExitOnSessionCloseStrategy = false;
                        ExitOnSessionCloseSeconds = 30;
                        IsFillLimitOnTouch = true;
                        MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                        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)
                    {
                         AddDataSeries("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                         AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                         AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                         AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last); }
                     else if (State == State.DataLoaded)
                     {
                           Sim22_DeltaV21 = Sim22_DeltaV2(Closes[1], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1);
                      }
                   }
      
                   protected override void OnBarUpdate()
                   {
                         if (BarsInProgress != 0)
                             return;
                         if (CurrentBars[0] < 1)
                             return;
      
                        Print("------------------");
                        Print(Sim22_DeltaV21.DeltaClose[0]);
                        Print(Sim22_DeltaV21.DeltaClose[1]);​
                   }
             }
      }



      And the output of 1 minute chart is the following:

      Click image for larger version  Name:	grafico.png Views:	0 Size:	180.9 KB ID:	1235560

      As you can see, the output of "Print(Sim22_DeltaV21.DeltaClose[0]);" is 0 every time

      Also, If I change the Closes[1] to Closes[2] in order to get the 15 seconds delta (Sim22_DeltaV21 = Sim22_DeltaV2(Closes[2], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1). I get the 94 as a result, but on the 15 seconds chart, we can see that delta is -4

      Click image for larger version  Name:	FULL.png Views:	0 Size:	138.4 KB ID:	1235565
      Attached Files
      Last edited by speedytrade02; 02-15-2023, 03:36 PM.

      Comment


        #4
        Hello,

        Thanks for your note.

        I see that you are running the script with Calculate.OnBarClose. This means that the strategy will only print out the value of the bar when the bar closes. This means that the value being printed is from the bar that just closed, not the value of the currently forming bar on the chart.

        To print the value of the currently forming bar, you would need to use Calculate.OnPriceChange or Calculate.OnEachTick so that logic can process intrabar.

        See this help guide page about Calculate: https://ninjatrader.com/support/help.../calculate.htm

        Instead of using Closes[barSeriesIndex], you could consider using BarsArray[int index], such as BarsArray[2] as seen in the help guide page linked below.

        Sim22_DeltaV21 = Sim22_DeltaV2(BarsArray[2], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1)

        Using BarsObjects as Input to Indicator Methods: https://ninjatrader.com/support/help...dicatorMethods

        Please let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi, and sorry to bother you

          I've tryied what you've said with the following code:
          HTML Code:
          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
              public class test2 : Strategy
              {
                  private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_Del taV2 Sim22_DeltaV21;
                  private EMA EMA1;
                  private SMA SMA1;
          
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                            Description = @"Joan strategy";
                            Name = "test2";
                            Calculate = Calculate.OnEachTick;
                            EntriesPerDirection = 1;
                            EntryHandling = EntryHandling.AllEntries;
                            IsExitOnSessionCloseStrategy = false;
                            ExitOnSessionCloseSeconds = 30;
                            IsFillLimitOnTouch = true;
                            MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                            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)
                        {
                             AddDataSeries("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                             AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                             AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                             AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last); }
                         else if (State == State.DataLoaded)
                         {
                              Sim22_DeltaV21 = Sim22_DeltaV2(BarsArray[1], Sim22_DeltaV2_Type.BidAsk, true, false, false, Sim22_DeltaV2_FilterModeType.None, 1);                }
                       }
          
                       protected override void OnBarUpdate()
                       {
                             if (BarsInProgress != 0)
                                 return;
                             if (CurrentBars[0] < 1)
                                 return;
                             Print("------------------");
                             Print(Sim22_DeltaV21.DeltaClose[0]);
                             Print(Sim22_DeltaV21.DeltaClose[1]);    ​
                      }
                 }
          }


          but I still getting same results:


          Click image for larger version

Name:	grafico.png
Views:	154
Size:	180.9 KB
ID:	1235581

          Comment


            #6
            Hello speedytrade02,

            Thanks for your note.

            Since the Sim22_DeltaV21 script is not an indicator created by NinjaTrader, I cannot say exactly how this indicator will calculate values. If this indicator was created by a third-party developer, you could contact them directly for information about how to access the values you want in your custom script.

            It seems that you are comparing the prints from the Sim22_DeltaV2 script to a script called BidAsk. You should be comparing the Sim22_DeltaV21.DeltaClose value to the Sim22_DeltaV2 indicator on a chart instead to see if the print values match the indicator.

            Please see the attached image and example script demonstrating how to initialize the SMA() indicator to calculate based on an added 1-Minute series and print the value to the Output window. A similar concept would need to be used in your script.

            When we add the attached script to a 5-minute chart, we see the SMA value of the ES 03-23 1-Minute chart print to the Output window.

            Please let me know if I may assist further.
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Okay, thanks so much Brandon. I'll ask the person who created the script how can I access the value

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by f.saeidi, Today, 12:14 PM
              2 responses
              5 views
              0 likes
              Last Post f.saeidi  
              Started by TradeForge, 04-19-2024, 02:09 AM
              2 responses
              28 views
              0 likes
              Last Post TradeForge  
              Started by aprilfool, 12-03-2022, 03:01 PM
              3 responses
              327 views
              0 likes
              Last Post NinjaTrader_Adrian  
              Started by giulyko00, Today, 12:03 PM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by AnnBarnes, Today, 12:17 PM
              1 response
              2 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Working...
              X