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

Dilemma with Delegates

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

    Dilemma with Delegates

    Hi, this is an extension to some work I was doing with NCalc in another thread, but not restricted to it.

    I'm trying to implement an array of delegates and I was doing pretty well until I ran into a problem.

    the code so far...

    // in variables section - my indicator name is WillsMTWave, and FunctionArgs is from class NCalc

    private FloatSeries[] wmtPlotValues;

    delegate void gwExtendedFunction(FunctionArgs args);
    gwExtendedFunction[] gwExtendedFunctions;
    GWExprEvaluatorFunctions gwExprEvaluatorFunctions;

    public class GWExprEvaluatorFunctions : NinjaTrader.Indicator.WillsMTWave {
    public void concaveCB(FunctionArgs args) {
    Print(wmtPlotValues[0][0].ToString());
    }
    }

    // in Initialize()

    wmtPlotValues = new FloatSeries[PLOTS];

    gwExprEvaluatorFunctions = new GWExprEvaluatorFunctions();
    gwExtendedFunctions = new gwExtendedFunction[3];
    gwExtendedFunctions[0] = new gwExtendedFunction(gwExprEvaluatorFunctions.concav eCB);

    // in OnBarUpdate()

    // call the delegate
    gwExtendedFunctions[0](args);

    Now, this actually all works fine (almost), I can call the delegate and get some correct debug out, including the parameters, but the line

    Print(wmtPlotValues[0][0].ToString());

    is giving the runtime error

    Error on calling 'OnBarUpdate' method for indicator 'WillsMTWave' on bar 22: Object reference not set to an instance of an object.

    bar 22 is the first bar for which it is called. Now strictly speaking the call gwExtendedFunctions[0](args) is not made from within OnBarUpdate(),
    it is actually made from another delegate function that is called from within OnBarUpdate(), I'm not sure if thats significant or not. I can correctly reference wmtPlotValues[0][0] from within OnBarUpdate(), its only when its referenced from the delegate that the error occurs.

    Anyone game to tackle this one for me?

    Thanks,
    will.

    #2
    Hello Will,

    Thank you for writing in. I am unsure if there is simply a typo in your code. Are you meaning to instantiate the wmtPlotValues FloatSeries using the PLOTS object or are you trying to instantiate a FloatSeries of PLOTS length?
    Please try the following:
    Code:
    wmtPlotValues = new FloatSeries(PLOTS);
    Please see our help guide here: http://ninjatrader.com/support/helpG...ub=floatseries

    Please provide me with more information/code concerning your PLOTS object if you require further investigation.

    Thank you in advance.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      hi, thanks, and sorry for the confusion. PLOTS is a constant defined earlier, I'm creating an array of FloatSeries (of size PLOTS).

      Thanks,

      edit....

      It must have something to do with the array declaration or instantiation, not the scope.

      If I do this :-

      // in variables section - my indicator name is WillsMTWave, and FunctionArgs is from class NCalc

      private FloatSeries[] wmtPlotValues = new FloatSeries[PLOTS];
      private int myInt=1;

      and in my deletgate the below gives the error I reported earlier :-

      public class GWExprEvaluatorFunctions : NinjaTrader.Indicator.WillsMTWave {
      public void concaveCB(FunctionArgs args) {
      Print(wmtPlotValues[0][0].ToString());
      }
      }


      but this works fine :-

      public class GWExprEvaluatorFunctions : NinjaTrader.Indicator.WillsMTWave {
      public void concaveCB(FunctionArgs args) {
      Print(myInt.ToString());
      }
      }
      Last edited by dontpanic; 08-26-2015, 04:14 PM.

      Comment


        #4
        Hello dontpanic,

        From what I understand, you are simply not instantiating the FloatSeries correctly. Please try the following:
        Code:
        private FloatSeries[] wmtPlotValues = new FloatSeries[PLOTS];
        
        //... and then somewhere in your code before you try and print anything from wmtPlotValues
        for(int i = 0; i < wmtPlotValues.Length; i++)
        {
            wmtPlotValues[i] = new FloatSeries(this);
        }
        If the issue persists, please provide an example code with which I can test this issue.
        Thank you in advance.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          Hi Michael

          yes I already have that code in Initialize()

          for(int i=0; i<PLOTS; i++) {
          wmtPlotValues[i] = new FloatSeries(this, MaximumBarsLookBack.Infinite);
          }

          I'm already using those dataseries in the indicator and they are working fine. Its just the delegate that is having a problem with it.

          Thanks
          will.

          Comment


            #6
            Hello Will,

            Please provide me with an full code example which illustrates the issue so I can test the issue properly and deliver you with a correct and complete answer. I recommend exporting the code using the export function of NinjaTrader, then attaching the .zip file to your response.

            If you do not wish to post the code on the forums, please attach it to an email to platformsupport[AT]ninjatrader[DOT]com with the subject line: "ATTN: Michael M http://www.ninjatrader.com/support/f...ad.php?t=77583 #1376619".

            Thank you in advance.
            Last edited by NinjaTrader_MichaelM; 08-28-2015, 06:46 AM.
            Michael M.NinjaTrader Quality Assurance

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by tsantospinto, 04-12-2024, 07:04 PM
            5 responses
            67 views
            0 likes
            Last Post tsantospinto  
            Started by cre8able, Today, 03:20 PM
            0 responses
            6 views
            0 likes
            Last Post cre8able  
            Started by Fran888, 02-16-2024, 10:48 AM
            3 responses
            49 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by martin70, 03-24-2023, 04:58 AM
            15 responses
            115 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by The_Sec, Today, 02:29 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X