Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizing Long/Short Market Exposure

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

    #16
    So let me make sure I understand:

    Right now, I am running a strategy with 200 stocks.

    Each name is measured against 1. a tick value, 2. a minute value, 3. a day value and 4. a volume value.

    Does this imply I need to add 800 bar objects to my strategy, or can i add the 200 securities and then the 4 bars and then use a method to make sure the strategy runs each name with each bar...

    Thanks again.

    Comment


      #17
      Hello Andrew,

      Thank you for your response.

      You will need to add each instrument with the period type as well. For example:
      Code:
      Add("AAPL", PeriodType.Tick, 150);
      Add("AAPL", PeriodType.Minute, 1);
      Add("AAPL", PeriodType.Day, 1);
      Add("AAPL", PeriodType.Volume, 1000);
      So each instrument would have four data series objects.

      Please let me know if I may be of further assistance.

      Comment


        #18
        understood, and thank you patrick.

        will this noticeably reduce performance at all?

        Comment


          #19
          Hello Andrew,

          Thank you for your response.

          As there will be 800 data series (not including the main instrument that the strategy is applied to) running in your strategy, you will see a difference in performance.
          For information on our performance tips please visit the following link: http://www.ninjatrader.com/support/h...ance_tips2.htm

          Please let me know if I may be of further assistance.

          Comment


            #20
            sorry, last question:

            Could a custom list of securities be added so I am not adding 800 lines of code? I'm happy to do it, it just seems so inefficient; alternatively, i may make a user defined method which is a hashtable, no?

            Comment


              #21
              Hello Andrew,

              Thank you for your response.

              There is no other way to add in the instruments to the strategy. Remember that the strategy will only report the information for the strategy itself.

              Even if you use a User Defined Method to add the other instruments or to process your calculations for the counting of the executions, it would still require the same amount of processing power to run the strategy as it would if the calculation logic and instruments were within the strategy.

              Please let me know if I may be of further assistance.

              Comment


                #22
                OK, I apologize, I have restructured the code I am using to run my strategy and a couple of questions have arisen:

                The strategy currently uses some 1200 lines for code for one equity....I am trading 200 equities. This is no problem when I am just entering and exiting trades: each name in my list has its own strategy, so I run 200 ninjascripts simultaneously, each with 4 DataSeries. This is a bit taxing on my machine from time to time but never really presents a problem.

                However:

                Now that I am using the "onExecute" method to tally longs and shorts....

                each security I trade needs its own bar object. This implies 800 bar objects, which is also not a problem.

                What is a problem is as follows: when i write my 1200 lines of strategy code, instead of saying
                __________________________________________________ ______
                Initialize

                Add (PeriodType.Minute, 1); //Bar index 1 is One Minute Bars


                OnBarUpdate

                if Closes[1][0] > Closes [1][1]

                {Buy Stock}
                __________________________________________________ _________
                I now have to say the following:

                Initialize

                Add("AAPL", PeriodType.Minute, 1); //Bar index 1 is One Minute AAPL Bars
                Add("MSFT", PeriodType.Minute, 1); //Bar index 2 is One Minute MSFT Bars
                Add("CSCO", PeriodType.Minute, 1); //Bar index 3 is One Minute CSCO Bars
                .
                .
                .
                .
                .
                .
                .
                .
                .
                All the way to

                Add("Stock200", PeriodType.Minute, 1); //Bar index 200 is One Minute Stock200 Bars


                OnBarUpdate

                if Closes[1][0] > Closes [1][1]

                {Buy Stock}

                if Closes[2][0] > Closes [1][1]

                {Buy Stock}

                if Closes[3][0] > Closes [3][1]

                {Buy Stock}

                if Closes[4][0] > Closes [4][1]

                {Buy Stock}

                if Closes[5][0] > Closes [5][1]

                {Buy Stock}

                .
                .
                .
                .
                .
                .
                .
                All the way to

                if Closes[200][0] > Closes [200][1]

                {Buy Stock}
                __________________________________________________

                In essence, this will turn my 1200 lines of code into 240,000 lines of code.

                This can not be the correct solution can it?

                There must be a way for me to use onExecute, or Trade Collection, or Execution Collection (if it exists) to write a script that says

                "You are trading 200 stocks. You can therefore have a maximum of 200 positions at any time. Right now you happen to have 48 positions. 30 are long, 18 is short, your portfolio ratio is 62.5% long."

                Sorry for the long post but this has taken me a long period of unsuccessful coding and probing NT resources for solutions.

                I appreciate any help that can be provided.

                Regards,


                Andrew
                Last edited by alabell; 10-31-2012, 07:06 AM.

                Comment


                  #23
                  Hello Andrew,

                  Thank you for your update on this matter.

                  The only way around this would be to have your 200 strategies running and use the unsupported method of Account.Executions in each strategy. Unfortunately, as Account.Executions is not supported there is no documentation on this matter.

                  Please understand this is all the executions made on the account (even manual entry) and not just the strategy executions.

                  The following link will take you to a thread on our Support Forum that gives an example of exposing the Account class: http://www.ninjatrader.com/support/f...ad.php?t=51239

                  Please let me know if I may be of further assistance.

                  Comment


                    #24
                    So after days of work and self education, the following code produces a couple of things:

                    -- it lets me know every time I have an execution
                    -- it lets me know that the account I have selected and named is the account in question.
                    -- it reveals a simple integer which is always equal to the number of open positions in said account.

                    I KNOW that you folks don't support this Account Based coding but given that it is effectively done, I have a simple question:

                    Is there any simple syntax that i can add that will only call and tally long or short entries?

                    At this point, the project will be complete.

                    NT Forum Vets, or NT Staff, any help would be amazing. Thanks in advance.

                    Andrew

                    Code:
                    protected override void OnExecution(IExecution Position)
                    		
                    {	
                    	Print("Inside OnExecution");
                    				
                    			
                            string accountName = "Sim101";			
                            Account account = NinjaTrader.Cbi.Globals.Accounts.FindByName(accountName);
                    	System.Int32 PositionCount = account.Positions.Count;
                            Print ("Account currently has " + PositionCount)

                    Comment


                      #25
                      Hello,
                      You would need to use additional variables and you could track/tally this in OnOrderUpdate()
                      Code:
                              #region Variables
                              private IOrder entryOrder     = null; // This variable holds an object representing our entry order
                              private IOrder stopOrder     = null; // This variable holds an object representing our stop loss order
                              private IOrder targetOrder     = null; // This variable holds an object representing our profit target order
                              [B]private int shortCounter = 0;[/B]
                      [B]         private int longCounter = 0;[/B]
                              #endregion
                      Code:
                              protected override void OnOrderUpdate(IOrder order)
                              {
                                  // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                                  if (entryOrder != null && entryOrder == order)
                                  {    
                                      // Reset the entryOrder object to null if order was cancelled without any fill
                                      if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                                      {
                                          entryOrder = null;
                                          [B]//HERE YOU WOULD INCREASE YOUR LONG/SHORT COUNTER[/B]
                                      }
                                  }
                              }
                      For more on OnOrderUpdate()

                      The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

                      Please let me know if I can be of further assistance.
                      LanceNinjaTrader Customer Service

                      Comment


                        #26
                        Lance,

                        I am trading 200 strategies (1 stock per strategy, identical strategies, but the list of stocks I trade is 200 long)

                        Will this code you provided below work across my entire account, by tallying all 200 strategies?

                        Because the code I posted below your response does exactly that.

                        Thanks for any help and regards,

                        Andrew

                        Comment


                          #27
                          Hello,

                          The code I posted will only work for a single strategy.

                          Unfortunately there is not a supported method I can offer for tracking multiple orders in multiple strategies.

                          If you are only looking to tally the total, you could have your NinjaScript write to an external file to provide you with tallies. This would be used in conjunction with the OnOrderUpdate() method.


                          Please let me know if I can be of further assistance.
                          LanceNinjaTrader Customer Service

                          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
                          48 views
                          0 likes
                          Last Post TheRealMorford  
                          Working...
                          X