• 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

Not trading within the desired timeframe>> executing a trade on every single candle.

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

    Not trading within the desired timeframe>> executing a trade on every single candle.

    Hi there,

    So I attempted to code a strategy, which part of, is to only trade between 9h30-945am.


    I am new to Ninjascript, and did attempt to code it myself.


    I did have the condition "
    (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)"


    which is an if condition, which says if its in that time frame ALL the other conditions are 'open' if not then the other conditions don't work.


    Here is the image of the script placing trades on every single candle attached.



    Here is my code with the conditions:





    if



    // All conditions only work between 930-945am!



    (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)



    // Condition set 1: If DM+ crosses above DM- AND the open is less than yesterday's close> GO LONG



    if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)&& (ToTime(Time[0]) == 93000)

    && CurrentDayOHL().CurrentOpen[0] < PriorDayOHLC().PriorClose[0]);



    {

    EnterLong(DefaultQuantity, "");

    }



    // Condition set 2

    if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1) && (ToTime(Time[0]) == 93000)

    && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorClose[0]);



    {

    EnterShort(DefaultQuantity, "");

    }



    // Condition set 3

    if (Position.MarketPosition == MarketPosition.Long)

    {

    Variable0 = High[0] + ATR(14)[0] * TickSize;

    Variable3 = Low[0] + -2 * TickSize;

    }



    // Condition set 4

    if (Position.MarketPosition == MarketPosition.Short)

    {

    Variable1 = Low[0] - ATR(14)[0] * TickSize;

    Variable2 = High[0] + 2 * TickSize;

    }
    }



    Why is executing trades on every candle? and how should I fix the code?

    Thanks! and have a great day.
    Attached Files

    #2
    Hello,

    Thank you for your note.

    What I do not see here is that you need to nest the conditions checks inside the Time check
    Code:
    if(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
    {
         //input the rest of your conditions here
    
        if(condition a){}...
    
    }
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      Hello,

      Thank you for your note.

      What I do not see here is that you need to nest the conditions checks inside the Time check
      Code:
      if(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
      {
           //input the rest of your conditions here
      
          if(condition a){}...
      
      }


      Hi,


      Thanks for the reply. Please can you explain how its not nested




      Code:
      if
      		
      			// All conditions only work between 930-945am!
      			
      		(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
      			
      				// Condition set 1: If DM+ crosses above DM- AND the open is less than yesterday's close> GO LONG
      
      
      				if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)&& (ToTime(Time[0]) == 93000)
      					&& CurrentDayOHL().CurrentOpen[0] < PriorDayOHLC().PriorClose[0]);
      					
      				{
      					EnterLong(DefaultQuantity, "");
      				}
      
      
      				// Condition set 2
      				if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1) && (ToTime(Time[0]) == 93000)
      					&& CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorClose[0]);
      				
      				{
      					EnterShort(DefaultQuantity, "");
      				}
      
      
      			// Condition set 3
      				if (Position.MarketPosition == MarketPosition.Long)
      				{
      					Variable0 = High[0] + ATR(14)[0] * TickSize;
      					Variable3 = Low[0] + -2 * TickSize;
      				}
      
      
      				// Condition set 4
      				if (Position.MarketPosition == MarketPosition.Short)
      				{
      					Variable1 = Low[0] - ATR(14)[0] * TickSize;
      					Variable2 = High[0] + 2 * TickSize;
      				}
              }

      Thank you

      Comment


        #4
        Your time condition does not have any { } around it.

        Therefore, only the first statement (Condition 1) after that is executed with time constraints. Conditions ,3,4 are executed ALL DAY LONG WITHOUT any time check.

        NT_cal's suggestion was to put the { } around all your IF statements.

        This rule on { } is standard across many programming languages.

        Comment


          #5
          Still not adhering to certain conditions

          @sledge @NinjaTrader_Cal
          Originally posted by sledge View Post
          Your time condition does not have any { } around it.

          Therefore, only the first statement (Condition 1) after that is executed with time constraints. Conditions ,3,4 are executed ALL DAY LONG WITHOUT any time check.

          NT_cal's suggestion was to put the { } around all your IF statements.

          This rule on { } is standard across many programming languages.
          Thank you for your suggestion.


          Now it does only trade within the specified timeframe, however it now enters a trade at the open of each and every candle (please see attached image)


          I only want it to enter a trade if the DM +- crossover.




          Here is the code again:




          Code:
           #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()
                  {
                      SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                      SetProfitTarget("", CalculationMode.Ticks, Variable1);
                      SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                      SetTrailStop("", CalculationMode.Ticks, Variable3, false);
          
          
                      CalculateOnBarClose = true;
                  }
          
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
          			
          		if
          		
          			// All conditions only work between 930-945am!
          			
          		(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
          			
          		{		// Condition set 1: If DM+ crosses above DM- AND the open is less than yesterday's close> GO LONG
          				
          if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)&& (ToTime(Time[0]) == 93000)
          					&& CurrentDayOHL().CurrentOpen[0] < PriorDayOHLC().PriorClose[0]);
          					
          				{
          					EnterLong(DefaultQuantity, "");
          				}
          
          
          				// Condition set 2
          				if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1) && (ToTime(Time[0]) == 93000)
          					&& CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorClose[0])};
          				
          				{
          					EnterShort(DefaultQuantity, "");
          				}
          
          
          				// Condition set 3
          				if (Position.MarketPosition == MarketPosition.Long)
          				{
          					Variable0 = High[0] + ATR(14)[0] * TickSize;
          					Variable3 = Low[0] + -2 * TickSize;
          				}
          
          
          				// Condition set 4
          				if (Position.MarketPosition == MarketPosition.Short)
          				{
          					Variable1 = Low[0] - ATR(14)[0] * TickSize;
          					Variable2 = High[0] + 2 * TickSize;
          				}
                  }
          
          
                  #region Properties
                  #endregion
              }
          }}



          • I have just tested this on the 1 min chart, do I have to have a daily chart for it to compare todays open to yesterday's close?


          Thank You so much for the assistance
          Attached Files

          Comment


            #6
            That's easy.

            Change:


            CalculateOnBarClose = true;

            to


            CalculateOnBarClose = false;




            Originally posted by teenagemutantNT View Post
            @sledge @NinjaTrader_Cal

            Thank you for your suggestion.


            Now it does only trade within the specified timeframe, however it now enters a trade at the open of each and every candle (please see attached image)


            I only want it to enter a trade if the DM +- crossover.




            Here is the code again:




            Code:
             #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()
                    {
                        SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                        SetProfitTarget("", CalculationMode.Ticks, Variable1);
                        SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                        SetTrailStop("", CalculationMode.Ticks, Variable3, false);
            
            
                        CalculateOnBarClose = true;
                    }
            
            
            }}



            • I have just tested this on the 1 min chart, do I have to have a daily chart for it to compare todays open to yesterday's close?


            Thank You so much for the assistance

            Comment


              #7
              Still not executing other conditions

              Originally posted by sledge View Post
              That's easy.

              Change:


              CalculateOnBarClose = true;

              to


              CalculateOnBarClose = false;
              Hi Sledge,


              Not really that easy.


              See these are my conditions (aside from the 9h30 - 9h45 timeframe):


              1. Compare the open at 9h30 to yesterday's close , if OPEN < Yday Close && There is a DM+ crosses above DMi THEN go long (action)
              2. Compare the open at 9h30 to yesterday's close , if OPEN > Yday Close && There is a DM- crosses above DM+ THEN go short (action)
              3. Profit target is 1 ATR above the high of the entry candle (long)
              4. Trailing Stop is two ticks below the low of every closing candle



              If you look at the picture again, you will see that it simply places buy orders on every candle between 9h30-45. It should of placed a sell order at 9h36 (assuming yday close > todays open).


              Here is the code again, (sorry I didn't paste it right the last time)


              Code:
                #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()
                      {
                          SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                          SetProfitTarget("", CalculationMode.Ticks, Variable1);
                          SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                          SetTrailStop("", CalculationMode.Ticks, Variable3, false);
              
              
                          CalculateOnBarClose = true;
                      }
              
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
              			
              		if
              		
              			// All conditions only work between 930-945am!
              			
              		(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
              			
              		{		// Condition set 1: If DM+ crosses above DM- AND the open is less than yesterday's close> GO LONG
              				if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)&& (ToTime(Time[0]) == 93000)
              					&& CurrentDayOHL().CurrentOpen[0] < PriorDayOHLC().PriorClose[0]);
              					
              				{
              					EnterLong(DefaultQuantity, "");
              				}
              
              
              				// Condition set 2
              				if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1) && (ToTime(Time[0]) == 93000)
              					&& CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorClose[0]);
              				
              				{
              					EnterShort(DefaultQuantity, "");
              				}
              
              
              				// Condition set 3
              				if (Position.MarketPosition == MarketPosition.Long)
              				{
              					Variable0 = High[0] + ATR(14)[0] * TickSize;
              					Variable3 = Low[0] + -2 * TickSize;
              				}
              
              
              				// Condition set 4
              				if (Position.MarketPosition == MarketPosition.Short)
              				{
              					Variable1 = Low[0] - ATR(14)[0] * TickSize;
              					Variable2 = High[0] + 2 * TickSize;
              				}
                      }
              
              
                      #region Properties
                      #endregion
                  }
              }}

              @NinjaTrader_Cal @sledge
              Attached Files

              Comment


                #8
                Ooooops... late night last night.

                Do you have EntriesPerDirection set anywhere? Is it set to more than 1?

                EntriesPerDirection will only allow x number of trades in that same direction.

                Is yours set to 0 or 100 instead of 1 ?



                Originally posted by teenagemutantNT View Post
                Hi Sledge,


                Not really that easy.


                See these are my conditions (aside from the 9h30 - 9h45 timeframe):


                1. Compare the open at 9h30 to yesterday's close , if OPEN < Yday Close && There is a DM+ crosses above DMi THEN go long (action)
                2. Compare the open at 9h30 to yesterday's close , if OPEN > Yday Close && There is a DM- crosses above DM+ THEN go short (action)
                3. Profit target is 1 ATR above the high of the entry candle (long)
                4. Trailing Stop is two ticks below the low of every closing candle



                If you look at the picture again, you will see that it simply places buy orders on every candle between 9h30-45. It should of placed a sell order at 9h36 (assuming yday close > todays open).


                Here is the code again, (sorry I didn't paste it right the last time)


                Code:
                  #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()
                        {
                            SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                            SetProfitTarget("", CalculationMode.Ticks, Variable1);
                            SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                            SetTrailStop("", CalculationMode.Ticks, Variable3, false);
                
                
                            CalculateOnBarClose = true;
                        }
                
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                			
                		if
                		
                			// All conditions only work between 930-945am!
                			
                		(ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
                			
                		{		// Condition set 1: If DM+ crosses above DM- AND the open is less than yesterday's close> GO LONG
                				if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)&& (ToTime(Time[0]) == 93000)
                					&& CurrentDayOHL().CurrentOpen[0] < PriorDayOHLC().PriorClose[0]);
                					
                				{
                					EnterLong(DefaultQuantity, "");
                				}
                
                
                				// Condition set 2
                				if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1) && (ToTime(Time[0]) == 93000)
                					&& CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorClose[0]);
                				
                				{
                					EnterShort(DefaultQuantity, "");
                				}
                
                
                				// Condition set 3
                				if (Position.MarketPosition == MarketPosition.Long)
                				{
                					Variable0 = High[0] + ATR(14)[0] * TickSize;
                					Variable3 = Low[0] + -2 * TickSize;
                				}
                
                
                				// Condition set 4
                				if (Position.MarketPosition == MarketPosition.Short)
                				{
                					Variable1 = Low[0] - ATR(14)[0] * TickSize;
                					Variable2 = High[0] + 2 * TickSize;
                				}
                        }
                
                
                        #region Properties
                        #endregion
                    }
                }}

                @NinjaTrader_Cal @sledge

                Comment


                  #9
                  Originally posted by sledge View Post
                  Ooooops... late night last night.

                  Do you have EntriesPerDirection set anywhere? Is it set to more than 1?

                  EntriesPerDirection will only allow x number of trades in that same direction.

                  Is yours set to 0 or 100 instead of 1 ?
                  No I dont.

                  so should I set it up like this?


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


                  Never used this before.


                  Thank You Sledge and have a great weekend

                  Comment


                    #10
                    Hello teenagemutantNT,

                    Thank you for your response.

                    Yes, please try using 1 for the EntriesPerDirection.
                    Patrick H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Patrick,

                      Thanks for replying back to my post.


                      PLease can you tell me specifically in which block of code to insert the entries per a direction code?

                      PLease refer the code I have in post # 7



                      Thank You and have a great day!

                      Comment


                        #12
                        teenagemutantNT, that would be done either from the user interface or in the script's Initialize() method - however I don't think that the issue you run into here. We will review your code posted and be back in touch with further thoughts. Thanks for the patience.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Hello,

                          To answer your original question of where to place the Entries per direction, as Bertrand said this can be used in the Initialize of your script. Here is the documentation link with an example:
                          http://www.ninjatrader.com/support/h...esPerDirection


                          As for whats happening in the script, can you provide what settings you are using for the variables in the script?

                          Also in your Initialize
                          Code:
                          protected override void Initialize()
                                  {
                                     SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                                      SetProfitTarget("", CalculationMode.Ticks, Variable1);
                                      SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                                      SetTrailStop("", CalculationMode.Ticks, Variable3, false);
                                      CalculateOnBarClose = true;
                                  }
                          I see that you have two SetTrailStop statements set to a blank signal name with different variables, was one of these mean to have a signal name?

                          After testing the script I have set the last SetTrailStop variable manually so I could test this and was able to get trade that span across multiple bars.

                          If you please tell me what instrument this is being used on and also what exact settings you are setting in the variables that would help determine what is happening.

                          I look forward to being of further assistance.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_Jesse View Post
                            Hello,

                            As for whats happening in the script, can you provide what settings you are using for the variables in the script?

                            Also in your Initialize
                            Code:
                            protected override void Initialize()
                                    {
                                       SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                                        SetProfitTarget("", CalculationMode.Ticks, Variable1);
                                        SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                                        SetTrailStop("", CalculationMode.Ticks, Variable3, false);
                                        CalculateOnBarClose = true;
                                    }
                            I see that you have two SetTrailStop statements set to a blank signal name with different variables, was one of these mean to have a signal name?

                            After testing the script I have set the last SetTrailStop variable manually so I could test this and was able to get trade that span across multiple bars.

                            If you please tell me what instrument this is being used on and also what exact settings you are setting in the variables that would help determine what is happening.

                            I look forward to being of further assistance.


                            Hi Jesse,


                            I apologize for the delay in getting back to you.




                            1. Settings:


                            Variable0 = High[0] + ATR(14)[0] * TickSize;
                            Variable3 = Low[0] + -2 * TickSize;



                            Variable1 = Low[0] - ATR(14)[0] * TickSize;
                            Variable2 = High[0] + 2 * TickSize;



                            ie. V0 = High of candle + ATR (profit target)


                            V3 (Trailing Stop) = 2 ticks below the Low of Candle


                            V1 (PT) = Low of candle - 1 ATR


                            V2 (Trail Stop) = 2 ticks above the High of candle




                            2. I did not intend to call a signal name.


                            3. I was testing this on the ES contract. , essentially it is supposed to make one trade between 9h30 -9h45.




                            Thanks for all the effort i am deeply grateful!



                            @ NinjaTrader_Jesse
                            Last edited by teenagemutantNT; 11-15-2014, 04:20 PM.

                            Comment


                              #15
                              Hello,

                              Thank you for the additioanl information.

                              After looking over the script again there are a couple items I had not noticed before.

                              First would be in the Initialize

                              Code:
                              SetProfitTarget("0", CalculationMode.Ticks, Variable0);
                              SetProfitTarget("", CalculationMode.Ticks, Variable1);
                              SetTrailStop("", CalculationMode.Ticks, Variable2, false);
                              SetTrailStop("", CalculationMode.Ticks, Variable3, false);
                              First the SetProfitTargets. In the script currently there is no signal name 0 so this profit target would not be called, the second would be used instead.

                              With the Trail Stops, these are both the same blank signal name so the first would be over taken by the second so in reality we are left with the following:
                              Code:
                              SetProfitTarget("", CalculationMode.Ticks, Variable1);
                              SetTrailStop("", CalculationMode.Ticks, Variable3, false);
                              I tried the code below and left the rest of the script as is and I am able to see trades span multiple bars and not fill in the same bar.

                              Code:
                              SetProfitTarget("", CalculationMode.Ticks, 4);
                              SetTrailStop("", CalculationMode.Ticks, 4, false);
                              I was using an ES chart for this test so that is where the 4 ticks is coming from.

                              Next the way you are calculating the variables, you specify Ticks but the output from the variables is a price.
                              Code:
                              Variable3 = Low[0] + -2 * TickSize;
                              Variable1 = Low[0] - ATR(14)[0] * TickSize;
                              These are both price amounts rather than ticks placing the target and trail in some pretty strange locations if you will be using a price you will need to use CalculationMode.Price instead of CalculationMode.Ticks or use an amount of ticks rather than a price.

                              I look forward to being of further assistance.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cstern71, 09-02-2019, 10:30 AM
                              4 responses
                              54 views
                              0 likes
                              Last Post Lancer
                              by Lancer
                               
                              Started by Hanstanz, Today, 04:44 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post Hanstanz  
                              Started by juniortut, Today, 04:38 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post juniortut  
                              Started by rojiinko, Today, 03:58 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post rojiinko  
                              Started by mreanuzlazx, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post mreanuzlazx  
                              Working...
                              X