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

Bars Ago / Draw Text Weirdness

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

    Bars Ago / Draw Text Weirdness

    I've been trying to find a bug for about 2 days. And in doing so, to confirm that I'm using correct bar indexes etc written some code to print the bar number below any bar I click on.

    In the attached screen clippings, you will see that I'm calling a RenderCandleNumber method, and passing in an args object with args.BarsAgo equal to 1 - I clicked the second last candle (the green one in the second clipping)

    I'm passing the args.BarsAgo value as the int barsAgo parameter of DrawText. I'm passing Low[args.BarsAgo] as the double y parameter to DrawText. The value is 1, so I expect it to print on the second bar from the end, and at the low of that bar.

    However as you will see in the attached screenshots, when clicking on the green candle indicated by the crosshair, the x-coordinate is printed as expected, beneath said candle. But the Y-coordinate is not at the low of that candle... it is actually at the low of the bar beside it.

    entering _indicator.Low[args.BarsAgo - 1] for the Y position and args.BarsAgo for the X position has the desired effect.

    I'm very confused. It seems that "bars ago" means one thing to Low and another thing to drawtext. Could someone explain?



    Attached Files

    #2
    Does Lows[0][barsAgo] do the same thing?

    And does it work in normal NT OnBarUpdate?

    I'm not exactly where in the process you are calling this from?

    Comment


      #3
      When I add

      DrawText(TagGenerator.CandleNumber(CurrentBar), CurrentBar.ToString(), 0, Low[0], Color.Aqua);
      To the OnBarUpdate method it works as expected. But not when I do it on the Click event on ChartPanel it doesn't. Any idea why that might that be the case?

      Comment


        #4
        Aha! I've discovered the TriggerCustomEvent page in the online docs

        This method provides a way to use your own custom events (such as a Timer object) so that internal NinjaScript indexes and pointers are correctly set prior to processing user code triggered by your custom event. When calling this event, NinjaTrader will synchronize all internal pointers and then call your custom event handler where your user code is located.
        I hope this method is a NinjaTrader relic from a long time ago before generic-types - but I fear not since they were added to .NET about a decade ago. Here's an improved version that avoids horrible type-casting from object in the handler:

        Add these methods to a static class ExtensionMethods and reference from your indicator....

        public static class ExtensionMethods
        {
        public static void TriggerCustomEvent<T>(this Indicator.Indicator indicator, EventHandler<T> handler, T args ) where T: EventArgs
        {
        indicator.TriggerCustomEvent(t => { handler.Invoke(indicator, args);}, args);
        }

        public static void TriggerCustomEvent<T>(this Indicator.Indicator indicator, EventHandler<T> handler, int barsInProgress, T args) where T : EventArgs
        {
        indicator.TriggerCustomEvent(t => { handler.Invoke(indicator, args); }, barsInProgress, args);
        }

        }

        Use as follows:

        Make a handler with the signature: public void MyHandler(object sender, MyEventArgs args)

        MyEventArgs should be a class which inherits from EventArgs e.g public class MyEventArgs : EventArgs { }


        then call the new TriggerCustomEvent with the TypeArguments: TriggerCustomEvent<MyEventArgs>(myHandlerMethod, args)

        myHandlerMethod can either be an event or a method delegate. If visual studio gets confused and can't find the new one with type arguments, rename the above to "NewTriggerCustomEvent" but it wasn't a problem for me.

        Comment


          #5
          Hi,
          Thanks for your post!

          Can you please upload a full usage example because I got a bit confused...

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by AveryFlynn, Today, 04:57 AM
          0 responses
          4 views
          0 likes
          Last Post AveryFlynn  
          Started by RubenCazorla, 08-30-2022, 06:36 AM
          3 responses
          77 views
          0 likes
          Last Post PaulMohn  
          Started by f.saeidi, Yesterday, 12:14 PM
          9 responses
          23 views
          0 likes
          Last Post f.saeidi  
          Started by Tim-c, Today, 03:54 AM
          0 responses
          3 views
          0 likes
          Last Post Tim-c
          by Tim-c
           
          Started by FrancisMorro, Today, 03:24 AM
          0 responses
          5 views
          0 likes
          Last Post FrancisMorro  
          Working...
          X