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

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.
    Chris L.NinjaTrader Customer Service

    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.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by andrewtrades, Today, 04:57 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        6 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        7 views
        0 likes
        Last Post FAQtrader  
        Started by rocketman7, Today, 09:41 AM
        5 responses
        19 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X