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

Account Positions not being called

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

    Account Positions not being called

    Hello,

    I use an unsupported piece of code to enter the acoounts data. This code worked fine to enter the account position quantity. I test my complete code always in market replay, in which I also have seen this piece of code did the job multiple times. From the one test on the other it did not work anymore and I can't recall what I have changed (it's a very large code on which I work on different areas at the the same time). But I do know for sure the problem is in the calling of the positions, as I tested with print statements the consequative steps in the account quering code.

    The accounts are being called, but the positions are not. It does not differ if I add a test for Positions != null of == null. But without this test It has worked previously also.

    Do you have any idea where to look at? Could it be something with market replay? I have resetted the db's, restarted NT multiple times, but the problem still exist and after the foreach Positions nothing is called anymore. When I deactivate the total foreach the rest of the code works fine again. I have tried anything I can think of, without any success.

    foreach (Account acct in Cbi.Globals.Accounts)
    {
    Print("Accounts are called");
    PositionCollection positions = acct.Positions;
    foreach (Position pos in positions)
    {
    Print("Positions are called");
    int intEnterQuantity = pos.MarketPosition == MarketPosition.Short ? pos.Quantity : 1;

    ... code...
    }
    }


    Kind regards,
    Francis

    #2
    Hello Francis,

    Thank you for your post.

    Unfortunately, as this is an unsupported method for pulling the account positions there would be no documentation on this item. We would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively: http://www.ninjatrader.com/support/f...ead.php?t=3418

    For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - http://www.ninjatrader.com/support/f...ead.php?t=3627

    It may also help to add drawing objects to your chart for signal and condition confirmation - http://www.ninjatrader.com/support/f...ead.php?t=3419

    If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - http://www.ninjatrader.com/partners#...pt-Consultants

    Comment


      #3
      Hello Patrick,

      Thank you for your reply.

      I understand you can't really support in this, while the code itself is not officially supported, but because it might have something to do with NT or market replay I took my shot. Especially while it has worked previously.

      I will try and debug further. Maybe it will result in a positive outcome.

      Thanks anyway,

      Francis

      Comment


        #4
        It's quite obvious after some debugging that the following is not being called:

        PositionCollection positions = acct.Positions;
        foreach (Position pos in positions)

        All the above is being called, so NinjaTrader.Cbi.Account and NinjaTrader.Cbi.PositionCollection. The next step, PositionCollection positions, results in nothing.

        Is there anybody who can throw me a bone here? NT won't support this, while I think it just should be a basic function of NT to query the account parameters. I am clearly not the only user who needs this info for a strategy. How can one hedge for example if there is no way to retrieve the open positions outside the strategy itself? Or NT should work something out one can trade against your own positiona WITHIN a strategy without flattening your position first. I just need at least two strategies (long and short) to overcome this shortcoming. But when it is also not supported to retrieve the account quantity, while stated in one of the control center tabs, one keeps to program work arounds for quite plain situations.

        Comment


          #5
          It's quite obvious after some debugging that the following is not being called:

          PositionCollection positions = acct.Positions;
          foreach (Position pos in positions)

          All the above is being called, so NinjaTrader.Cbi.Account and NinjaTrader.Cbi.PositionCollection. The next step, PositionCollection positions, results in nothing.

          Is there anybody who can throw me a bone here? NT won't support this, while I think it just should be a basic function of NT to query the account parameters. I am clearly not the only user who needs this info for a strategy. How can one hedge for example if there is no way to retrieve the open positions outside the strategy itself? Or NT should work something out one can trade against your own positiona WITHIN a strategy without flattening your position first. I just need at least two strategies (long and short) to overcome this shortcoming. But when it is also not supported to retrieve the account quantity, while stated in one of the control center tabs, one keeps to program work arounds for quite plain situations.

          Comment


            #6
            Hello,

            It seems that the calling of PositionCollection positions = acct.Positions; and foreach (Position pos in positions) needs a position first, before it can be called for an initial time. When I run a strategy at the same time without this piece of code and this other strategy triggeres an execution, from that point on the calling of the PositionCollection works as supposed.

            It is not solved buy adding a test for null, 0 or what so ever on the pos collection. The only place where to put this test in after the PositionCollection positions = acct.Positions; in my believe, but that's out of reach when no position is placed first.

            Is there any way to overcome this? Can the PositionCollection be fooled or somthing that there has been a previous or running position. For further entering en exiting of strategy positions I can substract this initial (ghost) position so it will not affect real quantity of trading positions.

            Francis

            Comment


              #7
              Originally posted by Francis View Post
              Hello,

              It seems that the calling of PositionCollection positions = acct.Positions; and foreach (Position pos in positions) needs a position first, before it can be called for an initial time. When I run a strategy at the same time without this piece of code and this other strategy triggeres an execution, from that point on the calling of the PositionCollection works as supposed.

              It is not solved buy adding a test for null, 0 or what so ever on the pos collection. The only place where to put this test in after the PositionCollection positions = acct.Positions; in my believe, but that's out of reach when no position is placed first.

              Is there any way to overcome this? Can the PositionCollection be fooled or somthing that there has been a previous or running position. For further entering en exiting of strategy positions I can substract this initial (ghost) position so it will not affect real quantity of trading positions.

              Francis
              I am a bit confused here. If there are no positions to query, there are no positions to query, and your code is telling you so. Why then do you need to create a fake position at any time?

              It seems to me that you just have to wait until there actually is a position, and your code will return any such position. Why not just put in some tracking variable that tells you that no position exists, because the code returned none? No?

              Comment


                #8
                Hello koganam,

                Thank you for your reply.

                I understand you might be confused.I expected that the both foreach parts would result any of the position posibility, so Long, Short and Flat, but it seems that when the real account is Flat, this is literally seen as no positions, without pushing this forward as a foreach (MarketPosition.Flat). So the code wil break until it's last bracket.
                As I wanted to use this for position size purposes, or more like hedging, I let the PositionCollection tell an int what this int should be. This int was part of the foreach code, as well was that code holding the entering of a position, as the int can't be used after the foreach brackets. Becasue I always start flat, the strategy would never enter a first position.

                I have this tackled by adding a variable (private int intEnterQuantity = 1. On a OnBarUpdate I tel this intEnterQuantity if the 1 still holds, or that the number has to altered, which is done with the foreach on the PositionCollection:

                foreach (Account acct in Cbi.Globals.Accounts)
                {
                PositionCollection positions = acct.Positions;

                if (acct.Positions == null)
                {
                intEnterQuantity = 1;
                }
                if (acct.Positions != null)
                {
                foreach (Position pos in positions)
                {
                intEnterQuantity = pos.MarketPosition == MarketPosition.Long ? pos.Quantity : 1;
                }
                }
                }

                ...code...

                Maybe this is even more elegant and it makes sure the rest of the code is processed also, what is needed because it contains the exit parameters.

                Kind regards,
                Francis

                Comment


                  #9
                  Originally posted by Francis View Post
                  Hello koganam,

                  Thank you for your reply.

                  I understand you might be confused.I expected that the both foreach parts would result any of the position posibility, so Long, Short and Flat, but it seems that when the real account is Flat, this is literally seen as no positions, without pushing this forward as a foreach (MarketPosition.Flat). So the code wil break until it's last bracket.
                  As I wanted to use this for position size purposes, or more like hedging, I let the PositionCollection tell an int what this int should be. This int was part of the foreach code, as well was that code holding the entering of a position, as the int can't be used after the foreach brackets. Becasue I always start flat, the strategy would never enter a first position.

                  I have this tackled by adding a variable (private int intEnterQuantity = 1. On a OnBarUpdate I tel this intEnterQuantity if the 1 still holds, or that the number has to altered, which is done with the foreach on the PositionCollection:

                  foreach (Account acct in Cbi.Globals.Accounts)
                  {
                  PositionCollection positions = acct.Positions;

                  if (acct.Positions == null)
                  {
                  intEnterQuantity = 1;
                  }
                  if (acct.Positions != null)
                  {
                  foreach (Position pos in positions)
                  {
                  intEnterQuantity = pos.MarketPosition == MarketPosition.Long ? pos.Quantity : 1;
                  }
                  }
                  }

                  ...code...

                  Maybe this is even more elegant and it makes sure the rest of the code is processed also, what is needed because it contains the exit parameters.

                  Kind regards,
                  Francis
                  Effectively what I proposed then, using a tracking variable?

                  Now that you explain it, I think I may understand, but I should also point out that a Flat position is actually not a position at all. I know that as traders, we think being in cash (i.e., Flat) is a position, but in reality if no money is being risked, and in a position to be lost, (pun intended) are we really in a position?

                  In any case, I am glad to hear that the code is running in the manner that you want.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by bmartz, Today, 09:30 AM
                  2 responses
                  11 views
                  0 likes
                  Last Post bltdavid  
                  Started by f.saeidi, Today, 11:02 AM
                  1 response
                  2 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by geotrades1, Today, 10:02 AM
                  4 responses
                  12 views
                  0 likes
                  Last Post geotrades1  
                  Started by rajendrasubedi2023, Today, 09:50 AM
                  3 responses
                  16 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by lorem, Today, 09:18 AM
                  2 responses
                  11 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X