Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple positions when not wanted

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

    #16
    Hello Xenophon99,

    Your post appears to be a duplicate of post #15.

    Also, while calling account information is not supported by NinjaTrader to do, I have suggested a fix in post #14. Have you tried this? Did this not correct the issue?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Understood. Well, almost. But I'm getting a CS0103 error on name "positions". I must declare this. Eventually I want to put all this logic into a NoPositions indicator.

      Comment


        #18
        Xenophon99,

        There is a capitalization error in the script.

        You would want to use Positions with a capital P.

        This will refer to the Position Collection of the account object.

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #19
          Am I to understand then that Account.Positions.Count == 0 will not tell me that I am free to trade because positions that are flat can be counted -- i.e. closing a position does not immediately remove it from the position collection? That certainly is a problem and helps explain the odd decision not to support account information calls. This is a burden because anyone running more than one strategy at a time has to know if there is a position open from another strategy in order to avoid margin violations.
          OK, some complex coding is called for. I want to put this code into a subroutine (i.e. indicator method) rather than copy that code into several strategies. The subroutine, function, method will return a value of true when there are no open positions and false otherwise. I have not yet worked out how to do this, as I can't find anything like a Return statement. The amount that has to be learned to do something elementary but crucial with this platform is daunting.

          Comment


            #20
            Following your clarification I built a method that is invoked by each strategy in my portfolio by: OrdString = "T20B HS " + Qty.ToString() + " Time " + ToDay(Time[0]).ToString() + " " + ToTime(Time[0]).ToString();
            //if (NoPositions(OrdString) == true)
            if (AccountFlat(OrdString) == true)
            { ..... }

            OrdString identifies in the output window the strategy that initiated the aborted order and the strategy origin of the position that blocked the new order. The method is:

            private string OrdString = "";
            private bool AccountFlat(string OrdString)
            {
            bool isFlat = true;
            foreach (Account acct in Cbi.Globals.Accounts)
            {
            if (acct.Positions != null)
            {
            foreach (Position pos in acct.Positions)
            {
            if (pos.MarketPosition != MarketPosition.Flat)
            {
            isFlat = false;
            if (OrdString != "")
            {
            OrderCollection orders = acct.Orders;
            foreach (Order ord in acct.Orders)
            {
            Print(OrdString + " by " + ord.ToString());
            }
            }
            break;
            }
            }
            }
            }
            return isFlat;
            }

            Several problems:
            1) I only what the strategy id of the blocking position but have not worked out how to extract this from the position order.
            2) Though AccountFlat is only invoked at the close of a bar, the output window has many lines for the same blocked order.
            3) I would prefer to embed this method in an external indicator (NoPositions) but have not worked out how to get a true or false return from it. Whatever I try I get compiler errors CS0127 or CS1502.

            Your comments will be much appreciated. Any reference you can give to discussions of NT Collections, what they contain and how to extract information from them would help too.

            Comment


              #21
              Hello Xenophon99,

              Thank you for your response.

              The Indicators are not going to be able to pull any account function or method, so this is not a viable option. I would recommend adding the loop alone to the OnPositionUpdate() of each strategy and checking for the bool in each OnBarUpdate().

              Comment


                #22
                As the point of this loop is to prevent the strategy from generating a position, I can't see how OnPositionUpdate could get called, hence how the loop would be executed. It is like closing the barn door after the horse has bolted. I have included the loop in every strategy, but as a function added to my list of variables. That works fine, apart from the oddities I mentioned. As for indicators, if SMA can deal with the data series of the calling strategy, why can't NoPositions deal with the account information of the calling strategy? The NT7 Help Guide isn't much help with such questions.
                I really would like to know how to print Strategy Id for the position that is blocking my strategies order, and also how to avoid getting a hundred lines printed when a strategy order is blocked..

                Comment


                  #23
                  Hello Xenophon99,

                  Thank you for your response.

                  The initial issue arises from the position update not occurring in time before the next entry is submitted. Adding the OnPositionUpdate() ensures you are checking after the position updates.

                  I am not sure what you are referring to as the SMA being able to deal with a data series from the calling strategy. Does this mean you are passing the input series for indicators as data series objects set in your strategy's logic?

                  An indicator cannot call account or position information from within it's own code.

                  Comment


                    #24
                    Hello Patrick,

                    Thank you for dispelling my misconceptions about indicators, and for the OnPositionUpdate() suggestion. If that method is triggered by a position change in any strategy running in an account, my problem is easily solved. But the documentation does not claim that. If I put each strategy's isFlat bool value in something like a global variable that any strategy can examine, I think that would give me what I need. Do you agree?

                    Comment


                      #25
                      Hello Xenophon99,

                      Thank you for your response.

                      Correct, my suggestion is for each strategy. You would need to add the code to each strategy.

                      For your idea, I cannot say for certain how this would work, I would recommend testing it.

                      Comment


                        #26
                        I am now storing the position indication for each strategy in a static DataTable. The compilation of the new indicator that accesses the table fails with CS0234 and CS0246 although I have using System.Data and System.Data.DataTable declared, and I have added references to the System.Data.SqlServerCe dlls. Please advise.

                        Comment


                          #27
                          Hello Xenophon99,

                          Thank you for your update.

                          Unfortunately, calling additional assemblies from System and implementing them is unsupported.

                          Can you provide the line of code that returns the error? If multiple, please provide those lines.

                          Comment


                            #28
                            // SetStratPosn saves the current position of the calling strategy

                            #region Using declarations
                            using System.Data;
                            using System;
                            using System.ComponentModel;
                            using System.Drawing;
                            using NinjaTrader.Cbi;
                            using NinjaTrader.Data;
                            using NinjaTrader.Gui.Chart;
                            using System.Collections.Generic;
                            #endregion

                            namespace NinjaTrader.Indicator
                            {
                            partial class Indicator
                            {
                            // table for historic data
                            public class GV
                            {
                            private static Dictionary<string, int> gvPosn = new Dictionary<string, int>();
                            line 25 private static DataTable gvTable = new DataTable(); // stores all historical
                            private static object lockObject = new object();
                            }

                            The compiler error is CS0246 on DataTable in line 25. I don't understand "unsupported". Writing to and reading from a DataTable is in principle like doing the same to an external hard drive. This technology has been around for 50 years. I cannot believe that C# is so primitive that it cannot do that.
                            The suggestion about adding references comes from NT documentation. Since when does NT document what is unsupported?

                            Comment


                              #29
                              Hello Xenophon99,

                              You would need to add System.Data.dll in the references. The use of System.Data would be supported in general C#.

                              Comment


                                #30
                                The problem is I do not have a System.Data.dll, even with .Net 2..0, .Net 3.5 and .Net 4.52 installed. It isn't in bin64 after these .Net installs. I don't know where else to obtain it.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by ghoul, Today, 06:02 PM
                                3 responses
                                13 views
                                0 likes
                                Last Post NinjaTrader_Manfred  
                                Started by jeronymite, 04-12-2024, 04:26 PM
                                3 responses
                                44 views
                                0 likes
                                Last Post jeronymite  
                                Started by Barry Milan, Yesterday, 10:35 PM
                                7 responses
                                20 views
                                0 likes
                                Last Post NinjaTrader_Manfred  
                                Started by AttiM, 02-14-2024, 05:20 PM
                                10 responses
                                180 views
                                0 likes
                                Last Post jeronymite  
                                Started by DanielSanMartin, Yesterday, 02:37 PM
                                2 responses
                                13 views
                                0 likes
                                Last Post DanielSanMartin  
                                Working...
                                X