Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Scale in and out

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

    Scale in and out

    Hi,
    I have problems understanding your code sample for scaling in and out of a position
    HTML Code:
    http://www.ninjatrader.com/support/forum/showthread.php?t=3751
    .

    I try to code something quite simple like:
    If SMA 0 > SMA 1 add 100 to position.
    If SMA 0 < SMA 1 subtract 100 from position.
    The strategy should do this five times. The position size would float from -500 to +500.

    With the strategy builder I can create multiple entries, but the total position reverse one the condition change from long to short.

    Could you be so kind and provide a code sample for this scaling in an out system?
    Thank you.

    #2
    Hello moon_121,

    Thank you for your post.

    So I may provide you with the proper information please clarify on one matter here; when you list +500 I assume this means Long, and when you list -500 I assume this means Short.

    Am I correct on this part of your strategy?

    I look forward to your response.

    Comment


      #3
      Long and short

      Hi Patrick,
      that is correct. My position size would be maximum 500 long or 500 short. Of course it could also be - 400, -300, -200, -100, flat, 100, 200, 300 or 400.

      Comment


        #4
        Hello moon_121,

        Thank you for the clarification.

        What you will need to take from the SampleScaleOut strategy is the idea to use the MarketPosition to assist you in your calculations and entries and exits (scaling in and out).

        Your strategy, as you have said, is relatively simple in concept and once you have the required knowledge of NinjaScript it will be fairly easy to code.

        The following is my take on your strategy based on the details you provided, please let me know if you have any questions on the following:

        The first item is to set the maximum number of entries allowed on either side of the market. This is achieved with the use of EntryHandling (http://www.ninjatrader.com/support/h...handling.htm):
        Code:
                protected override void Initialize()
                {
        			EntriesPerDirection = 500;
        			EntryHandling = EntryHandling.AllEntries;
                }
        The next step is to set our entry and exit conditions based on our MarketPosition (http://www.ninjatrader.com/support/h...position.htm):
        Code:
        			if(Position.MarketPosition == MarketPosition.Flat)
        			{
        				if(SMA(12)[0] > SMA(12)[1])
        				{
        					EnterLong(100);
        				}
        				if(SMA(12)[0] < SMA(12)[1])
        				{
        					EnterShort(100);
        				}
        			}
        			if(Position.MarketPosition == MarketPosition.Long)
        			{
        				if(SMA(12)[0] > SMA(12)[1])
        				{
        					EnterLong(100);
        				}
        				if(SMA(12)[0] < SMA(12)[1])
        				{
        					ExitLong(100);
        				}
        			}
        			if(Position.MarketPosition == MarketPosition.Short)
        			{
        				if(SMA(12)[0] < SMA(12)[1])
        				{
        					EnterShort(100);
        				}
        				if(SMA(12)[0] > SMA(12)[1])
        				{
        					ExitShort(100);
        				}
        			}
        *Please note I used a period of 12 for the SMA, this of course can instead be set to your value or a user defined value so you may change it when applying the strategy. For information on user defined input parameters please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=5782

        Please let me know if you have any questions.
        Last edited by NinjaTrader_PatrickH; 12-11-2012, 09:42 AM.

        Comment


          #5
          Not working

          Thanks for your help, but I think I need more of it.
          I entered the code, but don't get the desired results. I get 5 entries, but everything goes flat once long and short direction changes. (See picture)
          I attached the code, Hopefully you can spot the error.

          To make myself clear: I want to gradually reduce or build a position. 500 long should decrease to 400 at the next signal (bar), 300 the next one and so on. I would take 10 signals to completely reverse a position.
          Now the strategy goes from 500 long to flat and is entering 100 short on the same bar.

          Attached Files
          Last edited by moon_121; 12-11-2012, 12:46 PM.

          Comment


            #6
            Hello moon_121,

            Thank you for your response.

            You are using EnterShort() when you are long (this will close your position) and EnterLong() when you are short (this will also close your position).

            For example you use the following:
            Code:
            			if(Position.MarketPosition == MarketPosition.Long)
            			{
            				if(SMA(Period)[0] > SMA(Period)[1])				{
            					EnterLong(100000);
            				}
            				if (SMA(Period)[0] < SMA(Period)[1])
            				{
            					EnterShort(100000);
            				}
            			}
            Instead of the above you will want to use the following to scale out properly:
            Code:
            			if(Position.MarketPosition == MarketPosition.Long)
            			{
            				if(SMA(Period)[0] > SMA(Period)[1])				{
            					EnterLong(100000);
            				}
            				if (SMA(Period)[0] < SMA(Period)[1])
            				{
            					ExitLong(100000);
            				}
            			}
            Please let me know if you have any questions.

            Comment


              #7
              OK

              Thanks Patrick, that works as expected.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              41 views
              0 likes
              Last Post futtrader  
              Started by Option Whisperer, Today, 09:55 AM
              1 response
              11 views
              0 likes
              Last Post bltdavid  
              Started by port119, Today, 02:43 PM
              0 responses
              7 views
              0 likes
              Last Post port119
              by port119
               
              Started by Philippe56140, Today, 02:35 PM
              0 responses
              7 views
              0 likes
              Last Post Philippe56140  
              Started by 00nevest, Today, 02:27 PM
              0 responses
              7 views
              0 likes
              Last Post 00nevest  
              Working...
              X