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

Problem with MarketPosition state

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

    Problem with MarketPosition state

    Hi everyone,
    I have a problem using market position state. I have an enterLong position which is open when specific onditions are meet. I want to verify while the long position is ON, i wanna do somme calculation so can i exit the position if a conidtion is meet.
    I used just for example ( Block is in the onBarUpdate):
    While( Position.MarketPosition== MarketPosition.Long)
    {
    Print("Position Ok");
    }
    But when i apply the strategy it doesn't return anything on the output

    #2
    NinjaTrader and it's NinjaScript framework is event driven.

    But your code sample shows an infinite loop -- I mean, it
    looks like you never exit that loop, is that correct?

    If you don't exit that loop and return, so that OnBarUpdate
    returns control back to the internals of NinjaTrader, then the
    internals of NinjaTrader cannot process any additional new
    events and update the value of the Position object.

    There is a problem with your thinking of how things work.

    You'll need to completely change your concept of how to 'verify
    while the Long posiiton is active' -- the entire paradigm of event
    driven breaks down when you have an infinite loop like that.

    Event driven programming is a cooperative style of programming.

    Code inside the internals of NinjaTrader detect the events and then
    that code calls your code via OnBarUpdate, OnOrderUpdate, and
    OnExecutionUpdate, etc, etc. All those methods are called when
    NinjaTrader internal code detects the events -- but you must allow
    those methods to return back to NinjaTrader
    , so that new events
    can be processed.

    Inside OnBarUpdate(), try using a simple if statement to detect
    the Long position, not a while loop.

    Comment


      #3
      thanks bltdavid,
      I didn't understand you very good, but the while loop should end on the exit session. the code within the while loop is just an example, my goal is to verify while my long position is open, that the takeprofit is growing so i can exit the position if i begin to lose!
      is there a way to check just the long position?

      Comment


        #4
        Originally posted by KacemRhaz View Post
        thanks bltdavid,
        I didn't understand you very good, but the while loop should end on the exit session. the code within the while loop is just an example, my goal is to verify while my long position is open, that the takeprofit is growing so i can exit the position if i begin to lose!
        is there a way to check just the long position?
        No, your comments I highlighted in red still show incorrect thinking.

        That thinking is extremely wrong because that is not how event driven
        programming works.

        Let me ask you this: If you don't return from OnBarUpdate, how do you
        expect NinjaTrader to regain control and process new events?

        And let's ask this same question for the other event driven callbacks,
        such as OnStateChange, OnOrderUpdate, etc. If you use an infinite
        loop, or a while loop that only ends on the exit session -- how is NT
        ever going to process new events?

        It is not the code within the while loop that is the problem.

        The while loop itself is the problem -- it never exits -- if a loop
        never exits, it's called an infinite loop, and is a classic example of
        a beginner's programming bug. It shows a lack of understanding
        of the sequential nature of programming -- but NinjaTrader requires
        more than sequential understanding, it also requires an understanding
        of event driven programming.

        (EDIT: You don't need to use return to 'return from OnBarUpdate'. You
        just have to make sure the sequential nature of execution is returned back
        to the caller of OnBarUpdate -- this idea of sequential execution is basic
        programming knowledge. The problem is: a while loop that never exits
        does not allow a natural sequential return back to the internal code that
        called the OnBarUpdate. If you wish to program NinjaScript, you must
        understand this concept. A while loop that never exits is a bug.)

        (EDIT: It may sound like I'm chastising you, or being discourteous, but
        I'm not being rude, I'm just being blunt. It appears you may need to learn
        more about programming to really understand all of this. I enjoy helping
        others understand these things, so I'm trying to help fix what appears to be
        a deficiency of fundamental knowledge.)

        Forget using a while loop.

        There is already an event driven 'loop' executing at a much higher level.

        Try something like this,

        Code:
        protected override void OnBarUpdate()
        {
        [COLOR=#e74c3c]   if [/COLOR](Position.MarketPosition == MarketPosition.Long)
            {
                double CurrPNL = **calculate current profit here**;
        [COLOR=#e74c3c]       if[/COLOR] (CurrPNL > **maximum loss**)
                   ExitLong();
            }
        }

        See those if statements in red? They must be if statements,
        because the if statement allows the OnBarUpdate() method
        to return back to NinjaTrader. The return back to NinjaTrader
        happens naturally (aka natural sequential statement execution
        of the calling of subroutines) despite no explicit use of return.

        This concept of 'returning back to NinjaTrader' is absolutely critical
        to making NinjaScript indicators and strategies work.

        Using a while loop rather than an if statement does not work there.

        A while loop would loop forever (unless you explicitly designed a
        way to exit the loop), never returning back to NinjaTrader.

        The moral of the story is this:
        It is OnBarUpdate's return back to NinjaTrader (implicit or explicit)
        that allows the next incoming ticks to be processed, which allows
        your position to be updated, which allows the next bar to close,
        which allows the next OnBarUpdate to be called. The choice of
        'if' vs 'while' can be extremely critical to this flow.

        The real problem I'm trying to help you overcome is this: The
        true problem is not the use of while loop, and not so much the
        contents of the while loop -- it's that your while loop never exits.
        This is the fundamental misunderstanding that must be corrected
        if you wish to advance your NinjaScript programming knowledge.
        Last edited by bltdavid; 08-23-2020, 05:20 PM.

        Comment


          #5
          Thank you very much mr @bltdavid for your deep explanations. Don't worry! you're a gentle man!
          I understand now my misuse of while loop and the problem is resolved!
          Thank you again!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Barry Milan, Today, 10:35 PM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by WeyldFalcon, 12-10-2020, 06:48 PM
          14 responses
          1,427 views
          0 likes
          Last Post Handclap0241  
          Started by DJ888, Yesterday, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          16 views
          0 likes
          Last Post bill2023  
          Working...
          X