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

Draw TimeSpan

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

    Draw TimeSpan

    Hi,

    I have an indicator which gives me the timespan between two objects. I'd like to draw this number but can't seem to be able to convert it. It measures as far as milliseconds.

    PHP Code:
    if (e.MarketDataType == MarketDataType.Last)
                    
                    
    ts.Set(DateTime.Now);
                    
    TimeSpan myDiff ts[0] - ts[1]; 
    Any ideas?

    #2
    Hello dsraider,
    Thanks for your post and I am happy to assist you.

    Without the complete code it would be difficult to say as your sample does not contain enough information. Please provide us with a sample code containing all of the relevant points to your issue.

    I look forward to assist you.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi Joydeep,

      I THINK this should do it. Let me know if I haven't posted enough:

      PHP Code:
      if (e.MarketDataType == MarketDataType.Last)
                      
                      
      ts.Set(DateTime.Now);
                      
      TimeSpan myDiff ts[0] - ts[1];
                              
                  if (
      DateTime.Compare (ts[0], ts[1]) > -1)
                      
                          Print(
      myDiff.ToString()); 
      Basically, myDiff is the measurement in TimeSpan between the two last trades. I'd like to draw that number on my chart as an indicator.

      Comment


        #4
        Hello dsraider,
        I presume “ts” is a DataSeries/Plot here. If so, a dataseries/plot doesn’t takes any DateTime argument. You have to pass a double.

        Again a difference between, ts[0] – ts[0] will return a double, and you cannot assign the same to a TimeSpan. But again my answer is assumptive as I don’t know for sure what the ts field is.

        I look forward to assist you.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Yes. Okay, here's the rest.

          PHP Code:
          private DateTimeSeries ts;
          DateTime myDiff;

          protected 
          override void Initialize()
          {
          ts = new DateTimeSeries(thisMaximumBarsLookBack.Infinite);

          And when I go to do something like...

          PHP Code:
          Speed.Set(myDiff); 
          I get

          1. The best overload method match for NinjaTrader.Data.DataSeries.Set(double) has some invalid arguments.
          2. Argument 1: cannot convert from System.TimeSpan to double.

          Comment


            #6
            Hello tsraider,
            Thanks for the clarification, much appreciated.

            Please try this code

            Code:
            TimeSpan myDiff = ts[0].TimeOfDay.Subtract(ts[1].TimeOfDay);
            In your last post you have assigned myDiff as DateTime which is different from TimeSpan, so do take care on that.

            Please let me know if I can be of any further assistance.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Good catch. Well, I made the following changes:

              PHP Code:
              TimeSpan myDiff;

              protected 
              override void OnMarketData(MarketDataEventArgs e)
              {
              ts.Set(DateTime.Now);
              TimeSpan myDiff ts[0].TimeOfDay.Subtract(ts[1].TimeOfDay);

              if (
              DateTime.Compare (ts[0], ts[1]) > -1)
              Speed.Set(myDiff);

              But still get errors. The first is the same as last night while the second says it cannot convert from TimeSpan to double.

              Thanks for your help so far.

              Comment


                #8
                Originally posted by dsraider View Post
                Hi Joydeep,

                I THINK this should do it. Let me know if I haven't posted enough:

                PHP Code:
                if (e.MarketDataType == MarketDataType.Last)
                                
                                
                ts.Set(DateTime.Now);
                                
                TimeSpan myDiff ts[0] - ts[1];
                                        
                            if (
                DateTime.Compare (ts[0], ts[1]) > -1)
                                
                                    Print(
                myDiff.ToString()); 
                Basically, myDiff is the measurement in TimeSpan between the two last trades. I'd like to draw that number on my chart as an indicator.
                Apart from any other issues, this is what looks like your problem:

                Code:
                ts.Set(DateTime.Now);
                It sets the your ts series to the time of the clock as at whenever the statement is called. In other words, it is not a set time, but an uncontrolled variable.

                You want to set it to the time of the transaction. The would be e.Time.

                Hence, more correct will be:

                Code:
                ts.Set(e.Time);

                Comment


                  #9
                  Hello again koganam,

                  Thanks for your help but I'm afraid there was no change in error messages. Maybe I need to change something else in order to accommodate it?

                  Comment


                    #10
                    Originally posted by dsraider View Post
                    Hello again koganam,

                    Thanks for your help but I'm afraid there was no change in error messages. Maybe I need to change something else in order to accommodate it?
                    I was only dealing with the cleanness of your code in that aspect.

                    OTOH, if you are talking about your error in regard to Speed.Set(), evidently Speed() is most probably a DataSeries. You cannot set it to a TimeSpan value. The error means what it says. I was talking about ts(), not Speed(). That is another issue that you will have to correct. Are we talking about a different error message?

                    Comment


                      #11
                      Ahh, gotcha. Well, the error message is that I can't convert System.TimeSpan to double. I changed "Speed" to "Diff" just in case, though.

                      Comment


                        #12
                        Originally posted by dsraider View Post
                        Ahh, gotcha. Well, the error message is that I can't convert System.TimeSpan to double. I changed "Speed" to "Diff" just in case, though.
                        I am getting more confused by the minute with what you are trying to do. Yes, you cannot convert TimeSpan to a double, without doing so explicitly. Is that what you are trying to do in the first place? e.g., given a TimeSpan variable, tSpan, the double value, tSpan.TotalSeconds will tell you how many seconds tSpan represents etc.,

                        I think it will be better if you told us the intent of the code, then showed us how you are trying to achieve it. You have multiple assignations in different places, and I have no idea of how it ties together. I can see each error and what it means, but we may be going in circles with this. This piecemeal approach is getting rather confusing for me.

                        Comment


                          #13
                          I would but "myDiff.TotalMilliseconds" was all I needed. Thanks again for your help and definitely for the laugh

                          DS

                          Comment


                            #14
                            Ugh. I spoke too soon. What I'm trying to do is measure the time between actual trades and draw that on a chart as an indicator. While it now draws, it doesn't update from bar to bar as you can see by the pic below.
                            Attached Files

                            Comment


                              #15
                              And which just started working without making any changes (cue horror music).
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 08:14 AM
                              3 responses
                              4 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frankthearm, Today, 09:08 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post frankthearm  
                              Started by samish18, Yesterday, 08:57 AM
                              11 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by yertle, Today, 08:38 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X