Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Generic classes, runtime error

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

    Generic classes, runtime error

    Hello,
    I want to use help-classes/generic classes when developing indicators. I have not found out a recommended way to do this within Ninjatrader environment, so my approach is to use generic classes as “help-indicators”. I will be thankful if anyone in the forum can recommend other ways of using generic classes 

    I run into problem when my “help-indicator” ( public class GetChartControlData : Indicator ) is called from my indicator ( public class ClickEvent : Indicator).

    The help-indicator will return some Chartpanel and ChartControl values:

    Code in help-indicator:
    if ((indicatorObj.ChartPanel != null) && (indicatorObj.ChartControl != null))
    {
    Print(indicatorObj.ToString());
    MinX = indicatorObj.ChartPanel.X;
    MaxX = MinX + indicatorObj.ChartPanel.W;
    MinY = indicatorObj.ChartPanel.Y;
    MaxY = MinY + indicatorObj.ChartPanel.H;
    Height = (int) indicatorObj.ChartControl.AxisXHeight;
    }
    (the last line “Height = … “ compile, but gives a runtime error: “Indicator 'ClickEvent': Error on calling 'OnBarUpdate' method on bar 0: The calling thread cannot access this object because a different thread owns it.” If I remove this line, everything works fine …)


    The help class is used like this in my “main-indicator”:

    private GetChartControlData GetChartControlData_Obj; // at variable decl

    GetChartControlData_Obj = new GetChartControlData(this); // at State.SetDefaults

    GetChartControlData_Obj.GetData(); // at OnBarUpdate()


    1. How should I do to avoid the runtime error?
    2. Any ideas of different ways of using generic classes?

    Thank you, AndBro, using: 8.0.0.11 64-bit (Multi-Broker) - BETA

    #2
    Hello AndBro,

    You can use the Addon folder and namespace (or use the Indicator namespace from a file in the Addon folder) to have classes used in multiple scripts.

    Below is a link to a thread where I have posted an example using static classes in an Addon.
    http://ninjatrader.com/support/forum...980#post442980

    You need to use the dispatcher of the chart and invoke into the thread of the chart to make any changes to the chart or get information about the chart.

    Below is a link to the help guide on Multi-Threading Consideration for NinjaScript.
    http://ninjatrader.com/support/helpG...-threading.htm

    Attached is an example I've made that changes the quantity in the ChartTrader area when a button is clicked. Invoking is necessary to add the buttons, and when the button is clicked to change the chart.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello AndBro,

      You can use the Addon folder and namespace (or use the Indicator namespace from a file in the Addon folder) to have classes used in multiple scripts.

      Below is a link to a thread where I have posted an example using static classes in an Addon.
      http://ninjatrader.com/support/forum...980#post442980

      You need to use the dispatcher of the chart and invoke into the thread of the chart to make any changes to the chart or get information about the chart.

      Below is a link to the help guide on Multi-Threading Consideration for NinjaScript.
      http://ninjatrader.com/support/helpG...-threading.htm

      Attached is an example I've made that changes the quantity in the ChartTrader area when a button is clicked. Invoking is necessary to add the buttons, and when the button is clicked to change the chart.
      The way that your code is written means that it will probably be subject to the spurious termination events as described in this thread.

      ref: http://ninjatrader.com/support/forum...ad.php?t=83608

      Comment


        #4
        ChelseaB: Thank you for a fast reply. I will read your links carefully, and rewrite my code.
        I’m very impressed that the customer service team are so quick and patient in communication with NT users. Very good 

        Koganam: Thank you for pointing out the “mystery” of an objects lifecycle in the NT world. I have not yet enough experience of this subject. However, I have seen strange things in my output window.
        - I still get a trace-print, from an indicator that no longer exists. No files, nothing, and everything is deleted … somehow, I’m afraid that object is still alive 
        - I got a warning (Visual Studio) that I have a conflict with “@AndrewsPitchfork.cs”. I don’t remember if it was a compile or a run time error.It was something about Line ??? (I have never touched anything about @AndrewsPitchfork.cs).
        Anyway, I have not payed much attention to this yet. I tell you just for information.

        Comment


          #5
          Hi AndBro,

          Sometimes a file can end up in the wrong folder and make the print / log calls hard to find.
          Other times, they can be buried in regions.

          I'm happy to assist and look for the script causing the print. If you would like to schedule a call please send an email to platformsupport [at] ninjatrader [dot] com. In the email please include a link to this forum thread.

          With the visual studio warning, this could mean that a file is missing or has been replaced by a different version or may just be warning that a class being called is ambiguous.

          Do you get the same error in NinjaTrader's NinjaScript Editor?
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cls71, Today, 04:45 AM
          0 responses
          1 view
          0 likes
          Last Post cls71
          by cls71
           
          Started by mjairg, 07-20-2023, 11:57 PM
          3 responses
          213 views
          1 like
          Last Post PaulMohn  
          Started by TheWhiteDragon, 01-21-2019, 12:44 PM
          4 responses
          544 views
          0 likes
          Last Post PaulMohn  
          Started by GLFX005, Today, 03:23 AM
          0 responses
          3 views
          0 likes
          Last Post GLFX005
          by GLFX005
           
          Started by XXtrader, Yesterday, 11:30 PM
          2 responses
          12 views
          0 likes
          Last Post XXtrader  
          Working...
          X