Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

same bar "crossing below" twice given Calculate.OnBarClose?

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

    same bar "crossing below" twice given Calculate.OnBarClose?

    Hi,

    I'm developing a trend line crossing above/below strategy given a manually drawn trend line. In the tests I found the same bar when crossing the line generated more than one signal, while Calculate=Calculate.OnBarClose. If OnBarUpdate() executes only once at the close of each bar, how can this be? Additionally, where does the line "Line Object: NinjaScriptInfo Manually Drawn: false" come from?

    Below are the code/chart/Output screen shot. ​Kindly advise. Appreciate very much.


    Cheers,
    Steven


    Click image for larger version

Name:	image.png
Views:	191
Size:	372.9 KB
ID:	1280391
    Click image for larger version

Name:	image.png
Views:	148
Size:	582.8 KB
ID:	1280392

    #2
    Hello i019945nj,

    Thanks for your post.

    The draw.IsUserDrawn property will return true of the drawing object detected is manually drawn on the chart when you loop through the DrawObjects collection.

    If the drawing object is not drawn manually then draw.IsUserDrawn will return false.

    IDrawingTools: https://ninjatrader.com/support/help...rawingtool.htm
    DrawObjects: https://ninjatrader.com/support/help...rawobjects.htm

    Please create a simple reduced test script that demonstrates the behavior you are reporting and share it with us along with the steps you are taking to reproduce the behavior so we may accurately assist.

    Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed. To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.

    We look forward to assisting further.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      Thank you for your reply. Please refer to the attached reduced code.

      Additionally, there seemed to be problems with 'draw.IsUserDrawn property' setting, because the line you see in above screenshot was actually drawn manually by myself - so it should be 'true' as opposed to 'false' shown above Am I understanding this correctly?

      Regards,
      Steven
      Attached Files

      Comment


        #4
        Hello i019945nj,

        Thanks for your notes.

        Where you have a note stating that a closing curly brace is the "//of OnBarUpdate", this is actually the end of the DrawObjects loop in your script, not the end of OnBarUpdate.

        The actual closing curly brace is where you have a note stating "// of class STR_LineCrossing".

        You should add a closing curly brace after the closing curly brace of the DrawObjects loop and try calling your conditions to print the CurrentBar outside of the DrawObjects loop.

        And, ensure that your custom method is placed outside of OnBarUpdate().

        Further, note that looping through the DrawObjects collection will detect any objects drawn manually or drawn by other indicators or strategies.

        I suggest creating a New > Chart window that only has this script enabled on it when testing the script to ensure no other objects are being drawn on the chart by other scripts.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hi Brandon,

          Thank you for your reply. I've tested the code and above mentioned problems no longer appears.

          However, given "Calculate.OnBarClose", attributes of a line are only calculated when a bar closes - which is too slow. And when I move the line, I need to wait till another bar close. I tried ".OnPriceChange" but it stayed the same. Do you have recommendation for this?

          Or are there event handlers that notifies me immediately upon a line being manually drawn?

          Thank you indeed for your input.

          Regards,
          Steven

          Comment


            #6
            Hello Steven,

            Thanks for your notes.

            You could consider possibly separating the logic in the script to calculate some logic OnEachTick and calculate other logic OnBarClose to accomplish your goals.

            Logic can be separated between Calculate.OnEachTick and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnEachTick processing.

            Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values on each tick and others only on the close of a bar. You will set your host script to Calculate.OnEachTick and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnEachTick outside of the IsFirstTickOfBar condition check.

            SampleEnterOnceExitEveryTick -https://ninjatrader.com/support/help...either_cal.htm
            Last edited by NinjaTrader_BrandonH; 12-11-2023, 11:08 AM.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hi Brandon,

              Wonderful! Should be the solution I need.

              But the sample link is "Page Not Found ..."

              Cheers,
              Steven

              Comment


                #8
                Hello Steven,

                Thanks for your notes.

                Attached below is the correct link to the help guide reference sample.


                I have also corrected the link in post # 6.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Hello Brandon,

                  Thank you for your reply. Questions first.

                  1. How do I change the width/color of a manually drawn Line? I successfully set the tag, but found no where to change the color and and width.
                  2. Or - can I move a line (change its positions, extend it, ...) that's drawn by the script, as I can do a manual line)? Is a script generated line always locked as I saw it?
                  3. I tested Calculate.OnEachTick but it did not work (yet). I believe the technique you suggested is something like below yellow circled.

                  Click image for larger version

Name:	image.png
Views:	118
Size:	266.1 KB
ID:	1281906​​
                  But the output suggests it's still calculated on bar close.

                  Click image for larger version

Name:	image.png
Views:	115
Size:	328.3 KB
ID:	1281907
                  Kindly advise. Really need help here. My script is also attached just in case it's useful.

                  Last but not the least, strangely the link still brings me to Page Not Found. But if you intended to direct me to below sample I've found it in the Help Guide.

                  protected override void OnBarUpdate()
                  {
                  // Only process entry signals on a bar by bar basis (not tick by tick)
                  if (IsFirstTickOfBar)
                  {
                  if (CCI(20)[1] < -250)
                  EnterLong();
                  return;
                  }
                  // Process exit signals tick by tick
                  if (CCI(20)[0] > 250)
                  ExitLong();

                  }

                  Grateful, as always.

                  Best Regards,
                  Steven
                  Attached Files

                  Comment


                    #10
                    Hello Steven,

                    Thanks for your notes.

                    This help guide page demonstrates detecting a draw object drawn on a chart and changing the color of that line: https://ninjatrader.com/support/help...rawobjects.htm

                    In the sample code on that help guide page, globalLine.Stroke.Brush is used to change the color of the line to black. To change the width, globalLine.Stroke.Width could be used.​

                    For example:
                    Code:
                    foreach (DrawingTool draw in DrawObjects.ToList())
                    {
                        if (draw is DrawingTools.Line)
                        {              
                    
                            DrawingTools.Line myLine = draw as DrawingTools.Line;
                    
                            myLine.Stroke.Brush = Brushes.Red;
                            myLine.Stroke.Width = 5;
                    
                            Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
                            Print("Color: " + myLine.Stroke.Brush + " Width: " + myLine.Stroke.Width);
                        }
                    }​
                    You may also consider drawing the line from the script using the Draw.Line() method.

                    You could call the Draw.Line() method again in the script using the same Tag name as the draw object you want to modify to draw the line on the chart with different color/width.

                    To change where a line is drawn i ncode, you could call the Draw.Line() method again in a script and pass in the same Tag name as the draw object you want to move on the chart.

                    From the Draw.Line() help guide: "For example, if you pass in a value of "myTag", each time this tag is used, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time."

                    Draw.Line(): https://ninjatrader.com/support/help.../draw_line.htm

                    Regarding calculating some logic OnEachTick and other logic OnBarClose, you must set the script to use Calculate.OnEachTick, place any logic you want to calculate OnBarClose within the IsFirstTickOfBar condition, and then place all code you want to calculate OnEachTick outside of the IsFirstTickOfBar condition.

                    See this reference sample for an example of calculating some logic OnEachTick and some logic OnBarClose: https://ninjatrader.com/support/help...either_cal.htm

                    Further, you could find the reference sample in the help guide by going to the NinjaScript section > Educational Resources section > Reference Samples section > Strategy section > Separating logic to either calculate once on bar close or on every tick.

                    I also attached the reference sample to this post.
                    Attached Files
                    Last edited by NinjaTrader_BrandonH; 12-12-2023, 09:53 AM.
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Hi Brandon,

                      Thank you so much for the information. Great help. I'm now able to set the tag/color/width of a manually drawn line. Also the by-tick logic is running. Wish me lucks. :-)

                      Regards,
                      Steven

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      64 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      139 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      75 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      45 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      50 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X