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

Little modification to Flash/Sound Bar Timer

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

    Little modification to Flash/Sound Bar Timer

    I was wondering if you possibly know what code should I use in the Flash Sound Bar Timer by Paul in order to get an alert 30 seconds before bar instead of at the actual bar close?

    HTML Code:
    https://ninjatraderecosystem.com/user-app-share-download/bar-timer-flash-sound-alert-2/
    I was trying to find it as I've made some indicators in the past but I struggle with this one to be honest.

    #2
    Hello Ludwik,

    The indicator you linked to is using IsFirstTickOfBar in its conditions to know when bars change to do the action. If you wanted to do something X time before a bar close you would have to remove IsFirstTickOfBar and also calculate when the bar is going to close to make a time condition to check for that time. That would be totally dependent on what time series you select as the primary. For example if you use 1 minute bars the timestamps are only in 1 minute intervals so checking 30 seconds before a bar close would not be possible on that series. You could use a series that has 30 second intervals like a 1 second bar or 30 second bar and then it would be able to have timestamps with 30 second granularity so you could form a time condition to check for that.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you Jesse
      1. What about calculating On Price Change or Tick and have some sort of formular which would either add time to most recent bar close or deduct from the next bar close?
      2. Also I need it for 15 min and 5 min timeframes only.

      Would it be too much work for you to give an example of how to approach it?

      Comment


        #4
        Hello Ludwik,

        The script already uses OnEachTick so you wouldn't need to change that. To be able to detect 30 second increments you need OnBarUpdate to be called for a series that has that granularity of timestamps.If you plan on using a series that does not have 30 second time increments like 15 or 5 minute series you would have to add a secondary series that does have those time increments. That is so OnBarUpdate is called at that frequency and so that the bar times have 30 second increments for your time conditions.

        You can see examples of how to add secondary series and also how to make BarsInProgress conditions to separate logic here:

        https://ninjatrader.com/support/help...ghtsub=adddata
        https://ninjatrader.com/support/help...barsinprogress
        https://ninjatrader.com/support/help...lightsub=multi

        TO make time conditions you can see a sample of that here: https://ninjatrader.com/support/help...ightsub=totime

        To generate a bar time that is 30 seconds before the close of another series you would need to take the primary series last closed bars time and then add the amount of time minus 30 seconds. For example if you used a 15 minute bar that would look like the following:

        Code:
        DateTime newTime = Times[0][0].AddMinutes(15).AddSeconds(-30);
        Add 15 minutes to the last closed time to get the next bar close time and then subtract 30 seconds from that time. YOu can then use that time in the secondary series events to check if the time is past that time:

        Code:
        if(BarsInProgress == 1)
        {
            DateTime newTime = Times[0][0].AddMinutes(15).AddSeconds(-30);
            if(Time[0] > newTime)
            {
            }
        }
        https://learn.microsoft.com/en-us/do...ramework-4.8.1
        https://learn.microsoft.com/en-us/do...ramework-4.8.1




        Last edited by NinjaTrader_Jesse; 11-10-2023, 11:11 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I've been trying to add the code which you sent me and tried few different methods but I'm keep getting CS0246 error message about "DataTime" directive missing.
          Would you mind taking to take a look where I've got it wrong? I really feel like I'm missing something fairly simple here.
          Code:
                          Calculate                                    = Calculate.OnBarClose;
                          IsOverlay                                    = true;
                          DisplayInDataBox                            = true;
                          DrawOnPricePanel                            = true;
                          DrawHorizontalGridLines                        = true;
                          DrawVerticalGridLines                        = true;
                          PaintPriceMarkers                            = true;
                          ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                          //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                          //See Help Guide for additional information.
                          IsSuspendedWhileInactive                    = true;
          
                          BarWidth                        = 150;
                          BarHeight                        = 10;
                          LeftRightOffset                    = 0;
                          VerticalOffset                    = 0;
                          LeftRightMarginPercent            = .05;
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if(BarsInProgress == 1)
                      {
                          DataTime newTime = Times[0][0].AddMinutes(15).AddSeconds(-30);
                              if(Time[0] > newTime)
          
                             {            
                              if (timer == null && DisplayTime() && !DelayedData() && MinuteBars())
                      {
                              timer = new System.Windows.Forms.Timer();
                              timer.Interval = 1000;
                              timer.Tick += OnFancyBarTimerTick;
                              timer.Enabled = true;
                      }
                          }
                      }​

          Comment


            #6
            Hello Ludwik,

            Do you have the correct using statements at the top of the file? You can see the standard using statements by opening any stock indicator like the ADL.

            Another note is that you should not use the Windows.Forms timer in a WPF application, you can see an example of what would be required for a timer in NT8 in the following link:


            JesseNinjaTrader Customer Service

            Comment


              #7
              Hey Jesse,

              I think I've done everything as per your suggestions from scratch:
              • Checked Standard Using Statements
              • File is also free from Windows.Forms
              However for some strange reason I'm still getting the same error at line 120 related to DataTime. Would you mind and take a look where I've got it wrong?
              I'm attaching an indicator for you to make it easier to see instead of sending a code.
              Attached Files

              Comment


                #8
                Hello Ludwik,

                It looks like there is just a spelling mistake, that should be DateTime.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jesse,

                  So an error disappeared. Thanks but unfortunately the indicator doesn't seem to work.
                  • There is no elapsing time or clock displayed like with the original version
                  • There is no alarm triggered either
                  This is the data which I'm using for testing purposes:
                  Code:
                          protected override void OnBarUpdate()
                          {
                  
                              if(BarsInProgress == 1)
                              {
                                  DateTime newTime = Times[0][0].AddMinutes(2).AddSeconds(-30);
                                  if(Time[0] > newTime)
                                  {    
                                                  if (State == State.Realtime)
                                      {
                                              hasRealtimeData = true;
                                              connected = true;
                  
                                      if (IsFirstTickOfBar && Sound)
                                          {
                                          PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");}
                                      }
                  
                                     }
                                }
                          }​
                  Would you be able to point me in the direction where I've got it wrong?

                  Comment


                    #10
                    Hello Ludwik,

                    If something is not working you would usually have to use prints to debug the conditions to see why that's happening. The code that I had suggested is just how to calculate the time and would need to be used with a secondary series, the script you previously attached is not adding a secondary series so that code won't be executed. You can use AddDataSeries to add a secondary series. That secondary series needs to have intervals of time that supports the amount of seconds you waned, for example a 30 second series.





                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      OK, I've added 2 min data series, as when there was a "AddMinutes(2)" further in the code then the clock wouldn't appear and alert wouldn't trigger at all.

                      Now:
                      • Time lapse and clock is visible
                      • Alarm goes on
                      • However alarm supposed to go on -30 sec. before 1 min candle closes and it goes at the same time as before upon candle close
                      Could you please take a look at the code and let me know where I got it wrong as it doesn't seem to trigger -30 sec before? I believe I've got everything as instructed now.
                      Code:
                          public class ADelayedBarTimerFlashFusion : Indicator
                          {
                              private string            timeLeft    = string.Empty;
                              private DateTime        now             = Core.Globals.Now;
                              private bool            connected,
                                                      hasRealtimeData;
                              private SessionIterator sessionIterator;
                      
                              private System.Windows.Threading.DispatcherTimer timer;
                      
                              private DisplayedType    dType    = DisplayedType.All;
                              private FlashedType    fType    = FlashedType.WholePanel;
                      
                              private int var1 = 0;
                              private int var2 = 0;
                      
                              private TimeSpan barTimeLeft;
                      
                              private System.Windows.Media.Brush    textBrush;
                      
                              private SimpleFont textFont;
                      
                              protected override void OnStateChange()
                              {
                                     if (State == State.Configure)
                                    {AddDataSeries(BarsPeriodType.Minute, 2);}
                      
                      
                                  else if (State == State.SetDefaults)
                                  {
                                      Description = @"Displays time countdown of interval based bar series.";
                                      Name = "ADelayedBarTimerFlashFusion";
                                      Calculate = Calculate.OnEachTick;
                                      DrawOnPricePanel = false;
                                      IsChartOnly = true;
                                      IsOverlay = true;
                                      DisplayInDataBox = false;
                                      Sound            = true;
                      
                                      AlertDuration = 10;
                                      Alert = 12;
                      
                                      TextBrush = System.Windows.Media.Brushes.DodgerBlue;
                      
                                      TextFont = new SimpleFont("Arial", 12);
                                  }
                                  else if (State == State.DataLoaded)
                                  {
                                      if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute)
                                          var2 = (BarsPeriod.Value*60) - Alert;
                                      else if (BarsPeriod.BarsPeriodType == BarsPeriodType.Second)
                                          var2 = BarsPeriod.Value - Alert;
                                      var1 = var2 - AlertDuration;
                                  }
                                  else if (State == State.Historical)
                                  {
                                      SetZOrder(-1);
                                  }
                                  else if (State == State.Realtime)
                                  {
                                      if (timer == null)
                                      {
                                          if (Bars.BarsType.IsTimeBased && Bars.BarsType.IsIntraday)
                                          {
                                              lock (Connection.Connections)
                                              {
                                                  if (Connection.Connections.ToList().FirstOrDefault(c => c.Status == ConnectionStatus.Connected && c.InstrumentTypes.Contains(Instrument.MasterInstrument.InstrumentType)) == null)
                                                      Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerDisconnectedError, TextPosition.BottomRight);
                                                  else
                                                  {
                                                      if (!SessionIterator.IsInSession(Now, false, true))
                                                          Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerSessionTimeError, TextPosition.BottomRight);
                                                      else
                                                          Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerWaitingOnDataError, TextPosition.BottomRight);
                                                  }
                                              }
                                          }
                                          else
                                              Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerTimeBasedError, TextPosition.BottomRight);
                                      }
                                  }
                                  else if (State == State.Terminated)
                                  {
                                      if (timer == null)
                                          return;
                      
                                      timer.IsEnabled = false;
                                      timer = null;
                                  }
                              }
                      
                              protected override void OnBarUpdate()
                              {
                      
                                  if(BarsInProgress == 1)
                                  {
                                      DateTime newTime = Times[0][0].AddMinutes(1).AddSeconds(-30);
                                      if(Time[0] > newTime)
                                      {    
                                                      if (State == State.Realtime)
                                          {
                                                  hasRealtimeData = true;
                                                  connected = true;
                      
                                          if (IsFirstTickOfBar && Sound)
                                              {
                                              PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");}
                                          }
                      
                                              }
                      
                      
                      
                                  }
                              }​
                      Thank you

                      Comment


                        #12
                        Hello Ludwik,

                        a 2 minute data series does not have 30 second intervals, that won't work to find 30 seconds before the end of the primary bar. You need to add a series that supports the amount of time difference you want to use. If you want 30 seconds before the end of a primary bar you need to use at maximum a 30 second secondary series so you have timestamps with 30 seconds.

                        Just to make sure that you are on the same page here the condition BarsInProgress == 1 means to run that code on the secondary series only. If you add a 30 second secondary series that code will be executed every 30 seconds. If we make a simple example using a 15 minute bar primary series the time calculation would need to look like the following:

                        DateTime newTime = Times[0][0].AddMinutes(15).AddSeconds(-30);

                        You are adding 15 minutes to the last closed primary 15 minute bar to get the next bars close time and then subtracting 30 seconds from that projected time. That gives you 30 seconds before the end of the upcoming not yet closed 15 minute bar.

                        If you wanted you could also use a 1 second secondary series instead to allow the time condition to react more quickly when it reaches 30 seconds before the primary bar close.







                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hey Jesse,

                          I did exactly as per your suggestion I added 30 seconds data series in the code:

                          Code:
                              {
                                         if (State == State.Configure)
                                        {AddDataSeries(BarsPeriodType.Second, 30);}​


                          However when I did that clock disapeared and alarm doesn't fire up at all. What am I doing wrong here? I'm puzzled. Please point me in the right direction or maybe pass the ticked to Patrick who creaed an indicator to help figure out the solution here?

                          Comment


                            #14
                            Hello Ludwik,

                            I would suggest using a Print to see how your logic is working. I can't really answer any questions about your custom logic as I don't know what all changes you had made. I can answer questions about making a time projection like I had previously done.

                            Making updates to user app share scripts is not something our support can directly help but we can make suggestions that you can work with. In regard to passing this to Patrick, He was not the author of this script so that would not be possible. We generally do not create those items ourselves, in the case of the link that you provided that was not made by Patrick but was made by a user in NT7.
                            Conversion of the Bar Time Flash/Sound Alert by Mindset for NinjaTrader 7
                            This was a conversion from nt7 which was an effort that was lead by the NinjaScript team back when NT8 was first released. Now that those conversions have been completed a NinjaTrader members name may be the uploaded by name on the ecosystem however that does not mean we actually made that script or know how it works. Conversions were simply changing NT7 to NT8 compliant code but no other logical changes were made by the person who was given the task.





                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hey Jesse,

                              Thank you for your reply.

                              I really only added these two pieces of code (as per screenhosts in the attachements) as per your instructions. Being data series of 30 seconds and the piece of code which you've recommended adding.

                              I haven't changed anything else from this original file: https://ninjatraderecosystem.com/use...sound-alert-2/

                              I would truly appreciate if you could only eye ball it and let me know what I might be doing wrong here and will go over with Prints to figure out what it might be as I'm not really very literate in NT8 code and was only able to create very basic indicators for interanl use.
                              Maybe it is just some marginal error which I'm making stopping the script from running properly.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, Today, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Working...
                              X