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:


    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.

    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.

          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.

              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.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    75 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    146 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    79 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    50 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    54 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X