• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the direction of a Position?

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

    How to get the direction of a Position?

    I tried something like this but I can't access Long...

    I want to count the # of long positions

    protected override void OnBarUpdate()
    {

    foreach (Position pos in Positions) {
    if (pos.Instrument == Instrument && pos.MarketPosition.Long ) {

    }
    }

    #2
    Hello NinjaCustomer,

    Thank you for the question.

    It would instead be suggested to use the specific index with Positions collection rather than a loop for the instrument you want to target:
    https://ninjatrader.com/support/help...tsub=Positions

    The condition you created is also not complete to check the market position. Here is an example of checking a positions direction:

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
    https://ninjatrader.com/support/help...MarketPosition
    https://ninjatrader.com/support/help...s/position.htm

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      thanks, I'm also confused how to check in OnBarUpdate if I'm on the latest bar - I don't want to open positions while previous bars are loading

      Comment


        #4
        how do I get MarketPosition to exist in the current context?

        Comment


          #5
          Hello NinjaCustomer,

          If you want to wait until realtime, you can check the state:

          Code:
          if(State == State.Realtime)
          {
             // code here
          }

          If you are seeing an error that MarketPosition does not exist in the current context, you have likely placed the code somewhere code should not go or have incorrectly use MarketPosition. Can you provide a sample of what you coded? Without being able to see what you coded I could only guess at what the problem may be.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            well it's basically just what I wrote up there.. I'm trying to count the # of long positions from within OnBarUpdate()

            do you think that is not possible and I should set this as a variable from OnPositionUpdate() instead? and then access that variable in OnBarUpdate() ?

            Comment


              #7
              Hello NinjaCustomer,

              The code you originally provided was not valid, however, I provided some links which have some samples that are correct. Have you modified your code to be like the samples and now you are getting this error? Or was that the error you originally got with what you first provided?

              You will need to modify what you provided originally to look like the samples in the help guide. If you modified your code since the first post, I wouldn't know what you changed so I would need an updated example to see why you may be getting that error. As noted this may relate to placing the code in the wrong location in your script, so if you can include wherein the script you placed this code for example:
              Code:
              protected override void OnBarUpdate()
              {
                  if (Position.MarketPosition == MarketPosition.Long)
                  {}
              }
              That would be helpful.



              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                ah, sorry - it's late here and my eyes were tired. I didn't notice that I had a typo in MarketPosition.Long in my code

                Comment


                  #9
                  quick question - do I need to check the instrument in a strategy in OnBarUpdate() or OnPositionUpdate() or are these only triggered by the instrument of the chart?

                  Comment


                    #10
                    Hello NinjaCustomer,

                    The orders your strategy observes would only be those which originate from that strategy, so this would just be the strategies orders exclusively. If you are using a single instrument, you would not need to do an instrument check. If you are using multiple instruments, you would generally need to do other checks to confirm you are referring to the right position. The Positions link I provided has some details on how to specify a specific instrument in your script using the Index.

                    https://ninjatrader.com/support/help...?positions.htm

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Tiang, Today, 04:23 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post Tiang
                    by Tiang
                     
                    Started by gbux11, Today, 02:41 AM
                    0 responses
                    8 views
                    0 likes
                    Last Post gbux11
                    by gbux11
                     
                    Started by TazoTodua, Today, 01:50 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post TazoTodua  
                    Started by lonestar, Today, 01:42 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post lonestar  
                    Started by CandySNK, Yesterday, 10:24 PM
                    1 response
                    21 views
                    0 likes
                    Last Post NinjaTrader_Marco  
                    Working...
                    X