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

+9Bar Timer Modification

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

    +9Bar Timer Modification

    I made a copy of BarTimer and did what I thought was necessary to make it visible to my strategy. I added a plot in State.Default: AddPlot(new Stroke(Brushes.Transparent, 2), .PlotStyle.Bar,"TimerSignal");
    I added in properties :
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> TimerSignal
    {
    get { return Values[0]; }
    }
    and I placed the following in the body of the text:
    TimerSignal[0] =barTimeLeft.Seconds;
    Print(" Secs Left "+TimerSignal[0]);
    With this I am printing the bar countdown on the Output screen and it is this countdown that I wish to pick up in my strategy.
    My problem is that when I deploy the indicator I get the following error message :2022-03-15 13:04:08:004|3|4|Indicator 'MyBarTimer': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    Can someone tell me what the problem is?

    #2
    Hello galsermil,

    Thank you for your reply.

    The error you're getting means that there's an issue in your OnStateChange method. Can you supply the code for OnStateChange from your version of the Bar Timer?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Kate,

      I am attaching the .cs file as I do not know how to send part as a viable file. I want to emphase that the only change that I made was to ADDPlot in an attempt to make it visible to my strategy. Also let me say that my Print of the Bar sec remaining is good.
      Attached Files

      Comment


        #4
        Hello galsermil,

        Thank you for your reply.

        You're trying to assign the current number of seconds left to the plot in OnTimerTick. This is called when OnConnectionStatusUpdate is checked in OnStateChange and the timer is started, and since that occurs prior to reaching State.DataLoaded the plot hasn't been synced to the bars yet. You'll need to make this assignment in OnBarUpdate:

        Code:
        //initialize TimeSpan variable at class level instead of in OnTimerTick
        private TimeSpan         barTimeLeft;
        
        protected override void OnBarUpdate()
        {
        if (State == State.Realtime)
        {
        hasRealtimeData = true;
        connected = true;
        // my insert to identify seconds left
        TimerSignal[0] =barTimeLeft.Seconds;
        Print(" Secs Left "+TimerSignal[0]);
        }
        }
        Then in OnTimerTick you'd just have to remove the TimeSpan initialization since you'd already done that at the class level:

        Code:
        private void OnTimerTick(object sender, EventArgs e)
        {
        ForceRefresh();
        
        if (DisplayTime())
        {
        if (timer != null && !timer.IsEnabled)
        timer.IsEnabled = true;
        
        if (connected)
        {
        if (SessionIterator.IsInSession(Now, false, true))
        {
        if (hasRealtimeData)
        {
        [B]barTimeLeft = Bars.GetTime(Bars.Count - 1).Subtract(Now);[/B]
        
        timeLeft = (barTimeLeft.Ticks < 0
        ? "00:00:00"
        : barTimeLeft.Hours.ToString("00") + ":" + barTimeLeft.Minutes.ToString("00") + ":" + barTimeLeft.Seconds.ToString("00"));
        
        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerTimeRemaining + timeLeft, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
        }
        else
        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerWaitingOnDataE rror, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
        }
        else
        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerSessionTimeErr or, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
        }
        else
        {
        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerDisconnectedEr ror, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
        
        if (timer != null)
        timer.IsEnabled = false;
        }
        }
        }
        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Kate,

          There must be something else necessary as the value of TimerSignal[0] is printing as -0- every time.

          Comment


            #6
            Hello galsermil,

            Thank you for your reply.

            Are you running this directly on a chart to test or are you calling it from a strategy? If from a strategy, that strategy would need to be set to use OnEachTick as that would be required for the bar timer to function.

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Kate,

              The Print in the indicator is strictly to test the functionality. I want the strategy to know when there is just one or two seconds left in the bar. Let me explain the use. My strategy is basically a scalping strategy. I am entering my trades at present <IsFirstTickOfBar> an it is not doing an adequate job as the entry may come as many as 4 or 5 ticks off of the Open so I want to try having my entry at 1 or 2 seconds before the bar closes.

              Comment


                #8
                Hello galsermil,

                Thank you for your reply.

                I take it then you are testing by calling the indicator from within a strategy. Is the strategy running On Bar Close, On Price Change or On Each tick?

                Thanks in advance; I look forward to assisting you further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Kate,

                  I would like to ask another question. Since the intended use of my timer is strictly with one minute charts, cannot a lot of the script be eliminated for MyBarTimer?

                  Comment


                    #10
                    Kate,

                    I would like to ask another question. Since the intended use of my timer is strictly with one minute charts, cannot a lot of the script be eliminated for MyBarTimer?

                    Comment


                      #11
                      Every indicator and every Strategy is calculating <OnEachTick>.
                      A side note: for some reason, I am not getting email advice of a response from you.

                      Comment


                        #12
                        I got round to testing to see if my Strategy would pick up the signal from MyBarTimer but I didn't get that far when the same error about 'OnStateChange' appeared.

                        Comment


                          #13
                          Kate, I think that I just realized that the error doesn't pop up until I enable the strategy that goes with the MyBarTimer.

                          Comment


                            #14
                            Kate,

                            That is exactly it. I enabled the Strategy with the MyBarTimer prep and the chart did not have MyBarTimer installed at all. So I will include the .cs file for the strategy NakedRockStarTimed.
                            Attached Files

                            Comment


                              #15
                              Hello galsermil,

                              Thank you for your replies.

                              The issue is that the MyBarTimer script is hitting an error because it checks for the ChartControl and you're not adding the indicator to the chart, you're just trying to access it's values and it can't check ChartControl if it's not actually applied to the chart. Try adding it to the chart in State.DataLoaded in the strategy after you've assigned it to the indicator variable:

                              Code:
                              else if (State == State.DataLoaded)
                              {
                              //other code omitted
                              MyBarTimer1 = MyBarTimer();
                              AddChartIndicator(MyBarTimer1);
                              }
                              Please let us know if we may be of further assistance to you.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,404 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              95 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              159 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X