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

Activate Indicator Only during certain Timeframes

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

    Activate Indicator Only during certain Timeframes

    Hello Ninjatrader Team!

    I would like to know how can I Activate an Indicator only during certain Timeframes:

    For example, the user sets from 14:15 untill 14:45 activate this indicator.

    I made the following:

    First on State.DataLoaded I made Add the chart wenn the Bool NewsTimeframeActive = True.

    Then OnBarUpdate I set the conditions for NewsTimeframeActive = True.

    It does not work... What I am doing wrong?

    Code:
    private RangoHighLowBROKOLI RangoHighLowBROKOLI1;
    
    
    else if (State == State.DataLoaded)
                {    
    
                    
                    DirectXRenderizationPruebas1            = DirectXRenderizationPruebas();
                    
                        
                    if (NewsTimeframeActive)    
                        {
                        AddChartIndicator(DirectXRenderizationPruebas1);
                        }
                        
                        
                    ClearOutputWindow(); //Clears Output window every time strategy is enabled
    
                }​
    
    
    
    
            protected override void OnBarUpdate()
            {​
    
                if (ActivateAvoidNews
                    && (Avoid_News1 ? Time[1].TimeOfDay >= News1_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    && (Avoid_News1 ? Time[1].TimeOfDay <= News1_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    
                    && (Avoid_News2 ? Time[1].TimeOfDay >= News2_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    && (Avoid_News2 ? Time[1].TimeOfDay <= News2_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    
                    && (Avoid_News3 ? Time[1].TimeOfDay >= News3_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    && (Avoid_News3 ? Time[1].TimeOfDay <= News3_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    
                    && (Avoid_News4 ? Time[1].TimeOfDay >= News4_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    && (Avoid_News4 ? Time[1].TimeOfDay <= News4_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    
                    && (Avoid_News5 ? Time[1].TimeOfDay >= News5_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                    && (Avoid_News5 ? Time[1].TimeOfDay <= News5_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time)
                    )
                {
                    
                NewsTimeframeActive = true;
                Print(Time[1] + " News Timeframe Activated due to NewsTimeframeActive is: " + NewsTimeframeActive);
                
                }            ​
           }

    #2
    Hello tradingnasdaqprueba,

    AddChartIndicator is only called once when the script starts so it wouldn't be able to be used to control when an indicator is visible by using a variable. To control the visibility the indicator would need to be programmed to stop plotting or reset its drawings after a specific time so its no longer visible.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hey Jesse,

      Thanks for your help! Can you please explain me how can I reset the drawings after a specific time?

      Thanks!

      Comment


        #4
        Hello tradingnasdaqprueba,

        If the indicator should be fully visible it could toggle its IsVisible property to true/false if it should be visible or not. To draw only during certain hours you would need to make time conditions in the indicators logic so it does not draw outside of the set hours.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          I did it with .isvisible, the activation is okey but when the End time comes it does not deactivate the visualization... How can I update the chart so that the indicator disapears at the Endtime?

          Here the code:

          Code:
          if (ActivateAvoidNews
                          && (Avoid_News1 ? Time[1].TimeOfDay >= News1_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          && (Avoid_News1 ? Time[1].TimeOfDay <= News1_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          
                          && (Avoid_News2 ? Time[1].TimeOfDay >= News2_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          && (Avoid_News2 ? Time[1].TimeOfDay <= News2_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          
                          && (Avoid_News3 ? Time[1].TimeOfDay >= News3_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          && (Avoid_News3 ? Time[1].TimeOfDay <= News3_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          
                          && (Avoid_News4 ? Time[1].TimeOfDay >= News4_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          && (Avoid_News4 ? Time[1].TimeOfDay <= News4_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          
                          && (Avoid_News5 ? Time[1].TimeOfDay >= News5_StartTime.TimeOfDay : true) //Trade is being avoided within specified News time
                          && (Avoid_News5 ? Time[1].TimeOfDay <= News5_EndTime.TimeOfDay : true) //Trade is being avoided within specified News time)
                          )
                      {
                          
                      NewsTimeframeActive = true;
                      Print(Time[1] + " News Timeframe Activated due to NewsTimeframeActive is: " + NewsTimeframeActive);
                      
                      }            ​
          
                  if (NewsTimeframeActive)
                      {
                      DirectXRenderizationPruebas1.IsVisible = true;
                      
                      }
                      
                      else if (NewsTimeframeActive == false)    
                          {    
                              DirectXRenderizationPruebas1.IsVisible = false;    
                          }    
                          ​

          Comment


            #6
            Hello tradingnasdaqprueba,

            Are there any bar events after the end time? If the end time is the end of the session then it wont be able to be turned off because no further bar events will call that logic. You would need at least 1 bar event after the time when NewsTimeframeActive is false so DirectXRenderizationPruebas1 visibility can be set to false.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hi Jesse,

              Yes there are... for example I make one timeframe from 20:20 to 20:22 and it starts well but it doesnt end, the indicator keeps active...

              I would apreciate a bit help to resolve this.

              Thanks!

              Comment


                #8
                Hello tradingnasdaqprueba,

                You will need to add a Print into your else condition and make sure that is becoming true.

                Code:
                else if (NewsTimeframeActive == false)
                {
                    DirectXRenderizationPruebas1.IsVisible = false;
                    Print("Condition to turn off indicator is true");
                }
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jesse,

                  I did this but it does not print this... Only the TRUE condition.

                  Click image for larger version

Name:	image.png
Views:	39
Size:	16.4 KB
ID:	1302679

                  Comment


                    #10
                    Hello tradingnasdaqprueba,

                    That means the else is not happening to turn off the indicator, your condition remained true. You would need to look at the condition you made that sets NewsTimeframeActive to true and add an else statement to set it to false when that condition is not true.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Jesse,

                      I tried it but it does not work with the indicator, I think my indicator is the problem, just to clarify, it is an indicator with 2 lines rendering on the chart forming an X. (See the indicator attached), I tried initially put the code of the indicator in the Strategy but I got an Error: Error calling Onrender method object reference not set to an instance of an object, thats why I thought to do this way... Is there a way to solve this?

                      Click image for larger version

Name:	image.png
Views:	33
Size:	106.6 KB
ID:	1302739

                      If I use this it works, with the indicator deactivated. The thing is I need the X to appear and dissapear also...

                      Click image for larger version

Name:	image.png
Views:	48
Size:	39.8 KB
ID:	1302738

                      Code:
                      if (NewsTimeframeActive)
                      {
                      //DirectXRenderizationPruebas1.IsVisible = true;
                      NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 10) { Size = 30, Bold = true };
                      Draw.TextFixed(this, "Text2", "Alert! - Economic News Timeframe!", TextPosition.Center, Brushes.Crimson, myFont, Brushes.Transparent, Brushes.White, 70);
                      }
                      
                      else
                      {
                      //DirectXRenderizationPruebas1.IsVisible = false;
                      RemoveDrawObject("Text2");
                      }​

                      Comment


                        #12
                        Hello tradingnasdaqprueba,

                        For an indicator to be used as a signal it needs to have a plot. If you just want to render something on the chart you can do like you are from the indicator. To remove the indicator you would have to set its IsVisible to false. Your prints in the image end with activated so the last state the indicator sees is activated, if you want to disable it your condition to disable it needs to become true.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hey Jesse,

                          Could you please make an example for this?

                          Thanks!
                          TN

                          Comment


                            #14
                            Hello tradingnasdaqprueba,

                            What part are you asking about?

                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Jesse,

                              For an indicator to be used as a signal.

                              Thanks!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Critical_Catch, Today, 08:35 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Critical_Catch  
                              Started by smartromain, Today, 02:52 AM
                              0 responses
                              14 views
                              0 likes
                              Last Post smartromain  
                              Started by Marklhc1988, 04-19-2023, 11:11 AM
                              12 responses
                              576 views
                              1 like
                              Last Post victor68133  
                              Started by nicthe, Yesterday, 02:58 PM
                              1 response
                              13 views
                              0 likes
                              Last Post nicthe
                              by nicthe
                               
                              Started by percy3687, 05-17-2024, 12:28 AM
                              3 responses
                              35 views
                              0 likes
                              Last Post percy3687  
                              Working...
                              X