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 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:


    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)



    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.



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

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Mindset, 05-06-2023, 09:03 PM
                    10 responses
                    262 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by michi08, 10-05-2018, 09:31 AM
                    5 responses
                    741 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by The_Sec, Today, 02:29 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post The_Sec
                    by The_Sec
                     
                    Started by tsantospinto, 04-12-2024, 07:04 PM
                    4 responses
                    62 views
                    0 likes
                    Last Post aligator  
                    Started by sightcareclickhere, Today, 01:55 PM
                    0 responses
                    1 view
                    0 likes
                    Last Post sightcareclickhere  
                    Working...
                    X