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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        46 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        22 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        31 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        50 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Working...
        X