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

How to store last known value greater then 0

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

    How to store last known value greater then 0

    HI i have an indicator1 where i have this logic and indicator2 where its getting ll values from.I want to use LL values as a stop loss but it prints as 0 most of the time. The number that prints last is the correct value.

    What can i do to store last known value that is greater then 0 so i can later use it as stop loss, i dont want to have stoploss as 0 or unknown...

    private ISeries<double> ll;

    ll = FVGIndicator(UseFVGDataSeries, FVGBarsPeriodType, FVGSeriesPeriod, 50, true, ImpulseFactor, 10, MinimumFVGSize, AllBarsSameDirection, FillType, false, false, false, tsSilverBullet1.Add(tsTZDifference), 60, false, tsSilverBullet2.Add(tsTZDifference), 60, false, tsSilverBullet3.Add(tsTZDifference), 60,12,true,10,14,Brushes.Green,Brushes.Red,2,DashS tyleHelper.Solid,true).Values[27];

    in onbarupdate
    Print("ll:"+ll[0]);

    for button to submit order
    entryOrder = account.CreateOrder(instr, OrderAction.Buy, OrderType.Market, TimeInForce.Gtc, numericStepperValue, 0, 0, string.Empty, "Entry", null);
    account.Submit(new[] {entryOrder});​
    stopOrder = account.CreateOrder(instr, OrderAction.Sell, OrderType.StopMarket, TimeInForce.Gtc, numericStepperValue, 0, lowBar - TickSize, string.Empty, "Stop Loss", null);
    account.Submit(new[] {stopOrder});​

    my output
    ll:14758.25
    ll:14758.25
    ll:0
    ll:0
    ll:0
    ll:0

    ​​Click image for larger version

Name:	image.png
Views:	116
Size:	28.6 KB
ID:	1275312

    #2
    Hello tkaboris,

    Thanks for your post.

    If the script is not behaving as expected then you would need to add debugging prints to the script to understand exactly how it is behaving.

    If the LL value tracked using is a custom Series object or plot, you could consider saving the CurrentBar value to an int variable called something like 'LLDrawn' when LL text is drawn on the chart. Then, when accessing the value you could pass in CurrentBar-<variable name> for the barsAgo when accessing the custom Series or accessing the plot using Value[int barsAgo]

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your response.
      I see what you are saying and implemented it as below in my indicator1.


      private int LLDrawn;

      if(fr_dw) {
      if (bLbl) {
      if (Low[Period] <= dDw)
      {
      if(ShowMarketStructure){
      ll = Low[Period];
      LLDrawn = CurrentBar;

      Draw.Text(this,"swLL" + CurrentBar, false, "LL", Period, Low[Period] - delta, 5, SellClr, myFont, TextAlignment.Center, null, null, 1);
      }
      }
      else
      if(ShowMarketStructure){
      Draw.Text(this,"swHL" + CurrentBar, false, "HL", Period, Low[Period] - delta, 5, SellClr, myFont, TextAlignment.Center, null, null, 1);
      }
      }
      dDw = Low[Period];

      Values[27][0] = ll;

      dtDw = Time[Period];
      bDw = true;
      }

      }​

      I am stuck with my indicator2(chart trader) where i want to call those values from. Plots are accepting doubles only and llDrawn is int. I believe i need to plot LLDrawn value? so it can see it? Or
      as i already plotted

      ll = FVGIndicator(UseFVGDataSeries, FVGBarsPeriodType, FVGSeriesPeriod, 50, true, ImpulseFactor, 10, MinimumFVGSize, AllBarsSameDirection, FillType, false, false, false, tsSilverBullet1.Add(tsTZDifference), 60, false, tsSilverBullet2.Add(tsTZDifference), 60, false, tsSilverBullet3.Add(tsTZDifference), 60,12,true,10,14,Brushes.Green,Brushes.Red,2,DashS tyleHelper.Solid,true).Values[27];

      If i need to plot LLDrawn
      do i do it like this in indicator1?

      AddPlot(Brushes.Green, "LLDrawn");



      [XmlIgnore()]
      public Series<int> LLDrawn
      { get { return Values[0]; } }​

      How do i go about adding plot to the line where i store to current bar?
      LLDrawn = CurrentBar.Value[0];?
      Last edited by tkaboris; 10-30-2023, 05:59 AM.

      Comment


        #4
        Hello tkaboris,

        Thanks for your notes.

        You could make the LLDrawn variable in your script a double instead of an int if you want to assign that value to a plot. Assigning a value to a plot would look something like this: Value[0] = LLDrawn. The code you shared LLDrawn = Value[0] would assign the plot to the variable.

        AddPlot(): https://ninjatrader.com/support/help...t8/addplot.htm
        Value: https://ninjatrader.com/support/help.../nt8/value.htm

        Otherwise. here is a reference sample demonstrating exposing indicator values that are not plots.

        SampleBoolSeries: https://ninjatrader.com/support/help...alues_that.htm

        You would need to add debugging prints to the script to understand exactly how it is behaving and calculating logic.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you.
          private double LLDrawn;

          {
          if(ShowMarketStructure){
          ll = Low[Period];
          Value[28] = LLDrawn;
          Draw.Text(this,"swLL" + CurrentBar, false, "LL", Period, Low[Period] - delta, 5, SellClr, myFont, TextAlignment.Center, null, null, 1);
          }​

          [Browsable(false)]
          [XmlIgnore]
          public Series<double> LLDrawnValue
          {
          get { return Values[28]; }
          }​
          I get this error when calling value from indicator1 to indicator2

          Click image for larger version

Name:	image.png
Views:	83
Size:	193.7 KB
ID:	1275486​​
          Attached Files

          Comment


            #6
            Hello tkaboris,

            Thanks for your notes.

            You may need to provide a barsAgo value when calling the series from the first indicator in the second indicator since this is a public Series<double>.

            For example, FVGIndicator().LLDrawnValue[0]

            Review the SampleBoolSeries reference sample linked in post # 4 demonstrating how this is accomplished.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              I haveLLDrawn as series and i assign Values[28][0] = LLDrawn; but it complains that cant convert to <double> which i declared to be a series...

              private Series<double> LLDrawn;
              AddPlot(Brushes.Transparent, "LL Drawn"); // 28 LL Drawn
              in configure
              LLDrawn = new Series<double>(this);
              and this which draws text ok

              if (Low[Period] <= dDw)
              {
              if(ShowMarketStructure){
              ll = Low[Period];
              Values[28][0] = LLDrawn;
              Print("LLDrawn"+LLDrawn);
              Draw.Text(this,"swLL" + CurrentBar, false, "LL", Period, Low[Period] - delta, 5, SellClr, myFont, TextAlignment.Center, null, null, 1);
              }

              ​​Click image for larger version

Name:	image.png
Views:	79
Size:	142.4 KB
ID:	1275517

              Comment


                #8
                Hello tkaboris,

                Thanks for your notes.

                Custom Series<double> variables require that you supply a barsAgo value when accessing the variable.

                For example, LLDrawn[0] where the barsAgo being passed in is 0.

                Please review this help guide page thoroughly for more information about using Series<T> variables: https://ninjatrader.com/support/help...t8/seriest.htm
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  sorry about that.

                  it appears to be working now with this code
                  indicator1 source Onbarclose:

                  Values[27][0] = Low[Period];
                  Values[29][0] = CurrentBar-Period;
                  Print("Lowvalue:"+Values[27][0]);
                  Print("CurrentBar:"+Values[29][0]);​

                  Output:
                  Lowvalue:14634.25
                  CurrentBar:10542
                  Lowvalue:14662.75
                  CurrentBar:10607
                  Lowvalue:14666.25
                  CurrentBar:10690
                  Lowvalue:14674.25
                  CurrentBar:10751
                  Lowvalue:14667.75
                  CurrentBar:10776​


                  Howver when i print same values from indicator2 its initially prints values and then lots of 0s... even with isfirsttickofbar, and for stop loss i cant have 0s..
                  I need low and bars ago values when i send stop order...

                  ll = FVGIndicator(UseFVGDataSeries, FVGBarsPeriodType, FVGSeriesPeriod, 50, true, ImpulseFactor, 10, MinimumFVGSize, AllBarsSameDirection, FillType, false, false, false, tsSilverBullet1.Add(tsTZDifference), 60, false, tsSilverBullet2.Add(tsTZDifference), 60, false, tsSilverBullet3.Add(tsTZDifference), 60,12,true,10,14,Brushes.Green,Brushes.Red,2,DashS tyleHelper.Solid,true).Values[27];

                  llBarIndex = FVGIndicator(UseFVGDataSeries, FVGBarsPeriodType, FVGSeriesPeriod, 50, true, ImpulseFactor, 10, MinimumFVGSize, AllBarsSameDirection, FillType, false, false, false, tsSilverBullet1.Add(tsTZDifference), 60, false, tsSilverBullet2.Add(tsTZDifference), 60, false, tsSilverBullet3.Add(tsTZDifference), 60,12,true,10,14,Brushes.Green,Brushes.Red,2,DashS tyleHelper.Solid,true).Values[29];


                  Print("llBarIndex:"+llBarIndex[0]);
                  Print("ll:"+ll[0]);​

                  my output:

                  llBarIndex:11407
                  ll:14727
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0
                  ll:0
                  llBarIndex:0

                  Thank you​

                  Comment


                    #10
                    Hello tkaboris,

                    Thanks for your notes.

                    To truly know what is causing the issue it would be necessary to use prints and debug by looking at all of the information the script is using for decisions.

                    You would need to further debug your strategy by reducing code and adding further prints to understand how the logic is evaluating in each script. Once you fully understand how your logic is behaving you could modify the logic to suit your overall goals.

                    When identifying an issue, it is extremely important to have extremely simple test scripts and very simple steps to reproduce that only have the bare minimum code necessary to reproduce the behavior without any other code and do not require specific market conditions to reproduce (unless the issue is with specific market conditions).

                    Print out each of the values being used in your scripts, the CurrentBar, and the Time[0] to understand how those values are evaluating on each bar.

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121

                    You may want to consider creating a public int variable, assigning CurrentBar-Period to the variable. and then accessing that value in your other script instead of assigning a value to a plot. An example of creating a public variable that could be accessed in another script could be seen in the SampleBoolSeries reference sample. In the reference sample the 'ExposedVariable' is a regular double variable that can be accessed in another script.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi i finally made it work, thank you.
                      public int llDrawnBar;
                      my prints for lldrawbar: its correct but when i want to use it in as barsago its compalining
                      llDrawnBar:13338



                      I want to draw fibonacci with it but its defaulting to time instead of barsago
                      Draw.FibonacciRetracements(this, "fib", true, CurrentBar-llDrawnbar, llPriceLevel , CurrentBar-hhDrawnbar, hhPriceLevel);
                      Click image for larger version

Name:	image.png
Views:	83
Size:	184.1 KB
ID:	1275562

                      Comment


                        #12
                        Hello tkaboris,

                        Thanks for your notes.

                        To draw fibonnaci retracements on the chart you would need to make sure you are using the proper syntax seen below.

                        Draw.FibonacciRetracements(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY)

                        or

                        Draw.FibonacciRetracements(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, bool isGlobal, string templateName)

                        Note that the startBarsAgo and endBarsAgo in the syntax above must be int variables, not double variables. You could consider creating int variables in your script and assigning a value to them. Then passing the int variables into the Draw.FibonacciRetracements() method for the startBarsAgo and endBarsAgo arguments.

                        Review this help guide page for more information on Draw.FibonacciRetracements(): https://ninjatrader.com/support/help...tracements.htm
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          HI ok i will look into it but i also have another issue
                          I hope i willl communicate it clearly. I dont know why its happening but here is my issue.
                          when i use indicator1(market structure shift) and print hhPriceLevel, it prints only HH levels which is what i want, i want to get only HH not HL...
                          However when I import indicator inside another indicator2(my chart trader) when i print the same hhPriceLevel it prints both HH and HLs.

                          Code is identical and both onbarclose. I cant figure out why its doing it...

                          if (High[Period] >= dUp)
                          {
                          if(ShowMarketStructure){
                          hhDrawnBar = CurrentBar-Period;
                          hhPriceLevel = High[Period];
                          Print("hhPriceLevel in fvg ind"+hhPriceLevel);
                          Draw.Text(this,"swHH" + CurrentBar, false, "HH", Period, High[Period] + delta, 5, BuyClr, myFont, TextAlignment.Center, null, null, 1);

                          }
                          }

                          else
                          if(ShowMarketStructure){
                          hhDrawnLabel = false;
                          Draw.Text(this,"swLH" + CurrentBar, false, "LH", Period, High[Period] + delta, 5, BuyClr, myFont, TextAlignment.Center, null, null, 1);
                          }​​


                          Click image for larger version

Name:	image.png
Views:	108
Size:	21.4 KB
ID:	1275639

                          Comment


                            #14
                            Hello tkaboris,

                            Thanks for your notes.

                            Without seeing the full scripts code, debugging, and thoroughly testing the behavior of the scripts I cannot say for certain what logic is causing the behavior you are reporting.

                            If the hhPriceLevel variable in the first script is an exposed public variable then you could access that variable value within your second script. The SampleBoolSeries reference sample linked on post # 4 demonstrates creating an exposed public variable that could be accessed from another script.

                            Ultimately, you would have to further debug your scripts to know exactly how the logic is processing in each script.

                            Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                            https://ninjatrader.com/support/foru...121#post791121
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #15
                              I went through an example and wanted to make sure i implemented it the right way.
                              Is it?
                              thank you

                              public double hhPriceLevel;

                              hhPriceLevel = High[Period];

                              public double HHPriceLevel
                              {
                              get { Update(); return hhPriceLevel; }
                              }

                              and in my other script in onbarupdate i access it like this
                              hhPriceLevel = FVGIndicator(UseFVGDataSeries, FVGBarsPeriodType, FVGSeriesPeriod, 50, true, ImpulseFactor, 10, MinimumFVGSize, AllBarsSameDirection, FillType, false, false, false, tsSilverBullet1.Add(tsTZDifference), 60, false, tsSilverBullet2.Add(tsTZDifference), 60, false, tsSilverBullet3.Add(tsTZDifference), 60,12,true,10,14,Brushes.Green,Brushes.Red,2,DashS tyleHelper.Solid,true).HHPriceLevel;

                              ​​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by carnitron, Today, 08:42 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,973 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post rbeckmann05  
                              Started by rhyminkevin, Today, 04:58 PM
                              4 responses
                              58 views
                              0 likes
                              Last Post dp8282
                              by dp8282
                               
                              Working...
                              X