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

Stop and Target based on difference between two indicators.

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

    Stop and Target based on difference between two indicators.

    I am trying to program the stop and target in my strategy to be something like-
    (Max-Min)/2=stop/target.
    The max and the min being the indicators that come with Ninjatrader.
    I know there is probably a simple way to do this, I'm just not familiar with it.
    Any suggestions?
    Thanks!

    #2
    Hello Adamdetx,

    Thank you for your post.

    What does the line of code you are currently using look like?

    With the MAX and MIN you will need to use the correct Syntax.

    For information on MAX please visit the following link: http://www.ninjatrader.com/support/h...aximum_max.htm

    For information on MIN please visit the following link: http://www.ninjatrader.com/support/h...inimum_min.htm

    I look forward to your response.
    Last edited by NinjaTrader_PatrickH; 12-31-2012, 09:19 AM.

    Comment


      #3
      I recognize that there will need to be correct syntax for the indicator.
      Here's what I've tried so far-
      SetProfitTarget("RBShort", CalculationMode.Ticks, ((MAX(MaxPeriod)[0] - MIN(MinPeriod)[0])/2));
      I thought I could just toss it all in and it would spit out the target value I wanted, but when I tried this the strategies wouldn't load properly, so I tried the next method-

      public double stoploss()
      {
      double x = Math.Round(MAX(MaxPeriod)[0] - MIN(MinPeriod)[0]);
      return x * stopVariable;
      }

      SetStopLoss("RBShort", CalculationMode.Ticks, stoploss, false);

      But I got a couple of different errors
      Strategy\Test.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.SetProfitTarget (string NinjaTrader.Strategy.CalculationMode double)' has some invalid arguments CS1502 - click for info 52 4

      and
      Strategy\Test.cs Argument '3': cannot convert from 'method group' to 'double' CS1503 - click for info 52 54

      So what I'm trying to do is set the stop/target equal to a fraction of the difference between two indicators. Am I looking in the right direction?
      Last edited by Adamdetx; 12-31-2012, 09:16 AM. Reason: add a little more to the post

      Comment


        #4
        Hello Adamdetx,

        Thank you for your response.

        Are you trying to simply set the profit target and stop loss to the difference between MAX and MIN?
        This can be accomplished with SetProfitTarget("RBShort", CalculationMode.Ticks, (MAX(MaxPeriod)[0] - MIN(MinPeriod)[0]));

        When dividing MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] you will get a price and not a tick value. You can see this by printing the values with Print().

        For example:
        Code:
        double tick = (MAX(2)[0] - MIN(2)[0]/2);
        Print("Tick: " + tick);
        // The above will print a price such as 1200.75
        
        double tick = (MAX(2)[0] - MIN(2)[0]);
        Print("Tick: " + tick);
        // The above will print a tick such as 1.25
        If you use divide by two you will also need to use the CalculationMode.Price rather than Ticks.

        If you place MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] in parenthesis and then divide by two you get an invalid value. For instance the ES tick size is .25, let's say MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] returns .25 then you divide that by two and get .125, .125 is not a valid tick value for the instrument

        Please let me know if you have any questions.
        Last edited by NinjaTrader_PatrickH; 01-02-2013, 07:50 AM.

        Comment


          #5
          I'll try that and see what I can do. Thanks!

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello Adamdetx,

            Thank you for your response.

            Are trying to simply set the profit target and stop loss to the difference between MAX and MIN?
            This can be accomplished with SetProfitTarget("RBShort", CalculationMode.Ticks, (MAX(MaxPeriod)[0] - MIN(MinPeriod)[0]));

            When dividing MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] you will get a price and not a tick value. You can see this by printing the values with Print().

            For example:
            Code:
            double tick = (MAX(2)[0] - MIN(2)[0]/2);
            Print("Tick: " + tick);
            // The above will print a price such as 1200.75
            
            double tick = (MAX(2)[0] - MIN(2)[0]);
            Print("Tick: " + tick);
            // The above will print a tick such as 1.25
            If you use divide by two you will also need to use the CalculationMode.Price rather than Ticks.

            If you place MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] in parenthesis and then divide by two you get an invalid value. For instance the ES tick size is .25, let's say MAX(MaxPeriod)[0] - MIN(MinPeriod)[0] returns .25 then you divide that by two and get .125, .125 is not a valid tick value for the instrument

            Please let me know if you have any questions.
            I have been experimenting with it some. How would I integrate the code sample you provided into a simple script like the following?

            namespace NinjaTrader.Strategy
            {
            /// <summary>
            ///
            /// </summary>
            [Description("")]
            public class CoinToss : Strategy
            {
            #region Variables
            private int target = 2;
            private int stop = 4;


            #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()
            {
            SetProfitTarget("", CalculationMode.Ticks, target);
            SetStopLoss("", CalculationMode.Ticks, stop, false);
            CalculateOnBarClose = true;

            }
            ///<summary>
            /// Called on each bar update event (incoming tick)
            ///</summary>
            protected override void OnBarUpdate()
            {

            if (Position.MarketPosition != MarketPosition.Flat
            )

            return;
            // Condition set 1
            Random randNum = new Random();



            if (randNum.Next(0,2) == 1)
            {
            EnterLong(DefaultQuantity, "");
            }
            else
            {
            EnterShort(DefaultQuantity, "");
            }

            }

            #region Properties
            [Description("Set to desired profit target")]
            [GridCategory("Parameters")]
            public int Target
            {
            get { return target; }
            set { target = Math.Max(0, value); }
            }
            [Description("Set to desired profit target for long trades")]
            [GridCategory("Parameters")]
            public int Stop
            {
            get { return stop; }
            set { stop = Math.Max(0, value); }
            }
            #endregion
            }
            }


            I'd like to use it instead of the private ints for profit and stop

            Comment


              #7
              Hello Adamdetx,

              Thank you for your response on this matter.

              You can implement it in the following manner (I use 10 here for the MIN and MAX periods):
              Code:
              protected override void Initialize()
              {
              SetProfitTarget("RBShort", CalculationMode.Ticks, (MAX(10)[0] - MIN(10)[0]));
              SetStopLoss("RBShort", CalculationMode.Ticks, (MAX(10)[0] - MIN(10)[0]), false);
              Please let me know if I may be of further assistance.

              Comment


                #8
                I really appreciate you taking so much time on this with me. I tried this setup, and then when I tried to run a backtest it didn't load correctly. When I select the strategy with this code to backtest it shows the parameters from the prior selection, and the test results are those of the prior selection.
                I don't know why this is an issue, but I tried it with two different indicators in the sample code to the same result.
                Any ideas?
                Thanks.

                Comment


                  #9
                  Hello Adamdetx,

                  Thank you for your update on this matter.

                  So are the results from a previous instance of the strategy? Have you compiled the Strategy and ran the Backtest again?

                  Do you receive any error messages in the log tab of the NinjaTrader Control Center?

                  I look forward to your response.

                  Comment


                    #10
                    It isn't that it backtests a prior version of the strategy, it tests whatever the last strategy was that I had selected. For instance the code in question is part of a simple script titled "Cointoss".
                    So I go to the Strategy Analyzer and try to backtest. Automatically the dropdown selection box is selecting a strategy titled "A" since it is in Alphabetical order. When I select Cointoss it does not show me parameters for Cointoss, but for "A". When I run the backtest after selecting cointoss it shows me the results for "A" complete with a label at the top of the page that says "A" not "Cointoss".
                    Any ideas?

                    Comment


                      #11
                      Hello Adamdetx,

                      Thank you for your response.

                      So I may investigate this matter on my end please send me your Cointoss strategy 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=54842

                      I look forward to your response.

                      Comment


                        #12
                        Hello Adam,

                        Thank you for that file.

                        I have tested your strategy and found that in the Log tab of the NinjaTrader Control Center there was an error listed as "Object Reference Not Set To An Instance Of An Object".

                        This is due to the SetProfitTarget() and SetStopLoss() calling the entry order "RBShort", "RBShort" is not assigned to any of your entry orders.

                        In addition, you have SetProfitTarget() and SetStopLoss() with values that need to be called in the OnBarUpdate() method and not the Initialize() method. These would be FHHRange(6, 30, 9, 30).HighestHigh[0] - FHHRange(6, 30, 9, 30).LowestLow[0] and the MAX(5)[0] - MIN(5)[0]. The Intialize() method would not work for these calls to the current bars.

                        Please correct these items and advise if you can now see the correct variables in the Backtest menu for your strategy.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by dcriador, Today, 12:06 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post dcriador  
                        Started by dcriador, Today, 12:04 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post dcriador  
                        Started by cutzpr, Today, 08:54 AM
                        0 responses
                        11 views
                        0 likes
                        Last Post cutzpr
                        by cutzpr
                         
                        Started by benmarkal, Today, 08:44 AM
                        0 responses
                        20 views
                        0 likes
                        Last Post benmarkal  
                        Started by Tin34, Today, 03:30 AM
                        2 responses
                        29 views
                        0 likes
                        Last Post Tin34
                        by Tin34
                         
                        Working...
                        X