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

mark specific bar when condition occurs and reset with another condition

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

    mark specific bar when condition occurs and reset with another condition

    Hi all,

    please be patient with me cause I'm kinda new in NinjaScript. I'm struggling and cannot solve it on my own. In OnBarUpdate method I am checking if the current close is within the actual bar price range. When this condition occurs I am starting drawing two horizontal dashed lines which are indicating the upper and lower range of the high and low of the bar before. The blue diamond is indicating the bar that triggered the condition. See following screenshot how this looks like for better understanding:

    Click image for larger version  Name:	example.jpg Views:	0 Size:	6.1 KB ID:	1125922

    Code:
    [...]
     private double myOffset;
     int initBarIndex = 0;
    [...]
    if ( Close[0]>=Low[1] && Close[0]<=High[1] )
    {
    int initBarIndex = CurrentBar-1;
    initBarHigh = High[1];
    initBarLow = Low[1];
    Draw.Line(this, "initBarTop", false, CurrentBar - initBarIndex, initBarHigh, -1, initBarHigh, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    Draw.Line(this, "initBarBot", false, CurrentBar - initBarIndex, initBarLow, -1, initBarLow, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    Diamond myDiamond = Draw.Diamond(this, "hit", true, 0, Low[0] - myOffset, Brushes.RoyalBlue);
    myDiamond.AreaBrush = Brushes.RoyalBlue;
    }
    The goal is to flag the most left bar where the horizontal range lines begin as long as the close of actual bars are within that range. Whenever a bar appears with a close higher or lower that range, I wanna reset the most left bar and remove the drawing lines. The condition should rearmed and wait until next time it occurs.

    The current issues I'm facing:
    As this condition is checked on each bar update, whenever a new bar has closed within its previous bar it's matched, too. That results in a nested triggering. In the screenshot above, when the most right bar (red) would close, it would trigger the condition and the middle green bar will become the new reference bar. I don't want this. I want to keep the most left bar as the important (=flagged) reference bar and as long as no close has occured above or below the most left reference bar.

    I tried assigning initBarIndex = 0 and include this condition at very first check or integrate it in the existing if clause, something like:
    Code:
    if ( Close[0]>=Low[1] && Close[0]<=High[1] && initBarIndex == 0 )
    ...
    else if ( Close[0]>initBarHigh || Close[0]<initBarLow)
     {
      initBarIndex = 0;
      RemoveDrawObject("initBarTop");
      RemoveDrawObject("initBarBot");
     }
    but whatever I tried so far, I fail. I really have tried a lot but don't know how to proceed. Any help really appreciated.
    thanks to all in advance
    Patricia
    Last edited by patricia70; 11-02-2020, 04:21 PM.

    #2
    I guess I solved it will check in detail tomorrow and report back.

    Comment


      #3
      Hello patricia70, thanks for your post.

      To understand why your conditions are becoming true at unexpected times use the Print method:



      This is helpful for printing out the condition elements individually to the output window.

      Print(CurrentBar);
      Print(Close[0]);
      Print(Low[1]);
      Print(High[1]);
      if ( Close[0]>=Low[1] && Close[0]<=High[1] )

      I see you found a solution to this while I was writing back, so if you could provide the full context of what this script needs to do I will be happy to take a look if needed.
      ​​​
      Please let me know if this does not resolve your inquiry.
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Hello Chris,

        this is the code I ended up with:
        Code:
        [...]
        #region variables
          private double myOffset;
          private double myOffsetTicks = 4;
          int initBarIndex = 0;
          double initBarHigh;
          double initBarLow;
        #endregion
        [...]
        protected override void OnStateChange()
        [...]
        else if (State == State.DataLoaded)
         {
         myOffset = myOffsetTicks * TickSize;
         }
        [...]
        protected override void OnBarUpdate()
        {
         if (CurrentBar < 2) return;
         {
          if ( initBarIndex==0 && Input[0]>=Low[1] && Input[0]<=High[1])
          {
          initBarIndex = CurrentBar - 1;
          initBarHigh = High[1];
          initBarLow = Low[1];
          /* for visual debugging: draw a diamond below the triggering bar which closed within range of previous bar
          Diamond myDiamond = Draw.Diamond(this, "tag", true, 0, Low[0] - myOffset, Brushes.RoyalBlue);
          myDiamond.AreaBrush = Brushes.RoyalBlue;
          */
          }
        if ( initBarIndex>0 && Input[0]>=initBarLow && Input[0]<=initBarHigh )
         Draw.Line(this, "initBarTop", false, CurrentBar - initBarIndex, initBarHigh,-1, initBarHigh, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
         Draw.Line(this, "initBarBot", false, CurrentBar - initBarIndex, initBarLow, -1, initBarLow, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
        }
          if ( Input[0]>initBarHigh || Input[0]<initBarLow )
          {
          initBarIndex = 0;
          Draw.TextFixed(this, "innerBarDOW_infotext", "no innerBar detected in current time interval", TextPosition.TopRight);
          RemoveDrawObject("initBarUpperRange");
          RemoveDrawObject("initBarBottomRange");
          }
         }
        }
        In the beginning I did the well-known error where I used a single equal sign in the condition (wrong: if (initBarIndex = 0 ..., correct: if (initBarIndex ==0 ...). I am pretty sure this code is not very nice and certainly could be written with much less code or lines and maybe not nested like I did, but as I said I'm a beginner. Any tip appreciated.

        thanks for support!
        Last edited by patricia70; 11-03-2020, 08:15 AM.

        Comment


          #5
          Hello patricia70, thanks for your reply.

          If there are any specific questions you have I will be happy to help out. Practicing and debugging using drawing tools + print statements will be the best way to resolve most errors. Some examples to take a look at will be the Trend Lines and Swing indicator for related examples.

          Best regards,
          ChrisL.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            I was just wondering if my code could be clean up just to demonstrate how to minimize code lines and use a clean structure. However, thanks for your time. Can you point me out which example you mean with "trend line indicator" and "swing indicator" ? couldn't find such in the NT8 example indicators.

            Comment


              #7
              Hello patricia70, thanks for your reply.

              Those two can be found in the Indicators folder of the NinjaScript Editor. All of the default indicators are open source and viewable from this folder.

              Best regards,
              -ChrisL

              Chris L.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by fx.practic, 10-15-2013, 12:53 AM
              5 responses
              5,404 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by Shai Samuel, 07-02-2022, 02:46 PM
              4 responses
              95 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by DJ888, Yesterday, 10:57 PM
              0 responses
              8 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by MacDad, 02-25-2024, 11:48 PM
              7 responses
              159 views
              0 likes
              Last Post loganjarosz123  
              Started by Belfortbucks, Yesterday, 09:29 PM
              0 responses
              8 views
              0 likes
              Last Post Belfortbucks  
              Working...
              X