Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizing Long/Short Market Exposure

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

    Optimizing Long/Short Market Exposure

    Strategy is going well, thanks to all who have helped out in the past. Preparing to go live and putting in a few safeguards, including a ratio of Long/Short that is capped on either side.

    I could create

    Int longTrades, and increment it as part of my long entry code;
    Int shortTrades, and increment it as part of my short entry codes;
    Int allTrades, which is a sum of the above two lines.

    But this morning I found the following:


    int allTrades = Performance.AllTrades.Count;
    int longTrades = Performance.LongTrades.Count;
    int shortTrades = Performance.ShortTrades.Count;

    and have implemented it "onBarUpdate"

    Regrettably, every time the bar updates (tick bar) and trades have been executed that should increment these values, the values print out as zero.

    Any thoughts?

    Perhaps I am missing a piece of code involving "Get" these values?

    Ty in advance,

    Andrew

    #2
    Hello Andrew,

    Thank you for your post.

    Are these executions (a Buy or Sell) or trades (an Entry and an Exit to complete the trade) that are not being counted with Performance?
    If these are only executions (not full trades) then they will not be counted.

    You can find a reference on using Performance at the following link: http://www.ninjatrader.com/support/f...ead.php?t=4804

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

    Comment


      #3
      i see the distinction; is there a way to count

      -- entry generation long
      -- entry generation short
      -- exit generation long
      -- exit generation short

      as 4 completely distinct values then?

      Ty,

      Andrew

      Comment


        #4
        Hello Andrew,

        Thank you for your response.

        There is no supported method for this, however you can create a new int to count the number of exits and entries for long and short executions.

        For example:

        Code:
        int numberOfLongEntries = 0;
        
        if (yourLongEntryCondition)
        
        {
        
        EnterLong(1);
        
        numberOfLongEntries = (numberOfLongEntries + 1);
        
        Print("There have been " + numberOfLongEntries + " Long Entries.");
        
        }
        
        // etc...
        Please let me know if I may be of further assistance.

        Comment


          #5
          yep, thanks patrick this was my original thought as well. regards.

          Comment


            #6
            Hello Andrew,

            Thank you for your response.

            In retrospect I believe using the OnExecution() method will assist in ensuring you count the execution only when filled.

            For example:
            Code:
            protected override void OnExecution(IExecution execution)
            {
            if(execution.Order != null && execution.Order.OrderState == OrderState.Filled && Position.MarketPosition == MarketPosition.Long)
            {
            numberOfLongEntries = (numberOfLongEntries + 1);
            Print("There have been " + numberOfLongEntries + " Long Entries.");
            }
            }
            For information on IExecution please visit the following link: http://www.ninjatrader.com/support/h...iexecution.htm

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

            Comment


              #7
              so as a final point, my understanding is that using this code i could increment long and short trades as they enter, and decrement them as they exit, thereby determining my realtime Long/Short exposure ratio.

              would i slot "on Execution" in after "initialize", and/or before or after on bar update? my guess is after, but i await confirmation.

              Kind regards.

              Comment


                #8
                Hello Andrew,

                Thank you for your response.

                Place OnExecution() after Initialize() and OnBarUpdate().
                For a reference using OnExecution please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=7499

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

                Comment


                  #9
                  Here is my current first best attempt at implementation. It is generating a number of errors, but I feel like I am just missing one concept here.

                  -- Should I be including any logic from OnOrderUpdate?

                  -- I added the lines in Bold so that I can then decrement similar code each time i exit long vs. exit short; will these lines work with the OnExecution logic?

                  The errors are not listed as errors in these lines of code, so It is somewhat hard to manually debug, but it gives me hope that perhaps I have just missed one or two small things.

                  Thanks in advance and regards,

                  Andrew

                  Code:
                  protected override void OnBarUpdate()
                          
                  		{	
                  			
                  		private IOrder entryOrder = null;
                  			
                  		protected override void OnExecution(IExecution execution)
                  			{
                  				if(execution.Order != null 
                  				&& execution.Order.OrderState == OrderState.Filled 
                  				[B]&& execution.Order.OrderAction == OrderAction.Buy [/B]
                  				&& Position.MarketPosition == MarketPosition.Long)
                  				
                  					{
                  						numberOfLongEntries = (numberOfLongEntries + 1);
                  						Print("There are " + numberOfLongEntries + " Long Positions.");
                  					}
                  				
                  				else if(execution.Order != null 
                  				&& execution.Order.OrderState == OrderState.Filled 
                  				[B]&& execution.Order.OrderAction == OrderAction.SellShort [/B]
                  				&& Position.MarketPosition == MarketPosition.Short)
                  				
                  					{
                  						numberOfShortEntries = (numberOfShortEntries + 1);
                  						Print("There are " + numberOfShortEntries + " Short Positions.");
                  					}

                  Comment


                    #10
                    Hello Andrew,

                    Thank you for your response.

                    Please move the entire OnExecution() method outside of the OnBarUpdate() method and test your strategy once more.
                    I recommend reviewing the reference sample listed at the following link as it will show you how to use the OnExecution() method and the OnBarUpdate() method in the same strategy: http://www.ninjatrader.com/support/f...ead.php?t=7499

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

                    Comment


                      #11
                      thanks patrick, will do and will respond.

                      edit: Terrific reference samples. worked almost immediately.

                      I will test today and report back with any (hopefully very few) final questions.

                      Regards.
                      Last edited by alabell; 10-26-2012, 07:21 AM.

                      Comment


                        #12
                        Patrick/Colleagues:

                        I worked with the provided code samples this weekend and thought I had it nailed, but I am finding a couple of confusing outcomes.

                        For now, I have tightened things up into a short piece of toy code and I was wondering if someone might run the file and tell me if they are finding the same unexpected results?

                        Thanks in advance,

                        Andrew

                        Comment


                          #13
                          Hello Andrew,

                          Thank you for your update on this matter.

                          Please describe the item you are experiencing and any steps we can take on our end to reproduce as well as attaching your strategy to your response. If you prefer you can send the strategy in an e-mail to support[at]ninjatrader[dot]com with 'ATTN: Patrick' in the subject line and a reference to this thread in the body of the e-mail: http://www.ninjatrader.com/support/f...ad.php?t=53448

                          I look forward to assisting you further.

                          Comment


                            #14
                            I don't mind placing the code here, its fairly basic non proprietary stuff.

                            so basically this is just a test to say get long or short after 3 ticks, and then exit on either side at 12 cents p/l.

                            First thing I notice is that because NT runs each stock in its own strategy, I am not getting a tally of the combined stocks in the portfolio, but instead a 1 or zero per stock, depending on whether or not we have opened or closed a position. Unless there is a coding method I have missed, since the real purpose though is to count the events within in broad portfolio, this doesn't really help. .

                            AT this point in testing my porfolio consists of 5 stocks:


                            ebay
                            csco
                            orcl
                            lulu and
                            vrsn

                            as semi-randomly selected names that might have different styles of price action.

                            Code is attached as a text file.

                            Thanks in advance.
                            Attached Files

                            Comment


                              #15
                              Hello Andrew,

                              Thank you for your response.

                              The strategy will only pull the information for the executions from within the strategy and not any others ran on other instruments.
                              So you will need to add each instrument into the strategy with the Add() method. For information on using multiple instruments in a strategy please visit the following link: http://www.ninjatrader.com/support/h...nstruments.htm

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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by benmarkal, Yesterday, 12:52 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              16 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              244 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Working...
                              X