Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mouse Up Event

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

    Mouse Up Event

    Given the following code I have two questions.

    After the mouse up event fires:

    1. Why would CurrentBar equal -1

    2. Why does an "Index was out range" error get thrown when accessing Close[0] when the Close count is greater than zero?

    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.ComponentModel;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Diagnostics;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing.Drawing2D;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Xml.Serialization;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] NinjaTrader.Cbi;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] NinjaTrader.Data;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] NinjaTrader.Gui.Chart;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] NinjaTrader.Indicator[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]kpMouseUp[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Indicator[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _init = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Initialize()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]CalculateOnBarClose = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]Overlay = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]PriceTypeSupported = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MyMouseUpEvent([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MouseEventArgs[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][SIZE=2]Print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"MyMouseUpEvent CurrentBar={0} Close.Count={1} Bars.Count={2}"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2], CurrentBar, Close.Count, Bars.Count));[/SIZE]
     
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]try[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    [SIZE=2]Print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Close[0]={0:N2}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Close[0]));[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Exception[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ex)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]Print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Exception occured: {0}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], ex.Message));[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OnBarUpdate()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (CurrentBar == Bars.Count - 2)[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ((ChartControl != [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) && (!_init))[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ClearOutputWindow();[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ChartControl.ChartPanel.MouseUp += [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MouseEventHandler[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](MyMouseUpEvent);[/SIZE]
    [SIZE=2]_init = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]Print([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Events Initialized"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Dispose()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (ChartControl != [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ChartControl.ChartPanel.MouseUp -= MyMouseUpEvent;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]base[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Dispose();[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    Thanks, Ken
    Attached Files

    #2
    hemlock,

    You are using a lot of unsupported methods and properties and I cannot be of much assistance. All I can suggest is for you to check your if (CurrentBar...) statement.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      The if (CurrentBar...) statement in the OnBarUpdate event is not what I am having problems with, it is fine.

      Not sure if I agree with you that I am using "a lot" of unsupported methods and properties. I am adding an event handler to a normal C# MouseUp event. I realize that you as a support staff do not handle/help with C# programming and that is fine. What I am asking is why the documented NinjaTrader property CurrentBar returns -1 in my mouse up event and also why does referencing the documented NinjaTrader property Close[0] throw an index error when the Close.Count property contains a value greater than zero.

      You may not support or recommend the use of a mouse event handler but my questions relate to why do the documented Ninja properties not work within my event handler.

      Please don't get me wrong, I love NinjaTrader and I love it because I can do the things that I am trying to do here. For me that is what separates Ninja from the rest of the pack.

      Ken

      Comment


        #4
        As mentioned, the use of things like Bars.Count are not supported. Same with Close.Count.

        You need to check your if (CurrentBar...) statement because we do not support Bars.Count. You need to see what CurrentBar is evaluate to at that point in time, not what it is when you do a mouse event.

        If you wanted some type of total bar counter you should use "Count". Then remember CurrentBar is guaranteed to be <= Count - 1.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hi Ken,

          I found it dangerous to access CurrentBar outside from OnBarUpdate(), in my case by a context menu extension. I loaded a chart with historical data (no live connection), processed the context menu, and saw CurrentBar = -1 too. So, for this purpose I kept track of the system's status (i.e. saved the number of bars loaded)

          Regards
          Ralph

          Comment


            #6
            Originally posted by NinjaTrader_Josh View Post
            As mentioned, the use of things like Bars.Count are not supported. Same with Close.Count.

            You need to check your if (CurrentBar...) statement because we do not support Bars.Count. You need to see what CurrentBar is evaluate to at that point in time, not what it is when you do a mouse event.

            If you wanted some type of total bar counter you should use "Count". Then remember CurrentBar is guaranteed to be <= Count - 1.
            Josh,

            I just tested this and Close.Count, Bars.Count and Count all return the same value (1043 in this instance) at the time of the mouse up event.

            Also, Bars.Count is used in numerous standard Ninja indicators, @ZigZag.cs for one, so it would appear to be supported.

            I guess the answer then is like Ralph says. Using CurrentBar or any other Ninja properties outside of the standard Ninja indicator events (ie. OnBarUpdate, etc...) is dangerous and unpredictable. This is a little disappointing in that it limits the extensibility of Ninja.

            Ken

            Comment


              #7
              hemlock,

              As mentioned, they are not supported. What is supported is what is in the Help Guide. NinjaTrader indicators do make use of unsupported features.

              Offering support for methods and properties outside the scope of what NinjaTrader provides is something we just cannot offer support for. You can try Ralph's suggestion though.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by Ralph View Post
                Hi Ken,

                I found it dangerous to access CurrentBar outside from OnBarUpdate(), in my case by a context menu extension. I loaded a chart with historical data (no live connection), processed the context menu, and saw CurrentBar = -1 too. So, for this purpose I kept track of the system's status (i.e. saved the number of bars loaded)

                Regards
                Ralph
                Ralph,

                Thanks for the input.

                I was trying to call the DrawText method in my mouse up event.

                DrawText uses a "barsAgo" parameter which I had set at 100 and this error was being thrown "DrawText: startBarsAgo out of valid range 0 through -1, was 100". That led to determining that CurrentBar was -1 even though "Count" was 1043. So in this case, "CurrentBar is guaranteed to be <= Count - 1" is not true. Again it must be because I am accessing Ninja properties outside of the standard events. But it would really be cool if we could.

                Ken

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  hemlock,

                  As mentioned, they are not supported. What is supported is what is in the Help Guide. NinjaTrader indicators do make use of unsupported features.

                  Offering support for methods and properties outside the scope of what NinjaTrader provides is something we just cannot offer support for. You can try Ralph's suggestion though.
                  Thanks Josh, understood and I appreciate your help. Although it might be a bit of a bug when Count = 1043 and you can't access Close[0] even though it is happening in a "unsupported" event handler. Maybe pass that along to development.

                  Thanks again for the assistance.

                  Comment


                    #10
                    I have forwarded this to development.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Ken,

                      had a look at my code with the C#-events again. I used BarsGet(index) and Bars.Count. It works reliable, never had a problem with that. And it is a clean coding style in my opinion because one accesses the data from the source. But yes, it is outside the scope of support. Should the NT-guys change their data concept somewhen in the future, then I guess I still could get the necessary informations from the indicators' source code.

                      Regards
                      Ralph

                      Comment


                        #12
                        Hey Ralph,

                        Thanks for the additional feedback. At this point I don't have a need to access bar values from my mouse up event. The example in my first post was using Close[0] to illustrate some of the issues I found when I was debugging what I was actually trying to do in the event.

                        I was trapping the mouse up event to enumerate the ChartObjects collection looking for horizontal lines that might be on the chart. When one was found I was using the DrawText function to label the line with the price where the line was drawn. As I said in an earlier post the DrawText function failed because the barsAgo parameter was not within the bounds of the number of bars Ninja thought it had at the time. By trapping the mouse up event, a line would be relabeled when it was moved.

                        The horizontal line labeling code works fine if called from the OnBarUpdate event but I wanted a line relabeled immedialtely after it was moved instead of waiting for the OnBarUpdate event to fire. Of course I could set CalculateOnBarClose to false but it seems silly to relabel the lines on every tick.

                        Maybe NT7 will label horizontal lines for us. Another thing I am hopeful for in NT7 in access to the "tag" property of a ChartObject (ie. access the ChartObjects collection via the original tag string).

                        Ken

                        Comment


                          #13
                          Hi Ken,

                          understand your problem. To solve that task some coding would be required. The only way I know of would be to extend the Plot()-method and use ChartGraphics.DrawString(). But that requires that you calculate the canvas coordinates for the text string by your own.

                          Regards
                          Ralph

                          Comment


                            #14
                            Originally posted by hemlock View Post
                            I was trapping the mouse up event to enumerate the ChartObjects collection looking for horizontal lines that might be on the chart. When one was found I was using the DrawText function to label the line with the price where the line was drawn. As I said in an earlier post the DrawText function failed because the barsAgo parameter was not within the bounds of the number of bars Ninja thought it had at the time. By trapping the mouse up event, a line would be relabeled when it was moved.

                            The horizontal line labeling code works fine if called from the OnBarUpdate event but I wanted a line relabeled immediately after it was moved instead of waiting for the OnBarUpdate event to fire. Of course I could set CalculateOnBarClose to false but it seems silly to relabel the lines on every tick.
                            Ken, You have some good ideas here. Did you perchance try invoking a TriggerCustomEvent from your MouseUpEvent handler? I'm not sure that this would work, but it might be worth a try. I tried something different from this in some code that I was writing and couldn't get it to work, and I was wondering if this was because my skills in this area are weak, or because Ninja just isn't made to do this, in spite of my wanting to give it the benefit of the doubt.

                            (I made a few more comments on this in this other thread: http://www.ninjatrader-support2.com/...ad.php?t=14474.)

                            Comment


                              #15
                              KBJ,

                              I had not tried using TriggerCustomEvent from my MouseUpEvent handler and must admit that I was not even aware of the method. I have now since tried it and it solved my problem and appears to work as advertised. Thanks for the tip KBL, much appreciated.

                              Ken

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              649 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              576 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X