Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Tweaking a Strategy

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

    Tweaking a Strategy

    Hello,

    This is my first time posting a question so you'll have to excuse me if I accidentally leave out some relevant information.

    I have a strategy that is essentially a simple stochastic cross over strategy. I'd like to preface the code by saying that it might look goofy. That is because I was really hacking around trying to get it to behave as desired. Here is some code:

    Code:
    /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
    			ExitOnClose = false;                       // We do not want to exit on close
    			Add(Stochastics(periodD, periodK, smooth));// We want to see the Stochastics on the chart  	
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			// If we're flat, just get us in the market
    			if(Position.MarketPosition == MarketPosition.Flat)
    			{
    				EnterShort();
    			}
    			
    			// If the D value of the stochastic has crossed above our topThrs
    			if( CrossAbove(Stochastics(periodD,periodK,smooth).D,(double) topThrs, 1))
    			{	
    				// Next amount to long by will be 1
    				j=1;
    				// Trigger OnPositionUpdate
    				Position.Close();
    				// Next amount to short will be the current amount + 1
    				i++;	
    			}
    			// If the D value of the stochastic has crossed below our botThrs
    			if( CrossBelow(Stochastics(periodD,periodK,smooth).D, (double) botThrs,1))
    			{	
    				// Next amount to short by will be 1
    				i=1;
    				// Trigger OnPositionUpdate
    				Position.Close();
    				// Next amount to loong will be the current amount + 1
    				j++;
    			}
    			
            }
    		
    		// When our position updates, enter a long or short at the desired position value
    		protected override void OnPositionUpdate(IPosition position)
    		{
    			if(Position.MarketPosition == MarketPosition.Flat)
    			{
    				// If the D value of the stochastic has crossed above our topThrs
    				if( CrossAbove(Stochastics(periodD,periodK,smooth).D,(double) topThrs, 1))
    				{
    					EnterShort(i);
    				}
    				// If the D value of the stochastic has crossed below our botThrs
    				if( CrossBelow(Stochastics(periodD,periodK,smooth).D, (double) botThrs,1))
    				{
    					EnterLong(j);
    				}
    			}
    			
    		}
    This strategy uses
    Code:
    Position.Close()
    to trigger
    Code:
    OnPositionUpdate()
    . It is from inside this function that I was able to enter the market at the position and quantity I wanted. However, this produced a negative side effect.

    Attached is a photo of how this strategy trades. I would like to NOT have to close my positions to put myself in a new position. I'd like to be able to enter a +1 position at the first cross below, then at another cross below, enter +2, then at the next cross below +3 and finally, at the cross ABOVE, enter -1.

    I hope that this text and attached photo is enough but please ask for more information if needed. Thank you in advanced!
    Attached Files

    #2
    Point of Clarification

    Attached is a photo of how this strategy trades. I would like to NOT have to close my positions to put myself in a new position. I'd like to be able to enter a +1 position at the first cross below, then at another cross below, enter +2, then at the next cross below +3 and finally, at the cross ABOVE, enter -1.
    To clarify, I simply want to add one "Long" to my position every time it Crosses Below the Bottom Threshold in a row, uninterrupted by a Cross Above of the top Threshold. This would mean on the third consecutive cross below I would own 3 Long positions.

    Thanks again.

    Comment


      #3
      Hello and welcome to the forum!

      First thing I wanted to point out is a common misunderstanding about how Initialize() can be called at times when you might not expect. Because of this we do not support the dynamic adding to a chart like this

      Add(Stochastics(periodD, periodK, smooth));

      Instead you'll want to hard code these values. It can still work but we don't support it because unexpected results can arise when using inputs for values in Initialize()

      In terms of order handling, if you're wanting to have multiple entries in the same direction you will need to change the EntriesPerDerection (can be done through the strategy interface, the default can be hard coded)


      Calling EnterLong() or EnterShort() will first check to see if the position is currently flat. If its not flat and is in the oposite direction it will first close the position with a market order and then re-enter the opposite direction with the market order.

      From the sounds of it you could set entries per direction to 3

      Use EnterLong() on each cross below and EnterShort() on each cross above.

      I don't see any reason to use OnPositionUpdate() in this example and it sounds like everything could be contained within OnBarUpdate()

      If you're looking to get into more advanced order handling I'd suggest looking at this example: http://www.ninjatrader.com/support/f...ead.php?t=7499

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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by 00nevest, Today, 02:27 PM
      1 response
      8 views
      0 likes
      Last Post 00nevest  
      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
      12 views
      0 likes
      Last Post bltdavid  
      Started by port119, Today, 02:43 PM
      0 responses
      8 views
      0 likes
      Last Post port119
      by port119
       
      Started by Philippe56140, Today, 02:35 PM
      0 responses
      7 views
      0 likes
      Last Post Philippe56140  
      Working...
      X