Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Week 3 of attempting to Count my Open Positions

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

    Week 3 of attempting to Count my Open Positions

    I am now struggling with a third week on this problem.

    First, I used a basic method, which I learned was failing because each stock that I trade was in a separate strategy; I was informed that there is no counting between strategies. Fair enough I said.


    Second I was instructed to use the OnExecution update method to expose account information. Tricky, but accomplished. However, my counter seemed to get stuck in a repeating loop and ignore the -1 i assigned to exited positions. So it would grow all day regardless of how many positions I had just closed. This method is unsupported by NT
    staff, so again I was stuck. I even found a sub method that would display the correct number of Positions, but I could not extract long versus short.

    Code:
    System.Int32 PositionCount1 = account.Positions.Count;
    So this method was eventually abandoned after much frustration.

    Finally, I spent all weekend working with Arrays in a single strategy. I am not sure if OnBarUpdate has issues working with Arrays, or if I am doing something wrong, but in creating a strategy meant to execute against Bars 2,3 and 4, bar 3 never outputs and bar 4 never increments. Whatever stock is placed as Bar 2 works just fine, however.
    This leaves me with one working equity, exactly where I started.

    I will post the code below and ask for someone to step up and provide a dose of help on this problem, as this risk management tool is the final barrier that is preventing my partner and me from going live in an account which has been set up, fully funded and ready to go.

    Code below.

    Thanks and regards,

    Andrew
    Attached Files
    Last edited by alabell; 11-12-2012, 08:17 AM.

    #2
    Hello Andrew,
    Thanks for your post.

    NinjaScript is basically C# and Arrays or any C# methods will work fine.

    To assist you further may I know what is the Primary bar of the strategy (i.e. the chart on which you are applying the strategy on).

    I look forward to assisting you further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I am not using a chart at all. This is strictly reliant upon data. The theory would be that I could use any stock one second bar as the primary bar.

      Regards.

      Comment


        #4
        Hello Andrew,
        Whether you are initializing the strategy via the chart or the Strategies tab, it will have a primary series. If you are initializing the strategy via the Strategies tab then have to specify the primary bar via the Strategies parameters dialog.

        Now our help guide on Positions states.

        Holds an array of Position objects that represent positions managed by the strategy. This property should only be used when your strategy is executing orders against multiple instruments.

        Index value is based on the the array of Bars objects added via the Add() method. For example:

        Primary Bars is ES 1 Minute
        Secondary Bars is ES 3 Minute
        Third Bars is ER2 1 Minute

        Positions[0] == ES position
        Positions[1] == Always a flat position, ES position will always be Positions[0]
        Positions[2] == ER2 position


        Thus if the primary bar is say ORCL then the position of the 2nd secondary series (BarsInProgress == 2) will be reported in the primary bar.


        Also I would suggest to append the below code in the Initialize section of the code and see what exact errors you are receiving.
        TraceOrders = true;
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          I am not receiving Errors, I am receiving a lack of response on bars 3 and 4.


          Every Bar is a one second stock bar in a different security. There are no bars that share the same security. For now, assume I always set the primary bar to 1 second MSFT.

          Then you can see the other bars in my toy code. They are different securities and they don't generate errors. They simply don't respond with data.

          This is where I need help.

          Comment


            #6
            Hello Andrew,
            If you try submitting the orders with a unique order name and set the entry handling to Unique Entries then are you able to get the fills for all the series.

            Code:
            //make sure order names are unique
            EnterShort(i, quantity, "EnterShort" + Instruments[i].FullName);
            To know more about entry handling please refer to our help guide

            JoydeepNinjaTrader Customer Service

            Comment


              #7
              thank you joydeep. The Array should be functional, I assume, up to the 200 stocks I plan to eventually be trading...by using the single line below?

              Code:
              //make sure order names are unique
              EnterShort(i, quantity, "EnterShort" + Instruments[i].FullName);
              With three stocks this is simply an exercise, but with over 100 is becomes necessary.

              1. Is my assumption Correct?

              2. Also, finally, do i need to add quotes of any kind around

              + Instruments[i].FullName

              or are they implied by virtue of the fact that + Instruments[i].FullName is predefined as a string?

              Regards and ty,

              Andrew

              Comment


                #8
                Hello Andrew,
                Yes, that is right. A single line will do to enter orders for each bar series (with the entry handling set to UniqueEntries)

                FullName is a string. So no need to add quotes ("").

                Also do remember if you add 200 1 seconds bars array, then you may face performance issues. So please bear it in mind before hand. Some connectivity provider also limits market data upto a specific numbers of data streams. For example Interactive Brokers does not lets you open more than 100 data streams.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Joydeep.

                  Final Question:

                  It seems as if when I close a position, it is still decrementing all open positions, causing my count of open longs or shorts to go negative, which is impossible.

                  (Example:

                  I am Short 2, Long 0;
                  I close a short;
                  I am now decrementing too many times it seems;
                  My counter now shows Short -1, Long 0 ; )

                  Did I perhaps miss another variable that I will need to add the following code to?


                  Code:
                  + Instruments[i].FullName);

                  Thanks in advance,

                  Andrew
                  Last edited by alabell; 11-12-2012, 08:26 AM.

                  Comment


                    #10
                    Hello Andrew,
                    You need to append the appropriate fromEntrySignal name while submitting the exit entry. Please make sure you are doing so as per the below code.

                    Code:
                    ExitLong(i, quantity, "ExitLong", "EnterLong" + Instruments[i].FullName);
                    What codes you are using to count the entries etc.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Joydeep View Post
                      Hello Andrew,
                      You need to append the appropriate fromEntrySignal name while submitting the exit entry. Please make sure you are doing so as per the below code.

                      Code:
                      ExitLong(i, quantity, "ExitLong", "EnterLong" + Instruments[i].FullName);
                      What codes you are using to count the entries etc.

                      I did this. The code I am using to count appears in the "toy code" I attached to the original post in this thread.

                      Comment


                        #12
                        Hello Andrew,
                        Thanks for the clarifications.

                        In your code the longPos/shortPos may be added or subtracted multiple times due to say exchange delays etc.

                        I would suggest counting the longPos/shortPos in the OnExecution event. A basic sample code will be like:

                        Code:
                        protected override void OnExecution(IExecution execution)
                        {
                        			
                        	if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Positions[BarsInProgress].MarketPosition == MarketPosition.Long)
                        {
                        	longPos++;
                        }
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          TY Joydeep, I am familiar with OnExecution so I will test this, but 3 simple questions:

                          1. Do I leave BarsInProgress as stated below, or do I match it to my Array int which is "i"?

                          2. When I close a position, I assume

                          == MarketPosition.Long)

                          will become

                          == MarketPosition.Flat)

                          3. Do I need to set any variable to null before I implement this portion of my code?

                          such as: IExecution = null;

                          or IOrder = null;

                          If yes, I would make these public variables in my variables section, or add them in the OnExecution area?

                          I feel like I am getting to the bottom of this but so far the results have been incorrect, so I simply want to make sure I am leaving no questions unasked.

                          TY and Regards,

                          Andrew

                          Comment


                            #14
                            Hello Andrew,
                            1. BarInProgress will be fine.

                            2. Yes, right. When you close a position, you evaluate it with MarketPosition.Flat.

                            3. No, you are not using any IOrder objects, so no need to set anything to null.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              I think I need an Order Action line too, so as to differentiate between :

                              Flat from Long

                              (longPos--)

                              and Flat from Short

                              (shortPos--)

                              Correct?

                              If yes, I will code this and run it and report back, hopefully with good news
                              Last edited by alabell; 11-12-2012, 10:00 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RubenCazorla, Today, 09:07 AM
                              2 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              7 responses
                              81 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by timmbbo, 07-05-2023, 10:21 PM
                              4 responses
                              158 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by tkaboris, Today, 08:01 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Lumbeezl, 01-11-2022, 06:50 PM
                              31 responses
                              819 views
                              1 like
                              Last Post NinjaTrader_Adrian  
                              Working...
                              X