Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

defining a timeperiod?

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

    defining a timeperiod?

    Can anyone please tell me how to define and use a timeframe in NT?

    Maybe someone can translate the short example into a code for better understanding.

    Its part of a tiny and simple 90% winning strategy i attached below. Though the trigger doesnt occure very often on a daily bar chart, you can use smaller time frames to trade. It works on any timeframe!
    Would be great if someone could translate that into a code and post it or send it to me via email: [email protected]

    Thanks in advance!

    Example:

    If RSI (4) was >=70 during the last (-1)-(-20) bars and cross below 30 on bar 0
    then enter short.


    In the condition builder, if i use the "bars ago" feature it only compares the current value with the value of exact # of bar ago. There is no possibility to campare current value to a value during a timeframe.
    ------------------------------------------------------------

    Here is the complete strategy!

    If RSI 4 was below 30 during last 20 bars
    If RSI 9 was above 70 during last 5 bars
    If RSI 4 was above 70 during last 5 bars
    -----------
    If RSI cross below 30 during last 5 bars

    enter short
    -------------
    if RSI 9 <= 30 (or cross below 30)

    exit short.


    This can be mirrored for a long position
    Last edited by sosMsos; 04-22-2011, 04:01 AM.

    #2
    Welcome to our forums here - for getting started in NinjaScript I would suggest those tutorials -



    For your code I think you're looking for the CountIf method -



    For a professional code conversion please look into this page from our website where several consultants specialized in this service are showcased -

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hello can someone help out please, check the code?

      I have coded the strategy above but, backtesting gives no results:

      thats the Idea:


      If RSI 4 was below 30 during last 20 bars
      If RSI 9 was above 70 during last 5 bars
      If RSI 4 was above 70 during last 5 bars
      -----------
      If RSI cross below 30 during last 5 bars

      enter short
      -------------
      if RSI 9 <= 30 (or cross below 30)

      exit short.


      ->mirrored for a long position
      ---------------------------------------

      thats the 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>
      /// thrust strategy
      /// </summary>
      [Description("thrust strategy")]
      public class RSIindicator : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int rSIperiod01 = 4; // Default setting for RSIperiod01
      private int rSIPeriod02 = 9; // Default setting for RSIPeriod02

      // 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(RSI(RSIperiod01, 4));
      Add(RSI(RSIPeriod02, 9));

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CountIf(delegate {(RSI(RSIperiod01, 0).Avg[0] < 30)
      ;}, 20)
      &&
      if (CountIf(delegate {(RSI(RSIperiod01, 0).Avg[0] >= 70)
      ;}, 5)
      &&
      if (CountIf(delegate {(RSI(RSIperiod02, 0).Avg[0] >= 70);}, 5)
      &&
      if CrossBelow(RSI(RSIperiod01, 0).Avg, 30, 1);
      {
      EnterShort(DefaultQuantity, "");
      }


      // Condition set 2
      if (RSI(RSIPeriod02, 0).Avg[0] <= 30)
      {
      ExitShort("", "");
      }

      // Condition set 3


      if (CountIf(delegate {(RSI(RSIperiod01, 0).Avg[0] > 70)
      ;}, 20)
      &&
      if (CountIf(delegate {(RSI(RSIperiod01, 0).Avg[0] <= 30)
      ;}, 5)
      &&
      if (CountIf(delegate {(RSI(RSIperiod02, 0).Avg[0] <= 30);}, 5)
      &&
      if CrossAbove(RSI(RSIperiod01, 0).Avg, 70, 1);
      {
      EnterLong(DefaultQuantity, "");
      }

      // Condition set 4

      if (RSI(RSIPeriod02, 0).Avg[0] >= 70)
      {
      ExitLong("", "");
      }


      }
      }

      #region Properties
      [Description("RSI Period 1")]
      [GridCategory("Parameters")]
      public int RSIperiod01
      {
      get { return rSIperiod01; }
      set { rSIperiod01 = Math.Max(1, value); }
      }

      [Description("RSI Period 2")]
      [GridCategory("Parameters")]
      public int RSIPeriod02
      {
      get { return rSIPeriod02; }
      set { rSIPeriod02 = Math.Max(1, value); }
      }

      [Description("Stop Loss Offset")]
      [GridCategory("Parameters")]
      public int StopLoss
      {
      get { return stopLoss; }
      set { stopLoss = Math.Max(1, value); }
      }
      #endregion
      }
      }
      Last edited by sosMsos; 04-22-2011, 07:41 AM.

      Comment


        #4
        Hello,

        Thanks for your note.

        I dont see any off at first look. I would need to test it on my side.

        Unfortunately when I copy the code into my NinjaTrader I cannot get it to compile most likely missing a } somewhere when you copied it in.

        However lets do this first.

        First please add Print(); statements tyo your code in your If Statements. We need to verify that they are running and continue adding Print()'s and printing out variables until we find where the error is.

        Please see this guide on how to do this.



        Also, please insure you have data for the instrument your trying to backtest.

        If you are still having trouble tracking it down please let me know.

        Comment


          #5
          Thanks for the reply Brett. I have also problems with compiling. Though even when i use the condition builder alone, unlock code then compile (without any additional coding) compiling gives error messages.

          Can you give a brief example how to insert the print() code?

          -> if (CountIf(delegate {(RSI(RSIperiod01, 0).Avg[0] < 30) ;}, 20)

          Comment


            #6
            Hello,

            First, please before we worry about the prints.

            We need to have code the compiles.

            What compile errors are you getting and what lines are reported by the compiler as having issue.

            Please post the entire code lines for these that are in error and I can attempt to assist.

            I look forward to assisting you further.

            Comment


              #7
              Thanks Brett:
              Here the error messages (error in code marked red), the errors i did not code. these codes were attached by NT itself after compiling what was build in the wizzard before, after unlocking the code to attache the coding for the "Countif" commands :



              line 21-22:
              [Description("thrust strategy")]
              public class RSIindicator : Strategy

              line 93-96:
              #region Properties
              [Description("RSI Period 1")]
              [GridCategory("Parameters")]
              public int RSIperiod01


              line 101-104:

              #region Properties
              [Description("RSI Period 1")]
              [GridCategory("Parameters")]
              public int RSIperiod01

              line 110-112:

              [Description("RSI Period 2")]
              [GridCategory("Parameters")]
              public int RSIPeriod02

              line 118-120:

              [Description("Stop Loss Offset")]
              [GridCategory("Parameters")]
              public int StopLoss
              --------

              119:

              }

              127:

              }

              This is strange, that he indicates a bracket error in line 119, cause the same bracket error is valid for line 127 end of document. In fact the line 119 he marked as bracket error does not exist this way. See error message line 118-120.
              Last edited by sosMsos; 04-22-2011, 10:32 AM.

              Comment


                #8
                Hello sosMsos,

                What may work best is first copying over the critical elements of those scripts into notepad or another text editor, and then deleting and recreating the scripts.

                If you have a bracket in the wrong place or are missing one, it can be challenging to track down for a beginner, so it's best to start over with a fresh new template to work from.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Hi i have now eliminated all coding errors, and it works.
                  Though when i add the the last "if" condition,
                  during backtesting i dont get any trades.But without it i dont get what i want.
                  May check with SPY daily.

                  Are these conditions setting each other off?

                  Strategy should be:
                  if RSI4 was at least one time below 30 for the last 20 bars
                  if RSI4 was at least one time above 70 for the last 20 bars
                  if RSI9 was at least one time above 70 for the last 5 bars
                  and if RSI4 crossed below 30 during the last 5 bars

                  then enter short

                  exit short if RSI9 crosses 30

                  Here is the coding:

                  #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>
                  /// test
                  /// </summary>
                  [Description("test")]
                  public class test : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  // 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(RSI(4, 0));
                  Add(RSI(9, 0));

                  CalculateOnBarClose = true;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (CountIf(delegate {return (RSI(4, 0).Avg[0] <=30);}, 20) >1)
                  if (CountIf(delegate {return (RSI(4, 0).Avg[0] >= 70);}, 20) >1)
                  if (CountIf(delegate {return (CrossBelow(RSI(9, 0).Avg, 70, 1));}, 5) >1)
                  if (CountIf(delegate {return (CrossBelow(RSI(4, 0).Avg, 30, 1));}, 5) >1)
                  {

                  EnterShort(DefaultQuantity, "");

                  }
                  // Condition set 2
                  if (RSI(9, 0).Avg[0] <= 30)
                  {
                  ExitShort("", "");
                  }
                  }


                  #region Properties
                  #endregion
                  }
                  }
                  Last edited by sosMsos; 04-22-2011, 05:21 PM.

                  Comment


                    #10
                    Update: everything is working fine now. But its not right the idea i have in mind. Maybe i need a nother counting method. The code doesnt differentiate between the last 5 bars and the last 20 bars it should in fact be last 20bars before the last 5 bars. I dont know from where NT starts counting the bars. The events that should happen in the last 5 bars, NT is mixing together with the events that should happen in the last 20 bars to give a trigger.

                    Idea is that:
                    RSI4 wents during the last 20 bars from overbought (70) to oversold (30) and during the last 5 bars from oversold to overbought.
                    +
                    RSI9 crosses above oversold (30) during last 5 bars.
                    The (working!) code that i thought would track that behavior is here, but its not doing it right:

                    if (CountIf(delegate {return (CrossBelow(RSI(4, 0).Avg, 70, 1));}, 20) >0)
                    if (CountIf(delegate {return (CrossBelow(RSI(4, 0).Avg, 30, 1));}, 20) >0)
                    if (CountIf(delegate {return (CrossAbove(RSI(9, 0).Avg, 30, 1));}, 5) >0)
                    if (CountIf(delegate {return (CrossAbove(RSI(4, 0).Avg, 70, 1));}, 5) >0)
                    if (CountIf(delegate {return (CrossAbove(RSI(4, 0).Avg, 30, 1));}, 5) >0)
                    {
                    EnterLong(DefaultQuantity, "");
                    }
                    Any ideas how to improve the code?
                    Last edited by sosMsos; 04-23-2011, 06:37 AM.

                    Comment


                      #11
                      update:
                      i have solved everything on my own, dont need any more help on this issue
                      - you can close thead

                      Comment


                        #12
                        Hi,

                        Thanks for letting us know. We will close this thread.

                        Enjoy the upcoming week and Happy Trading!!
                        Vince B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Shansen, 08-30-2019, 10:18 PM
                        24 responses
                        940 views
                        0 likes
                        Last Post spwizard  
                        Started by Max238, Today, 01:28 AM
                        0 responses
                        9 views
                        0 likes
                        Last Post Max238
                        by Max238
                         
                        Started by rocketman7, Today, 01:00 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post rocketman7  
                        Started by wzgy0920, 04-20-2024, 06:09 PM
                        2 responses
                        28 views
                        0 likes
                        Last Post wzgy0920  
                        Started by wzgy0920, 02-22-2024, 01:11 AM
                        5 responses
                        33 views
                        0 likes
                        Last Post wzgy0920  
                        Working...
                        X