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

cannot implicitly convert 'System.DateTime' to 'double' (CS0029)

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

    cannot implicitly convert 'System.DateTime' to 'double' (CS0029)

    Hi,

    I'm trying to add Time[0] to an indicator as plot to then use in the Market Analyzer as reference to other numerical values.

    I set the #region Properties to <DateTime> after getting the error "cannot implicitly convert 'System.DateTime' to 'double' (CS0029).

    CS0029 does not provide the needed example for the error.



    I've checked the documentation here, but it doesn't tell what to use:




    I tried ,DateTime> instead of <double> as below Properties:
    [Browsable(false)]
    [XmlIgnore]
    public Series<DateTime> PriorDate
    {
    get { return Values[0]; }
    }


    ​​​​​​​But it returns the same errors:
    "cannot implicitly convert 'System.DateTime' to 'double' (CS0029).
    "cannot implicitly convert 'NinjaTrader.NinjaScript.Series<double> (CS0029).


    What's the solution?
    Last edited by Cormick; 07-24-2021, 01:04 AM. Reason: Added tags

    #2
    Hello Cormick, thanks for posting.

    All Plot series much be of Series<double>; the get method would need to return a Series<DateTime>. Assuming the indicator is running Calculate.OnBarClose you can write the time on every bar with Draw.Text(). Note this could cause performance issues if there is a unique drawing object on every bar of the chart. Code to limit the number of bars would need to be made, or, for a more advanced approach, use custom rendering in SharpDX to render the time value.

    https://ninjatrader.com/support/help..._rendering.htm

    Also, see the example indicator "SampleCustomRender" and note how OnRender can iterate quickly through only the visible bars on the chart e.g.

    https://ninjatrader.com/support/help..._fromindex.htm

    Kind regards.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for the answer.

      I'm not sure what you mean by:

      the get method would need to return a Series<DateTime>.
      Can you please give an example?

      How would I update the #region Properties code below?
      [Browsable(false)]
      [XmlIgnore]
      public Series<DateTime> PriorDate
      {
      get { return Values[0]; }
      }


      What I need is a simple reference to the time corresponding to the PriorOHLC values of the PriorDayOHLC indicator stock indicator.
      For example, with the PriorDayOHLC indicator adding the time value next to the Prior OHLC plots/numerical values on the Market Analyzer.

      Instrument Time Open High Low Close
      CL 09-21 07/21/2021 13:00:00 60.00 61.00 59.00 59.50


      How to fetch it without plots if possible (or only for the current bar/most recent 2 bars)?
      Or how to do it as simple display 'clock' on the chart, then take that value to use in the Market Analyzer?
      How to do it the simplest way?

      EDIT:
      I found the Bar Timer Flash/Sound Alert from User Apps Share indicator fromNinjaTrader_PatrickH / Mindset here:

      NT8 NT7
      The Full NT8 code:
      I'd favor first testing it with the Draw.Text() method.

      Draw.Text() from the documentation Example:
      Draw.Text(this, "tag1", "Text to draw", 10, 1000, Brushes.Black);

      Y-axis 2 More Examples:
      Draw.TextFixed(this, "tag1", DateTime.Now.ToString(), TextPosition.TopLeft, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Blue, Brushes.Transparent, 0);
      Print(Time[0].ToString("MM/dd/yyyy"));
      http://www.geekzilla.co.uk/View00FF7...59AA20581F.htm


      DateTime:
      What way can I call the Date & Time of the most recent 2 bars?

      I tried adding those 2 lines of code
      Draw.Text(this, "tag1", Time[0].ToString("MMM dd yyyy HH:mm:ss"), 0, 1000, Brushes.Pink);
      Draw.Text(this, "tag2", Time[0].ToString("MMM dd yyyy HH:mm:ss"), 1, 500, Brushes.Cyan);

      to execute as action to my modified PriorDayOHLC Indicator OnBarUpdate If Statement but no Date and Time displays on the Chart/panel 2:


      if (IsFirstTickOfBar && Times[0][0].DayOfWeek != DayOfWeek.Monday)
      {
      Values[0][0] = Open[0];
      Values[1][0] = High[0];
      Values[2][0] = Low[0];
      Values[3][0] = Close[0];

      //Values[4][0] = Open[1] - (Close[1]);
      Values[4][0] = Values[0][0] - (Values[3][0]);
      Draw.Text(this, "tag1", Time[0].ToString("MMM dd yyyy HH:mm:ss"), 0, 1000, Brushes.Pink);
      //Draw.Text(this, "tag2", Time[0].ToString("MMM dd yyyy HH:mm:ss"), 1, 500, Brushes.Cyan);
      }

      I suspect it does not display because did not refer a Value.
      When I try adding a value it return the same original error but for DrawingTools
      cannot implicitly convert 'NinjaTrader.NinjaScript.DrawingTools.Text' to 'double' (CS0029)

      CS0029 does not provide the needed example for the error 'NinjaTrader.NinjaScript.DrawingTools.Text' to 'double' (CS0029).
      https://ninjatrader.com/support/help...t8/?cs0029.htm


      AddPlot(Brushes.Yellow, "DateTime");

      Values[5][0] = Draw.Text(this, "tag1", Time[0].ToString("MMM dd yyyy HH:mm:ss"), 0, 1000, Brushes.Pink);

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> DateTime
      {
      get { return Values[5]; }
      }

      Do you have sample code for the DrawText() method? I don't understand how to make it work in context. Else can you explain the step to make it work?


      Aside Question:
      Can we use the Draw.TextFixed() parameters instead of y-axis pixel numbers?
      https://ninjatrader.com/support/helpGuides/nt8/?draw_textfixed.htm

      TextPosition.BottomLeft
      TextPosition.TopLeft
      Last edited by Cormick; 07-22-2021, 06:14 AM.

      Comment


        #4
        Hi Cormick, thanks for your reply.

        The compile error is coming from trying to return a Series<double> in a Series<DateTime> public property, all Series in the Values array must be of Series<double>. All timestamps of a price series are contained within the Time[] array, so accessing the last two timestamps would be Times[0] and Times[1]. If you need a series with the DateTime values of each bar, Time[] is the series to use. To detect if a bar is the first bar of a session, use Bars.IsFirstBarOfSession. I attached a relavent test script that detects green bars and draws their high and low price to the bar.

        Kind regards,
        -ChrisL
        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris,

          Thanks for the sample and explaining comments.

          After some code adjustment, I'll only need the most recent bar's/daily bar's Timestamp,
          as the OnBarUpdate IF() Else() statement returns now the correct values for PriorOHLC on all weekdays.

          I corrected the Draw.Text() code as per your explaining comment as this:
          Draw.Text(this, "tag1", Times[0][0].ToString("MMM dd yyyy HH:mm:ss"), 0, 1000, Brushes.Pink);

          And removed the 6th Values[5]/DateTime Propery code from the #region Properties.
          And removed also the 6th AddPlot(Brushes.Yellow, "DateTime"); code from the State.SetDefaults scope.

          Now it complies without errors.

          But I see no plot/digital 'clock' on the Chart's Panel 2/Chart Panel 1.

          What I don't understand still is how to display the DateTime/'Times[0][0].ToString("MMM dd yyyy HH:mm:ss")' on the chart's Panel 2/Chart Panel 1.
          Since no Value other than <double> can be added to the Public Properties #region,
          how else can I get the DateTime/'Times[0][0].ToString("MMM dd yyyy HH:mm:ss")' to display on the Charts Panel 2/Chart Panel 1?

          Also, I'm not sure how to use the y-axis parameter from the Draw.Text() method.
          I tested
          y = 1000
          and
          y = 500

          But the DateTime/'Times[0][0].ToString("MMM dd yyyy HH:mm:ss")' still does not show on the Chart's Panel 2/Chart Panel 1.

          I've searched the Bar Timer Flash/Sound Alert from User Apps Share indicator from NinjaTrader_PatrickH
          The Full NT8 code:

          for
          Time[
          Times[
          but there are no instances of them.

          A DateTime variable is used but with built in Ninjascript functions I think, so I can't see how it fetches the Date Time.
          And I don't understand how it displays it neither.

          Do you have a simpler sample that demonstrates how to fetch and display the Time[] / Times[] series on the chart?
          I only used them previously with the Prints without need to code the displaying part so I don't see how to do it with for the Chart.

          Comment


            #6
            Hi Cormick, thanks for your reply.

            This code will draw the bar time stamp below each bar on the chart, this is just a simple example:

            Code:
            protected override void OnBarUpdate()
            {
            Draw.Text(this, "DateTime"+CurrentBar, " " + Time[0], 0, Low[0]-TickSize*2);
            }
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hi Chris,

              Thanks a lot for the example, it works! You rock!

              Test results:

              All three Time[0], Times[0][0] and Times[0][1] return timestamps on every bars:
              Code:
              Draw.Text(this, "DateTime"+CurrentBar, " " + Time[0].ToString("MMM dd yyyy HH:mm:ss"), 0, Low[0]-TickSize*2);
              Code:
              Draw.Text(this, "DateTime"+CurrentBar, " " + Times[0][0].ToString("MMM dd yyyy HH:mm:ss"), 0, Low[0]-TickSize*2);
              Code:
              Draw.Text(this, "DateTime"+CurrentBar, " " + Times[0][1].ToString("MMM dd yyyy HH:mm:ss"), 0, Low[0]-TickSize*2);
              Click image for larger version  Name:	NVIDIA_Share_jLVyYwltVE.png Views:	0 Size:	299.5 KB ID:	1164812




              However I only need the Timestamp of the most recent Prior bar.

              After some posts research I found the solution here:


              unique Tag ("Tag") = TIMESTAMPS/DRAWN TEXT IS DRAWN ONLY ON THE SINGLE BAR (SELECTED BY ITS INDEX)

              non-unique Tag ("Tag"+CurrentBar) = TIMESTAMPS/DRAWN TEXT IS DRAWN ON ALL BARS

              So I removed the '+CurrentBar' from the "DateTime"+CurrentBar Tag and now it displays the most recent Prior timestamp only.

              Code:
              Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy HH:mm:ss"), 0, Low[0]-TickSize*2, Brushes.Pink);

              Click image for larger version  Name:	NVIDIA_Share_CUHErZTx5c.png Views:	0 Size:	33.2 KB ID:	1164813

              I had a vague memory of using the '+CurrentBar' before but I had forgotten about.

              Just a common sense commitment to memory remark. Why call it +CurrentBar? and not +AllBars?

              Because really it's rather confusing-contrary to what it stands for which is adding the Drawn Text to all Bars — not the Current Bar.

              About the Draw.Text() Y-Axis reference:

              Other takeaway from your code is that we must not use the y-Axis referencing for Drawing Text meant for referencing a Bar timestamp.
              Instead we must use a OHLC and it's bar index reference + specify the number of Ticks away from the referenced Bar OHLC to display the timestamp at (your Low[0]-TickSize*2 parameter).



              Thanks again Chris for the great help!

              Be well!
              Attached Files
              Last edited by Cormick; 07-23-2021, 03:39 AM.

              Comment


                #8
                Hi Chris,

                After testing on the Market Analyzer side:


                Days Back 0 | 1 | 2 | 3 all don't display the date:

                Click image for larger version  Name:	NVIDIA_Share_x4eNSYNhhc.png Views:	0 Size:	109.6 KB ID:	1164820

                Click image for larger version  Name:	NVIDIA_Share_kb4yODWks0.png Views:	0 Size:	944.4 KB ID:	1164821

                Click image for larger version  Name:	NVIDIA_Share_J2jk8TOOst.png Views:	0 Size:	948.3 KB ID:	1164822

                Click image for larger version  Name:	NVIDIA_Share_H6tag77240.png Views:	0 Size:	820.2 KB ID:	1164824


                I tested those 4 Draw.Text codes,
                which all do Display the PriorDate on the Chart,
                but do not display the PriorDate on the Market Analyzer.

                Code:
                Draw.Text(this, "DateTime", " " + Time[0], 0, Low[0]-TickSize*2);
                Code:
                Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);
                Code:
                Draw.Text(this, "DateTime"+CurrentBar, " " + Time[0], 0, Low[0]-TickSize*2);
                Code:
                Draw.Text(this, "DateTime"+CurrentBar, " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);
                What's preventing the display on the Market Analyzer?

                What to do next to get it display on the Market Analyzer?


                EDIT (Added MarketAnalyzer Column):

                I found NinjaTrader_AlanP comment about 'exposing values to the market analyzer without setting a plot' in this post:

                Does this method applies to the current context with Time[0] value/Draw.text string?

                If so could you please share the steps to update my code the required way?

                If not, thanks for your next recommendations.
                Attached Files
                Last edited by Cormick; 07-23-2021, 06:51 AM. Reason: EDIT (Added MarketAnalyzer Column)

                Comment


                  #9
                  Hi Cormick, thanks for your reply.

                  The example from Alan shows that you can expose a public series to other script then make a market analyzer column that displays the value of that series. The original example is here:



                  Instead of a public Series<bool> you would need a public Series<DateTime>, filling in the values of this series with values from the Time[] array and access this through the MarketAnalyzerColumn script, setting CurrentText rather than CurrentValue.

                  Kind regards,
                  -ChrisL
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,

                    Thanks a lot for the steps and the documentation example.

                    Both Alan and the documentation example do not show how to handle a DateTime value (they only show how to do it with bool and double.).

                    I'm not sure if what you say I must do is what follows:
                    you would need a public Series<DateTime>
                    I interpret it as:

                    Change the #region Properties code that way

                    From the 2 Examples:


                    Documentation Example:

                    aNT8ExposePlot.cs #region Properties Code:

                    Full Code:



                    [Browsable(false)]
                    [XmlIgnore]
                    public Series < double> BullIndication
                    {
                    get { return bullIndication; } // Allows our public BullIndication Series<bool> to access and expose our interal bullIndication Series<bool>
                    }


                    ExposedPlotMa.cs #region Properties Code:

                    Full Code:

                    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
                    {
                    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
                    CurrentValue = expPlot.BullIndication[0];
                    }


                    Alan's Example:

                    SampleBoolSeries.cs #region Properties Code:

                    Full Code:



                    public double ExposedVariable
                    {
                    // We need to call the Update() method to ensure our exposed variable is in up-to-date.
                    get { Update(); return exposedVariable; }
                    }

                    No #region Properties in the SampleBoolStrategy.cs Code:


                    How else do I need to change it?



                    That way?

                    [Browsable(false)]
                    [XmlIgnore]
                    public Series <DateTime> PriorDate
                    {
                    get { return priorDate; }
                    }


                    Or That way?

                    public DateTime PriorDate
                    {
                    // We need to call the Update() method to ensure our exposed variable is in up-to-date.
                    get { Update(); return priorDate; }
                    }

                    Or something else? What else if Something else?


                    I've found those posts about correct #region Properties templates:

                    But they do not show how to set a DateTime one.


                    Other issue:


                    I've added those codes to the Indicator:

                    Before the OnStateChage() scope:

                    Click image for larger version  Name:	NVIDIA_Share_n4Mjf10cwI.png Views:	0 Size:	48.5 KB ID:	1164883

                    Code:
                     public class myDailyBarsPriorOHLC : Indicator
                    {
                    //private PriorDayOHLC PriorDayOHLC1;
                    
                    private Series<DateTime> priorDate;
                    
                    protected override void OnStateChange()
                    {


                    Within the State.Configure Scope:

                    Click image for larger version  Name:	NVIDIA_Share_7UUsBJTJ9r.png Views:	0 Size:	33.5 KB ID:	1164884

                    Code:
                     }
                    else if (State == State.Configure)
                    {
                    priorDate = new Series<DateTime>(this);
                    }
                    else if (State == State.DataLoaded)
                    {



                    Updated the Draw.tex in the IF() Else of the OnBarUpdate() to:

                    priorDate = Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);

                    (for this line, I still get the CS0029 error but this time as
                    'Cannot Implicitely convert 'NinjaTrader.Ninjascript.DrawingTools.Text' to 'NinjaTrader.Ninjascript.Series<System.DatTime>'
                    which the CS0029 documentation does not provide example for.


                    And finally added the PriorDate code to the #redion Properties:

                    Click image for larger version  Name:	NVIDIA_Share_eTBSuWWms9.png Views:	0 Size:	32.5 KB ID:	1164885

                    Code:
                     [Browsable(false)]
                    [XmlIgnore]
                    public Series <DateTime> PriorDate
                    {
                    get { return priorDate; }
                    }
                    #endregion

                    I've followed both examples and adapted my code to their demonstration, but it's not working in context.

                    Can you please refer a sample code demonstrating how to set the DateTime value?

                    Also is there a simpler and more straightforward way to get the expected result than with the Market Analyzer new column way? If there is could you please detail the steps?


                    Last edited by Cormick; 07-23-2021, 10:43 AM.

                    Comment


                      #11
                      Hi Cormick, thanks for your reply.

                      I attached a modified sample for reference.

                      Kind regards,
                      -ChrisL
                      Attached Files
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chris,

                        Thanks for the sample.
                        I'll do further testing tomorrow and be back with next results.

                        Be well!

                        Comment


                          #13
                          Hi Chris,

                          I did correct the OnBarUpdate() Time[0] variable according to your example.

                          Your example:
                          timeIndication[0] = Time[0];


                          My correction (removing the Draw.Text() method):
                          PriorDate[0] = Time[0];


                          Now it compiles without error CS0029.
                          But the plot is not showing on the chart anymore.

                          Is this as expected?

                          From your other CurrentText() method code,
                          Code:
                           protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
                          {
                          if (marketDataUpdate.MarketDataType == MarketDataType.Last)
                          [B]CurrentText[/B] = expPlot.TimeIndication[0].ToString(CultureInfo.InvariantCulture);
                          }


                          I infer that the Draw.text() method is still needed.
                          If that's correct, how to combine the Draw.text() code with the indexed variable and get successful compiling?

                          I tested the following and the CS0029 error remains:
                          Code:
                          PriorDate[0] = Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);

                          Code:
                          PriorDate[0] = Draw.Text(this, "DateTime", " " + Time[0], 0, Low[0]-TickSize*2, Brushes.Pink);
                          Code:
                          PriorDate[0] = Draw.Text(this, "DateTime", " " + Time[0], 0, Low[0]-TickSize*2);




                          What other solution do you recommend?

                          Comment


                            #14
                            Hi Cormick, thanks for your reply.

                            Drawing the time value to the chart is completely separate from exposing this series for the Market Analyzer to use. You will need to run the market analyzer column script and the indicator which draws time data to the chart, ensuring the Data Series that is selected in the Market Analyzer column is the same data series as the chart.

                            Doing PriorDate[0] = Draw.Text(...) will not compile because Draw.Text returns a Text draw object, not a DateTime object.

                            Please let me know if I can assist any further.
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Chris,

                              Thanks for the answer.

                              You say:
                              You will need to run the market analyzer column script and the indicator which draws time data to the chart, ensuring the Data Series that is selected in the Market Analyzer column is the same data series as the chart.
                              It will be the same:
                              Both in the Market Analyzer Column code and the Indicator Code.

                              The problem is that in the Market Analyzer Column Code I have to get the Indexed Variable Time[0] without the Draw.Text() as

                              PriorDate[0] = Time[0];

                              for it to compile,

                              while in the Indicator I have to get the Draw.Text() method with the Bar reference in place of Y-Axis parameter for it to draw on the chart as:

                              Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);

                              But the Draw.Text() method generates a compile error with the use of the indexed variable Time[0].

                              So on one end I must use the Indexed variable Time[0] alone for it to compile and on the other I must embed it within the Draw.Text() method for it to display on the chart — but doing so produces a compile error—

                              —Ok. After some thoughts, it occurred that I might use both the Draw.Text() method AND the Indexed Variable. I thought you could only call the Time[0] once.

                              So I put them one after the other within the OnBarUpdate() scope like this:

                              protected override void OnBarUpdate()
                              {
                              ...

                              if (...)
                              {
                              ...

                              Draw.Text(this, "DateTime", " " + Time[0].ToString("MMM dd yyyy"), 0, Low[0]-TickSize*2, Brushes.Pink);

                              PriorDate[0] = Time[0];
                              }
                              else (...)
                              {
                              ...
                              }
                              }

                              Now it both compiles without error and displays the PriorDate on the Chart!


                              Thanks for the AND clue!
                              You will need to run the market analyzer column script and the indicator which draws time data to the chart, ensuring the Data Series that is selected in the Market Analyzer column is the same data series as the chart.

                              About your ExposedPlotMA Code:

                              Full Code:

                              The namespace line is as follows:

                              namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns

                              I'm not familiar with the .MarketAnalyzerColumns container reference.

                              Where can recreate the script in?

                              Can I just launch the Indicator wizard and create an Indicator where I next paste your ExposedPlotMA code?

                              Or do I need to do it via the Strategy Builder?

                              Or from somewhere else from the Ninjascript editor?

                              If so which of the menus?

                              Click image for larger version

Name:	vmplayer_XXR84c4lFP.png
Views:	448
Size:	34.8 KB
ID:	1164938

                              I'll test the New Market Analyzer Column Menu to see it it works.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post tkaboris  
                              Started by BarzTrading, Today, 07:25 AM
                              1 response
                              11 views
                              1 like
                              Last Post NinjaTrader_Clayton  
                              Started by EB Worx, 04-04-2023, 02:34 AM
                              7 responses
                              161 views
                              0 likes
                              Last Post VFI26
                              by VFI26
                               
                              Started by Mizzouman1, Today, 07:35 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X