Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Casting Error

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

    Casting Error

    Hello,

    I am getting this error and want to know if there is a way around it.

    I have an indicator that is looking through the strategies on the chart and saving one so that it can be edited but I keep getting this casting error because the strategy I am trying to save is saying it is not of type strategy. Here is the code I am using to get the strategy

    if(ChartControl != null && strat == null)
    {
    foreach(Strategy s in ChartControl.Strategies)
    {
    if(s.Name.ToUpper() == StrategyName.ToUpper())
    {
    strat = s;
    }
    }

    }​

    I also tried

    if(ChartControl != null && strat == null)
    {
    foreach(var s in ChartControl.Strategies)
    {
    if(s.Name.ToUpper() == StrategyName.ToUpper())
    {
    strat = (Strategy) s;
    }
    }
    }​​

    But I keep getting this error
    Error on calling 'OnBarUpdate' method on bar 5038: Unable to cast object of type 'NinjaTrader.NinjaScript.Strategies.ARC.ARC_VABOAl go' to type 'NinjaTrader.NinjaScript.Strategies.Strategy

    Any idea on how I can cast and not get this error?

    Thanks!

    #2
    Hello Vikuno1,

    You may need to cast as StrategyRenderBase as this is the collection type.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      It says that type or namespace cannot be found. What library do I need to use?

      Will I still be able to access the position of that strategy?

      Thanks for the speedy reply!

      Comment


        #4
        Hello Vikuno1,

        NinjaTrader.Gui.NinjaScript.StrategyRenderBase

        "Will I still be able to access the position of that strategy?"

        This is not a supported approach so this would not be guaranteed.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ok so I realize it may not be supported but wanted to ask anyway.

          I was able to not cast and just grab the current strategy on the chart with ChartControl.Strategies[0]

          From there I can Exit/Enter in my indicator by using commands like so "ChartControl.Strategies[0].ExitShort("Blah", "");"

          It works for the Sample MA Crossover but when using it on a specific strategy I get the error that is attached below. Ignore the tooltip just look at the orange error

          Any ideas?

          Attached Files

          Comment


            #6
            Hello Vikuno1,

            The error appears to state that a Profit target order does not have the same quantity of the position.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yes, that is what it says.

              I am using the function
              ExitLong()

              so it should automatically calculate the entire position. Also it actually does exit the entire position but then will throw that error and disable the strategy

              Comment


                #8
                Hello Vikuno1,

                Is this at the same time a profit target is working?

                If so, use Exit orders for the profit target and cancel the order before placing the ExitLong() order.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello Vikuno1,

                  Is this at the same time a profit target is working?

                  If so, use Exit orders for the profit target and cancel the order before placing the ExitLong() order.
                  I don't have access to the original strategy, I am just trying to have it exit based on my own logic. So I should try and cancel the stop and profit target that it uses first and then market order exit?

                  Comment


                    #10
                    Hello Vikuno1,

                    Yes, use Exit orders for the stop and target, cancel these before sending a replacement order.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

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