Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy using PositionAccount vs Strategy Position

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

    #31
    Hello DTSSTS,

    Thanks for your reply.

    Correct, you would want to use lock() in the same manner as shown in the help guide.
    For reference, here is a link to C# lock: https://docs.microsoft.com/en-us/dot...lock-statement

    In the help guide example, the first use of lock (in State.setDefaults) is to lock all accounts while your statement account = Account.All.FirstOrDefault(a => a.Name == AccountNametoTrade); searches for the account string found in AccountNameToTrade. Note that you are creating the object reference to account.

    In the next use in the help guide, in State.DataLoaded, you should now use lock(account.Positions)

    If I may suggest, I would not use account as an object name due to the many uses of account and it can also be confusing. The example uses myAccount to help differentiate.

    Comment


      #32
      you actually help me with the account, so you think I should change everything in my scripts to say myaccount

      will that include lock(account.Positions) to be lock(myaccount.Positions)

      and in my order does myaccount replace account.CreateOrder and account.Submit

      Order mar****rder = account.CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, OrderEntry.Automated, TimeInForce.Day, DefaultQuantity, 0, 0, string.Empty, "Auto Long", new DateTime(), null);
      account.Submit(new Order[] { mar****rder });

      NEXT IN THE FIRST USE OF lock in State.setDefaults) we did not use account = Account.All.FirstOrDefault(a => a.Name == AccountNametoTrade);

      we only had the one line AccountNametoTrade = null;

      the longer line was in the State.DataLoaded area,

      SO DO I NEED THE LONGER VERSION in both area

      thanks

      Comment


        #33
        Hello DTSSTS,

        Thanks for your reply.

        I provided a suggestion, you can decide if you want to follow it or not. You are the programmer of your code.

        "will that include lock(account.Positions) to be lock(myaccount.Positions)". The best answer is to study the help guide example and keep in mind that C# is case-sensitive.

        "and in my order does myaccount replace account.CreateOrder and account.Submit" Yes, you can see this in the example here: https://ninjatrader.com/support/help...reateorder.htm

        "SO DO I NEED THE LONGER VERSION in both area" I would again say to follow the help guide.

        Comment


          #34
          I changed all the account to myAccount, got rid of compile errors, but now when applied to my charts, have a log error and indicator not longer works

          Error on calling 'OnStateChange' method: Object reference not set to an instance of an object

          ONLY thing in OnStateChange area is the new lock

          lock(Account.All)
          AccountNametoTrade = null;

          i blocked the lock(Account.All) still have the error

          i then added the long line

          lock(Account.All)
          myAccount = Account.All.FirstOrDefault(a => a.Name == AccountNametoTrade);
          AccountNametoTrade = null;


          still have the error

          update: my mistake the OnStateChange as you know covers the State.DataLoaded area as well

          i think is likely this line

          if (myAccount != null)
          Account.OrderUpdate += Account_OrderUpdate;

          should this both be myAccount
          Last edited by DTSSTS; 12-13-2021, 01:27 PM.

          Comment


            #35
            the help guide does not address all these items in each area.

            Comment


              #36
              Hello DTSSTS,

              Thanks for your reply.

              Regarding: if (myAccount != null)
              Account.OrderUpdate += Account_OrderUpdate;


              I believe it should be: if (myAccount != null)
              myAccount.OrderUpdate += OnOrderUpdate;


              Comment


                #37
                this is what is causing NOT to work any longer and the log error and is located in the State.DataLoaded per the help guide example
                *******************************************
                // added all this for lock
                // {
                // lock(myAccount.Positions)
                //
                // {
                // Print("Positions in State.DataLoaded:");
                // myAccount = Account.All.FirstOrDefault(a => a.Name == AccountNametoTrade); // <<<< TRIED WITH AND WITHOUT THIS LINE as this was in my original working script
                // foreach (Position position in myAccount.Positions)
                // {
                // Print(String.Format("Position: {0} at {1}", position.MarketPosition, position.AveragePrice));
                // }
                // }
                // end added area for lock


                ********************************************

                https://ninjatrader.com/support/help...ns_account.htm

                update

                moved this up above these items and log error is now gone

                // myAccount = Account.All.FirstOrDefault(a => a.Name == AccountNametoTrade); // <<<< TRIED WITH AND WITHOUT THIS LINE as this was in my

                ************************************************** *

                however the purpose of all this code was to add a condition to reference the current position before triggering an order

                in which you suggested

                // first attempt * will not compile
                foreach (Position position in myAccount.Positions)
                {
                if (positiion.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)
                {
                Print(position.Quantity);
                }
                }

                WHICH STILL DOES NOT COMPILE FOR

                if (positiion.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)

                == cannot be applied to operands of the type NinjaTrade.Cbi.Instrument and method group
                thanks
                Last edited by DTSSTS; 12-13-2021, 02:15 PM.

                Comment


                  #38
                  Hello DTSSTS,

                  Thanks for your reply.

                  Getting back to what you want to do, to find the position of instruments, I've created a minimal example that you can test and study. In keeping it lite I named my account object Fred to help differentiate. Apply the strategy to a chart and open a ninjascript output window, place a few entries on various instruments and you will see the code pick up and print the position information.

                  AccountStuff.zip

                  If you continue to have compiler or run time errors, please advise what those specific errors are and what lines they relate to in your code.

                  Comment


                    #39
                    thanks, the only error for now is the use of == with Cbi I cannot find anything in forums or help guide about that

                    if (positiion.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)

                    the == gives the error CS0019

                    Comment


                      #40
                      thanks for the AccoutStuff.zip, I had already tried the OnPositionUpdate code without success as well

                      I dont really follow as None of the examples are anything like the code we have been viewing examples on or the example suggestions you have shared over the last few days

                      like this one



                      foreach (Position position in myAccount.Positions)
                      {
                      if (positiion.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)
                      {
                      Print(position.Quantity);
                      }
                      }

                      ***********************

                      nothing in the AccoutStuff shows anything like "foreach" line of code

                      So am I starting over and DO NOT NEED any of that code

                      thanks again

                      Comment


                        #41
                        ok items added and compiled no log errors - still cannot make the following statements

                        && (Position.MarketPosition != MarketPosition.Long)// HAD TO BLOCK BECAUSE OF ERRORS
                        && (Position.Quantity == 0)// HAD TO BLOCK BECAUSE OF ERRORS

                        or
                        && (myAccount.Position.MarketPosition != myAccount.Positions.MarketPosition.Long)// HAD TO BLOCK BECAUSE OF ERRORS
                        && (myAccount.Position.Quantity == 0)// HAD TO BLOCK BECAUSE OF ERRORS

                        error is NinjaTrader.Cbi.Account does not contain a definition for 'Position' and no extension method 'Position' accepting a first argument of type NinjaTrade.Cbi.Account could be found

                        still cannot use below because of == not allow with NinjaTradeer.Cbi

                        foreach (Position position in myAccount.Positions)// myAccount
                        {
                        if (position.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)
                        {
                        Print(position.Quantity);
                        }
                        }



                        thanks

                        Comment


                          #42
                          Hello DTSSTS,

                          Thanks for your reply.

                          In your foreach section, you are wanting (I think) to see if the instrument of the position is equal to some instrument. The GetInstrument() method requires an input and you are not providing an input. https://ninjatrader.com/support/help...instrument.htm However the method returns an object and I think you really only want to compare a name.

                          Do you know what instrument you are looking for?

                          Please note that the example I provided in post #38 is using a private OnPositionUpdate() and you may be confusing this with the strategy OnPositionUpDate() which it is not the same thing. The difference here is that the private OnPositionUpdate() would return all positions on the account and not just the strategy position.

                          In the example I provided if you add the following inside the OnPositionUpdate and place a trade on MES 03-22 and perhaps other instruments you can see the use of the OnPositionUpdate() to respond to trades.

                          if (e.Position.Instrument.FullName == "MES 03-22")
                          Print ("Instrument found!");

                          Comment


                            #43
                            right now I am working with the indicator that actually does not need the instrument , but i am also going back to the version trading a secondary symbol

                            so for the input of the get instrument when i go back to that indicaotor will the == operator work there and exactly how do i put the input

                            if (position.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument)

                            ??? if (position.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument.(Symbol2)

                            THE ABOVE ACTUALLY HAS AN EXIT THAT WE ARE WANTING TO EXIT THE Position.Quanity and were attempting to make sure we had a position and the current position quanitiy for exit order

                            would i be using

                            if (e.Position.Instrument.FullName == "MES 03-22")
                            Print ("Instrument found!");

                            but with Symbol2

                            if (e.Position.Instrument.FullName == Symbol2) << no quoatations
                            Print ("Instrument found!");

                            *******************************************

                            back to the current indicator without the exit with the error for position, we know the symbol of the chart, there is no Symbol2 and are looking for the position NOT Long or short as part of the conditions of the trade trigger

                            still not sure how to be able to create


                            && (Position.MarketPosition != MarketPosition.Long)// HAD TO BLOCK BECAUSE OF ERRORS
                            && (Position.Quantity == 0)// HAD TO BLOCK BECAUSE OF ERRORS

                            or
                            && (myAccount.Position.MarketPosition != myAccount.Positions.MarketPosition.Long)// HAD TO BLOCK BECAUSE OF ERRORS
                            && (myAccount.Position.Quantity == 0)// HAD TO BLOCK BECAUSE OF ERRORS

                            error is NinjaTrader.Cbi.Account does not contain a definition for 'Position' and no extension method 'Position' accepting a first argument of type NinjaTrade.Cbi.Account could be found

                            thanks

                            Comment


                              #44
                              Hello DTSSTS,

                              Thanks for your reply.

                              for " if (position.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument.(Symbol2) " The GetInstrument() method returns an instrument object so no the instrument name is not equal to an object This is important to understand. You could do: if (position.Instrument == NinjaTrader.Cbi.Instrument.GetInstrument(Symbol2). FullName) . Using FullName will return a string of the symbols full name (like ES 03-22)

                              This: if (e.Position.Instrument.FullName == Symbol2) will work but only in the OnPositionUpdate() as shown in my example and I would encourage you to test that in that simple example.

                              "back to the current indicator without the exit with the error for position, we know the symbol of the chart, there is no Symbol2 and are looking for the position NOT Long or short as part of the conditions of the trade trigger" Please review the example I provided and test in OnPositionUpdate() of the example.

                              Comment


                                #45
                                Not following the as paragraph, on that indicator I have added all of the OnPositionUpdate() code to the various area per the example and compile without error

                                where the below does not compile without the error described

                                && (myAccount.Position.MarketPosition != myAccount.Positions.MarketPosition.Long)// HAD TO BLOCK BECAUSE OF ERRORS
                                && (myAccount.Position.Quantity == 0)// HAD TO BLOCK BECAUSE OF ERRORS

                                SO I DO NOT GUESS I UNDERSTAND WHAT YOU MEAN BY

                                test in OnPositionUpdate() of the example.



                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Today, 05:17 AM
                                0 responses
                                52 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                130 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                70 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                43 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                47 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X