• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

My Bug Strategy

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

    My Bug Strategy

    Hi,
    I coded my strategy ,but in the Strategy Analyser didn't make any trade,something is wrong.
    My strategy open a entry position if the sma(40)>sma(200),when the prices breakout the upper Donchian Channel(55).And exit long position when the prices breakout the slower Donchian Channel(20) or the prices = Entry Price - ATR of the Entry Price*2,whichever comes first.
    Please,see all my strategy below and tell me what's wrong.
    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
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// The system 2 of The Original Turtle Trading Rules with the filter of Curtis Faith.
        /// </summary>
        [Description("The system 2 of The Original Turtle Trading Rules with the filter of Curtis Faith.")]
        public class TurtleTradingSystembyCurtisFaith : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    			Add(DonchianChannel(55));
    			Add(DonchianChannel(20));
    			Add(SMA(40));
    			Add(SMA(200));
    			Add(ATR(20));
    			
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			// Condition set 1
    			double myATR = ATR(20)[0];
                if (Close[0] >= DonchianChannel(55).Upper[0]
                    && SMA(40)[0] > SMA(200)[0])
                {
                    EnterLong(DefaultQuantity, "EnterLong");
                }
    			// Exit Condition 1 - Breakout
                if (Close[0] <= DonchianChannel(20).Lower[0])
                {
                    ExitLong("ExitLongDC", "");
                }
                if (Position.MarketPosition == MarketPosition.Long)
                {
                //Print ATRStopLoss
                SetStopLoss(CalculationMode.Price, Position.AvgPrice-(myATR*2));
                }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Pedro,

    Please enable TraceOrders = true in the Initialize section of your code.

    Then run the Strategy in the Strategy Analyzer window and check the Tools--> Output window.

    This will give you information as to why the orders did not trigger

    http://www.ninjatrader.com/support/f...ead.php?t=3627
    MatthewNinjaTrader Product Management

    Comment


      #3
      Matt,
      I did this and in the output window appear this:
      22/11/2008 03:00:00 Entered internal PlaceOrder() method at 22/11/2008 03:00:00: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='ExitLongDC' FromEntrySignal=''

      22/11/2008 03:00:00 Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='ExitLongDC' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
      How can I resolve this?
      Pedro

      Comment


        #4
        Pedro,

        There are some order handling rules you need to be aware of.

        Please see our Help Guide article on Internal Order Handling Rules that Reduce Unwanted Positions:

        http://www.ninjatrader.com/support/h...d_approach.htm

        The reason it is ignored is because of this line:

        Code:
        			// Exit Condition 1 - Breakout
                    if (Close[0] <= DonchianChannel(20).Lower[0])
                    {
                        ExitLong("ExitLongDC", "");
                    }
        It is attempting to exit where there is not a position.

        I'd recommend putting in a position check to this statement so it will only be true when you are in a Long position:

        Code:
        			// Exit Condition 1 - Breakout
                    if (Close[0] <= DonchianChannel(20).Lower[0] && Position.MarketPosition == MarketPosition.Long)
                    {
                        ExitLong("ExitLongDC", "");
                    }
        MatthewNinjaTrader Product Management

        Comment


          #5
          Matt,
          Now I got.But my StopLoss is wrong,the exit price is:Entry Price - ATR of Entry Price*2
          Please look in the imagehttp://img441.imageshack.us/img441/4984/jdsaidsa.png
          The StopLoss would be :13,26,not 13,00.
          14,32-0,647*2=13,26

          Comment


            #6
            Your StopLoss value is likely changing with each OnBarUpdate call.

            You will need to Print() these values to see if the stop is submitted correct at at 13,26 and if it is being readjusted as the ATR value changes.

            Print(Position.AvgPrice-(myATR*2));
            MatthewNinjaTrader Product Management

            Comment


              #7
              The StopLoss didn't calculate like I want(Entry Price-ATR of Entry Price*2).i put Print(Position.AvgPrice-(myATR*2)); above the SetStopLoss(CalculationMode.Price, Position.AvgPrice-(myATR*2)); is it correct.
              And I found another bug,in some cases,the stop loss is the same of the entry price,like in this imagehttp://img641.imageshack.us/img641/8595/semttulocel.png
              Pedro

              Comment


                #8
                You will have to add more print statements to determine exactly where these values are not being set as you expect.

                One thing to note is you have your SetStopLoss order under a "MarketPosition.Long" condition. This means it is going to resubmit this stop loss at the current ATR value for each bar that is formed.
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  So,how can I change the "MarketPosition.Long"?

                  Comment


                    #10
                    Another problem it's the Entry Postion,in my strategy enter a long position if the high == Donchian Channel.Where I went wrong.Please see my entry code below and this image:http://img401.imageshack.us/img401/5080/adadsaad.png

                    if (High[0] == DonchianChannel(55).Upper[0]
                    && SMA(40)[0] > SMA(200)[0])
                    {
                    EnterLong(DefaultQuantity, "EnterLong");
                    }

                    Comment


                      #11
                      When you're backtesting, the events will always be processed at the end of the bar. This is the same as running your strategy as CalculateOnBarClose = true.

                      This means that the order will not be submitted until the bar where the condition is true has closed. In your case, the High[0] is equal to the Don.Upper[0] on the previous bar, and results in the strategy submitting orders on the next bar.

                      Please review our Help Guide article on Discrepancies: Real-Time vs Backtest
                      http://www.ninjatrader.com/support/h...ime_vs_bac.htm
                      MatthewNinjaTrader Product Management

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by w0lverine, Today, 08:22 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post w0lverine  
                      Started by dkbyond, Today, 04:11 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post dkbyond
                      by dkbyond
                       
                      Started by markus3500, Today, 03:29 PM
                      1 response
                      18 views
                      0 likes
                      Last Post NinjaTrader_EricB  
                      Started by bohenriksen, Today, 04:00 AM
                      1 response
                      21 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by brett0, Today, 12:33 PM
                      3 responses
                      17 views
                      0 likes
                      Last Post marcus2300  
                      Working...
                      X