Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How can I create a function to be called from onbarupdate

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

    How can I create a function to be called from onbarupdate

    Hi All,
    I am learning how to program and I am trying to reuse part of my code. I have duplicated chunk of code all over OnBarUpdate(). Instead of repeating this code I would like to create an object (or whatever proper name applies) and can call it when needed.

    Question 1: Creating a method is what I need? eg:

    Code:
    static void MyMethod()
    { Console.WriteLine("I just got executed!"); }
    Question 2: I should add this outside OnBarUpdate() under the public class of my strategy ?


    Question 3: and then call it from OnbarUpdate() like this?

    Code:
    MyMethod();

    Question 4: When trying to compile I am receiving error saying "An object reference is required for the on-static field, method or property...."

    Thanks in advance for your help.


    #2
    Hi Danilod, thanks for posting.

    Double click the error to see the line of code that is throwing this compile error. Also, use Print() instead of Console since the platform does not run with a console open.

    Best regards,
    -ChrisL
    Last edited by NinjaTrader_ChrisL; 01-14-2022, 09:56 AM.

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      The method can not be static because it is not within a static class
      You sure?
      Taken as a rule, that statement is incorrect.

      Comment


        #4
        Sorry, I was mistaken, thanks for correcting.

        Comment

        Latest Posts

        Collapse

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