Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Will this work in Initialize?

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

    Will this work in Initialize?

    Hi guys

    I've read the help on this but I'm still not 100% sure about this.

    Is it the case that if I enter say the following in the Initialize method:

    ProfitNow = Position.GetProfitLoss(Close[0], PerformanceUnit.Points);

    then in the OnBarUpdate method, whenever ProfitNow is read, the program understands Position.GetProfitLoss(Close[0], PerformanceUnit.Points)?

    (ProfitNow will of course be declared in Variables.)

    If I understand it right, the Initialize method is the place to make 'substitutions', as it were, which are then valid universally.

    Thanks for your help with this.

    #2
    arburthnot,

    In this case ProfitNow should only get set to that value once. I would suggest calling Position.GetProfitLoss(Close[0], PerformanceUnit.Points) each time you want to get this value.

    There are some things that can be set in the initialize method which are defined as part of the Indicator or Strategy class, however its probably more appropriate to think of the Initialize() method as a place to initialize variables to what you want them to be at the very start of the strategy or indicator.

    Some variables have a default global setting so you don't have to set them in Initialize(), but if you define custom variables they wouldn't have a default setting unless you made it that way either when you declare it, or set it in the Initialize() method.

    In your case, you are "Initializing" the ProfitNow to the current profit loss, which would either not exist or be 0.

    Examples like CalculateOnBarClose default to True, but can be overridden to be false if you set them in the Initialize() section. The same is true of other variables as well.

    Please let me know if I may clarify anything further.
    Last edited by NinjaTrader_AdamP; 05-23-2012, 02:06 PM.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      That's a terrific explanation, Adam. I understand the position completely - and my strategy would never have worked at all!

      It's just that Position.GetProfitLoss(Close[0], PerformanceUnit.Points) is so unwieldy and I was hoping to have something shorter for most of the coding. But from what you're saying, there is no way in C# of making universal substitutions for functions. What a shame...

      Coming to think of it, that's why Initialize is so-called...

      Much obliged as always.
      Last edited by arbuthnot; 05-23-2012, 02:22 PM.

      Comment


        #4
        arbuthnot,

        If you do something like this :

        private override void OnBarUpdate()
        {

        if(Position.MarketPosition != MarketPosition.Flat)
        {
        ProfitNow = Position.GetProfitLoss(Close[0], PerformanceUnit.Points);
        }

        // all my other code here

        }


        You could basically refer to ProfitNow throughout the rest of your code. You just may need to check you are in a position first before using it so you know that its valid.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Thanks very much again, Adam. I've learnt not only how to handle this kind of substitution but also quite a bit about C# itself.

          Comment

          Latest Posts

          Collapse

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