Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Analyser -> Orders.Name = Close Position

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

    Strategy Analyser -> Orders.Name = Close Position

    To keep track of orders I tag them with a unique (string) ulong. But when I look at the "Orders" tab of the "strategy analyser" the "Name" column contains entries "Close Position" in addition to my numbered orders.

    I enter all of my orders like this:
    EnterShort(i, orderQuantity, orderNumber.ToString());
    orderNumber = orderNumber + 1;

    Where are the "Close Position" orders coming from and how can I get rid of them?

    Thanks,
    Matthew.

    #2
    Matthew, this would be expected if you reverse the position with the Enter() methods, one contract would close the existing position, the other one take you the intended way then...

    Comment


      #3
      Thanks, that makes a bit of sense.

      If I have a variable called int positionIWant, which may have a different value and be long or short for each call of OnBarUpdate, how can I use the EnterLong, EnterShort functions to enter the orders?

      Whenever I try to do it I end up with a hell of a mess of code, so I thought I would ask because someone must have done this already.

      thanks,
      Matthew.

      Comment


        #4
        To make my question more specific, what do I write below in the function "adjustPositionTo" such that the actual positions will mirror/track the variable positionIWant.

        Thanks,
        Matthew.

        #region Using declarations
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Data;
        using NinjaTrader.Indicator;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Strategy;
        #endregion

        namespace NinjaTrader.Strategy
        {
        [Description("Enter the description of your strategy here")]
        public class testAdjustPosition : Strategy
        {
        #region Variables
        int timeCounter = 0;
        #endregion

        protected override void Initialize()
        {
        CalculateOnBarClose = true;
        }


        void adjustPositionTo(int newPosition) {
        // what do I write here to change the position to newPosition
        }

        protected override void OnBarUpdate()
        {
        int positionIWant = (int) (100000 * Math.Sin(timeCounter*2*Math.PI/60));
        adjustPositionTo(positionIWant);
        }

        #region Properties
        #endregion
        }
        }

        Comment


          #5
          oops, forgot to increment the timeCounter, OnBarUpdate shoud read:

          protected override void OnBarUpdate()
          {
          int positionIWant = (int) (100000 * Math.Sin(timeCounter*2*Math.PI/60));
          adjustPositionTo(positionIWant);
          timeCounter = timeCounter + 1;
          }

          Comment


            #6
            Matthew, sorry not sure I follow you here - is this calcuating the positon size you want to Enter()?

            Comment


              #7
              The adjustPositionTo(int i, int newPosition, string id) funciton in the program below illustrates what I want to acheive (it appears to work but I have not tested it rigorously).

              It adjusts the positions held to the required positions, which in reality of course would not be a sine wave! But does it have to be this complicated in order to acheive this? Have I missed something simple?

              Code:
              #region Using declarations
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Indicator;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Strategy;
              #endregion
              
              namespace NinjaTrader.Strategy
              {
                  [Description("Enter the description of your strategy here")]
                  public class testAdjustPosition : Strategy
                  {
                      #region Variables
                      int timeCounter = 0;
                      int positionIWant;
                      int prevPositionIWant;
                      public enum positionSetting {Short=-1, Neutral, Long};
                     #endregion
              
                      protected override void Initialize()
                      {
                          CalculateOnBarClose = true;
                      }
              
                      
                      void adjustPositionTo(int i, int newPosition, string id) {            
                          // have not used id.
                          // does this need to be this complicated to acheive what it does?
                          Position current = Positions[i];
                          int side = 0;
                          switch (current.MarketPosition)
                          {
                              case MarketPosition.Short : { side = -1; break;}
                              case MarketPosition.Flat : { side = 0; break;}
                              case MarketPosition.Long : { side = 1; break;}
                          }
                          positionSetting currentSide = (positionSetting) side;
                          int delta = newPosition - side*current.Quantity;
                          positionSetting direction = positionSetting.Neutral;
                          if (delta < 0) direction = positionSetting.Short;
                          if (delta > 0) direction = positionSetting.Long;
                          if (delta == 0) direction = positionSetting.Neutral;
                          bool changingSides = current.Quantity < Math.Abs(delta);
                          
                          if( ((currentSide == positionSetting.Neutral) || (currentSide == positionSetting.Short)) 
                              && (direction == positionSetting.Short) ) { EnterShort(Math.Abs(delta)); }
                          if( ((currentSide == positionSetting.Neutral) || (currentSide == positionSetting.Long))
                              && (direction == positionSetting.Long) ) { EnterLong(Math.Abs(delta)); }
                           
                  
                             if( (currentSide == positionSetting.Short) && (direction == positionSetting.Long) ) {
                              if ( Math.Abs(delta) > current.Quantity ) {  
                                  ExitShort(current.Quantity ); 
                                  EnterLong(Math.Abs(delta) - current.Quantity );
                              } else {
                                  ExitShort(Math.Abs(delta));
                              }
                          }
                          if( (currentSide == positionSetting.Long) && (direction == positionSetting.Short) ) {
                              if ( Math.Abs(delta) > current.Quantity ) { 
                                  ExitLong(current.Quantity ); 
                                  EnterShort(Math.Abs(delta) - current.Quantity);
                              } else {
                                  ExitLong( Math.Abs(delta)); 
                              }
                          }
                      }
                      
                      protected override void OnBarUpdate()
                      {
                          prevPositionIWant = positionIWant;
                          positionIWant = (int) (100000 * Math.Sin(timeCounter*2*Math.PI/60));
              
                          adjustPositionTo(0, positionIWant, timeCounter.ToString());
                          timeCounter = timeCounter + 1;
                          
                          int side = 0;
                          switch (Position.MarketPosition)
                          {
                              case MarketPosition.Short : { side = -1; break;}
                              case MarketPosition.Flat : { side = 0; break;}
                              case MarketPosition.Long : { side = 1; break;}
                          }
                          Print(prevPositionIWant + ", " + Position.Quantity*side); 
                      }
              
                      #region Properties
                      #endregion
                  }
              }
              This is the output:
              0, 0
              10452, 10452
              20791, 20791
              30901, 30901
              40673, 40673
              49999, 49999
              58778, 58778
              66913, 66913
              74314, 74314
              80901, 80901
              86602, 86602
              91354, 91354
              95105, 95105
              97814, 97814
              99452, 99452
              100000, 100000
              99452, 99452
              97814, 97814
              95105, 95105
              91354, 91354
              86602, 86602
              80901, 80901
              74314, 74314
              66913, 66913
              58778, 58778
              49999, 49999
              40673, 40673
              30901, 30901
              20791, 20791
              10452, 10452
              0, 0
              -10452, -10452
              -20791, -20791
              -30901, -30901
              -40673, -40673
              -49999, -49999
              -58778, -58778
              -66913, -66913
              -74314, -74314
              -80901, -80901
              -86602, -86602
              -91354, -91354
              -95105, -95105
              -97814, -97814
              -99452, -99452
              -100000, -100000
              -99452, -99452
              -97814, -97814
              -95105, -95105
              -91354, -91354
              -86602, -86602
              -80901, -80901
              -74314, -74314
              -66913, -66913
              -58778, -58778
              -49999, -49999
              -40673, -40673
              -30901, -30901
              -20791, -20791
              -10452, -10452
              0, 0

              Comment


                #8
                Matthew, the output you posted seems to align well - still not sure why you would need this - are you trying to programmatically sync strategy vs accounts positions? NinjaTrader 7 will have an option to do this automatically for you at startup. For 6.5 you could work with WaitUntilFlat and then just take the next entry signal...

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                77 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                45 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                27 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                32 views
                0 likes
                Last Post TheRealMorford  
                Started by Mindset, 02-28-2026, 06:16 AM
                0 responses
                63 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Working...
                X