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

drawing a horizontal line on the stop level

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

    #16
    Thanks very much for the explanation.

    Comment


      #17
      Originally posted by Pete77
      Re my RemoveDrawObject effort, for some strange reason I am getting price instead of bar number for DrawnAtBar. I entered "private int DrawnAtBar = 0" under public class, otherwise entered your very helpfull code suggestion..
      Hmm. I think if you want more help you need to post your code.

      Otherwise, congrats, sounds like you're making good progress!

      Comment


        #18
        My last reply was a mistake. There was a Print command in the draw line that was coming up on the output screen that gave a Y value. I will get rid of that and keep looking for the reason the remove Object s not working.

        Comment


          #19
          Hi bitDavid. I will try to upload my little program. Hopefully you can see why the RemoveDrawObject is not working EntryLines.cs,thanks.

          Comment


            #20
            Did you get my code?

            Comment


              #21
              Hello Pete77,

              I believe bltdavid is suggesting this be a separate condition set.

              private int drawnOnBar;

              if (/* conditions to draw object */)
              {
              DrawDot("tag1", true, 0, Low[0] - TickSize, Color.Red);
              drawnOnBar = CurrentBar;
              }

              // conditions to remove drawing object
              if (CurrentBar - drawnOnBar >= 3)
              {
              RemoveDrawObject("tag1");
              }
              Last edited by NinjaTrader_ChelseaB; 03-27-2019, 02:05 PM.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                I made the corrections (Capitalization errors etc) but I believe there is something wrong with the logic.When I Print "Current bar - drawnOnBar" I get big numbers, in the hundreds,so the line is immediately removed instead of three bars later. EntryLines.cs WhenI comment "RemoveDrawObject " the line shows up in the correct place. I will try to post again.
                Attached Files
                Last edited by Pete77; 03-26-2019, 03:03 PM.

                Comment


                  #23
                  Hello Pete77,

                  In the current file you have provided, you are no longer saving the bar number the object was drawn on to a variable.

                  Also, the code to remove the drawing object is not in a separate condition set. Instead, this is nested inside of the 'if (Close[1] > Open[1] && (Close[0] < Open[0]))' condition set.

                  A separate condition would not be within the action block of the other condition set. Instead, this would be outside of the curly braces of the condition set it is in.

                  Currently, your object will only be removed if it is drawn on the same bar because the condition to check if its 3 bars later is in the same action block as the condition set that draws the object. It is only able to draw and remove the object at the same time. Since it will never the drawing bar and 3 bars later at the same time, this nested condition will never evaluate as true.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Thanks, I will try to sort out what you have told me. Meanwhile I am very confused about CurrentBar. If I Print(CurentBar I get 0's on the Output window. I thought CurrentBar would give me the number of bars to the left. Can you help me with that?

                    Comment


                      #25
                      Hello Pete77,

                      CurrentBar is the bar number that is processing.

                      If you save the bar number that is processing to a variable, on a later bar you can subtract that bar number from the current bars number to find out how many bars ago that occurred.

                      The condition set where you are checking the variable should be in a different condition set than where the variable is being set.

                      If you Print(CurrentBar); outside of any conditions in OnBarUpdate(), this will print the bar number that is processing in OnBarUpdate().

                      Attached is an example.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        Thanks for the explanation. I separated the condition sets as suggested.Still not working. When I Print "CurrentBar - drawnOnBar", On Output I get a bunch of zeros then EntryLines.cs numbers like 309.I will post current code.About to give up on this.

                        Comment


                          #27
                          Hello Pete77,

                          Attached is an example that draws an object on the 10th to last historical bar.

                          Then on each new bar gives the number of bars ago that object was drawn.


                          You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                          Attached Files
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            Originally posted by Pete77 View Post
                            When I Print "CurrentBar - drawnOnBar", On Output I get a bunch of zeros
                            Don't use Calculate.OnPriceChange.
                            You don't want your OnBarUpdate to run that often, do you?
                            Running on each bar close is probably more what you want.

                            Make sure you understand this page,
                            https://ninjatrader.com/support/help...?calculate.htm

                            So, change your code to this,

                            Code:
                            Calculate = Calculate.OnBarClose;
                            But, you have other choices.

                            You could wrap your entire contents of OnBarUpdate inside IsFirstTickOfBar.
                            That would make your code impervious to whatever Calculate happens to be set to.

                            To make it impervious to the whims of the Calculate property,
                            change your current OnBarUpdate code from this,

                            Code:
                            protected override void OnBarUpdate()
                            {
                                 if (Close[1] > Open[1] && (Close[0] < Open[0]))
                                 {
                                      Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red);
                                      drawnOnBar = CurrentBar;
                                 }
                            
                                 Print(CurrentBar - drawnOnBar);
                            
                                 if ((CurrentBar - drawnOnBar) >= 3)
                                     RemoveDrawObject("Tag1");
                            }
                            to this,

                            Code:
                            protected override void OnBarUpdate()
                            {
                                if (IsFirstTickOfBar)
                                {
                                    if (Close[1] > Open[1] && (Close[0] < Open[0]))
                                    {
                                        Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red);
                                        drawnOnBar = CurrentBar;
                                    }
                            
                                    Print(CurrentBar - drawnOnBar);
                            
                                    if ((CurrentBar - drawnOnBar) >= 3)
                                        RemoveDrawObject("Tag1");
                                }
                            }
                            or this,

                            Code:
                            protected override void OnBarUpdate()
                            {
                                if (!IsFirstTickOfBar)
                                    return;
                            
                                if (Close[1] > Open[1] && (Close[0] < Open[0]))
                                {
                                    Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red);
                                    drawnOnBar = CurrentBar;
                                }
                            
                                Print(CurrentBar - drawnOnBar);
                            
                                if ((CurrentBar - drawnOnBar) >= 3)
                                    RemoveDrawObject("Tag1");
                            }
                            The reason you're seeing lots of zeros is exactly because Calculate.OnPriceChange is calling
                            your OnBarUpdate repeatedly for every price change during the life of that bar, which is a
                            lot of calls. Thus, the calculation "CurrentBar - drawnAtBar" produces zero at every price change,
                            until the bar finally closes and CurrentBar is incremented by 1.

                            CurrentBar does not increment just because OnBarUpdate is called. Because of the "flexibility"
                            of what the Calculate property is able to achieve, your OnBarUpdate could be called hundreds
                            or even thousands of times and CurrentBar hasn't changed.

                            Remember, CurrentBar is only incremented by 1 when the bar closes.

                            Again, I can't emphasize this enough, make sure you completely understand this page,
                            https://ninjatrader.com/support/help...?calculate.htm

                            This may help, too,
                            https://ninjatrader.com/support/help...ttickofbar.htm
                            Last edited by bltdavid; 03-27-2019, 10:15 PM.

                            Comment


                              #29
                              Thanks very much bitDavid, great help! Along with the sample files that ChelseaB gave me, hopefully I can work this out.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Segwin, 05-07-2018, 02:15 PM
                              14 responses
                              1,789 views
                              0 likes
                              Last Post aligator  
                              Started by Jimmyk, 01-26-2018, 05:19 AM
                              6 responses
                              837 views
                              0 likes
                              Last Post emuns
                              by emuns
                               
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,293 views
                              1 like
                              Last Post jgualdronc  
                              Started by Touch-Ups, Today, 10:36 AM
                              0 responses
                              13 views
                              0 likes
                              Last Post Touch-Ups  
                              Started by geddyisodin, 04-25-2024, 05:20 AM
                              11 responses
                              63 views
                              0 likes
                              Last Post halgo_boulder  
                              Working...
                              X